Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Inline::C-Cookbook's "Object Oriented Inline"

by syphilis (Archbishop)
on Aug 27, 2007 at 10:40 UTC ( #635269=note: print w/replies, xml ) Need Help??

in reply to Inline::C-Cookbook's "Object Oriented Inline"

Part of the attraction of Ken's rewrite is that it seems to lend itself more readily to typemapping(as regards the OUTPUT section of the typemap)

That's only marginally true. Using Ken's rewrite, the typemap OUTPUT consists of one line of code. Sticking with the original version of Soldier::new(), five lines of OUTPUT code are required.

For anyone who wants to <readmore>, below my sig is a version of the script that I originally posted, modified to use either of the typemaps (which are also presented below).

I still haven't found an answer to either question 1) or 2) in my op.


The script:
use Devel::Peek; my $obj1 = Soldier->new('Benjamin', 'Private', 11111); my $obj2 = Soldier->new('Sanders', 'Colonel', 22222); my $obj3 = Soldier->new('Matt', 'Sergeant', 33333); for my $obj ($obj1, $obj2, $obj3) { print ($obj->get_serial, ") ", $obj->get_name, " is a ", $obj->get_rank, "\n"); } Dump($obj1); print "\n"; package Soldier; use Inline C => Config => TYPEMAPS => './typemap', BUILD_NOISY => 1; use Inline C => <<'END'; typedef struct { char* name; char* rank; long serial; } Soldier; Soldier * new(char* class, char* name, char* rank, long serial) { Soldier* soldier; New(42, soldier, 1, Soldier); soldier->name = savepv(name); soldier->rank = savepv(rank); soldier->serial = serial; return soldier; } char* get_name(Soldier * obj) { return obj->name; } char* get_rank(Soldier * obj) { return obj->rank; } long get_serial(Soldier * obj) { return obj->serial; } void DESTROY(Soldier* obj) { Safefree(obj->name); Safefree(obj->rank); Safefree(obj); } END
The typemap that preserves the READONLY flag:
Soldier * SOLDIER INPUT SOLDIER $var = INT2PTR($type, SvIV(SvRV($arg))) OUTPUT SOLDIER $arg = newSViv(0); SV * obj = newSVrv($arg, \"Soldier\"); sv_setiv(obj, (IV)$var); SvREADONLY_on(obj); $arg;
The typemap that dispenses with the READONLY flag:
Soldier * SOLDIER INPUT SOLDIER $var = INT2PTR($type, SvIV(SvRV($arg))) OUTPUT SOLDIER sv_setref_pv($arg, \"Soldier\", (void*)$var);

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://635269]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2022-11-28 19:15 GMT
Find Nodes?
    Voting Booth?