Re: Perl bindings for libaosd: v0.01




On Mar 15, 2008, at 11:47 AM, Jörn Reder wrote:

Torsten Schoenfeld wrote:

Then you need to alter the constructor so it can be used as
X11::Aosd->new and just ignores the class name:

Aosd *
aosd_new(class)
   C_ARGS:
        /* void */

Then remove every method in Aosd.pm.

Is there a better way than building the Perl object on a scalar
reference to Aosd*?

In typemap, you specify that T_PTROBJ is to be used for Aosd*.  That
means that whenever a Aosd* pointer is encountered, it automatically
gets wrapped as an opaque scalar.  So that should be fine.

This way I get a blessed AosdPtr SCALAR ref and Perl can't find any of
the binded methods in the AosdPtr package (hmm, is this namespace
pollution acceptable?) - only new() and DESTROY() work as expected now
;)

T_PTROBJ blesses into ${typename}Ptr.

What you really want is a typemap that blesses the object into the package of your choosing.

Personally, i take the approach of using T_GPERL_GENERIC_WRAPPER (since you already depend on GLib for GPerlCallback and the Pango bindings), which makes the bindings look for symbols named newSV$ {type} and Sv${type}. Then you can supply those with functions like these:


SV *
newSVAosd (Aosd * aosd)
{
        /* If given a NULL pointer, return a copy of the undef scalar. */
        if (!aosd)
                return newSV (&PL_sv_undef);

        /*
         * Return a blessed reference to an integer whose value is the
         * C pointer address.  The quoted name here will be used as
         * the package into which to bless, and must match the
         * XS file's PACKAGE name for method resolution to work.
         */
        return sv_setref_pv (newSV (0), "Aosd", aosd);
}

Aosd *
SvAosd (SV *sv)
{
        /* map undef to NULL */
        if (! gperl_sv_is_defined (sv))
                return NULL;

        /* type-check for sanity, with a useful message for debugging */
        if (! sv_derived_from (sv, "Aosd"))
                croak ("%s is not an Aosd",
                       gperl_format_variable_for_output (sv));

        /*
         * newSVAosd() wrapped up a blessed reference to a scalar whose
         * value was the pointer address; undo that.  SvRV() gets the referee
         * value, SvIV() gets the integer value, and INT2PTR() does a portable
         * cast to ensure that the pointer isn't truncated.
         */
        return INT2PTR (Aosd*, SvIV (SvRV (sv)));
}


These functions should go before the MODULE line in the XS file, where you have the callback function. If this is your only XS file, then you don't have to worry about headers and prototypes and all that and everything will be rainbows and unicorns.



Obviously I'm missing something. X11/Aosd.pm is more or less empty
(besides XSLoader, AUTOLOADER and constants stuff) and I added the
PREFIX to the MODULE line.

That all sounds right.


--
Leia/Lois:  Aren't you a little fat for a stormtrooper?
Luke/Chris: Well, stay here and rot, you stuck-up bitch.
  -- Family Guy, "Blue Harvest" (A "Star Wars" parody)




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]