]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/serialization/include/boost/serialization/singleton.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / serialization / include / boost / serialization / singleton.hpp
1 #ifndef BOOST_SERIALIZATION_SINGLETON_HPP
2 #define BOOST_SERIALIZATION_SINGLETON_HPP
3
4 /////////1/////////2///////// 3/////////4/////////5/////////6/////////7/////////8
5 // singleton.hpp
6 //
7 // Copyright David Abrahams 2006. Original version
8 //
9 // Copyright Robert Ramey 2007. Changes made to permit
10 // application throughout the serialization library.
11 //
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)
15 //
16 // The intention here is to define a template which will convert
17 // any class into a singleton with the following features:
18 //
19 // a) initialized before first use.
20 // b) thread-safe for const access to the class
21 // c) non-locking
22 //
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.
30 //
31
32 // MS compatible compilers support #pragma once
33 #if defined(_MSC_VER)
34 # pragma once
35 #endif
36
37 #include <boost/assert.hpp>
38 #include <boost/config.hpp>
39 #include <boost/noncopyable.hpp>
40 #include <boost/serialization/force_include.hpp>
41
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
45
46 #ifdef BOOST_MSVC
47 # pragma warning(push)
48 # pragma warning(disable : 4511 4512)
49 #endif
50
51 namespace boost {
52 namespace serialization {
53
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
58 // details.
59 //
60
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.
66
67 // This base class has two functions.
68
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.
73
74 // Second, it provides a mechanism to detect when a non-const function
75 // is called after initialization.
76
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.
83
84 class BOOST_SYMBOL_VISIBLE singleton_module :
85 public boost::noncopyable
86 {
87 private:
88 static bool & get_lock();
89 public:
90 BOOST_SERIALIZATION_DECL static void lock();
91 BOOST_SERIALIZATION_DECL static void unlock();
92 BOOST_SERIALIZATION_DECL static bool is_locked();
93 };
94
95 #include <boost/archive/detail/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
96
97 namespace detail {
98
99 template<class T>
100 class singleton_wrapper : public T
101 {
102 public:
103 static bool m_is_destroyed;
104 ~singleton_wrapper(){
105 m_is_destroyed = true;
106 }
107 };
108
109 template<class T>
110 bool detail::singleton_wrapper< T >::m_is_destroyed = false;
111
112 } // detail
113
114 template <class T>
115 class singleton : public singleton_module
116 {
117 private:
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);
126 use(& instance);
127 return static_cast<T &>(t);
128 }
129 public:
130 BOOST_DLLEXPORT static T & get_mutable_instance(){
131 BOOST_ASSERT(! is_locked());
132 return get_instance();
133 }
134 BOOST_DLLEXPORT static const T & get_const_instance(){
135 return get_instance();
136 }
137 BOOST_DLLEXPORT static bool is_destroyed(){
138 return detail::singleton_wrapper< T >::m_is_destroyed;
139 }
140 };
141
142 template<class T>
143 BOOST_DLLEXPORT T & singleton< T >::instance = singleton< T >::get_instance();
144
145 } // namespace serialization
146 } // namespace boost
147
148 #ifdef BOOST_MSVC
149 #pragma warning(pop)
150 #endif
151
152 #endif // BOOST_SERIALIZATION_SINGLETON_HPP