# from Rutger Vos
# on Sunday 31 July 2005 04:36 pm:

> #---------------------------------------------------------

void set_parent(SV* obj, SV* parent) {
    /* this ought to work, no? */
    // work: yes...
    // good idea: ?
    Node* apparent = (Node*)SvIV(SvRV(parent));
    printf("name: %s\n", apparent->name);
    ((Node*)SvIV(SvRV(obj)))->parent = apparent;
}

SV* get_parent(SV* obj) {
    Node* self = (Node*)SvIV(SvRV(obj));
    printf("parent name: %s\n", self->parent->name);
    SV* obj_ref         = newSViv(0);
    // duplicate large portions of code from new() without the benefit
    // of knowing what class the parent is in...
    return(obj_ref);
}

> #---------------------------------------------------------

The problem with your set_parent is that you have thrown away the perl 
object to get the pointer value and only stored that integer.  If you 
for some reason need $parent to come from a different class, 
get_parent() will never be privy to that info.  If that's okay, I guess 
you could rewrite most of your new() inside of get_parent() so that you 
can return a blessed ref.

If you keep the whole object (SV* parent) inside your struct (can you do 
that? I dunno (refcount issues?)), then your C code will always have to 
unpack it (but I guess a macro or function could help relieve the 
tedium there.)  If you don't, then you will always have to recreate it 
when your C code needs to return a blessed ref and this could get messy 
if you don't know the parent's class (which I guess could be part of 
your struct if you want to go that route.)

--Eric
-- 
Peer's Law: The solution to the problem changes the problem.
---------------------------------------------
    http://scratchcomputing.com
---------------------------------------------

Reply via email to