Discussion:
Bless stuct* into perl object
(too old to reply)
Ruz
2006-06-15 01:05:05 UTC
Permalink
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.

--
Best regards, Ruslan.
Muppet
2006-06-15 11:17:19 UTC
Permalink
Post by Ruz
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.


--
However, like all drugs, PANEXA can produce some notable side
effects, all of which are probably really, really terrific and
nothing that anyone should be concerned about, let alone notify any
medical regulatory commission about.
-- http://www.panexa.com
Muppet
2006-06-15 11:59:23 UTC
Permalink
Post by Ruz
Post by Ruz
Hi.
I'm trying to embed perl into C programm.
In C function I have tMyStruct* xxx and want bless this pointer
into
Post by Ruz
class 'MyStruct' and pass this object as argument into perl
function.
Post by Ruz
I really don't understand how to write something like "bless
$ref_to_xxx, 'MyStruct'". Could give me small example or pointer
to a
Post by Ruz
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.
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
Ruslan Zakirov
2006-06-15 11:31:23 UTC
Permalink
Post by Muppet
Post by Ruz
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)));

Looks like it works, no much progress yet, but anyway thanks for your help.
Post by Muppet
--
However, like all drugs, PANEXA can produce some notable side
effects, all of which are probably really, really terrific and
nothing that anyone should be concerned about, let alone notify any
medical regulatory commission about.
-- http://www.panexa.com
--
Best regards, Ruslan.
Loading...