123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- #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/archive/detail/auto_link_archive.hpp>
- #include <boost/serialization/config.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:
- static bool & get_lock();
- public:
- BOOST_SERIALIZATION_DECL static void lock();
- BOOST_SERIALIZATION_DECL static void unlock();
- BOOST_SERIALIZATION_DECL static bool is_locked();
- };
- #include <boost/archive/detail/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
- namespace detail {
- template<class T>
- class singleton_wrapper : public T
- {
- public:
- static bool m_is_destroyed;
- ~singleton_wrapper(){
- m_is_destroyed = true;
- }
- };
- template<class T>
- bool detail::singleton_wrapper< T >::m_is_destroyed = false;
- }
- template <class T>
- class singleton : public singleton_module
- {
- private:
- BOOST_DLLEXPORT static T & instance;
-
- static void use(T const *) {}
- BOOST_DLLEXPORT static T & get_instance() {
- static detail::singleton_wrapper< T > t;
-
-
- BOOST_ASSERT(! detail::singleton_wrapper< T >::m_is_destroyed);
- use(& instance);
- return static_cast<T &>(t);
- }
- public:
- BOOST_DLLEXPORT static T & get_mutable_instance(){
- BOOST_ASSERT(! 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 >::m_is_destroyed;
- }
- };
- template<class T>
- BOOST_DLLEXPORT T & singleton< T >::instance = singleton< T >::get_instance();
- }
- }
- #ifdef BOOST_MSVC
- #pragma warning(pop)
- #endif
- #endif
|