66 std::lock_guard<std::mutex> lock(
_mutex);
70 instance.second->release_instance();
71 instance.second.reset();
84 CBEAM_LOG_DEBUG(
"cbeam::lifecycle::singleton_control::set_operational");
95 static inline std::map<std::string, std::unique_ptr<singleton_control>>
_instances;
151 template <
typename T,
typename... Args>
155 struct private_construction_tag
163 singleton(private_construction_tag,
const std::string& name, Args... args)
165 , _instance{std::make_shared<T>(std::forward<Args>(args)...)}
186 static std::shared_ptr<T>
get(
const std::string& name, Args... args)
190 CBEAM_LOG_DEBUG(
"cbeam::lifecycle::singleton::get: " + name +
": refused to create an instance because singleton_control::reset() "
191 "had been called. Use singleton_control::set_operational() to enable again.");
194 std::lock_guard<std::mutex> lock(
_mutex);
197 const bool create_base_instance = !base_instance;
199 if (create_base_instance)
201 base_instance = std::make_unique<singleton>(private_construction_tag{}, name, std::forward<Args>(args)...);
204 auto derived_instance =
dynamic_cast<singleton*
>(base_instance.get());
206 if (derived_instance)
208 return derived_instance->_instance;
212 if (create_base_instance)
214 base_instance.reset();
230 std::lock_guard<std::mutex> lock(
_mutex);
246 CBEAM_LOG_DEBUG(
"cbeam::lifecycle::singleton::release_instance: " + _name);
257 std::shared_ptr<T> _instance;
A Cbeam-specific runtime error that also acts like std::runtime_error.
Definition runtime_error.hpp:46
Definition singleton.hpp:153
static void release(const std::string &name)
Explicitly removes the named singleton instance from the global map.
Definition singleton.hpp:227
singleton(private_construction_tag, const std::string &name, Args... args)
Internal constructor. Use get() to create a new instance.
Definition singleton.hpp:163
static std::shared_ptr< T > get(const std::string &name, Args... args)
Retrieves (or creates) the shared instance of type T by name.
Definition singleton.hpp:186
~singleton() noexcept override
Definition singleton.hpp:170
void release_instance() override
Called by singleton_control::reset() to release the managed resource.
Definition singleton.hpp:244
#define CBEAM_LOG_DEBUG(s)
Logs a debug message if CBEAM_DEBUG_LOGGING is enabled.
Definition log_manager.hpp:138
Manages the lifecycle of singletons, item registries, and scoped variables. This namespace introduces...
Definition item_registry.hpp:38
Base class for controlling the lifecycle of all singleton instances across different types.
Definition singleton.hpp:51
static std::map< std::string, std::unique_ptr< singleton_control > > _instances
Definition singleton.hpp:95
virtual ~singleton_control()=default
static bool _shutdown
Definition singleton.hpp:97
static void reset()
Resets (shuts down) all stored singleton instances.
Definition singleton.hpp:62
static std::mutex _mutex
Definition singleton.hpp:96
virtual void release_instance()=0
Releases the internal resource of the derived singleton. Called during a global reset.
static void set_operational()
Ends the shut-down state and allows singletons to be created again.
Definition singleton.hpp:82