[Gambas-devel] How to pass objects and access their properties

Benoît Minisini gambas at ...1...
Sun Feb 5 09:14:07 CET 2012


Le 04/02/2012 16:03, Randall Morgan a écrit :
> Hi Benoit,
>
> I didn't want to commit code that I was using to experiment and learn
> the inner workings of Gambas.
>
> Anyhow, I figured out that a pointer to the GB_OBJECT structure is what
> is being passed to my class method.
>  From there it had to learn that the value held a pointer to my
> GSLCOMPLEX structure and using the value
> stored in gb_object:value cast to a GSLCOMPLEX pointer I could access my
> object members.
>
> So I've ended up with code that looks like this:
>
> // In gsl_complex.h
>
> extern GB_DESC CGslComplexDesc[];
>
> typedef
>      struct __GSLCOMPLEX
>      {
>          GB_OBJECT ob;

---> Nope.

GB_OBJECT is a structure that represents a Gambas object reference.

You must use GB_BASE there. GB_BASE is the structure that is the "base" 
of all Gambas objects.

>          double real;
>          double imagined;
>      }
>      GSLCOMPLEX;
>
>
>
> // In gsl_complex.c
>
> BEGIN_METHOD(GSLCOMPLEX_X, GB_OBJECT x;)
>      GSLCOMPLEX *p;
>      p = ARG(x)->value; // Get GSLCOMPLEX pointer from GB_OBJECT:value
> member

You must use the VARG(x) macro instead of ARG(x)->value. At the moment 
it is the same thing of course, but if the GB_OBJECT macro changes, 
VARG(x) will be updated accordingly, not ARG(x)->value.

>      // Now using our GSLCOMPLEX pointer we can access the GSLCOMPLEX
> structure members
>      GB.ReturnFloat(p->real);
> END_METHOD
>
>
> /**************************************************
>    Describe Class properties and methods to Gambas
> **************************************************/
> GB_DESC CGslComplexDesc[] =
> {
>      GB_DECLARE("Complex", sizeof(GSLCOMPLEX)),
>
>      GB_PROPERTY("real", "f", GSLCOMPLEX_REAL),
>      GB_PROPERTY("imag", "f", GSLCOMPLEX_IMAGINED),
>
>      GB_STATIC_METHOD("X", "f", GSLCOMPLEX_X, "(x)o"),
>      GB_END_DECLARE
> };
>
>

And I strongly suggest to name implementation functions differently: not 
"GSLCOMPLEX_REAL",  but "GslComplex_Real" (The 'Pascal' way).

Regards,

-- 
Benoît Minisini




More information about the Devel mailing list