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.

Cheers,
Rob

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

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.