[Gambas-devel] Returning a reference to an array ...

Benoit Minisini gambas at ...1...
Mon Jan 12 14:27:39 CET 2009


On lundi 12 janvier 2009, Gareth Bult wrote:
> Hi,
>
> I'm getting a little bogged down with arrays, any help much appreciated.
>
> I have a connection object;
>
> typedef struct
> {
> GB_BASE ob; // inherited from Gambas
> char* host; // server host name or IP
> char* user; // user name
> char* password; // user password
> int id; // registration id
> int reply; // registration reply
> int count; // connection counter
> CIAX2DEVICE** devices; // array of devices + caps
>
> } CIAX2CONNECTION;
>
> With a device object;
>
> typedef struct
> {
> GB_BASE ob; // inherited from Gambas
> int id;
> char* name;
> long capabilities;
>
> } CIAX2DEVICE;
>
> So the connection contains an array of device objects.
> I need to return this information to Gambas so gambas can see the available
> devices.
>
> I have a routine to create the array;
>
> char ***devices = &THIS->devices;
> CIAX2DEVICE *device;
>
> GB.NewArray(devices,sizeof(CIAX2DEVICE),ndevs);
> GB.New(POINTER(&(*devices)[0]),GB.FindClass("IAX2Device"),NULL,NULL);
> device = (CIAX2DEVICE *)&(*devices)[0];
> GB.NewString(&(device->name),"MyName",0);
> GB.ReturnObject(devices);
>
> But the returning object is untyped .. how do I specify the return type as
> an array of "CIAX2Device[]"
>
> Can it be done??
> Or do I need to code part of this in Gambas and call that ?
>
> Gareth.

GB.NewArray() is just an utility function for creating a "C" dynamic array.

To return a Gambas array object, you must use the GB.Array.*() functions to 
construct the array object and fill it.

GB_ARRAY array;
int i;

GB.Array.New(&array, GB.FindClass("IAX2Device"), <<number of devices>>);

for (i = 0; i < <<number of devices>>; i++)
{
  GB.Ref(THIS->devices[i])
  *((void **)GB.Array.Get(array, i)) = THIS->devices[i];
}

GB.ReturnObject(array);

Another possibility is to create the Gambas array at IAX2Device 
initialization, and store it inside the object structure. 

Choosing the better method depends on your knowledge of the size of the array 
at initialization, or if its contents will change during the life of the 
IAX2Device object.

Regards,

-- 
Benoit Minisini




More information about the Devel mailing list