in reply to Keeping track of classes

If you want to spare the user the trouble of reblessing, do it yourself: make the class the return value should be blessed into an optional second argument to ChildGet().

sub ChildGet { my ( $self, $class ) = @_; $class = "Etk::Widget" unless defined $class; my $child = $self->{_CHILD} ? $self->{_CHILD} : child_get($self); return bless $child, $class; }

That's functionally equivalent to the C requirement that the user perform the casting. Since the information about what kind of Widget you've got can't be determined from the return value of the C function, if you want to make this DWIMmy Perl and not require that second argument, you're either going to have to add metadata to your C object or go with the global hash. You make the call. :)

You can also do this in XS, if you want.

void ChildGet(bin, ...) Etk_Bin * bin; PREINIT: char * class = "Etk::Widget"; Etk_Button * button; PPCODE: if (items > 1) class = SvPV_nolen( ST(1) ); button = TK_BUTTON(etk_bin_child_get(bin)); ST(0) = sv_newmortal(); sv_setref_pv(ST(0), class, (void*)button); XSRETURN(1);

I'm not sure that's quite right, because after spelunking your typemap and your .xs file, I still don't grok how you're getting hash objects -- but you get the general idea.

The fact that I'm a half-decent XS hacker and I can't figure out how to do this says something about its maintainability. :)

--
Marvin Humphrey
Rectangular Research ― http://www.rectangular.com

Replies are listed 'Best First'.
Re^2: Keeping track of classes
by Leviathan (Scribe) on Sep 05, 2006 at 08:40 UTC

    Thank you for your reply.

    I did some further digging around and found that the C objects keep track of the hierarchy, so that's how I solved that problem:

    I changed Etk_Widget in the typemap to call a function that takes the object, check the type through the C library and get a string representation of the type, I then use that to get the appropriate perl class and create the object like this.

    Latest cvs code is here. Object creation is now more simplified, so it should be obvious how it is being done.

    --
    Leviathan