Home
Base module

Singleton

Helper template class for managing singleton objects allocated on the heap.

Singleton

#include <icy/singleton.h>
template<class S>
class Singleton

Defined in src/base/include/icy/singleton.h:24

Helper template class for managing singleton objects allocated on the heap.

List of all members

NameKindOwner
SingletonfunctionDeclared here
~SingletonfunctionDeclared here
getfunctionDeclared here
swapfunctionDeclared here
destroyfunctionDeclared here
_ptrvariableDeclared here
_mvariableDeclared here

Public Methods

ReturnNameDescription
SingletonDefaulted constructor.
S *get inlineReturns a pointer to the managed singleton, instantiating it on first call. Thread-safe; protected by an internal mutex.
S *swap inlineReplaces the managed singleton with newPtr and returns the previous instance. The caller takes ownership of the returned pointer. Thread-safe; protected by an internal mutex.
voiddestroy inlineDestroys the managed singleton instance and resets the internal pointer to null. Thread-safe; protected by an internal mutex.

Singleton

Singleton() = default

Defined in src/base/include/icy/singleton.h:27

Defaulted constructor.


get

inline

inline S * get()

Defined in src/base/include/icy/singleton.h:34

Returns a pointer to the managed singleton, instantiating it on first call. Thread-safe; protected by an internal mutex.

Returns

Pointer to the singleton instance (never null).


swap

inline

inline S * swap(S * newPtr)

Defined in src/base/include/icy/singleton.h:47

Replaces the managed singleton with newPtr and returns the previous instance. The caller takes ownership of the returned pointer. Thread-safe; protected by an internal mutex.

Parameters

  • newPtr Replacement instance (may be nullptr).

Returns

Previously managed pointer (caller must delete if non-null).


destroy

inline

inline void destroy()

Defined in src/base/include/icy/singleton.h:57

Destroys the managed singleton instance and resets the internal pointer to null. Thread-safe; protected by an internal mutex.

Private Attributes

ReturnNameDescription
std::unique_ptr< S, Deleter >_ptr
std::mutex_m

_ptr

std::unique_ptr< S, Deleter > _ptr

Defined in src/base/include/icy/singleton.h:70


_m

std::mutex _m

Defined in src/base/include/icy/singleton.h:71