Re: What's a boxed type? (was Re: binding how-to)



Joe Smith said:

This sent me back to re-read the 'new-gtk2-perl' doc as well.

incidentally, the newly-added Glib::devel and Gtk2::devel manpages have been
created by splitting the new-gtk2-perl document in two and taking out the
comparisons with Inline.


Now, I
finally give up and admit that I don't understand what a 'boxed' type
is.  I mean, how is a boxed type different from the other {G,Gtk}Object
wrappers?

[this is a bit wordy, but i think it answers your question completely.  let me
know if i glossed over something important.]

http://developer.gnome.org/doc/API/2.0/gobject/gobject-Boxed-Types.html
is light on information, but the title says it all: "A mechanism to wrap
opaque C structures registered by the type system."

a GObject knows what type it is, and thus the type system can figure out from
an object pointer what functions to call to destroy or otherwise manipulate
it.

but a regular old C structure is just a contiguous block of memory; it doesn't
know its type, and thus your code has to know what it's looking at in order to
use it correctly.

this makes it very difficult to create generic value handling that would be
needed to do object properties (such as GObject's properties).

a "boxed type" associates free and copy methods with a type id.  the opaque
pointer is treated like a black box, and given an opaque pointer and a type
id, you can manipulate the pointer without actually having to know what was
inside.  this is how GdkEvents and GdkRectangles and GtkTreeIters can be
marshaled through generic code in GValue objects; the generic routing code
only needs to know the expected type, which tells the framework what actual
function to call to copy whatever is on the other end of this opaque pointer.


under the hood, the perl wrappers for GBoxed types work completely differently
that the perl wrappers for GObjects.  GObjects are reference-counted, but
GBoxeds are not (some may be, but this is not true in general).  thus, we have
a combined perl/C object for GObjects, and a typical opaque pointer-style
wrapper (blessed reference to a scalar holding a pointer value) for GBoxeds.


but even that's not entirely true; some of the types which offer GBoxed
wrappers are better implemented as native perl data structures than opaque
references.  thus, the perl bindings allow you to specify a GBoxedWrapperClass
which handles wrapping and unwrapping the pointer to the boxed object.  the
default implementation uses a rather sneaky double-layer wrapper (in other
words, $$boxed doesn't actually give you the C pointer value); other
implementations convert boxed objects to perl hashes and back.


-- 
muppet <scott at asofyet dot org>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]