Cbeam
Loading...
Searching...
No Matches
cbeam::lifecycle::singleton< T, Args > Class Template Reference

#include <singleton.hpp>

Inheritance diagram for cbeam::lifecycle::singleton< T, Args >:
Collaboration diagram for cbeam::lifecycle::singleton< T, Args >:

Public Member Functions

 singleton (private_construction_tag, const std::string &name, Args... args)
 Internal constructor. Use get() to create a new instance.
 
 ~singleton () noexcept override
 
- Public Member Functions inherited from cbeam::lifecycle::singleton_control
virtual ~singleton_control ()=default
 

Static Public Member Functions

static std::shared_ptr< T > get (const std::string &name, Args... args)
 Retrieves (or creates) the shared instance of type T by name.
 
static void release (const std::string &name)
 Explicitly removes the named singleton instance from the global map.
 
- Static Public Member Functions inherited from cbeam::lifecycle::singleton_control
static void reset ()
 Resets (shuts down) all stored singleton instances.
 
static void set_operational ()
 Ends the shut-down state and allows singletons to be created again.
 

Protected Member Functions

void release_instance () override
 Called by singleton_control::reset() to release the managed resource.
 

Additional Inherited Members

- Static Protected Attributes inherited from cbeam::lifecycle::singleton_control
static std::map< std::string, std::unique_ptr< singleton_control > > _instances
 
static std::mutex _mutex
 
static bool _shutdown {false}
 

Constructor & Destructor Documentation

◆ singleton()

template<typename T, typename... Args>
cbeam::lifecycle::singleton< T, Args >::singleton ( private_construction_tag ,
const std::string & name,
Args... args )
inline

Internal constructor. Use get() to create a new instance.

◆ ~singleton()

template<typename T, typename... Args>
cbeam::lifecycle::singleton< T, Args >::~singleton ( )
inlineoverridenoexcept

Member Function Documentation

◆ get()

template<typename T, typename... Args>
static std::shared_ptr< T > cbeam::lifecycle::singleton< T, Args >::get ( const std::string & name,
Args... args )
inlinestatic

Retrieves (or creates) the shared instance of type T by name.

If _shutdown is true, returns nullptr immediately. Otherwise, it either returns the existing instance (if present) or creates a new one.

Parameters
nameUnique name for the resource.
argsForwarded constructor arguments for T if a new instance is created.
Returns
A std::shared_ptr<T> to the managed resource, or nullptr if shut down.
Exceptions
cbeam::error::runtime_errorif a type conflict arises (rare).

◆ release()

template<typename T, typename... Args>
static void cbeam::lifecycle::singleton< T, Args >::release ( const std::string & name)
inlinestatic

Explicitly removes the named singleton instance from the global map.

If you need a full reset of all singletons, use singleton_control::reset().

Parameters
nameName of the resource to remove.

◆ release_instance()

template<typename T, typename... Args>
void cbeam::lifecycle::singleton< T, Args >::release_instance ( )
inlineoverrideprotectedvirtual

Called by singleton_control::reset() to release the managed resource.

Implements cbeam::lifecycle::singleton_control.


The documentation for this class was generated from the following file: