[cairo] API completeness of reference/user_data pairs

Steve Chaplin stevech1097 at yahoo.com.au
Sun Feb 4 17:46:10 PST 2007


On Sun, 2007-02-04 at 17:47 -0500, Behdad Esfahbod wrote:
> On Mon, 2007-01-29 at 23:39 -0500, William Lahti wrote:
> > 
> > Why does user data help bindings again? 
> 
> One thing: to assign a single wrapper object to one native object.
> That has various desired effects.  Consider this construct (written for
> pycairo, but applies to all bindings):
> 
> surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, 100, 100)
> cr = cairo.Context (surface)
> surface2 = cr.get_target ()
> 
> So, ideally you should not be able to tell between surface and surface2.
> That's essentially what user data can buy you:  you attach the wrapper
> object to the native object as user data and let the native object own
> the wrapper.  Instead of keeping a separate ref count on the wrapper
> object, you just redirect ref/unref to the native object.
> 
> This gives you the desired effect.  surface and surface2 in the above
> example cannot be differentiated.  Some important uses of this are:

>   - surface and surface2 will compare equal.  This can be achieved by
> overloading the equality operator on it to compare the underlying native
> object pointer, but that get out of control soon: imagine if all objects
> had such a compare function.
> 
>   - You can subclass cairo.ImageSurface, create a context for your
> subclass surface, and get the surface back from target and see it be of
> your subclassed type, not ImageSurface.  Currently you cannot even
> subclsas ImageSurface though.  I don't see why not.  PyGtk supports that
> and it's quite essential there (to write widgets for example).
The cairo manual section "Creating a language binding / Memory Management",
says (regarding the problem of subclassing) "This is not easily fixable
without creating memory leaks, and it's probably best to simply forbid
deriving from the language objects." - and that's what pycairo does (with the
exception of cairo.Context).

It can be misleading comparing GTK+/PyGTK with cairo/pycairo. GTK+ is
deliberately written to be object-oriented (requiring GObject) and cairo
is not object-oriented, and does not require GObject.

Steve

Send instant messages to your online friends http://au.messenger.yahoo.com 


More information about the cairo mailing list