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