Re: Hoe to create Singleton objects with GObjects ?



On Tue, 2007-06-26 at 16:04 +0100, Emmanuele Bassi wrote:
> hi;
> 
> I assume you're referring to the C bindings for GObject.
> 
> On Tue, 2007-06-26 at 20:11 +0530, Gautam Ravi wrote:
> 
> > I dont know whether this is the right place to ask this question, I
> > could not find any other list. Please excuse ;-)
> > 
> > I have two things to ask,
> > 
> > 1. Is it possible to create singleton objects in GObjects ? 
> 
> yes, there are two methods.
> 
>   1. you provide a foo_object_new() function and store the pointer
>      g_object_new() returns inside a static variable, and for any
>      subsequent call you return that pointer;
>   2. you override the GObjectClass::constructor() vfunc and you
>      store the pointer of the new instance there, returning the
>      same instance.

This would confuse language bindings and would probably confuse other
developers.

A simple foo_object_get_instance() is usually fine.

> the second method has the advantage that it works even if you call
> g_object_new(FOO_TYPE_OBEJCT, NULL) directly instead of passing through
> your own constructor function.
> 
> > 2. Is it possible to create a final class ? (i.e A class which cannot
> > be derived) in GObjects?
> 
> yes. typedef your struct inside the header file, but define it inside
> your c source file. if someone tries to subclass that object the
> compiler will print an error because the type is incomplete.

-- 
Murray Cumming
murrayc murrayc com
www.murrayc.com
www.openismus.com




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