40template <
typename Type,
41 typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
54 template <
bool B,
typename T =
int>
89 void*
operator new(size_t)
92 return static_cast<void*
>(ptr);
97 void operator delete(
void* ptr)
101 if(std::this_thread::get_id() == f_master_thread())
102 f_master_instance() =
nullptr;
115 return IsMasterThread() ? f_master_instance() : _local_instance().get();
133 void*
operator new[](std::size_t)
noexcept {
return nullptr; }
134 void operator delete[](
void*)
noexcept {}
136 template <
typename Tp =
Type,
typename PtrT = PointerT,
137 enable_if_t<(std::is_same<PtrT, std::shared_ptr<Tp>>::value)> = 0>
140 static deleter_t _instance = [](PointerT&) {};
144 template <
typename Tp =
Type,
typename PtrT = PointerT,
145 enable_if_t<!(std::is_same<PtrT, std::shared_ptr<Tp>>::value)> = 0>
148 static deleter_t _instance = [](PointerT& _master) {
149 auto& del = _master.get_deleter();
151 _master.reset(
nullptr);
158 struct persistent_data
160 thread_id_t m_master_thread = std::this_thread::get_id();
162 pointer m_master_instance =
nullptr;
165 persistent_data() =
default;
166 ~persistent_data() =
default;
167 persistent_data(
const persistent_data&) =
delete;
168 persistent_data(persistent_data&&) =
delete;
169 persistent_data&
operator=(
const persistent_data&) =
delete;
170 persistent_data&
operator=(persistent_data&&) =
delete;
172 persistent_data(
pointer _master, std::thread::id _tid)
173 : m_master_thread(_tid)
174 , m_master_instance(_master)
179 m_master_instance =
nullptr;
184 bool m_IsMaster =
false;
187 static pointer& f_master_instance();
188 static list_t& f_children();
190 static persistent_data& f_persistent_data()
192 static persistent_data _instance;
199template <
typename Type,
typename Po
interT>
201Singleton<Type, PointerT>::f_master_thread()
203 return f_persistent_data().m_master_thread;
208template <
typename Type,
typename Po
interT>
210Singleton<Type, PointerT>::f_master_instance()
212 return f_persistent_data().m_master_instance;
217template <
typename Type,
typename Po
interT>
219Singleton<Type, PointerT>::f_mutex()
221 return f_persistent_data().m_mutex;
226template <
typename Type,
typename Po
interT>
228Singleton<Type, PointerT>::f_children()
230 return f_persistent_data().m_children;
235template <
typename Type,
typename Po
interT>
243template <
typename Type,
typename Po
interT>
251template <
typename Type,
typename Po
interT>
254 auto& del = GetDeleter();
255 del(_master_instance());
260template <
typename Type,
typename Po
interT>
264 if(!f_master_instance())
266 f_master_thread() = std::this_thread::get_id();
267 f_master_instance() =
new Type();
273template <
typename Type,
typename Po
interT>
277 if(!f_master_instance())
279 f_master_thread() = std::this_thread::get_id();
280 f_master_instance() = ptr;
286template <
typename Type,
typename Po
interT>
290 if(std::this_thread::get_id() == f_master_thread() && f_master_instance())
292 delete f_master_instance();
293 f_master_instance() =
nullptr;
297 remove(_local_instance().get());
303template <
typename Type,
typename Po
interT>
307 if(std::this_thread::get_id() == f_master_thread())
308 return GetMasterInstance();
309 else if(!_local_instance().get())
311 _local_instance().reset(
new Type());
312 Insert(_local_instance().get());
314 return _local_instance().get();
319template <
typename Type,
typename Po
interT>
323 if(!f_master_instance())
325 f_master_thread() = std::this_thread::get_id();
326 f_master_instance() =
new Type();
328 return f_master_instance();
333template <
typename Type,
typename Po
interT>
339 if(_master_instance().get())
340 _master_instance().reset();
341 else if(f_master_instance())
343 auto& del = GetDeleter();
344 del(_master_instance());
345 f_master_instance() =
nullptr;
347 f_persistent_data().reset();
351 _local_instance().reset();
357template <
typename Type,
typename Po
interT>
362 _master_instance().reset();
363 _local_instance().reset();
364 f_persistent_data().reset();
369template <
typename Type,
typename Po
interT>
373 return std::this_thread::get_id() == f_master_thread();
378template <
typename Type,
typename Po
interT>
383 f_children().insert(itr);
388template <
typename Type,
typename Po
interT>
393 for(
auto litr = f_children().begin(); litr != f_children().end(); ++litr)
397 f_children().erase(litr);
Singleton object that allows a deleter class to be specified.
std::unique_lock< mutex_t > auto_lock_t
std::set< pointer > list_t
std::function< void(PointerT &)> deleter_t
static pointer GetMasterInstance()
std::thread::id thread_id_t
Singleton(const Singleton &)=delete
Singleton & operator=(const Singleton &)=delete
static bool IsMasterThread()
smart_pointer & GetSmartInstance()
static pointer GetRawMasterInstance()
Singleton & operator=(Singleton &&)=delete
static pointer GetInstance()
static void Remove(pointer)
static thread_id_t GetMasterThreadID()
static mutex_t & GetMutex()
static bool IsMaster(pointer ptr)
static smart_pointer & GetSmartMasterInstance()
typename std::enable_if< B, T >::type enable_if_t
Singleton(Singleton &&)=delete
std::recursive_mutex mutex_t
Singleton< Type, PointerT > this_type
static void Insert(pointer)