JUCE
Macros
juce_Singleton.h File Reference

Macros

#define juce_DeclareSingleton(classname, doNotRecreateAfterDeletion)
 Macro to declare member variables and methods for a singleton class. More...
 
#define juce_ImplementSingleton(classname)
 This is a counterpart to the juce_DeclareSingleton macro. More...
 
#define juce_DeclareSingleton_SingleThreaded(classname, doNotRecreateAfterDeletion)
 Macro to declare member variables and methods for a singleton class. More...
 
#define juce_DeclareSingleton_SingleThreaded_Minimal(classname)
 Macro to declare member variables and methods for a singleton class. More...
 
#define juce_ImplementSingleton_SingleThreaded(classname)
 This is a counterpart to the juce_DeclareSingleton_SingleThreaded macro. More...
 

Macro Definition Documentation

#define juce_DeclareSingleton (   classname,
  doNotRecreateAfterDeletion 
)

Macro to declare member variables and methods for a singleton class.

To use this, add the line juce_DeclareSingleton (MyClass, doNotRecreateAfterDeletion) to the class's definition.

Then put a macro juce_ImplementSingleton (MyClass) along with the class's implementation code.

It's also a very good idea to also add the call clearSingletonInstance() in your class's destructor, in case it is deleted by other means than deleteInstance()

Clients can then call the static method MyClass::getInstance() to get a pointer to the singleton, or MyClass::getInstanceWithoutCreating() which will return nullptr if no instance currently exists.

e.g.

1 class MySingleton
2 {
3 public:
4  MySingleton()
5  {
6  }
7 
8  ~MySingleton()
9  {
10  // this ensures that no dangling pointers are left when the
11  // singleton is deleted.
12  clearSingletonInstance();
13  }
14 
15  juce_DeclareSingleton (MySingleton, false)
16 };
17 
18 juce_ImplementSingleton (MySingleton)
19 
20 
21 // example of usage:
22 MySingleton* m = MySingleton::getInstance(); // creates the singleton if there isn't already one.
23 
24 ...
25 
26 MySingleton::deleteInstance(); // safely deletes the singleton (if it's been created).

If doNotRecreateAfterDeletion = true, it won't allow the object to be created more than once during the process's lifetime - i.e. after you've created and deleted the object, getInstance() will refuse to create another one. This can be useful to stop objects being accidentally re-created during your app's shutdown code.

If you know that your object will only be created and deleted by a single thread, you can use the slightly more efficient juce_DeclareSingleton_SingleThreaded() macro instead of this one.

See also
juce_ImplementSingleton, juce_DeclareSingleton_SingleThreaded
#define juce_ImplementSingleton (   classname)
Value:
\
classname* classname::_singletonInstance = nullptr; \
juce::CriticalSection classname::_singletonLock;

This is a counterpart to the juce_DeclareSingleton macro.

After adding the juce_DeclareSingleton to the class definition, this macro has to be used in the cpp file.

#define juce_DeclareSingleton_SingleThreaded (   classname,
  doNotRecreateAfterDeletion 
)

Macro to declare member variables and methods for a singleton class.

This is exactly the same as juce_DeclareSingleton, but doesn't use a critical section to make access to it thread-safe. If you know that your object will only ever be created or deleted by a single thread, then this is a more efficient version to use.

If doNotRecreateAfterDeletion = true, it won't allow the object to be created more than once during the process's lifetime - i.e. after you've created and deleted the object, getInstance() will refuse to create another one. This can be useful to stop objects being accidentally re-created during your app's shutdown code.

See the documentation for juce_DeclareSingleton for more information about how to use it, the only difference being that you have to use juce_ImplementSingleton_SingleThreaded instead of juce_ImplementSingleton.

See also
juce_ImplementSingleton_SingleThreaded, juce_DeclareSingleton, juce_DeclareSingleton_SingleThreaded_Minimal
#define juce_DeclareSingleton_SingleThreaded_Minimal (   classname)
Value:
\
static classname* _singletonInstance; \
\
static classname* getInstance() \
{ \
if (_singletonInstance == nullptr) \
_singletonInstance = new classname(); \
\
return _singletonInstance; \
} \
\
static inline classname* getInstanceWithoutCreating() noexcept\
{ \
return _singletonInstance; \
} \
\
static void deleteInstance() \
{ \
if (_singletonInstance != nullptr) \
{ \
classname* const old = _singletonInstance; \
_singletonInstance = nullptr; \
delete old; \
} \
} \
\
void clearSingletonInstance() noexcept\
{ \
if (_singletonInstance == this) \
_singletonInstance = nullptr; \
}

Macro to declare member variables and methods for a singleton class.

This is like juce_DeclareSingleton_SingleThreaded, but doesn't do any checking for recursion or repeated instantiation. It's intended for use as a lightweight version of a singleton, where you're using it in very straightforward circumstances and don't need the extra checking.

Juce use the normal juce_ImplementSingleton_SingleThreaded as the counterpart to this declaration, as you would with juce_DeclareSingleton_SingleThreaded.

See the documentation for juce_DeclareSingleton for more information about how to use it, the only difference being that you have to use juce_ImplementSingleton_SingleThreaded instead of juce_ImplementSingleton.

See also
juce_ImplementSingleton_SingleThreaded, juce_DeclareSingleton

Referenced by ModalComponentManager::Callback::~Callback().

#define juce_ImplementSingleton_SingleThreaded (   classname)
Value:
\
classname* classname::_singletonInstance = nullptr;

This is a counterpart to the juce_DeclareSingleton_SingleThreaded macro.

After adding juce_DeclareSingleton_SingleThreaded or juce_DeclareSingleton_SingleThreaded_Minimal to the class definition, this macro has to be used somewhere in the cpp file.