I'm wondering how best to carry along a reference to a parent object so
I can decrement its refcount as child objects are destroyed.  And I'm
wondering if I can use a typemap to help keep my XS methods simple.


I have a C structure that I return as a Perl object.  Part of that
struct contains a list of pointers to other structures.  I want to
provide methods on those individual list items.  So, ...

In my perl code I want to do this:

  my $parent_object = Foo->new;  # create the Foo object.

  # Return a list of Foo::List objects.
  my @list = $parent_object->return_list;

  for my $item ( @list ) {
      print $item->name, $item->whatever;
  }

Now, the problem is that the Foo::List objects are just a reference to
to the real items in the list located in the parent object.  So I don't
want the parent object destroyed before ALL of the Foo::List objects are
destroyed.  

So, I want to bump the ref count on $parent_object for each
item in the list.  But, then I need to be able to get back to the parent
object when destroying each item.

All I can think of is to return a list of arrays where one of the
elements points back the the SV of the parent so I can do this in my
destroy method:

    SvREFCNT_dec( parent );

I would think this a common problem, so I'm wondering how it's, well,  
commonly solved.

My current methods are very simple and map directly to the C API of my
library.  That is, I can do simple things like:


const char *
name( item )
    ITEM item

And let the typemap do all the work.

Is there a way to do a typemap that will pull out my pointer from the
complex data so my individual methods can remain simple?

Thanks,


-- 
Bill Moseley
[EMAIL PROTECTED]

Reply via email to