1 #ifndef BOOST_SERIALIZATION_SINGLETON_HPP
2 #define BOOST_SERIALIZATION_SINGLETON_HPP
4 /////////1/////////2///////// 3/////////4/////////5/////////6/////////7/////////8
7 // Copyright David Abrahams 2006. Original version
9 // Copyright Robert Ramey 2007. Changes made to permit
10 // application throughout the serialization library.
12 // Distributed under the Boost
13 // Software License, Version 1.0. (See accompanying
14 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
16 // The intention here is to define a template which will convert
17 // any class into a singleton with the following features:
19 // a) initialized before first use.
20 // b) thread-safe for const access to the class
23 // In order to do this,
24 // a) Initialize dynamically when used.
25 // b) Require that all singletons be initialized before main
26 // is called or any entry point into the shared library is invoked.
27 // This guarentees no race condition for initialization.
28 // In debug mode, we assert that no non-const functions are called
29 // after main is invoked.
32 // MS compatible compilers support #pragma once
37 #include <boost/assert.hpp>
38 #include <boost/config.hpp>
39 #include <boost/noncopyable.hpp>
40 #include <boost/serialization/force_include.hpp>
42 #include <boost/archive/detail/auto_link_archive.hpp>
43 #include <boost/serialization/config.hpp>
44 #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
47 # pragma warning(push)
48 # pragma warning(disable : 4511 4512)
52 namespace serialization {
54 //////////////////////////////////////////////////////////////////////
55 // Provides a dynamically-initialized (singleton) instance of T in a
56 // way that avoids LNK1179 on vc6. See http://tinyurl.com/ljdp8 or
57 // http://lists.boost.org/Archives/boost/2006/05/105286.php for
61 // singletons created by this code are guarenteed to be unique
62 // within the executable or shared library which creates them.
63 // This is sufficient and in fact ideal for the serialization library.
64 // The singleton is created when the module is loaded and destroyed
65 // when the module is unloaded.
67 // This base class has two functions.
69 // First it provides a module handle for each singleton indicating
70 // the executable or shared library in which it was created. This
71 // turns out to be necessary and sufficient to implement the tables
72 // used by serialization library.
74 // Second, it provides a mechanism to detect when a non-const function
75 // is called after initialization.
77 // make a singleton to lock/unlock all singletons for alteration.
78 // The intent is that all singletons created/used by this code
79 // are to be initialized before main is called. A test program
80 // can lock all the singletons when main is entereed. This any
81 // attempt to retieve a mutable instances while locked will
82 // generate a assertion if compiled for debug.
84 class BOOST_SYMBOL_VISIBLE singleton_module :
85 public boost::noncopyable
88 static bool & get_lock();
90 BOOST_SERIALIZATION_DECL static void lock();
91 BOOST_SERIALIZATION_DECL static void unlock();
92 BOOST_SERIALIZATION_DECL static bool is_locked();
95 #include <boost/archive/detail/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
100 class singleton_wrapper : public T
103 static bool m_is_destroyed;
104 ~singleton_wrapper(){
105 m_is_destroyed = true;
110 bool detail::singleton_wrapper< T >::m_is_destroyed = false;
115 class singleton : public singleton_module
118 BOOST_DLLEXPORT static T & instance;
119 // include this to provoke instantiation at pre-execution time
120 static void use(T const *) {}
121 BOOST_DLLEXPORT static T & get_instance() {
122 static detail::singleton_wrapper< T > t;
123 // refer to instance, causing it to be instantiated (and
124 // initialized at startup on working compilers)
125 BOOST_ASSERT(! detail::singleton_wrapper< T >::m_is_destroyed);
127 return static_cast<T &>(t);
130 BOOST_DLLEXPORT static T & get_mutable_instance(){
131 BOOST_ASSERT(! is_locked());
132 return get_instance();
134 BOOST_DLLEXPORT static const T & get_const_instance(){
135 return get_instance();
137 BOOST_DLLEXPORT static bool is_destroyed(){
138 return detail::singleton_wrapper< T >::m_is_destroyed;
143 BOOST_DLLEXPORT T & singleton< T >::instance = singleton< T >::get_instance();
145 } // namespace serialization
152 #endif // BOOST_SERIALIZATION_SINGLETON_HPP