tSingleton.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _TSINGLETON_H_
  23. #define _TSINGLETON_H_
  24. #ifndef _PLATFORMASSERT_H_
  25. #include "platform/platformAssert.h"
  26. #endif
  27. /// This is a simple thread safe singleton class based on the
  28. /// design of boost::singleton_default (see http://www.boost.org/).
  29. ///
  30. /// This singleton is guaranteed to be constructed before main() is called
  31. /// and destroyed after main() exits. It will also be created on demand
  32. /// if Singleton<T>::instance() is called before main() begins.
  33. ///
  34. /// This thread safety only works within the execution context of main().
  35. /// Access to the singleton from multiple threads outside of main() is
  36. /// is not guaranteed to be thread safe.
  37. ///
  38. /// To create a singleton you only need to access it once in your code:
  39. ///
  40. /// Singleton<MySingletonClass>::instance()->myFunction();
  41. ///
  42. /// You do not need to derive from this class.
  43. ///
  44. /// @note Generally stay away from this class (!!) except if your class T
  45. /// has no meaningful constructor. Otherwise, it is very easy to make
  46. /// execution of global ctors ordering dependent.
  47. template <typename T>
  48. class Singleton
  49. {
  50. private:
  51. // This is the creator object which ensures that the
  52. // singleton is created before main() begins.
  53. struct SingletonCreator
  54. {
  55. SingletonCreator() { Singleton<T>::instance(); }
  56. // This dummy function is used below to force
  57. // singleton creation at startup.
  58. inline void forceSafeCreate() const {}
  59. };
  60. // The creator object instance.
  61. static SingletonCreator smSingletonCreator;
  62. /// This is private on purpose.
  63. Singleton();
  64. public:
  65. /// Returns the singleton instance.
  66. static T* instance()
  67. {
  68. // The singleton.
  69. static T theSingleton;
  70. // This is here to force the compiler to create
  71. // the singleton before main() is called.
  72. smSingletonCreator.forceSafeCreate();
  73. return &theSingleton;
  74. }
  75. };
  76. template <typename T>
  77. typename Singleton<T>::SingletonCreator Singleton<T>::smSingletonCreator;
  78. /// This is a managed singleton class with explict creation
  79. /// and destruction functions which must be called at startup
  80. /// and shutdown of the engine.
  81. ///
  82. /// Your class to be managed must implement the following
  83. /// function:
  84. ///
  85. /// static const char* getSingletonName() { return "YourClassName"; }
  86. ///
  87. /// This allow us to provide better asserts.
  88. ///
  89. template <typename T>
  90. class ManagedSingleton
  91. {
  92. private:
  93. static T *smSingleton;
  94. public:
  95. /// Create the singleton instance.
  96. /// @note Asserts when the singleton instance has already been constructed.
  97. static void createSingleton()
  98. {
  99. AssertFatal( smSingleton == NULL, String::ToString( "%s::createSingleton() - The singleton is already created!", T::getSingletonName() ) );
  100. smSingleton = new T();
  101. }
  102. /// Destroy the singleton instance.
  103. /// @note Asserts when no singleton has been constructed.
  104. static void deleteSingleton()
  105. {
  106. AssertFatal( smSingleton, String::ToString( "%s::deleteSingleton() - The singleton doest not exist!", T::getSingletonName() ) );
  107. delete smSingleton;
  108. smSingleton = NULL;
  109. }
  110. /// Return the singleton instance.
  111. /// @note Asserts when called before createSingleton().
  112. static T* instance()
  113. {
  114. AssertFatal( smSingleton, String::ToString( "%s::instance() - The singleton has not been created!", T::getSingletonName() ) );
  115. return smSingleton;
  116. }
  117. /// Return the singleton instance or NULL if it has been deleted or not yet constructed.
  118. static T* instanceOrNull()
  119. {
  120. return smSingleton;
  121. }
  122. };
  123. ///
  124. template <typename T>
  125. T* ManagedSingleton<T>::smSingleton = NULL;
  126. #endif //_TSINGLETON_H_