singleton.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef BOOST_SERIALIZATION_SINGLETON_HPP
  2. #define BOOST_SERIALIZATION_SINGLETON_HPP
  3. /////////1/////////2///////// 3/////////4/////////5/////////6/////////7/////////8
  4. // singleton.hpp
  5. //
  6. // Copyright David Abrahams 2006. Original version
  7. //
  8. // Copyright Robert Ramey 2007. Changes made to permit
  9. // application throughout the serialization library.
  10. //
  11. // Distributed under the Boost
  12. // Software License, Version 1.0. (See accompanying
  13. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  14. //
  15. // The intention here is to define a template which will convert
  16. // any class into a singleton with the following features:
  17. //
  18. // a) initialized before first use.
  19. // b) thread-safe for const access to the class
  20. // c) non-locking
  21. //
  22. // In order to do this,
  23. // a) Initialize dynamically when used.
  24. // b) Require that all singletons be initialized before main
  25. // is called or any entry point into the shared library is invoked.
  26. // This guarentees no race condition for initialization.
  27. // In debug mode, we assert that no non-const functions are called
  28. // after main is invoked.
  29. //
  30. // MS compatible compilers support #pragma once
  31. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  32. # pragma once
  33. #endif
  34. #include <cassert>
  35. #include <boost/noncopyable.hpp>
  36. #include <boost/serialization/force_include.hpp>
  37. namespace boost {
  38. namespace serialization {
  39. //////////////////////////////////////////////////////////////////////
  40. // Provides a dynamically-initialized (singleton) instance of T in a
  41. // way that avoids LNK1179 on vc6. See http://tinyurl.com/ljdp8 or
  42. // http://lists.boost.org/Archives/boost/2006/05/105286.php for
  43. // details.
  44. //
  45. // singletons created by this code are guarenteed to be unique
  46. // within the executable or shared library which creates them.
  47. // This is sufficient and in fact ideal for the serialization library.
  48. // The singleton is created when the module is loaded and destroyed
  49. // when the module is unloaded.
  50. // This base class has two functions.
  51. // First it provides a module handle for each singleton indicating
  52. // the executable or shared library in which it was created. This
  53. // turns out to be necessary and sufficient to implement the tables
  54. // used by serialization library.
  55. // Second, it provides a mechanism to detect when a non-const function
  56. // is called after initialization.
  57. // make a singleton to lock/unlock all singletons for alteration.
  58. // The intent is that all singletons created/used by this code
  59. // are to be initialized before main is called. A test program
  60. // can lock all the singletons when main is entereed. This any
  61. // attempt to retieve a mutable instances while locked will
  62. // generate a assertion if compiled for debug.
  63. class singleton_module : public boost::noncopyable
  64. {
  65. private:
  66. static bool & get_lock(){
  67. static bool lock = false;
  68. return lock;
  69. }
  70. public:
  71. // static const void * get_module_handle(){
  72. // return static_cast<const void *>(get_module_handle);
  73. // }
  74. static void lock(){
  75. get_lock() = true;
  76. }
  77. static void unlock(){
  78. get_lock() = false;
  79. }
  80. static bool is_locked() {
  81. return get_lock();
  82. }
  83. };
  84. namespace detail {
  85. template<class T>
  86. class singleton_wrapper : public T
  87. {
  88. public:
  89. static bool m_is_destroyed;
  90. ~singleton_wrapper(){
  91. m_is_destroyed = true;
  92. }
  93. };
  94. template<class T>
  95. bool detail::singleton_wrapper<T>::m_is_destroyed = false;
  96. } // detail
  97. template <class T>
  98. class singleton : public singleton_module
  99. {
  100. private:
  101. BOOST_DLLEXPORT static T & instance;
  102. // include this to provoke instantiation at pre-execution time
  103. static void use(T const &) {}
  104. BOOST_DLLEXPORT static T & get_instance() {
  105. static detail::singleton_wrapper<T> t;
  106. // refer to instance, causing it to be instantiated (and
  107. // initialized at startup on working compilers)
  108. assert(! detail::singleton_wrapper<T>::m_is_destroyed);
  109. use(instance);
  110. return static_cast<T &>(t);
  111. }
  112. public:
  113. BOOST_DLLEXPORT static T & get_mutable_instance(){
  114. assert(! is_locked());
  115. return get_instance();
  116. }
  117. BOOST_DLLEXPORT static const T & get_const_instance(){
  118. return get_instance();
  119. }
  120. BOOST_DLLEXPORT static bool is_destroyed(){
  121. return detail::singleton_wrapper<T>::m_is_destroyed;
  122. }
  123. };
  124. template<class T>
  125. BOOST_DLLEXPORT T & singleton<T>::instance = singleton<T>::get_instance();
  126. } // namespace serialization
  127. } // namespace boost
  128. #endif // BOOST_SERIALIZATION_SINGLETON_HPP