/Src/Dependencies/Boost/boost/pool/detail/singleton.hpp

http://hadesmem.googlecode.com/ · C++ Header · 107 lines · 38 code · 16 blank · 53 comment · 0 complexity · c4a1ae93bbd7e8e9eb5abd319cffdd7d MD5 · raw file

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