123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- #ifndef BOOST_SERIALIZATION_SINGLETON_HPP
- #define BOOST_SERIALIZATION_SINGLETON_HPP
- #if defined(_MSC_VER)
- # pragma once
- #endif
- #include <boost/assert.hpp>
- #include <boost/config.hpp>
- #include <boost/noncopyable.hpp>
- #include <boost/serialization/force_include.hpp>
- #include <boost/serialization/config.hpp>
- #include <boost/archive/detail/auto_link_archive.hpp>
- #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
- #ifdef BOOST_MSVC
- # pragma warning(push)
- # pragma warning(disable : 4511 4512)
- #endif
- namespace boost {
- namespace serialization {
- class BOOST_SYMBOL_VISIBLE singleton_module :
- public boost::noncopyable
- {
- private:
- BOOST_DLLEXPORT bool & get_lock() BOOST_USED {
- static bool lock = false;
- return lock;
- }
- public:
- BOOST_DLLEXPORT void lock(){
- get_lock() = true;
- }
- BOOST_DLLEXPORT void unlock(){
- get_lock() = false;
- }
- BOOST_DLLEXPORT bool is_locked(){
- return get_lock();
- }
- };
- static inline singleton_module & get_singleton_module(){
- static singleton_module m;
- return m;
- }
- namespace detail {
- template<class T>
- class singleton_wrapper : public T
- {
- static bool & get_is_destroyed(){
-
-
-
- static bool is_destroyed_flag = false;
- return is_destroyed_flag;
- }
- public:
- singleton_wrapper(){
- BOOST_ASSERT(! is_destroyed());
- }
- ~singleton_wrapper(){
- get_is_destroyed() = true;
- }
- static bool is_destroyed(){
- return get_is_destroyed();
- }
- };
- }
- template <class T>
- class singleton {
- private:
- static T * m_instance;
-
- static void use(T const &) {}
- static T & get_instance() {
- BOOST_ASSERT(! is_destroyed());
-
-
- static detail::singleton_wrapper< T > t;
-
-
-
-
-
-
-
-
- if (m_instance) use(* m_instance);
- return static_cast<T &>(t);
- }
- protected:
-
-
- BOOST_DLLEXPORT singleton(){}
- public:
- BOOST_DLLEXPORT static T & get_mutable_instance(){
- BOOST_ASSERT(! get_singleton_module().is_locked());
- return get_instance();
- }
- BOOST_DLLEXPORT static const T & get_const_instance(){
- return get_instance();
- }
- BOOST_DLLEXPORT static bool is_destroyed(){
- return detail::singleton_wrapper< T >::is_destroyed();
- }
- };
- template<class T>
- T * singleton< T >::m_instance = & singleton< T >::get_instance();
- }
- }
- #include <boost/archive/detail/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
- #ifdef BOOST_MSVC
- #pragma warning(pop)
- #endif
- #endif
|