openshot-audio  0.1.4
juce_Singleton.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the juce_core module of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission to use, copy, modify, and/or distribute this software for any purpose with
8  or without fee is hereby granted, provided that the above copyright notice and this
9  permission notice appear in all copies.
10 
11  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
12  TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
13  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
15  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18  ------------------------------------------------------------------------------
19 
20  NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
21  All other JUCE modules are covered by a dual GPL/commercial license, so if you are
22  using any other modules, be sure to check that you also comply with their license.
23 
24  For more details, visit www.juce.com
25 
26  ==============================================================================
27 */
28 
29 #ifndef JUCE_SINGLETON_H_INCLUDED
30 #define JUCE_SINGLETON_H_INCLUDED
31 
32 
33 //==============================================================================
92 #define juce_DeclareSingleton(classname, doNotRecreateAfterDeletion) \
93 \
94  static classname* _singletonInstance; \
95  static juce::CriticalSection _singletonLock; \
96 \
97  static classname* JUCE_CALLTYPE getInstance() \
98  { \
99  if (_singletonInstance == nullptr) \
100  {\
101  const juce::ScopedLock sl (_singletonLock); \
102 \
103  if (_singletonInstance == nullptr) \
104  { \
105  static bool alreadyInside = false; \
106  static bool createdOnceAlready = false; \
107 \
108  const bool problem = alreadyInside || ((doNotRecreateAfterDeletion) && createdOnceAlready); \
109  jassert (! problem); \
110  if (! problem) \
111  { \
112  createdOnceAlready = true; \
113  alreadyInside = true; \
114  classname* newObject = new classname(); /* (use a stack variable to avoid setting the newObject value before the class has finished its constructor) */ \
115  alreadyInside = false; \
116 \
117  _singletonInstance = newObject; \
118  } \
119  } \
120  } \
121 \
122  return _singletonInstance; \
123  } \
124 \
125  static inline classname* JUCE_CALLTYPE getInstanceWithoutCreating() noexcept\
126  { \
127  return _singletonInstance; \
128  } \
129 \
130  static void JUCE_CALLTYPE deleteInstance() \
131  { \
132  const juce::ScopedLock sl (_singletonLock); \
133  if (_singletonInstance != nullptr) \
134  { \
135  classname* const old = _singletonInstance; \
136  _singletonInstance = nullptr; \
137  delete old; \
138  } \
139  } \
140 \
141  void clearSingletonInstance() noexcept\
142  { \
143  if (_singletonInstance == this) \
144  _singletonInstance = nullptr; \
145  }
146 
147 
148 //==============================================================================
154 #define juce_ImplementSingleton(classname) \
155 \
156  classname* classname::_singletonInstance = nullptr; \
157  juce::CriticalSection classname::_singletonLock;
158 
159 
160 //==============================================================================
180 #define juce_DeclareSingleton_SingleThreaded(classname, doNotRecreateAfterDeletion) \
181 \
182  static classname* _singletonInstance; \
183 \
184  static classname* getInstance() \
185  { \
186  if (_singletonInstance == nullptr) \
187  { \
188  static bool alreadyInside = false; \
189  static bool createdOnceAlready = false; \
190 \
191  const bool problem = alreadyInside || ((doNotRecreateAfterDeletion) && createdOnceAlready); \
192  jassert (! problem); \
193  if (! problem) \
194  { \
195  createdOnceAlready = true; \
196  alreadyInside = true; \
197  classname* newObject = new classname(); /* (use a stack variable to avoid setting the newObject value before the class has finished its constructor) */ \
198  alreadyInside = false; \
199 \
200  _singletonInstance = newObject; \
201  } \
202  } \
203 \
204  return _singletonInstance; \
205  } \
206 \
207  static inline classname* getInstanceWithoutCreating() noexcept\
208  { \
209  return _singletonInstance; \
210  } \
211 \
212  static void deleteInstance() \
213  { \
214  if (_singletonInstance != nullptr) \
215  { \
216  classname* const old = _singletonInstance; \
217  _singletonInstance = nullptr; \
218  delete old; \
219  } \
220  } \
221 \
222  void clearSingletonInstance() noexcept\
223  { \
224  if (_singletonInstance == this) \
225  _singletonInstance = nullptr; \
226  }
227 
228 //==============================================================================
246 #define juce_DeclareSingleton_SingleThreaded_Minimal(classname) \
247 \
248  static classname* _singletonInstance; \
249 \
250  static classname* getInstance() \
251  { \
252  if (_singletonInstance == nullptr) \
253  _singletonInstance = new classname(); \
254 \
255  return _singletonInstance; \
256  } \
257 \
258  static inline classname* getInstanceWithoutCreating() noexcept\
259  { \
260  return _singletonInstance; \
261  } \
262 \
263  static void deleteInstance() \
264  { \
265  if (_singletonInstance != nullptr) \
266  { \
267  classname* const old = _singletonInstance; \
268  _singletonInstance = nullptr; \
269  delete old; \
270  } \
271  } \
272 \
273  void clearSingletonInstance() noexcept\
274  { \
275  if (_singletonInstance == this) \
276  _singletonInstance = nullptr; \
277  }
278 
279 
280 //==============================================================================
286 #define juce_ImplementSingleton_SingleThreaded(classname) \
287 \
288  classname* classname::_singletonInstance = nullptr;
289 
290 
291 
292 #endif // JUCE_SINGLETON_H_INCLUDED