Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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);

In reply to Re: Inline::C-Cookbook's "Object Oriented Inline" by syphilis
in thread Inline::C-Cookbook's "Object Oriented Inline" by syphilis

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2022-11-28 17:34 GMT
Find Nodes?
    Voting Booth?