K_GLOBAL_STATIC and other macros

Thomas Zander zander at kde.org
Fri Apr 13 11:37:58 BST 2007


On Friday 13 April 2007 11:27, Thiago Macieira wrote:
> > I'm looking for a way to;
> > init the static to zero initially;
> > Define a method like 'instance' or 'self' that either returns an
> > existing one or creates a new one including some initialisation code.
>
> K_GLOBAL_STATIC(MyType, name)
>
> Usage:
>    [...]
>    name->member = foo;
>    name->setFoo(foo);
>    MyType *ptr = name;
>    MyType &ref = *name;
>
> I.e., "name" behaves just like a pointer to MyType (MyType*).

I'm sorry; the construct "MyType(MyType*)" is unknown to me.  I still 
don't really know how to use this.

On Friday 13 April 2007 11:30, Jos van den Oever wrote:
> This is really easy:
> MyObject&
> MyObject::self() {
>     static MyObject object;
>     return object;
> }

This also does not answer my question; where can I place my initialisation 
code?

For example my exiting code (that still needs a singleton definition in 
the header file) is;

KoShapeRegistry* KoShapeRegistry::instance()
{
    if(KoShapeRegistry::m_singleton == 0)
    {
        staticShapeRegistryDeleter.setObject(m_singleton, new 
KoShapeRegistry());
        KoShapeRegistry::m_singleton->init();
    }
    return KoShapeRegistry::m_singleton;
}


Where can I call my init() method in the new style?
Note that init can not move to the constructor as the init does loading of 
plugins which may call the instance() method.  So we'd get into an 
endless loop.

-- 
Thomas Zander
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20070413/a2c667c3/attachment.sig>


More information about the kde-core-devel mailing list