singleton::exists() addition for K_GLOBAL_STATIC

David Faure faure at kde.org
Sun May 4 13:12:53 BST 2008


I'm porting code like this
void KonqFMSettings::reparseConfiguration()
{
    if (globalEmbedSettings) {
        globalEmbedSettings->init();
    }
}
to K_GLOBAL_STATIC.
It used to do nothing if the singleton hadn't been constructed yet, but now it would create it
just to reparse configuration, which is useless in this case.

How about this addition?

Index: kglobal.h
===================================================================
--- kglobal.h   (revision 802526)
+++ kglobal.h   (working copy)
@@ -266,6 +266,10 @@ static struct K_GLOBAL_STATIC_STRUCT_NAM
     {                                                                          \
         return _k_static_##NAME##_destroyed;                                   \
     }                                                                          \
+    bool exists()                                                              \
+    {                                                                          \
+        return _k_static_##NAME != 0;                                          \
+    }                                                                          \
     inline operator TYPE*()                                                    \
     {                                                                          \
         return operator->();                                                   \

Then I could write 
    if (globalEmbedSettings.exists()) {
        globalEmbedSettings->init();
    }

-- 
David Faure, faure at kde.org, sponsored by Trolltech to work on KDE,
Konqueror (http://www.konqueror.org), and KOffice (http://www.koffice.org).




More information about the kde-core-devel mailing list