Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Trees using Inline::C

by rvosa (Curate)
on Jul 31, 2005 at 23:41 UTC ( [id://479754]=perlquestion: print w/replies, xml ) Need Help??

rvosa has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I am trying to apply the OO cookbook example for Inline::C to a situation where one has objects related in a parent->offspring relationship, so that you can instantiate a node (e.g. with a name, a description, and a branch-length), instantiate another node, and set one as the parent of the other - and subsequently retrieve it. Alas, I have to admit I don't know enough about the perl guts to know what to return from a $child->get_parent call. Has anyone else worked along similar lines (but *with* success)? I've appended an example of what I'm trying to achieve. Thank you for any and all pointers!
# Please fill in all missing code and fix the bugs below ;-) #--------------------------------------------------------- #!/usr/bin/perl use strict; use warnings; my $node = Node->new('Homo_sapiens', 'Hominid', 1.234); my $parent = Node->new('Homo_neanderthalensis', 'Hominid', 5.02); $node->set_parent($parent); # $node->get_parent; #--------------------------------------------------------- package Node; use Inline Config => BUILD_NOISY => 1; use Inline C => 'DATA', NAME => 'Node'; __DATA__ __C__ typedef struct treeNode { char* name; char* desc; double branch_length; struct treeNode *parent; } Node; SV* new(char* class, char* name, char* desc, double branch_length) { Node* node = malloc(sizeof(Node)); SV* obj_ref = newSViv(0); SV* obj = newSVrv(obj_ref, class); node->name = savepv(name); node->desc = savepv(desc); node->branch_length = branch_length; sv_setiv(obj, (IV)node); SvREADONLY_on(obj); return obj_ref; } void set_parent(SV* obj, SV* parent) { /* this ought to work, no? */ ((Node*)SvIV(SvRV(obj)))->parent = (Node*)SvIV(SvRV(parent)); } SV* get_parent(SV* obj) { /* ...erm...? */ } void DESTROY(SV* obj) { Node* node = (Node*)SvIV(SvRV(obj)); Safefree(node->name); Safefree(node); }

Replies are listed 'Best First'.
Re: Trees using Inline::C
by esskar (Deacon) on Aug 01, 2005 at 00:03 UTC
    SV* get_parent(SV* obj) { Node *node = (Node *)SvIV(SvRV(obj)); if(node != NULL && node->parent != NULL) return new( "Node", node->parent->name, node->parent->desc, node->parent->branch_length ); else return NULL; // not sure }
    but i would prefer to set parent to NULL inside the new function, and add an iditional member named "class" to your struct treeNode to ensure the same class name
      Thanks for your reply! But, is that efficient memory usage? Memory has already been allocated when:
      my $parent = Node->new('Homo_neanderthalensis', 'Hominid', 5.02);
      was called, and it seems like this new call to
      return new( /*..etc...*/);
      goes the same route? Or am I missing something? Thanks again!
        sure. you are right; but see, a (Node *) has no information about its (SV *). A workaround would be
        typedef struct treeNode { char* name; char* desc; char* class; //in case we need it double branch_length; SV* obj; // the perlobj SV* obj_ref; // the ref to the perlobj struct treeNode *parent; } Node; SV* new(char* class, char* name, char* desc, double branch_length) { Node* node = malloc(sizeof(Node)); node->obj_ref = newSViv(0); node->obj = newSVrv(obj_ref, class); node->class = savepv(class); node->name = savepv(name); node->desc = savepv(desc); node->branch_length = branch_length; node->parent = NULL; // no parent yet sv_setiv(node->obj, (IV)node); SvREADONLY_on(node->obj); return node->obj_ref; } SV* get_parent(SV* obj) { Node *node = (Node *)SvIV(SvRV(obj)); if(node != NULL && node->parent != NULL) return node->parent->obj_ref; else return NULL; // not sure }
        HTH

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://479754]
Approved by sk
Front-paged by sk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-16 05:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found