24cb1e0644a3914302b7226e331a10c3358bfcbb
[senf.git] / Utils / singleton.hh
1 // $Id$
2 //
3 // Copyright (C) 2007 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief singleton public header */
25
26 #ifndef HH_singleton_
27 #define HH_singleton_ 1
28
29 // Custom includes
30 #include <boost/utility.hpp>
31
32 //#include "singleton.mpp"
33 ///////////////////////////////hh.p////////////////////////////////////////
34
35 namespace senf {
36
37     /** \brief Mark a class as singleton and provide singleton accessor
38
39         This mixin class will mark a class as singleton and provide an accessor function to access
40         this singleton instance. The following preconditions must be met for this class to work as
41         advertised:
42         \li There must be only a single thread executing before main() starts. (This should always
43             be the case)
44         \li The singleton class must have a default constructor
45
46         If these conditions are met, this mixin will ensure that the singleton is constructed \e
47         before main even starts executing. If static construction code calls the instance() member,
48         it is ensured, that a valid instance is returned.
49
50         Usage example:
51         \code
52           class SomeClass
53               : public senf::singleton<SomeClass>
54           {
55           public:
56               SomeClass(); // Must have default constructor
57               
58               // ...
59           };
60
61           int main(int argc, char ** argv)
62           {
63               // At this point, the instance has already been constructed
64
65               SomeClass::instance().doSomething();
66           }
67         \endcode
68
69         \note This implementation is directly taken from
70             <tt>boost/pool/detail/singleton.hpp</tt>. See that file for a description of the
71             technique. The only difference is, that I prefer to advertise this class as a mixin
72             (though it may be used the same way as the original too).
73      */
74     template <class Self>
75     class singleton
76         : boost::noncopyable
77     {
78     public:
79         static Self & instance(); ///< Return singleton instance
80
81     private:
82         /** \brief Internal
83             \internal
84          */
85         struct force_creation 
86         {
87             force_creation();
88             void nop() const;
89         };
90
91         static force_creation creator_;
92     };
93
94 }
95
96 ///////////////////////////////hh.e////////////////////////////////////////
97 //#include "singleton.cci"
98 //#include "singleton.ct"
99 //#include "singleton.cti"
100 #endif
101
102 \f
103 // Local Variables:
104 // mode: c++
105 // fill-column: 100
106 // c-file-style: "senf"
107 // indent-tabs-mode: nil
108 // ispell-local-dictionary: "american"
109 // compile-command: "scons -u test"
110 // comment-column: 40
111 // End: