Base module

Singleton

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

Singleton

#include <icy/singleton.h>

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

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

Defaulted constructor.


get

inline

inline S * get()

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)

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()

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

_m

std::mutex _m