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
| Return | Name | Description |
|---|---|---|
Singleton | Defaulted constructor. | |
S * | get inline | Returns a pointer to the managed singleton, instantiating it on first call. Thread-safe; protected by an internal mutex. |
S * | swap inline | 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. |
void | destroy inline | Destroys the managed singleton instance and resets the internal pointer to null. Thread-safe; protected by an internal mutex. |
Singleton
Singleton() = defaultDefaulted 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
newPtrReplacement 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
_ptr
std::unique_ptr< S, Deleter > _ptr_m
std::mutex _m