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

Gareth Bult gareth at ...560...
Mon Jan 12 16:46:41 CET 2009


Hi,

I inserted your code to start with just replacing <<number of devices>> ...
I get;

iax2.c:455: warning: passing argument 2 of 'GB.Array.New' makes integer from pointer without a cast
iax2.c:459: warning: dereferencing 'void *' pointer
iax2.c:459: error: invalid use of void expression
iax2.c:460: error: void value not ignored as it ought to be

When compiling ..

GB.Array.New seems to be expecting a GB_T constant , whereas GB.FindClass is returning a pointer (???)

So whereas I would love to use;

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

It seems this option is not available .. which is why I was using;

#GB.Array.New(&array,GB_T_OBJECT,ndevs);

Help!

Gareth.


----- Original Message -----
From: "Benoit Minisini" <gambas at ...1...>
To: "mailing list for gambas developers" <gambas-devel at lists.sourceforge.net>
Sent: Monday, 12 January, 2009 1:27:39 PM GMT +00:00 GMT Britain, Ireland, Portugal
Subject: Re: [Gambas-devel] Returning a reference to an array ...

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

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
Gambas-devel mailing list
Gambas-devel at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-devel

-- 
Managing Director, Encryptec Limited
Tel: 0845 5082719, Mob: 0785 3305393
Email: gareth at ...560... 
Statements made are at all times subject to Encryptec's Terms and Conditions of Business, which are available upon request.




More information about the Devel mailing list