123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- #ifndef BOOST_SERIALIZATION_SINGLETON_HPP
- #define BOOST_SERIALIZATION_SINGLETON_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1020)
- # pragma once
- #endif
- #include <cassert>
- #include <boost/noncopyable.hpp>
- #include <boost/serialization/force_include.hpp>
- namespace boost {
- namespace serialization {
- class singleton_module : public boost::noncopyable
- {
- private:
- static bool & get_lock(){
- static bool lock = false;
- return lock;
- }
- public:
- static void lock(){
- get_lock() = true;
- }
- static void unlock(){
- get_lock() = false;
- }
- static bool is_locked() {
- return get_lock();
- }
- };
- 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;
-
-
- assert(! detail::singleton_wrapper<T>::m_is_destroyed);
- use(instance);
- return static_cast<T &>(t);
- }
- public:
- BOOST_DLLEXPORT static T & get_mutable_instance(){
- 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();
- }
- }
- #endif
|