The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_SINGLETON_JUCEHEADER__
  19. #define __JUCE_SINGLETON_JUCEHEADER__
  20. //==============================================================================
  21. /**
  22. Macro to declare member variables and methods for a singleton class.
  23. To use this, add the line juce_DeclareSingleton (MyClass, doNotRecreateAfterDeletion)
  24. to the class's definition.
  25. Then put a macro juce_ImplementSingleton (MyClass) along with the class's
  26. implementation code.
  27. It's also a very good idea to also add the call clearSingletonInstance() in your class's
  28. destructor, in case it is deleted by other means than deleteInstance()
  29. Clients can then call the static method MyClass::getInstance() to get a pointer
  30. to the singleton, or MyClass::getInstanceWithoutCreating() which will return 0 if
  31. no instance currently exists.
  32. e.g. @code
  33. class MySingleton
  34. {
  35. public:
  36. MySingleton()
  37. {
  38. }
  39. ~MySingleton()
  40. {
  41. // this ensures that no dangling pointers are left when the
  42. // singleton is deleted.
  43. clearSingletonInstance();
  44. }
  45. juce_DeclareSingleton (MySingleton, false)
  46. };
  47. juce_ImplementSingleton (MySingleton)
  48. // example of usage:
  49. MySingleton* m = MySingleton::getInstance(); // creates the singleton if there isn't already one.
  50. ...
  51. MySingleton::deleteInstance(); // safely deletes the singleton (if it's been created).
  52. @endcode
  53. If doNotRecreateAfterDeletion = true, it won't allow the object to be created more
  54. than once during the process's lifetime - i.e. after you've created and deleted the
  55. object, getInstance() will refuse to create another one. This can be useful to stop
  56. objects being accidentally re-created during your app's shutdown code.
  57. If you know that your object will only be created and deleted by a single thread, you
  58. can use the slightly more efficient juce_DeclareSingleton_SingleThreaded() macro instead
  59. of this one.
  60. @see juce_ImplementSingleton, juce_DeclareSingleton_SingleThreaded
  61. */
  62. #define juce_DeclareSingleton(classname, doNotRecreateAfterDeletion) \
  63. \
  64. static classname* _singletonInstance; \
  65. static JUCE_NAMESPACE::CriticalSection _singletonLock; \
  66. \
  67. static classname* JUCE_CALLTYPE getInstance() \
  68. { \
  69. if (_singletonInstance == 0) \
  70. {\
  71. const JUCE_NAMESPACE::ScopedLock sl (_singletonLock); \
  72. \
  73. if (_singletonInstance == 0) \
  74. { \
  75. static bool alreadyInside = false; \
  76. static bool createdOnceAlready = false; \
  77. \
  78. const bool problem = alreadyInside || ((doNotRecreateAfterDeletion) && createdOnceAlready); \
  79. jassert (! problem); \
  80. if (! problem) \
  81. { \
  82. createdOnceAlready = true; \
  83. alreadyInside = true; \
  84. classname* newObject = new classname(); /* (use a stack variable to avoid setting the newObject value before the class has finished its constructor) */ \
  85. alreadyInside = false; \
  86. \
  87. _singletonInstance = newObject; \
  88. } \
  89. } \
  90. } \
  91. \
  92. return _singletonInstance; \
  93. } \
  94. \
  95. static inline classname* JUCE_CALLTYPE getInstanceWithoutCreating() throw() \
  96. { \
  97. return _singletonInstance; \
  98. } \
  99. \
  100. static void JUCE_CALLTYPE deleteInstance() \
  101. { \
  102. const JUCE_NAMESPACE::ScopedLock sl (_singletonLock); \
  103. if (_singletonInstance != 0) \
  104. { \
  105. classname* const old = _singletonInstance; \
  106. _singletonInstance = 0; \
  107. delete old; \
  108. } \
  109. } \
  110. \
  111. void clearSingletonInstance() throw() \
  112. { \
  113. if (_singletonInstance == this) \
  114. _singletonInstance = 0; \
  115. }
  116. //==============================================================================
  117. /** This is a counterpart to the juce_DeclareSingleton macro.
  118. After adding the juce_DeclareSingleton to the class definition, this macro has
  119. to be used in the cpp file.
  120. */
  121. #define juce_ImplementSingleton(classname) \
  122. \
  123. classname* classname::_singletonInstance = 0; \
  124. JUCE_NAMESPACE::CriticalSection classname::_singletonLock;
  125. //==============================================================================
  126. /**
  127. Macro to declare member variables and methods for a singleton class.
  128. This is exactly the same as juce_DeclareSingleton, but doesn't use a critical
  129. section to make access to it thread-safe. If you know that your object will
  130. only ever be created or deleted by a single thread, then this is a
  131. more efficient version to use.
  132. If doNotRecreateAfterDeletion = true, it won't allow the object to be created more
  133. than once during the process's lifetime - i.e. after you've created and deleted the
  134. object, getInstance() will refuse to create another one. This can be useful to stop
  135. objects being accidentally re-created during your app's shutdown code.
  136. See the documentation for juce_DeclareSingleton for more information about
  137. how to use it, the only difference being that you have to use
  138. juce_ImplementSingleton_SingleThreaded instead of juce_ImplementSingleton.
  139. @see juce_ImplementSingleton_SingleThreaded, juce_DeclareSingleton, juce_DeclareSingleton_SingleThreaded_Minimal
  140. */
  141. #define juce_DeclareSingleton_SingleThreaded(classname, doNotRecreateAfterDeletion) \
  142. \
  143. static classname* _singletonInstance; \
  144. \
  145. static classname* getInstance() \
  146. { \
  147. if (_singletonInstance == 0) \
  148. { \
  149. static bool alreadyInside = false; \
  150. static bool createdOnceAlready = false; \
  151. \
  152. const bool problem = alreadyInside || ((doNotRecreateAfterDeletion) && createdOnceAlready); \
  153. jassert (! problem); \
  154. if (! problem) \
  155. { \
  156. createdOnceAlready = true; \
  157. alreadyInside = true; \
  158. classname* newObject = new classname(); /* (use a stack variable to avoid setting the newObject value before the class has finished its constructor) */ \
  159. alreadyInside = false; \
  160. \
  161. _singletonInstance = newObject; \
  162. } \
  163. } \
  164. \
  165. return _singletonInstance; \
  166. } \
  167. \
  168. static inline classname* getInstanceWithoutCreating() throw() \
  169. { \
  170. return _singletonInstance; \
  171. } \
  172. \
  173. static void deleteInstance() \
  174. { \
  175. if (_singletonInstance != 0) \
  176. { \
  177. classname* const old = _singletonInstance; \
  178. _singletonInstance = 0; \
  179. delete old; \
  180. } \
  181. } \
  182. \
  183. void clearSingletonInstance() throw() \
  184. { \
  185. if (_singletonInstance == this) \
  186. _singletonInstance = 0; \
  187. }
  188. //==============================================================================
  189. /**
  190. Macro to declare member variables and methods for a singleton class.
  191. This is like juce_DeclareSingleton_SingleThreaded, but doesn't do any checking
  192. for recursion or repeated instantiation. It's intended for use as a lightweight
  193. version of a singleton, where you're using it in very straightforward
  194. circumstances and don't need the extra checking.
  195. Juce use the normal juce_ImplementSingleton_SingleThreaded as the counterpart
  196. to this declaration, as you would with juce_DeclareSingleton_SingleThreaded.
  197. See the documentation for juce_DeclareSingleton for more information about
  198. how to use it, the only difference being that you have to use
  199. juce_ImplementSingleton_SingleThreaded instead of juce_ImplementSingleton.
  200. @see juce_ImplementSingleton_SingleThreaded, juce_DeclareSingleton
  201. */
  202. #define juce_DeclareSingleton_SingleThreaded_Minimal(classname) \
  203. \
  204. static classname* _singletonInstance; \
  205. \
  206. static classname* getInstance() \
  207. { \
  208. if (_singletonInstance == 0) \
  209. _singletonInstance = new classname(); \
  210. \
  211. return _singletonInstance; \
  212. } \
  213. \
  214. static inline classname* getInstanceWithoutCreating() throw() \
  215. { \
  216. return _singletonInstance; \
  217. } \
  218. \
  219. static void deleteInstance() \
  220. { \
  221. if (_singletonInstance != 0) \
  222. { \
  223. classname* const old = _singletonInstance; \
  224. _singletonInstance = 0; \
  225. delete old; \
  226. } \
  227. } \
  228. \
  229. void clearSingletonInstance() throw() \
  230. { \
  231. if (_singletonInstance == this) \
  232. _singletonInstance = 0; \
  233. }
  234. //==============================================================================
  235. /** This is a counterpart to the juce_DeclareSingleton_SingleThreaded macro.
  236. After adding juce_DeclareSingleton_SingleThreaded or juce_DeclareSingleton_SingleThreaded_Minimal
  237. to the class definition, this macro has to be used somewhere in the cpp file.
  238. */
  239. #define juce_ImplementSingleton_SingleThreaded(classname) \
  240. \
  241. classname* classname::_singletonInstance = 0;
  242. #endif // __JUCE_SINGLETON_JUCEHEADER__