Go to the documentation of this file. 29 #ifndef JUCE_SINGLETON_H_INCLUDED 30 #define JUCE_SINGLETON_H_INCLUDED 92 #define juce_DeclareSingleton(classname, doNotRecreateAfterDeletion) \ 94 static classname* _singletonInstance; \ 95 static juce::CriticalSection _singletonLock; \ 97 static classname* JUCE_CALLTYPE getInstance() \ 99 if (_singletonInstance == nullptr) \ 101 const juce::ScopedLock sl (_singletonLock); \ 103 if (_singletonInstance == nullptr) \ 105 static bool alreadyInside = false; \ 106 static bool createdOnceAlready = false; \ 108 const bool problem = alreadyInside || ((doNotRecreateAfterDeletion) && createdOnceAlready); \ 109 jassert (! problem); \ 112 createdOnceAlready = true; \ 113 alreadyInside = true; \ 114 classname* newObject = new classname(); \ 115 alreadyInside = false; \ 117 _singletonInstance = newObject; \ 122 return _singletonInstance; \ 125 static inline classname* JUCE_CALLTYPE getInstanceWithoutCreating() noexcept\ 127 return _singletonInstance; \ 130 static void JUCE_CALLTYPE deleteInstance() \ 132 const juce::ScopedLock sl (_singletonLock); \ 133 if (_singletonInstance != nullptr) \ 135 classname* const old = _singletonInstance; \ 136 _singletonInstance = nullptr; \ 141 void clearSingletonInstance() noexcept\ 143 if (_singletonInstance == this) \ 144 _singletonInstance = nullptr; \ 154 #define juce_ImplementSingleton(classname) \ 156 classname* classname::_singletonInstance = nullptr; \ 157 juce::CriticalSection classname::_singletonLock; 180 #define juce_DeclareSingleton_SingleThreaded(classname, doNotRecreateAfterDeletion) \ 182 static classname* _singletonInstance; \ 184 static classname* getInstance() \ 186 if (_singletonInstance == nullptr) \ 188 static bool alreadyInside = false; \ 189 static bool createdOnceAlready = false; \ 191 const bool problem = alreadyInside || ((doNotRecreateAfterDeletion) && createdOnceAlready); \ 192 jassert (! problem); \ 195 createdOnceAlready = true; \ 196 alreadyInside = true; \ 197 classname* newObject = new classname(); \ 198 alreadyInside = false; \ 200 _singletonInstance = newObject; \ 204 return _singletonInstance; \ 207 static inline classname* getInstanceWithoutCreating() noexcept\ 209 return _singletonInstance; \ 212 static void deleteInstance() \ 214 if (_singletonInstance != nullptr) \ 216 classname* const old = _singletonInstance; \ 217 _singletonInstance = nullptr; \ 222 void clearSingletonInstance() noexcept\ 224 if (_singletonInstance == this) \ 225 _singletonInstance = nullptr; \ 246 #define juce_DeclareSingleton_SingleThreaded_Minimal(classname) \ 248 static classname* _singletonInstance; \ 250 static classname* getInstance() \ 252 if (_singletonInstance == nullptr) \ 253 _singletonInstance = new classname(); \ 255 return _singletonInstance; \ 258 static inline classname* getInstanceWithoutCreating() noexcept\ 260 return _singletonInstance; \ 263 static void deleteInstance() \ 265 if (_singletonInstance != nullptr) \ 267 classname* const old = _singletonInstance; \ 268 _singletonInstance = nullptr; \ 273 void clearSingletonInstance() noexcept\ 275 if (_singletonInstance == this) \ 276 _singletonInstance = nullptr; \ 286 #define juce_ImplementSingleton_SingleThreaded(classname) \ 288 classname* classname::_singletonInstance = nullptr; 292 #endif // JUCE_SINGLETON_H_INCLUDED