16#ifndef GMP_DP_SINGLETON_HPP_
17#define GMP_DP_SINGLETON_HPP_
54template <
typename T,
bool LongLifeTime = false>
class singleton;
78 static T
obj(std::forward<Args>(
args)...);
116 destroyed_ ? on_dead_reference(std::forward<Args>(
args)...)
117 : create(std::forward<Args>(
args)...);
127 template <
typename...
Args>
static void create(
Args&&...
args) {
128 static T
obj(std::forward<Args>(
args)...);
132 template <
typename...
Args>
static void on_dead_reference(
Args&&...
args) {
133 create(std::forward<Args>(
args)...);
134 new (pInstance_) T(std::forward<Args>(
args)...);
135 std::atexit(kill_singleton);
139 static void kill_singleton() {
140 pInstance_->~singleton();
143 singleton(
const singleton&);
144 singleton& operator=(
const singleton&);
145 singleton(singleton&&);
146 singleton& operator=(singleton&&);
148 static dp::spin_lock lock_;
153 pInstance_ =
nullptr;
181#define GMP_DISABLE_CONSTRUCTION(Class) \
183 friend class gmp::singleton<Class>; \
A minimal spin lock built on std::atomic_flag.
static T & instance(Args &&... args)
Get the singleton instance.
static T & instance(Args &&... args)
Get the singleton instance, recreating it if necessary.
CRTP-based singleton helper with optional dead-reference recovery.