52template <
typename Type,
53 typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>,
65 static bool is_main_thread();
74 template <
typename TypeT,
typename Po
interT,
typename TagT>
77 static TIMEMORY_NOINLINE
thread_id_t& f_main_thread()
79 static auto _instance = std::this_thread::get_id();
89 return (std::this_thread::get_id() == f_main_thread());
97 (void) f_main_thread();
110template <
typename Type,
typename Po
interT,
typename TagT>
124 template <
bool B,
typename T =
int>
175 template <
typename PtrT = Po
interT>
177 enable_if_t<std::is_same<PtrT, std::shared_ptr<Type>>::value> = 0);
179 template <
typename PtrT = Po
interT>
181 enable_if_t<!std::is_same<PtrT, std::shared_ptr<Type>>::value> = 0);
184 struct persistent_data
187 thread_id_t m_master_thread = std::this_thread::get_id();
188 pointer m_master_instance =
nullptr;
192 persistent_data() =
default;
193 ~persistent_data() =
default;
194 persistent_data(
const persistent_data&) =
delete;
195 persistent_data(persistent_data&&) =
delete;
196 persistent_data&
operator=(
const persistent_data&) =
delete;
197 persistent_data&
operator=(persistent_data&&) =
delete;
201 m_master_instance =
nullptr;
206 static TIMEMORY_NOINLINE persistent_data& f_persistent_data()
208 static persistent_data _instance{};
214 static pointer& f_master_instance();
221template <
typename Type,
typename Po
interT,
typename TagT>
223singleton<Type, PointerT, TagT>::f_master_thread()
225 return f_persistent_data().m_master_thread;
230template <
typename Type,
typename Po
interT,
typename TagT>
232singleton<Type, PointerT, TagT>::f_master_instance()
234 return f_persistent_data().m_master_instance;
239template <
typename Type,
typename Po
interT,
typename TagT>
241singleton<Type, PointerT, TagT>::f_mutex()
243 return f_persistent_data().m_mutex;
248template <
typename Type,
typename Po
interT,
typename TagT>
250singleton<Type, PointerT, TagT>::f_children()
252 return f_persistent_data().m_children;
257template <
typename Type,
typename Po
interT,
typename TagT>
259singleton<Type, PointerT, TagT>::f_dtors()
261 return f_persistent_data().m_dtors;
266template <
typename Type,
typename Po
interT,
typename TagT>
274template <
typename Type,
typename Po
interT,
typename TagT>
277 if(std::this_thread::get_id() == f_master_thread())
278 f_master_instance() =
nullptr;
279 auto& del = get_deleter();
281 del(_master_instance());
286template <
typename Type,
typename Po
interT,
typename TagT>
290 if(!f_master_instance())
292 f_master_thread() = std::this_thread::get_id();
293 f_master_instance() =
new Type{};
299template <
typename Type,
typename Po
interT,
typename TagT>
303 if(std::this_thread::get_id() == f_master_thread())
305 return master_instance();
307 if(!_local_instance().
get())
309 _local_instance().reset(
new Type{});
310 insert(_local_instance());
312 return _local_instance().get();
317template <
typename Type,
typename Po
interT,
typename TagT>
321 if(!f_master_instance())
323 f_master_thread() = std::this_thread::get_id();
324 f_master_instance() =
new Type{};
326 return f_master_instance();
331template <
typename Type,
typename Po
interT,
typename TagT>
335 return is_master_thread() ? f_master_instance() : _local_instance().get();
340template <
typename Type,
typename Po
interT,
typename TagT>
344 return std::this_thread::get_id() == f_master_thread();
349template <
typename Type,
typename Po
interT,
typename TagT>
356 f_children().insert(itr.get());
357 f_dtors().emplace(itr.get(), [&]() { itr.reset(); });
362template <
typename Type,
typename Po
interT,
typename TagT>
371 for(
auto litr = f_children().begin(); litr != f_children().end(); ++litr)
375 f_children().erase(litr);
379 auto ditr = f_dtors().find(itr);
380 if(ditr != f_dtors().
end())
381 f_dtors().erase(ditr);
386template <
typename Type,
typename Po
interT,
typename TagT>
392 if(!f_dtors().empty())
395 std::swap(f_dtors(), _dtors);
396 for(
auto& itr : _dtors)
400 if(_master_instance().
get())
402 _master_instance().reset();
404 else if(f_master_instance())
406 auto& del = get_deleter();
407 del(_master_instance());
408 f_master_instance() =
nullptr;
410 f_persistent_data().reset();
414 remove(_local_instance().
get());
415 _local_instance().reset();
421template <
typename Type,
typename Po
interT,
typename TagT>
425 if(_local_instance())
427 remove(_local_instance().
get());
428 _local_instance().reset();
431 if(is_master_thread())
433 if(!f_dtors().empty())
436 std::swap(f_dtors(), _dtors);
437 for(
auto& itr : _dtors)
441 if(_master_instance().
get())
443 _master_instance().reset();
445 else if(f_master_instance())
447 auto& del = get_deleter();
448 del(_master_instance());
449 f_master_instance() =
nullptr;
452 f_persistent_data().reset();
457template <
typename Type,
typename Po
interT,
typename TagT>
458template <
typename PtrT>
461 enable_if_t<std::is_same<PtrT, std::shared_ptr<Type>>::value>)
463 static deleter_t _instance = [](PointerT&) {};
469template <
typename Type,
typename Po
interT,
typename TagT>
470template <
typename PtrT>
472 singleton<Type, PointerT, TagT>::get_deleter(
473 enable_if_t<!std::is_same<PtrT, std::shared_ptr<Type>>::value>)
475 static deleter_t _instance = [](PointerT& _master) {
476 auto& del = _master.get_deleter();
478 _master.reset(
nullptr);
static thread_id_t main_thread_id()
std::thread::id thread_id_t
Thread-safe singleton management.
static children_t children()
static mutex_t & get_mutex()
std::thread::id thread_id_t
std::recursive_mutex mutex_t
std::map< pointer, std::function< void()> > dtor_map_t
static smart_pointer & smart_instance()
static pointer master_instance()
std::unique_lock< mutex_t > auto_lock_t
singleton & operator=(const singleton &)=delete
std::function< void(PointerT &)> deleter_t
typename std::enable_if< B, T >::type enable_if_t
static pointer instance_ptr()
static pointer instance()
static bool is_master_thread()
singleton & operator=(singleton &&)=delete
static pointer master_instance_ptr()
static void insert(smart_pointer &itr)
static bool is_master(pointer ptr)
static smart_pointer & smart_master_instance()
singleton(const singleton &)=delete
static thread_id_t master_thread_id()
static void remove(pointer itr)
std::set< pointer > children_t
singleton(singleton &&)=delete
_reported insert(_hash_id)
void initialize(CompList< CompTypes... > &obj, std::initializer_list< EnumT > components)
typename std::enable_if< B, T >::type enable_if_t
Alias template for enable_if.
void init(Args &&... args)
auto get(const auto_bundle< Tag, Types... > &_obj)