Re: Introducing "toggle references"



On Tue, 2005-04-26 at 15:36 +0100, Mark McLoughlin wrote:

> 	Just to be clear, would this look like:
> 
> static void
> weak_ref_notify (gpointer  data,
>                  GObject  *gobject)
> {
>   FooObject *object = (FooObject *) data;
>   FooProxy  *proxy = (FooProxy *) gobject;
> 
>   g_object_set_data (object, "foo-proxy", NULL);
> }

Well, this needs to also remove the toggle reference 

 gobject_remove_toggle_ref (object, toggle_notify, proxy);
  
> static void
> toggle_notify (gpointer  data,
>                GObject  *gobject,
>                gboolean  is_last_ref)
> {
>   FooProxy  *proxy = (FooProxy *) data;
>   FooObject *object = (FooObject *) gobject;
> 
>   if (last_ref)
>     {
>       g_object_weak_ref (proxy, weak_ref_notify, object);
>       g_object_unref (proxy);
>     }
>   else
>     {
>       g_object_ref (proxy);
>       g_object_weak_unref (proxy, weak_ref_notify, object);
>     }
> }
> 
> void
> foo_proxy_bind_to_object (FooProxy  *proxy,
>                           FooObject *object)
> {
>   gobject_add_toggle_ref (object, toggle_notify, proxy);
>   proxy->object = object;
> 
>   g_object_set_data (object, "foo-proxy", g_object_ref (proxy));
> }
> 
> static void
> foo_proxy_finalize (FooProxy *proxy)
> {
>   if (proxy->object != NULL)
>     g_object_unref (proxy->object);
>   proxy->object = NULL;
> }
> 
> 	If so, then what's really hurting my brain is the fact that the
> reference add_toggle_ref() takes isn't the reference which toggles
> between weak and strong; its actually the reference from the object to
> the proxy which toggles.

I guess my mental conception is that a "toggle reference" is actually
a bidirectional link between the two objects... the two references
are related in intimate ways (see my fix to your example above) and
are not independent.

The part that g_object_add_toggle_ref() actually creates is the part
that is implementable in GObject ... it has to be supplemented with
code specific to the other garbage collection system.

(other than for example purposes proxying together two GObjects probably
isn't that useful in general, though I won't say its' *never* useful)

> 	Also, would it not make sense for add_toggle_ref() to return the
> reference?

I'm not sure what you mean by the 'reference' - the reference count?

> 	I guess explicitly maintaining the weak reference from the object to
> the proxy is fairly superflous in this example too ...

I think it's something you generally want. 

						Owen

Attachment: signature.asc
Description: This is a digitally signed message part



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