Packets: Fix VariantParser invalid parser access bug
[senf.git] / Utils / singleton.hh
index 24cb1e0..b7b2814 100644 (file)
@@ -1,8 +1,8 @@
 // $Id$
 //
-// Copyright (C) 2007 
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
+// Copyright (C) 2007
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
 //     Stefan Bund <g0dil@berlios.de>
 //
 // This program is free software; you can redistribute it and/or modify
@@ -23,8 +23,8 @@
 /** \file
     \brief singleton public header */
 
-#ifndef HH_singleton_
-#define HH_singleton_ 1
+#ifndef HH_SENF_Utils_singleton_
+#define HH_SENF_Utils_singleton_ 1
 
 // Custom includes
 #include <boost/utility.hpp>
@@ -41,20 +41,29 @@ namespace senf {
         advertised:
         \li There must be only a single thread executing before main() starts. (This should always
             be the case)
-        \li The singleton class must have a default constructor
+        \li There must be only a single thread executing after main() ends. (This is always
+            important, otherwise global object destruction might fail)
+        \li The singleton class must have a non throwing default constructor and destructor
 
         If these conditions are met, this mixin will ensure that the singleton is constructed \e
         before main even starts executing. If static construction code calls the instance() member,
-        it is ensured, that a valid instance is returned.
+        it is ensured, that the instance is constructed no later than the first call to instance().
 
         Usage example:
         \code
           class SomeClass
               : public senf::singleton<SomeClass>
           {
-          public:
-              SomeClass(); // Must have default constructor
-              
+              // Must have default constructor
+              SomeClass();
+
+              // Give singleton access to the constructor
+              friend class senf::singleton<SomeClass>;
+
+        public:
+              // By default 'instance()' is protected. If you want, you may make it public:
+              using senf::singleton<SomeClass>::instance;
+
               // ...
           };
 
@@ -66,7 +75,10 @@ namespace senf {
           }
         \endcode
 
-        \note This implementation is directly taken from
+        \warning The singleton class should \e not have any static data members since it cannot be
+            guaranteed, that these members will be constructed before the singleton instance.
+
+        \implementation This implementation is directly taken from
             <tt>boost/pool/detail/singleton.hpp</tt>. See that file for a description of the
             technique. The only difference is, that I prefer to advertise this class as a mixin
             (though it may be used the same way as the original too).
@@ -75,20 +87,25 @@ namespace senf {
     class singleton
         : boost::noncopyable
     {
-    public:
-        static Self & instance(); ///< Return singleton instance
+    protected:
+        singleton();
+        ~singleton();
+
+        static Self & instance();       ///< Return singleton instance
+        static bool alive();            ///< Return \c true, if instance ok, \c false otherwise
 
     private:
         /** \brief Internal
             \internal
          */
-        struct force_creation 
+        struct force_creation
         {
             force_creation();
             void nop() const;
         };
 
         static force_creation creator_;
+        static bool alive_;
     };
 
 }
@@ -96,7 +113,7 @@ namespace senf {
 ///////////////////////////////hh.e////////////////////////////////////////
 //#include "singleton.cci"
 //#include "singleton.ct"
-//#include "singleton.cti"
+#include "singleton.cti"
 #endif
 
 \f