On Jun 15, 2006, at 7:31 AM, Ruslan Zakirov wrote:

On 6/15/06, muppet <[EMAIL PROTECTED]> wrote:

On Jun 14, 2006, at 9:05 PM, ruz wrote:

>   Hi.
> I'm trying to embed perl into C programm.
>
> In C function I have tMyStruct* xxx and want bless this pointer into > class 'MyStruct' and pass this object as argument into perl function.
> I really don't understand how to write something like "bless
> $ref_to_xxx, 'MyStruct'". Could give me small example or pointer to a
> code where it's implemented.

The magic you want involves using sv_setref_pv() in a typemap.

See the sections "Perl Objects and C Structures" and "The Typemap" in
the perlxs manpage, and the sv_setref_pv() entry in the perlapi manpage.

Yesterday I've tried next thing:
XPUSHs(sv_2mortal(sv_setref_pv(newSViv((IV)xxx), "MyClass", (void*) xxx)));

The newSViv((IV)xxx) is unnecessary. If you just use newSV(0) (creates a zero-length SV), sv_setref_pv() will put the pointer value into the IV without any extra fuss. This is what i have in my code:

     sv = sv_setref_pv (newSV (0), class_name, object_ptr);


The next thing is to create a typemap for wrapping and unwrapping objects blessed into this class. This allows you to use the C type's name in XSUB declarations instead of writing the perl API code to deal with the SVs.



--
This naturally lead to a proliferation of code that was so complex that you had to put a drip pan under your computer to catch the pieces as the compiler melted down.
  - Pete Becker, on C++ templates


Reply via email to