[cairo] UserFontFace vs FtFontFace management (cairomm)

Anne et Damien Carbonne aetdcarbonne at free.fr
Fri Dec 5 14:05:59 PST 2008


Jonathon Jongsma a écrit :
> ...
> {
>    Cairo::RefPtr<Cairo::ToyFontFace> face = Cairo::ToyFontFace::create(...);
>    cr->set_font_face(face);
> } // scope for demonstration purposes
> cr->show_text("hello, world");
>
> Notice that in this example, the C++ wrapper 'face' will be destroyed by the smart pointer 
> when it reaches the closing brace, but the underlying C type (cairo_font_face_t) will live 
> on because cairo_set_font_face() takes a reference internally.  Then later if we call 
> cr->get_font_face(), we'll create a brand new C++ wrapper object for the very same 
> underlying C object.  This is usually fine because the C++ wrapper is just a very thin 
> layer that keeps no state of its own.  However, as soon as the C object needs something 
> from the C++ wrapper object (for example, it needs to call one of its virtual functions), 
> then all of a sudden it matters that the C object and the C++ wrapper have different 
> lifetimes.  I've been racking my brain trying to figure out a way to make this work, but 
> I'm afraid I don't have any good ideas so far.  Any other binding authors have any insight 
> on this?
>
>   
Sorry, if it's out of topic, but couldn't you use userdata for this?
In the Ada binding, there are "Ada handles" (in my view, they are 
equivalent to C++ smart pointers) that point to "Ada objects" that hold 
the C cairo pointers.
"Ada objects" follow the suggested class hierarchy.
When an "Ada object" is created, its reference is attached to the C 
cairo object using user data.
The "Ada object" is destroyed when the C cairo object is destroyed.
When an "Ada handle" is created, it reuses the previously created "Ada 
object" (if any) that is attached to the C object. Otherwise, a new "Ada 
object" is created and attached.
The advantage of this solution is that each C object is alway wrapped by 
only one "Ada object".
Lifetime of the "Ada object" usually starts after the Cobject, but 
always last as long as the C object.
Would this answer your problem?

Regards,

Damien




More information about the cairo mailing list