strange KStaticDeleter usage
Thiago Macieira
thiago at kde.org
Thu Apr 26 13:43:17 BST 2007
David Faure said:
> On Thursday 26 April 2007, Thiago Macieira wrote:
>> David Faure wrote:
>> >This is a hook for applications like kontact, which can reimplement the
>> > behavior. kontact creates and instanciates a derived class, which sets
>> > it as the singleton instead of the kdelibs one, and everything goes
>> > through it instead of using the default implementation from kdelibs.
>>
>> That's a pure singleton behaviour, which is acceptable. It's not a
>> global
>> static behaviour.
>
> And how do you implement a singleton (with a base and a derived class)
> without a global static? Apart from using KStaticDeleter? :-)
> We need something that deletes singletons at the end of the process, to
> make
> memory leak debugging simpler, and that's what KStaticDeleter was doing up
> to now.
Like this:
KMySingleton *singleton;
static void deleteSingleton()
{
delete singleton;
singleton = 0;
}
static void setSingleton(KMySingleton *ptr)
{
delete singleton;
singleton = ptr;
qAddPostRoutine(deleteSingleton);
}
Basically, KStaticDeleter is replaced by a static function that is added
via qAddPostRoutine.
Alternatively, you can construct a singleton using a global static. But
the singleton isn't *the* global static:
struct KSingletonHolder
{
KSingletonHolder() : singleton(0) { }
~KSingletonHolder() { delete singleton; singleton = 0; }
void set(KMySingleton *ptr)
{ delete singleton; singleton = ptr; }
KMySingleton *singleton;
};
K_GLOBAL_STATIC(KSingletonHolder, self)
Though this approach keeps a pointer to the pointer of the singleton object.
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
More information about the kde-core-devel
mailing list