Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
PTL::Singleton< Type, PointerT > Class Template Reference

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

#include <Singleton.hh>

Public Types

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

Public Member Functions

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

Static Public Member Functions

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

Protected Member Functions

smart_pointerGetSmartInstance ()
 
pointer GetRawInstance ()
 

Static Protected Member Functions

static smart_pointerGetSmartMasterInstance ()
 
static pointer GetRawMasterInstance ()
 

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.

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}

◆ 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

◆ 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(); }

◆ 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}

◆ 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());
312 Insert(_local_instance().get());
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

◆ 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(); }

◆ 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(); }

◆ GetRawInstance()

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

Definition at line 113 of file Singleton.hh.

114 {
115 return IsMasterThread() ? f_master_instance() : _local_instance().get();
116 }
static bool IsMasterThread()
Definition Singleton.hh:371

◆ GetRawMasterInstance()

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

Definition at line 117 of file Singleton.hh.

117{ return f_master_instance(); }

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

◆ 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(); }

◆ 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(); }

◆ 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

◆ 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

◆ 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=() [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();
364 f_persistent_data().reset();
365}

◆ 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 }
347 f_persistent_data().reset();
348 }
349 else
350 {
351 _local_instance().reset();
352 }
353}
static bool IsMaster(pointer ptr)
Definition Singleton.hh:74

Friends And Related Symbol 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.


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