4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief singleton public header */
31 #ifndef HH_SENF_Utils_singleton_
32 #define HH_SENF_Utils_singleton_ 1
35 #include <boost/utility.hpp>
37 //#include "singleton.mpp"
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42 /** \brief Mark a class as singleton and provide singleton accessor
44 This mixin class will mark a class as singleton and provide an accessor function to access
45 this singleton instance. The following preconditions must be met for this class to work as
47 \li There must be only a single thread executing before main() starts. (This should always
49 \li There must be only a single thread executing after main() ends. (This is always
50 important, otherwise global object destruction might fail)
51 \li The singleton class must have a non throwing default constructor and destructor
53 If these conditions are met, this mixin will ensure that the singleton is constructed \e
54 before main even starts executing. If static construction code calls the instance() member,
55 it is ensured, that the instance is constructed no later than the first call to instance().
60 : public senf::singleton<SomeClass>
62 // Must have default constructor
65 // Give singleton access to the constructor
66 friend class senf::singleton<SomeClass>;
69 // By default 'instance()' is protected. If you want, you may make it public:
70 using senf::singleton<SomeClass>::instance;
75 int main(int argc, char ** argv)
77 // At this point, the instance has already been constructed
79 SomeClass::instance().doSomething();
83 \warning The singleton class should \e not have any static data members since it cannot be
84 guaranteed, that these members will be constructed before the singleton instance.
86 \implementation This implementation is directly taken from
87 <tt>boost/pool/detail/singleton.hpp</tt>. See that file for a description of the
88 technique. The only difference is, that I prefer to advertise this class as a mixin
89 (though it may be used the same way as the original too).
99 static Self & instance(); ///< Return singleton instance
100 static bool alive(); ///< Return \c true, if instance ok, \c false otherwise
106 struct force_creation
112 static force_creation creator_;
118 //-/////////////////////////////////////////////////////////////////////////////////////////////////
119 //#include "singleton.cci"
120 //#include "singleton.ct"
121 #include "singleton.cti"
128 // c-file-style: "senf"
129 // indent-tabs-mode: nil
130 // ispell-local-dictionary: "american"
131 // compile-command: "scons -u test"
132 // comment-column: 40