Loading...
Searching...
No Matches
juce_Singleton.h File Reference

## Classes

struct | juce::SingletonHolder< Type, MutexType, onlyCreateOncePerRun >
| Used by the JUCE_DECLARE_SINGLETON macros to manage a static pointer to a singleton instance. More...

## Namespaces

namespace | juce

## Macros

#define | JUCE_DECLARE_SINGLETON(Classname, doNotRecreateAfterDeletion)
| Macro to generate the appropriate methods and boilerplate for a singleton class.
#define | JUCE_DECLARE_SINGLETON_INLINE(Classname, doNotRecreateAfterDeletion)
| The same as JUCE_DECLARE_SINGLETON, but does not require a matching JUCE_IMPLEMENT_SINGLETON definition.
#define | JUCE_IMPLEMENT_SINGLETON(Classname)
| This is a counterpart to the JUCE_DECLARE_SINGLETON macros.
#define | JUCE_DECLARE_SINGLETON_SINGLETHREADED(Classname, doNotRecreateAfterDeletion)
| Macro to declare member variables and methods for a singleton class.
#define | JUCE_DECLARE_SINGLETON_SINGLETHREADED_INLINE(Classname, doNotRecreateAfterDeletion)
| The same as JUCE_DECLARE_SINGLETON_SINGLETHREADED, but does not require a matching JUCE_IMPLEMENT_SINGLETON definition.
#define | JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL(Classname)
| Macro to declare member variables and methods for a singleton class.
#define | JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL_INLINE(Classname)
| The same as JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL, but does not require a matching JUCE_IMPLEMENT_SINGLETON definition.

## Macro Definition Documentation

## JUCE_DECLARE_SINGLETON

#define JUCE_DECLARE_SINGLETON | ( |  | Classname,
|  |  | doNotRecreateAfterDeletion )

Value:
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::CriticalSection, doNotRecreateAfterDeletion, , get)
juce::CriticalSection
A re-entrant mutex.
Definition juce_CriticalSection.h:55

Macro to generate the appropriate methods and boilerplate for a singleton class.

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

Then put a macro JUCE_IMPLEMENT_SINGLETON (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.

struct MySingleton
{
MySingleton() {}
~MySingleton()
{
// this ensures that no dangling pointers are left when the
// singleton is deleted.
clearSingletonInstance();
}
JUCE_DECLARE_SINGLETON (MySingleton, false)
};
// ...and this goes in a suitable .cpp file:
JUCE_IMPLEMENT_SINGLETON (MySingleton)
// example of usage:
auto* m = MySingleton::getInstance(); // creates the singleton if there isn't already one.
...
MySingleton::deleteInstance(); // safely deletes the singleton (if it's been created)
JUCE_IMPLEMENT_SINGLETON
#define JUCE_IMPLEMENT_SINGLETON(Classname)
This is a counterpart to the JUCE_DECLARE_SINGLETON macros.
Definition juce_Singleton.h:225
JUCE_DECLARE_SINGLETON
#define JUCE_DECLARE_SINGLETON(Classname, doNotRecreateAfterDeletion)
Macro to generate the appropriate methods and boilerplate for a singleton class.
Definition juce_Singleton.h:205

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_DECLARE_SINGLETON_SINGLETHREADED macro instead of this one.

See also
  JUCE_IMPLEMENT_SINGLETON, JUCE_DECLARE_SINGLETON_SINGLETHREADED

## JUCE_DECLARE_SINGLETON_INLINE

#define JUCE_DECLARE_SINGLETON_INLINE | ( |  | Classname,
|  |  | doNotRecreateAfterDeletion )

Value:
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::CriticalSection, doNotRecreateAfterDeletion, inline, get)

The same as JUCE_DECLARE_SINGLETON, but does not require a matching JUCE_IMPLEMENT_SINGLETON definition.

## JUCE_IMPLEMENT_SINGLETON

#define JUCE_IMPLEMENT_SINGLETON | ( |  | Classname | ) |

Value:
decltype (Classname::singletonHolder) Classname::singletonHolder;

This is a counterpart to the JUCE_DECLARE_SINGLETON macros.

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

This macro is not required for singletons declared with the INLINE macros, specifically JUCE_DECLARE_SINGLETON_INLINE, JUCE_DECLARE_SINGLETON_SINGLETHREADED_INLINE, and JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL_INLINE.

## JUCE_DECLARE_SINGLETON_SINGLETHREADED

#define JUCE_DECLARE_SINGLETON_SINGLETHREADED | ( |  | Classname,
|  |  | doNotRecreateAfterDeletion )

Value:
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::DummyCriticalSection, doNotRecreateAfterDeletion, , get)
juce::DummyCriticalSection
A class that can be used in place of a real CriticalSection object, but which doesn't perform any loc...
Definition juce_CriticalSection.h:146

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

This is exactly the same as JUCE_DECLARE_SINGLETON, 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_DECLARE_SINGLETON for more information about how to use it. Just like JUCE_DECLARE_SINGLETON you need to also have a corresponding JUCE_IMPLEMENT_SINGLETON statement somewhere in your code.

See also
  JUCE_IMPLEMENT_SINGLETON, JUCE_DECLARE_SINGLETON, JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL

## JUCE_DECLARE_SINGLETON_SINGLETHREADED_INLINE

#define JUCE_DECLARE_SINGLETON_SINGLETHREADED_INLINE | ( |  | Classname,
|  |  | doNotRecreateAfterDeletion )

Value:
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::DummyCriticalSection, doNotRecreateAfterDeletion, inline, get)

The same as JUCE_DECLARE_SINGLETON_SINGLETHREADED, but does not require a matching JUCE_IMPLEMENT_SINGLETON definition.

## JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL

#define JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL | ( |  | Classname | ) |

Value:
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::DummyCriticalSection, false, , getWithoutChecking)

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

This is like JUCE_DECLARE_SINGLETON_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.

See the documentation for JUCE_DECLARE_SINGLETON for more information about how to use it. Just like JUCE_DECLARE_SINGLETON you need to also have a corresponding JUCE_IMPLEMENT_SINGLETON statement somewhere in your code.

See also
  JUCE_IMPLEMENT_SINGLETON, JUCE_DECLARE_SINGLETON

## JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL_INLINE

#define JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL_INLINE | ( |  | Classname | ) |

Value:
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::DummyCriticalSection, false, inline, getWithoutChecking)

The same as JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL, but does not require a matching JUCE_IMPLEMENT_SINGLETON definition.

### Purchase

Get JUCE

### Discover

What's New in JUCE Features

### Learn

Documentaion Tutorials Made with JUCE Resources

### Support

JUCE Forum Newsletter Archive

### About

Contact Us JUCE Legal JUCE Licensing FAQ

### Events

Audio Developer Conference
© Raw Material Software Limited