Cbeam
|
Manages a single, shared resource of type T with controlled and explicit lifecycle management.
Manages a single, shared resource of type T with controlled and explicit lifecycle management.
In traditional singleton implementations, the singleton instance is often released at program termination by the runtime (via static object destruction). This can lead to issues if the singleton's destructor depends on resources that have already been destroyed earlier — for example, if other global or static objects that the singleton relies on have already been torn down.
By contrast, singleton
and its base class singleton_control
enable the creation and, most importantly, the explicit destruction of singleton objects without relying on the undefined order of static deinitialization. This ensures that resources can be released in a well-defined sequence at a consciously chosen point in time, which is particularly useful in scenarios with complex dependencies.
singleton<T>::get(name, args...)
either creates a new instance or returns the existing one, keyed by a string name
.singleton_control::reset()
. Once reset, no further singletons can be created until set_operational()
is called again (commonly used in tests).singleton<T>::release(name)
, which removes only the singleton with that specific name.myResource.reset()
Depending on the use case, either use the std::shared_ptr
interface, e.g. myResource.get().reset()
(also see SingletonTest::ResourceRelease) or remove the singleton globally via singleton<T>::release(name)
.
T | The resource type to manage. |
Args | Constructor parameter types for T. |