4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief singleton public header */
27 #define HH_singleton_ 1
30 #include <boost/utility.hpp>
32 //#include "singleton.mpp"
33 ///////////////////////////////hh.p////////////////////////////////////////
37 /** \brief Mark a class as singleton and provide singleton accessor
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
42 \li There must be only a single thread executing before main() starts. (This should always
44 \li There must be only a single thread executing after main() ends. (This is always
45 important, otherwise global object destruction might fail)
46 \li The singleton class must have a non throwing default constructor and destructor
48 If these conditions are met, this mixin will ensure that the singleton is constructed \e
49 before main even starts executing. If static construction code calls the instance() member,
50 it is ensured, that the instance is constructed no later than the first call to instance().
55 : public senf::singleton<SomeClass>
57 // Must have default constructor
60 // Give singleton access to the constructor
61 friend class senf::singleton<SomeClass>;
64 // By default 'instance()' is protected. If you want, you may make it public:
65 using senf::singleton<SomeClass>::instance;
70 int main(int argc, char ** argv)
72 // At this point, the instance has already been constructed
74 SomeClass::instance().doSomething();
78 \warning The singleton class should \e not have any static data members since it cannot be
79 guaranteed, that these members will be constructed before the singleton instance.
81 \implementation This implementation is directly taken from
82 <tt>boost/pool/detail/singleton.hpp</tt>. See that file for a description of the
83 technique. The only difference is, that I prefer to advertise this class as a mixin
84 (though it may be used the same way as the original too).
91 static Self & instance(); ///< Return singleton instance
103 static force_creation creator_;
108 ///////////////////////////////hh.e////////////////////////////////////////
109 //#include "singleton.cci"
110 //#include "singleton.ct"
111 #include "singleton.cti"
118 // c-file-style: "senf"
119 // indent-tabs-mode: nil
120 // ispell-local-dictionary: "american"
121 // compile-command: "scons -u test"
122 // comment-column: 40