Geant4-11
Data Structures | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Friends
PTL::Singleton< Type, PointerT > Class Template Reference

Singleton object that allows a deleter class to be specified. More...

#include <Singleton.hh>

Data Structures

struct  persistent_data
 

Public Types

using auto_lock_t = std::unique_lock< mutex_t >
 
using deleter_t = std::function< void(PointerT &)>
 
template<bool B, typename T = int>
using enable_if_t = typename std::enable_if< B, T >::type
 
using list_t = std::set< pointer >
 
using mutex_t = std::recursive_mutex
 
using pointer = Type *
 
using smart_pointer = PointerT
 
using this_type = Singleton< Type, PointerT >
 
using thread_id_t = std::thread::id
 

Public Member Functions

void Destroy ()
 
void Initialize ()
 
void Initialize (pointer)
 
void operator delete (void *ptr)
 
void * operator new (size_t)
 
Singletonoperator= (const Singleton &)=delete
 
Singletonoperator= (Singleton &&)=delete
 
void Reset ()
 
void Reset (pointer)
 
 Singleton ()
 
 Singleton (const Singleton &)=delete
 
 Singleton (pointer)
 
 Singleton (Singleton &&)=delete
 
 ~Singleton ()
 

Static Public Member Functions

static list_t Children ()
 
static pointer GetInstance ()
 
static pointer GetMasterInstance ()
 
static thread_id_t GetMasterThreadID ()
 
static mutex_tGetMutex ()
 
static void Insert (pointer)
 
static bool IsMaster (pointer ptr)
 
static bool IsMasterThread ()
 
static void Remove (pointer)
 

Protected Member Functions

pointer GetRawInstance ()
 
smart_pointerGetSmartInstance ()
 

Static Protected Member Functions

static pointer GetRawMasterInstance ()
 
static smart_pointerGetSmartMasterInstance ()
 

Private Member Functions

template<typename Tp = Type, typename PtrT = PointerT, enable_if_t<(std::is_same< PtrT, std::shared_ptr< Tp > >::value)> = 0>
deleter_tGetDeleter ()
 
template<typename Tp = Type, typename PtrT = PointerT, enable_if_t<!(std::is_same< PtrT, std::shared_ptr< Tp > >::value)> = 0>
deleter_tGetDeleter ()
 
void operator delete[] (void *) noexcept
 
void * operator new[] (std::size_t) noexcept
 

Static Private Member Functions

static smart_pointer_local_instance ()
 
static smart_pointer_master_instance ()
 
static list_tf_children ()
 
static pointerf_master_instance ()
 
static thread_id_tf_master_thread ()
 
static mutex_tf_mutex ()
 
static persistent_dataf_persistent_data ()
 

Private Attributes

bool m_IsMaster = false
 

Friends

class Type
 

Detailed Description

template<typename Type, typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
class PTL::Singleton< Type, PointerT >

Singleton object that allows a deleter class to be specified.

Definition at line 42 of file Singleton.hh.

Member Typedef Documentation

◆ auto_lock_t

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
using PTL::Singleton< Type, PointerT >::auto_lock_t = std::unique_lock<mutex_t>

Definition at line 48 of file Singleton.hh.

◆ deleter_t

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
using PTL::Singleton< Type, PointerT >::deleter_t = std::function<void(PointerT&)>

Definition at line 52 of file Singleton.hh.

◆ enable_if_t

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
template<bool B, typename T = int>
using PTL::Singleton< Type, PointerT >::enable_if_t = typename std::enable_if<B, T>::type

Definition at line 55 of file Singleton.hh.

◆ list_t

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
using PTL::Singleton< Type, PointerT >::list_t = std::set<pointer>

Definition at line 50 of file Singleton.hh.

◆ mutex_t

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
using PTL::Singleton< Type, PointerT >::mutex_t = std::recursive_mutex

Definition at line 47 of file Singleton.hh.

◆ pointer

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
using PTL::Singleton< Type, PointerT >::pointer = Type*

Definition at line 49 of file Singleton.hh.

◆ smart_pointer

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
using PTL::Singleton< Type, PointerT >::smart_pointer = PointerT

Definition at line 51 of file Singleton.hh.

◆ this_type

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
using PTL::Singleton< Type, PointerT >::this_type = Singleton<Type, PointerT>

Definition at line 45 of file Singleton.hh.

◆ thread_id_t

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
using PTL::Singleton< Type, PointerT >::thread_id_t = std::thread::id

Definition at line 46 of file Singleton.hh.

Constructor & Destructor Documentation

◆ Singleton() [1/4]

template<typename Type , typename PointerT >
PTL::Singleton< Type, PointerT >::Singleton

Definition at line 236 of file Singleton.hh.

237{
238 Initialize();
239}
void Initialize()
Definition: Singleton.hh:262

◆ Singleton() [2/4]

template<typename Type , typename PointerT >
PTL::Singleton< Type, PointerT >::Singleton ( pointer  ptr)

Definition at line 244 of file Singleton.hh.

245{
246 Initialize(ptr);
247}

◆ ~Singleton()

template<typename Type , typename PointerT >
PTL::Singleton< Type, PointerT >::~Singleton

Definition at line 252 of file Singleton.hh.

253{
254 auto& del = GetDeleter();
255 del(_master_instance());
256}
static smart_pointer & _master_instance()
Definition: Singleton.hh:127
deleter_t & GetDeleter()
Definition: Singleton.hh:138

◆ Singleton() [3/4]

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
PTL::Singleton< Type, PointerT >::Singleton ( const Singleton< Type, PointerT > &  )
delete

◆ Singleton() [4/4]

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
PTL::Singleton< Type, PointerT >::Singleton ( Singleton< Type, PointerT > &&  )
delete

Member Function Documentation

◆ _local_instance()

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
static smart_pointer & PTL::Singleton< Type, PointerT >::_local_instance ( )
inlinestaticprivate

Definition at line 121 of file Singleton.hh.

122 {
123 static thread_local smart_pointer _instance = smart_pointer();
124 return _instance;
125 }
PointerT smart_pointer
Definition: Singleton.hh:51

Referenced by PTL::Singleton< Type, PointerT >::GetRawInstance(), and PTL::Singleton< Type, PointerT >::GetSmartInstance().

◆ _master_instance()

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
static smart_pointer & PTL::Singleton< Type, PointerT >::_master_instance ( )
inlinestaticprivate

Definition at line 127 of file Singleton.hh.

128 {
129 static smart_pointer _instance = smart_pointer();
130 return _instance;
131 }

Referenced by PTL::Singleton< Type, PointerT >::GetSmartMasterInstance().

◆ Children()

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
static list_t PTL::Singleton< Type, PointerT >::Children ( )
inlinestatic

Definition at line 73 of file Singleton.hh.

73{ return f_children(); }
static list_t & f_children()
Definition: Singleton.hh:228

References PTL::Singleton< Type, PointerT >::f_children().

◆ Destroy()

template<typename Type , typename PointerT >
void PTL::Singleton< Type, PointerT >::Destroy

Definition at line 288 of file Singleton.hh.

289{
290 if(std::this_thread::get_id() == f_master_thread() && f_master_instance())
291 {
292 delete f_master_instance();
293 f_master_instance() = nullptr;
294 }
295 else
296 {
297 remove(_local_instance().get());
298 }
299}
static pointer & f_master_instance()
Definition: Singleton.hh:210
static thread_id_t & f_master_thread()
Definition: Singleton.hh:201
static smart_pointer & _local_instance()
Definition: Singleton.hh:121
auto get(Tuple< Elements... > &t) -> decltype(get_height< sizeof...(Elements) - I - 1 >(t))
Definition: Tuple.hh:139

References PTL::get().

◆ f_children()

template<typename Type , typename PointerT >
Singleton< Type, PointerT >::list_t & PTL::Singleton< Type, PointerT >::f_children
staticprivate

Definition at line 228 of file Singleton.hh.

229{
231}
static persistent_data & f_persistent_data()
Definition: Singleton.hh:190

Referenced by PTL::Singleton< Type, PointerT >::Children().

◆ f_master_instance()

template<typename Type , typename PointerT >
Singleton< Type, PointerT >::pointer & PTL::Singleton< Type, PointerT >::f_master_instance
staticprivate

◆ f_master_thread()

template<typename Type , typename PointerT >
Singleton< Type, PointerT >::thread_id_t & PTL::Singleton< Type, PointerT >::f_master_thread
staticprivate

◆ f_mutex()

template<typename Type , typename PointerT >
Singleton< Type, PointerT >::mutex_t & PTL::Singleton< Type, PointerT >::f_mutex
staticprivate

Definition at line 219 of file Singleton.hh.

220{
221 return f_persistent_data().m_mutex;
222}

Referenced by PTL::Singleton< Type, PointerT >::GetMutex().

◆ f_persistent_data()

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
static persistent_data & PTL::Singleton< Type, PointerT >::f_persistent_data ( )
inlinestaticprivate

Definition at line 190 of file Singleton.hh.

191 {
192 static persistent_data _instance;
193 return _instance;
194 }

◆ GetDeleter() [1/2]

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
template<typename Tp = Type, typename PtrT = PointerT, enable_if_t<(std::is_same< PtrT, std::shared_ptr< Tp > >::value)> = 0>
deleter_t & PTL::Singleton< Type, PointerT >::GetDeleter ( )
inlineprivate

Definition at line 138 of file Singleton.hh.

139 {
140 static deleter_t _instance = [](PointerT&) {};
141 return _instance;
142 }
std::function< void(PointerT &)> deleter_t
Definition: Singleton.hh:52

◆ GetDeleter() [2/2]

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
template<typename Tp = Type, typename PtrT = PointerT, enable_if_t<!(std::is_same< PtrT, std::shared_ptr< Tp > >::value)> = 0>
deleter_t & PTL::Singleton< Type, PointerT >::GetDeleter ( )
inlineprivate

Definition at line 146 of file Singleton.hh.

147 {
148 static deleter_t _instance = [](PointerT& _master) {
149 auto& del = _master.get_deleter();
150 del(_master.get());
151 _master.reset(nullptr);
152 };
153 return _instance;
154 }

◆ GetInstance()

template<typename Type , typename PointerT >
Singleton< Type, PointerT >::pointer PTL::Singleton< Type, PointerT >::GetInstance
static

Definition at line 305 of file Singleton.hh.

306{
307 if(std::this_thread::get_id() == f_master_thread())
308 return GetMasterInstance();
309 else if(!_local_instance().get())
310 {
311 _local_instance().reset(new Type());
313 }
314 return _local_instance().get();
315}
friend class Type
Definition: Singleton.hh:106
static pointer GetMasterInstance()
Definition: Singleton.hh:321
static void Insert(pointer)
Definition: Singleton.hh:380

References PTL::get().

◆ GetMasterInstance()

template<typename Type , typename PointerT >
Singleton< Type, PointerT >::pointer PTL::Singleton< Type, PointerT >::GetMasterInstance
static

Definition at line 321 of file Singleton.hh.

322{
323 if(!f_master_instance())
324 {
325 f_master_thread() = std::this_thread::get_id();
326 f_master_instance() = new Type();
327 }
328 return f_master_instance();
329}

◆ GetMasterThreadID()

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
static thread_id_t PTL::Singleton< Type, PointerT >::GetMasterThreadID ( )
inlinestatic

Definition at line 72 of file Singleton.hh.

72{ return f_master_thread(); }

References PTL::Singleton< Type, PointerT >::f_master_thread().

◆ GetMutex()

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
static mutex_t & PTL::Singleton< Type, PointerT >::GetMutex ( )
inlinestatic

Definition at line 78 of file Singleton.hh.

78{ return f_mutex(); }
static mutex_t & f_mutex()
Definition: Singleton.hh:219

References PTL::Singleton< Type, PointerT >::f_mutex().

◆ GetRawInstance()

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
pointer PTL::Singleton< Type, PointerT >::GetRawInstance ( )
inlineprotected

◆ GetRawMasterInstance()

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
static pointer PTL::Singleton< Type, PointerT >::GetRawMasterInstance ( )
inlinestaticprotected

◆ GetSmartInstance()

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
smart_pointer & PTL::Singleton< Type, PointerT >::GetSmartInstance ( )
inlineprotected

Definition at line 109 of file Singleton.hh.

109{ return _local_instance(); }

References PTL::Singleton< Type, PointerT >::_local_instance().

◆ GetSmartMasterInstance()

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
static smart_pointer & PTL::Singleton< Type, PointerT >::GetSmartMasterInstance ( )
inlinestaticprotected

Definition at line 110 of file Singleton.hh.

110{ return _master_instance(); }

References PTL::Singleton< Type, PointerT >::_master_instance().

◆ Initialize() [1/2]

template<typename Type , typename PointerT >
void PTL::Singleton< Type, PointerT >::Initialize

Definition at line 262 of file Singleton.hh.

263{
264 if(!f_master_instance())
265 {
266 f_master_thread() = std::this_thread::get_id();
267 f_master_instance() = new Type();
268 }
269}

◆ Initialize() [2/2]

template<typename Type , typename PointerT >
void PTL::Singleton< Type, PointerT >::Initialize ( pointer  ptr)

Definition at line 275 of file Singleton.hh.

276{
277 if(!f_master_instance())
278 {
279 f_master_thread() = std::this_thread::get_id();
280 f_master_instance() = ptr;
281 }
282}

◆ Insert()

template<typename Type , typename PointerT >
void PTL::Singleton< Type, PointerT >::Insert ( pointer  itr)
static

Definition at line 380 of file Singleton.hh.

381{
382 auto_lock_t l(f_mutex());
383 f_children().insert(itr);
384}
std::unique_lock< mutex_t > auto_lock_t
Definition: Singleton.hh:48

◆ IsMaster()

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
static bool PTL::Singleton< Type, PointerT >::IsMaster ( pointer  ptr)
inlinestatic

Definition at line 74 of file Singleton.hh.

74{ return ptr == GetRawMasterInstance(); }
static pointer GetRawMasterInstance()
Definition: Singleton.hh:117

References PTL::Singleton< Type, PointerT >::GetRawMasterInstance().

◆ IsMasterThread()

template<typename Type , typename PointerT >
bool PTL::Singleton< Type, PointerT >::IsMasterThread
static

Definition at line 371 of file Singleton.hh.

372{
373 return std::this_thread::get_id() == f_master_thread();
374}

Referenced by PTL::Singleton< Type, PointerT >::GetRawInstance().

◆ operator delete()

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
void PTL::Singleton< Type, PointerT >::operator delete ( void *  ptr)
inline

Definition at line 97 of file Singleton.hh.

98 {
99 this_type* _instance = (this_type*) (ptr);
100 ::delete _instance;
101 if(std::this_thread::get_id() == f_master_thread())
102 f_master_instance() = nullptr;
103 }
Singleton< Type, PointerT > this_type
Definition: Singleton.hh:45

References PTL::Singleton< Type, PointerT >::f_master_instance(), and PTL::Singleton< Type, PointerT >::f_master_thread().

◆ operator delete[]()

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
void PTL::Singleton< Type, PointerT >::operator delete[] ( void *  )
inlineprivatenoexcept

Definition at line 134 of file Singleton.hh.

134{}

◆ operator new()

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
void * PTL::Singleton< Type, PointerT >::operator new ( size_t  )
inline

Definition at line 89 of file Singleton.hh.

90 {
91 this_type* ptr = ::new this_type();
92 return static_cast<void*>(ptr);
93 }

◆ operator new[]()

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
void * PTL::Singleton< Type, PointerT >::operator new[] ( std::size_t  )
inlineprivatenoexcept

Definition at line 133 of file Singleton.hh.

133{ return nullptr; }

◆ operator=() [1/2]

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
Singleton & PTL::Singleton< Type, PointerT >::operator= ( const Singleton< Type, PointerT > &  )
delete

◆ operator=() [2/2]

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
Singleton & PTL::Singleton< Type, PointerT >::operator= ( Singleton< Type, PointerT > &&  )
delete

◆ Remove()

template<typename Type , typename PointerT >
void PTL::Singleton< Type, PointerT >::Remove ( pointer  itr)
static

Definition at line 390 of file Singleton.hh.

391{
392 auto_lock_t l(f_mutex());
393 for(auto litr = f_children().begin(); litr != f_children().end(); ++litr)
394 {
395 if(*litr == itr)
396 {
397 f_children().erase(litr);
398 break;
399 }
400 }
401}

◆ Reset() [1/2]

template<typename Type , typename PointerT >
void PTL::Singleton< Type, PointerT >::Reset

Definition at line 359 of file Singleton.hh.

360{
361 if(IsMasterThread())
362 _master_instance().reset();
363 _local_instance().reset();
365}

References G4Threading::IsMasterThread().

◆ Reset() [2/2]

template<typename Type , typename PointerT >
void PTL::Singleton< Type, PointerT >::Reset ( pointer  ptr)

Definition at line 335 of file Singleton.hh.

336{
337 if(IsMaster(ptr))
338 {
339 if(_master_instance().get())
340 _master_instance().reset();
341 else if(f_master_instance())
342 {
343 auto& del = GetDeleter();
344 del(_master_instance());
345 f_master_instance() = nullptr;
346 }
348 }
349 else
350 {
351 _local_instance().reset();
352 }
353}
static bool IsMaster(pointer ptr)
Definition: Singleton.hh:74

References PTL::get().

Friends And Related Function Documentation

◆ Type

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
friend class Type
friend

Definition at line 106 of file Singleton.hh.

Field Documentation

◆ m_IsMaster

template<typename Type , typename PointerT = std::unique_ptr<Type, std::default_delete<Type>>>
bool PTL::Singleton< Type, PointerT >::m_IsMaster = false
private

Definition at line 184 of file Singleton.hh.


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