Singleton.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright (C) 2009-2023, 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/Assert.h>
  8. #include <utility>
  9. namespace anki {
  10. #if ANKI_ENABLE_ASSERTIONS
  11. extern I32 g_singletonsAllocated;
  12. #endif
  13. /// @addtogroup util_patterns
  14. /// @{
  15. /// If class inherits that it will become a singleton.
  16. template<typename T>
  17. class MakeSingleton
  18. {
  19. public:
  20. ANKI_FORCE_INLINE static T& getSingleton()
  21. {
  22. ANKI_ASSERT(m_initialized);
  23. return *reinterpret_cast<T*>(m_global);
  24. }
  25. template<typename... TArgs>
  26. static T& allocateSingleton(TArgs... args)
  27. {
  28. ANKI_ASSERT(!m_initialized);
  29. ::new(m_global) T(args...);
  30. m_initialized = true;
  31. #if ANKI_ENABLE_ASSERTIONS
  32. ++g_singletonsAllocated;
  33. #endif
  34. return *reinterpret_cast<T*>(m_global);
  35. }
  36. static void freeSingleton()
  37. {
  38. if(m_initialized)
  39. {
  40. reinterpret_cast<T*>(m_global)->~T();
  41. m_initialized = false;
  42. #if ANKI_ENABLE_ASSERTIONS
  43. --g_singletonsAllocated;
  44. #endif
  45. }
  46. }
  47. static Bool isAllocated()
  48. {
  49. return m_initialized;
  50. }
  51. private:
  52. static U8 m_global[];
  53. static inline Bool m_initialized = false;
  54. };
  55. template<typename T>
  56. alignas(ANKI_SAFE_ALIGNMENT) U8 MakeSingleton<T>::m_global[sizeof(T)];
  57. /// If class inherits that it will become a singleton.
  58. template<typename T>
  59. class MakeSingletonPtr
  60. {
  61. public:
  62. ANKI_FORCE_INLINE static T& getSingleton()
  63. {
  64. return *m_global;
  65. }
  66. template<typename... TArgs>
  67. static T& allocateSingleton(TArgs... args)
  68. {
  69. ANKI_ASSERT(m_global == nullptr);
  70. m_global = new T(args...);
  71. #if ANKI_ENABLE_ASSERTIONS
  72. ++g_singletonsAllocated;
  73. #endif
  74. return *m_global;
  75. }
  76. static void freeSingleton()
  77. {
  78. if(m_global)
  79. {
  80. delete m_global;
  81. m_global = nullptr;
  82. #if ANKI_ENABLE_ASSERTIONS
  83. --g_singletonsAllocated;
  84. #endif
  85. }
  86. }
  87. static Bool isAllocated()
  88. {
  89. return m_global != nullptr;
  90. }
  91. private:
  92. static inline T* m_global = nullptr;
  93. };
  94. /// If class inherits that it will become a singleton. This is a simple version without a need for init.
  95. template<typename T>
  96. class MakeSingletonSimple
  97. {
  98. public:
  99. ANKI_FORCE_INLINE static T& getSingleton()
  100. {
  101. return m_global;
  102. }
  103. private:
  104. static T m_global;
  105. };
  106. template<typename T>
  107. T MakeSingletonSimple<T>::m_global;
  108. /// If class inherits that it will become a singleton. This is a simple version with implicit init.
  109. template<typename T>
  110. class MakeSingletonLazyInit
  111. {
  112. public:
  113. ANKI_FORCE_INLINE static T& getSingleton()
  114. {
  115. if(m_global == nullptr) [[unlikely]]
  116. {
  117. m_global = new T;
  118. }
  119. return *m_global;
  120. }
  121. private:
  122. static inline T* m_global = nullptr;
  123. };
  124. /// @}
  125. } // end namespace anki