in reply to How to keep C variables "alive" between invocations with Inline::C

{ package Node; use strict; use warnings; use Inline C => <<'__EOI__'; typedef struct { // ... } Node; Node* new_node() { Node* node; warn("new_node\n"); // DEBUG Newx(node, 1, Node); // node->... = ...; return node; } void DESTROY(Node* node) { warn("DESTROY\n"); // DEBUG Safefree(node); } __EOI__ } { use strict; use warnings; my $x = Node::new_node(); }

typemap:

Node* T_OBJECT INPUT T_OBJECT if (sv_derived_from($arg, \"${subtype}\")) { IV tmp = SvIV((SV*)SvRV($arg)); $var = INT2PTR($type,tmp); } else Perl_croak(aTHX_ \"%s: %s is not of type %s\", ${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]}, \"$var\", \"$subtype\") OUTPUT T_OBJECT sv_setref_pv($arg, \"${subtype}\", (void*)$var);

I was going to use the T_PTROBJ map for Node*, but it was blessing the objects into NodePtr instead of Node. T_OBJECT is just T_PTROBJ that uses $subtype instead of $type.

Replies are listed 'Best First'.
Re^2: How to keep C variables "alive" between invocations with Inline::C
by BrowserUk (Patriarch) on Sep 28, 2010 at 00:49 UTC

    Aren't you missing something? Like where to reference the TYPEMAP


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Inline looks for file "typemap", so there's no need to specify it explicitly.

      $ perl -MInline=FORCE,NOISY,NOCLEAN a.pl ... /home/ikegami/usr/perlbrew/perls/perl-5.12.1/bin/perl /home/ikegami/us +r/perlbrew/perls/perl-5.12.1/lib/5.12.1/ExtUtils/xsubpp -typemap /ho +me/ikegami/usr/perlbrew/perls/perl-5.12.1/lib/5.12.1/ExtUtils/typemap + -typemap /home/ikegami/xxx/typemap Node_ef45.xs > Node_ef45.xsc && + mv Node_ef45.xsc Node_ef45.c ... new_node DESTROY

        Could you add a method to your sample please?

        I tried adding this method:

        Node* insert( Node* root ) { warn("insert\n"); // DEBUG return root; }

        But I get the classic: Can't locate object method "insert" via package "Node" at ...


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.