singleton.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (C) 2000 Stephen Cleary
  2. // Copyright (C) 2008 Ion Gaztanaga
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org for updates, documentation, and revision history.
  9. //
  10. // This file is a modified file from Boost.Pool
  11. //////////////////////////////////////////////////////////////////////////////
  12. //
  13. // (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost
  14. // Software License, Version 1.0. (See accompanying file
  15. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  16. //
  17. // See http://www.boost.org/libs/container for documentation.
  18. //
  19. //////////////////////////////////////////////////////////////////////////////
  20. #ifndef BOOST_CONTAINER_DETAIL_SINGLETON_DETAIL_HPP
  21. #define BOOST_CONTAINER_DETAIL_SINGLETON_DETAIL_HPP
  22. #ifndef BOOST_CONFIG_HPP
  23. # include <boost/config.hpp>
  24. #endif
  25. #if defined(BOOST_HAS_PRAGMA_ONCE)
  26. # pragma once
  27. #endif
  28. #include <boost/container/detail/config_begin.hpp>
  29. #include <boost/container/detail/workaround.hpp>
  30. //
  31. // The following helper classes are placeholders for a generic "singleton"
  32. // class. The classes below support usage of singletons, including use in
  33. // program startup/shutdown code, AS LONG AS there is only one thread
  34. // running before main() begins, and only one thread running after main()
  35. // exits.
  36. //
  37. // This class is also limited in that it can only provide singleton usage for
  38. // classes with default constructors.
  39. //
  40. // The design of this class is somewhat twisted, but can be followed by the
  41. // calling inheritance. Let us assume that there is some user code that
  42. // calls "singleton_default<T>::instance()". The following (convoluted)
  43. // sequence ensures that the same function will be called before main():
  44. // instance() contains a call to create_object.do_nothing()
  45. // Thus, object_creator is implicitly instantiated, and create_object
  46. // must exist.
  47. // Since create_object is a static member, its constructor must be
  48. // called before main().
  49. // The constructor contains a call to instance(), thus ensuring that
  50. // instance() will be called before main().
  51. // The first time instance() is called (i.e., before main()) is the
  52. // latest point in program execution where the object of type T
  53. // can be created.
  54. // Thus, any call to instance() will auto-magically result in a call to
  55. // instance() before main(), unless already present.
  56. // Furthermore, since the instance() function contains the object, instead
  57. // of the singleton_default class containing a static instance of the
  58. // object, that object is guaranteed to be constructed (at the latest) in
  59. // the first call to instance(). This permits calls to instance() from
  60. // static code, even if that code is called before the file-scope objects
  61. // in this file have been initialized.
  62. namespace boost {
  63. namespace container {
  64. namespace dtl {
  65. // T must be: no-throw default constructible and no-throw destructible
  66. template <typename T>
  67. struct singleton_default
  68. {
  69. private:
  70. struct object_creator
  71. {
  72. // This constructor does nothing more than ensure that instance()
  73. // is called before main() begins, thus creating the static
  74. // T object before multithreading race issues can come up.
  75. object_creator() { singleton_default<T>::instance(); }
  76. inline void do_nothing() const { }
  77. };
  78. static object_creator create_object;
  79. singleton_default();
  80. public:
  81. typedef T object_type;
  82. // If, at any point (in user code), singleton_default<T>::instance()
  83. // is called, then the following function is instantiated.
  84. static object_type & instance()
  85. {
  86. // This is the object that we return a reference to.
  87. // It is guaranteed to be created before main() begins because of
  88. // the next line.
  89. static object_type obj;
  90. // The following line does nothing else than force the instantiation
  91. // of singleton_default<T>::create_object, whose constructor is
  92. // called before main() begins.
  93. create_object.do_nothing();
  94. return obj;
  95. }
  96. };
  97. template <typename T>
  98. typename singleton_default<T>::object_creator
  99. singleton_default<T>::create_object;
  100. } // namespace dtl
  101. } // namespace container
  102. } // namespace boost
  103. #include <boost/container/detail/config_end.hpp>
  104. #endif //BOOST_CONTAINER_DETAIL_SINGLETON_DETAIL_HPP