[cairo] API completeness of reference/user_data pairs

Vladimir Vukicevic vladimir at pobox.com
Tue Jan 30 12:42:16 PST 2007


William Lahti wrote:
> On Mon, 2007-01-29 at 16:13 -0500, Behdad Esfahbod wrote:
>> I wanted to propose new API for cairo_font_options_reference().
>> I reported this in bugzilla somewhere, but can't find it.
>> In short, I think the required malloc for an opaque value type is
>> unnecessary overhead.
>>
>> But when I thought about it, I remembered that to make life
>> easier for language bindings, all referenced types need to have
>> destroy notification for the least, and user data preferably.  In
>> cairo, both are available using the user_data idiom.
> 
> Why does user data help bindings again?

For Mozilla's Cairo wrapper (Thebes), we store a pointer to the wrapper 
in a surface's user_data.  That way if we only have the cairo_surface_t* 
somewhere, we can always recover the gfxASurface* wrapper.  Before we 
started doing this, we were constantly creating and destroying wrappers, 
because we use push/pop group quite a bit and needed to get at the 
actual created internal surface.  As our wrappers were refcounted 
themselves, we've now tied that reference count directly to the cairo 
reference count for the wrapped object as well.

This makes it possible to do things like wrap image surfaces with the 
wrapper owning the data and not the cairo_surface.  It's certainly 
possible to write bindings/wrappers without destroy notification/user 
data, but it does make bindings that need to hold extra data other than 
just simply passing function calls from one thing to another easier.

     - Vlad


More information about the cairo mailing list