Leviathan has asked for the wisdom of the Perl Monks concerning the following question:
Fellow monks,
I've written perl bindings for etk, (cpan). I'm currently writing the test suit and came across some inconsistencies:
Etk's objects mostly all extend the Etk::Widget object and the perl objects are simply blessed hash references that have an IV that points to the C object.
Most of the C functions that take objects take Etk_Widget and those that return them will also return an Etk_Widget, which the programmer has to cast to the appropriate type. Here's an example:
Etk_Bin * bin; Etk_Button * button; // assume the above initialized. etk_bin_child_set(bin, ETK_WIDGET(button); // and the get the button. button = ETK_BUTTON(etk_bin_child_get(bin));
Now in the perl version:
$bin->ChildSet($button); $button = $bin->ChildGet();
The problem is, passing $button to ChildSet works because of the inheritance, and $button is a widget, so it's cast correctly in the XS code, but ChildGet will return an Etk_Widget, and through the typemap the returned object will have Etk::Widget as a class, and therefore the programmer has to bless the object to the appropriate type, but that's not very perly.
I'm wondering on how best to keep track of what the objects were to return them. Here are the few options I can think of:
package Etk::Bin; sub ChildSet { my $self = shift; my $widget = shift; # do some checks. child_set($self, $widget); # this is the XS call $self->{_CHILD} = $widget; } sub ChildGet { my $self = shift; return $self->{_CHILD} if $self->{_CHILD}; return child_get($self); }
Any pointers or comments are greatly appreciated.
--
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Keeping track of classes
by tye (Sage) on Sep 03, 2006 at 16:53 UTC | |
|
Re: Keeping track of classes
by traveler (Parson) on Sep 03, 2006 at 17:44 UTC | |
|
Re: Keeping track of classes
by creamygoodness (Curate) on Sep 05, 2006 at 02:45 UTC | |
by Leviathan (Scribe) on Sep 05, 2006 at 08:40 UTC |