Singleton.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/StdTypes.h>
  7. #include <AnKi/Util/Thread.h>
  8. namespace anki {
  9. #if ANKI_ASSERTIONS_ENABLED
  10. extern I32 g_singletonsAllocated;
  11. #endif
  12. /// @addtogroup util_patterns
  13. /// @{
  14. /// If class inherits that it will become a singleton.
  15. template<typename T>
  16. class MakeSingleton
  17. {
  18. public:
  19. ANKI_FORCE_INLINE static T& getSingleton()
  20. {
  21. ANKI_ASSERT(m_initialized);
  22. return *reinterpret_cast<T*>(m_global);
  23. }
  24. template<typename... TArgs>
  25. static T& allocateSingleton(TArgs... args)
  26. {
  27. ANKI_ASSERT(!m_initialized);
  28. ::new(m_global) T(args...);
  29. m_initialized = true;
  30. #if ANKI_ASSERTIONS_ENABLED
  31. ++g_singletonsAllocated;
  32. #endif
  33. return *reinterpret_cast<T*>(m_global);
  34. }
  35. static void freeSingleton()
  36. {
  37. if(m_initialized)
  38. {
  39. reinterpret_cast<T*>(m_global)->~T();
  40. m_initialized = false;
  41. #if ANKI_ASSERTIONS_ENABLED
  42. --g_singletonsAllocated;
  43. #endif
  44. }
  45. }
  46. static Bool isAllocated()
  47. {
  48. return m_initialized;
  49. }
  50. private:
  51. alignas(ANKI_SAFE_ALIGNMENT) static U8 m_global[];
  52. static inline Bool m_initialized = false;
  53. };
  54. template<typename T>
  55. alignas(ANKI_SAFE_ALIGNMENT) U8 MakeSingleton<T>::m_global[sizeof(T)];
  56. /// If class inherits that it will become a singleton.
  57. template<typename T>
  58. class MakeSingletonPtr
  59. {
  60. public:
  61. ANKI_FORCE_INLINE static T& getSingleton()
  62. {
  63. return *m_global;
  64. }
  65. template<typename... TArgs>
  66. static T& allocateSingleton(TArgs... args)
  67. {
  68. ANKI_ASSERT(m_global == nullptr);
  69. m_global = new T(args...);
  70. #if ANKI_ASSERTIONS_ENABLED
  71. ++g_singletonsAllocated;
  72. #endif
  73. return *m_global;
  74. }
  75. static void freeSingleton()
  76. {
  77. if(m_global)
  78. {
  79. delete m_global;
  80. m_global = nullptr;
  81. #if ANKI_ASSERTIONS_ENABLED
  82. --g_singletonsAllocated;
  83. #endif
  84. }
  85. }
  86. static Bool isAllocated()
  87. {
  88. return m_global != nullptr;
  89. }
  90. private:
  91. static inline T* m_global = nullptr;
  92. };
  93. /// If class inherits that it will become a singleton. This is a simple version without a need for init.
  94. template<typename T>
  95. class MakeSingletonSimple
  96. {
  97. public:
  98. ANKI_FORCE_INLINE static T& getSingleton()
  99. {
  100. return m_global;
  101. }
  102. private:
  103. static T m_global;
  104. };
  105. template<typename T>
  106. T MakeSingletonSimple<T>::m_global;
  107. /// If class inherits that it will become a singleton. This is a simple version with implicit init.
  108. template<typename T, Bool kThreadsafe = true>
  109. class MakeSingletonLazyInit
  110. {
  111. public:
  112. ANKI_FORCE_INLINE static T& getSingleton()
  113. {
  114. if constexpr(kThreadsafe)
  115. {
  116. m_mtx.lock();
  117. }
  118. if(m_global == nullptr) [[unlikely]]
  119. {
  120. m_global = new T;
  121. }
  122. if constexpr(kThreadsafe)
  123. {
  124. m_mtx.unlock();
  125. }
  126. return *m_global;
  127. }
  128. private:
  129. static inline T* m_global = nullptr;
  130. static inline SpinLock m_mtx;
  131. };
  132. /// @}
  133. } // end namespace anki