HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_SingletonWithLock< T, DESTRUCTONEXIT, LOCK > Class Template Reference

#include <UT_Singleton.h>

+ Inheritance diagram for UT_SingletonWithLock< T, DESTRUCTONEXIT, LOCK >:

Public Types

typedef UT_Singleton< T,
DESTRUCTONEXIT > 
Base
 

Public Member Functions

Tget ()
 
template<typename S >
Tget (S s)
 
void unsafeSet (T *newp)
 
Toperator* ()
 
Toperator-> ()
 
- Public Member Functions inherited from UT_Singleton< T, DESTRUCTONEXIT >
 UT_Singleton ()
 
 ~UT_Singleton ()
 
template<class LOCK >
Tget (LOCK &lock)
 
template<class LOCK , typename S >
Tget (LOCK &lock, S s)
 
template<class LOCK >
void unsafeSet (LOCK &lock, T *newp)
 

Additional Inherited Members

- Protected Member Functions inherited from UT_NonCopyableNS::UT_NonCopyable
 UT_NonCopyable ()=default
 
 ~UT_NonCopyable ()=default
 
 UT_NonCopyable (const UT_NonCopyable &)=delete
 
UT_NonCopyableoperator= (const UT_NonCopyable &)=delete
 

Detailed Description

template<typename T, bool DESTRUCTONEXIT = false, class LOCK = UT_Lock>
class UT_SingletonWithLock< T, DESTRUCTONEXIT, LOCK >

This is a singleton constructed on-demand with a double-checked lock. The lock is only locked if get() is called when myPointer is 0.

This is normally simpler to use than a UT_DoubleLock, and this should be used for all on-demand singleton construction, as well as for pointer member variables that are allocated on-demand, if applicable. If it is preferable in such a case to use an existing lock instead of using the lock member in this class, please use UT_Singleton.

To use this class in the simplest case, where OBJECT is to be default-constructed:

static UT_SingletonWithLock<OBJECT> theSingleton;

void useSingleton() { OBJECT &obj = theSingleton.get(); ... }

When special construction or destruction are necessary, subclass OBJECT:

class OBJECTWrapper : public OBJECT { public: OBJECTWrapper() : OBJECT(12345) { doSpecialInit(67890); } ~OBJECTWrapper() { doBeforeDestruction(7654); } };

static UT_SingletonWithLock<OBJECTWrapper> theSingleton;

void useSingleton() { OBJECT &obj = theSingleton.get(); ... }

NOTE: Do not roll your own on-demand singleton construction! This class should be threadsafe, and it's very easy to miss a store memory fence without noticing for a very long time.

Definition at line 232 of file UT_Singleton.h.

Member Typedef Documentation

template<typename T, bool DESTRUCTONEXIT = false, class LOCK = UT_Lock>
typedef UT_Singleton<T, DESTRUCTONEXIT> UT_SingletonWithLock< T, DESTRUCTONEXIT, LOCK >::Base

Definition at line 235 of file UT_Singleton.h.

Member Function Documentation

template<typename T, bool DESTRUCTONEXIT = false, class LOCK = UT_Lock>
T& UT_SingletonWithLock< T, DESTRUCTONEXIT, LOCK >::get ( )
inline

Definition at line 237 of file UT_Singleton.h.

template<typename T, bool DESTRUCTONEXIT = false, class LOCK = UT_Lock>
template<typename S >
T& UT_SingletonWithLock< T, DESTRUCTONEXIT, LOCK >::get ( S  s)
inline

Definition at line 243 of file UT_Singleton.h.

template<typename T, bool DESTRUCTONEXIT = false, class LOCK = UT_Lock>
T& UT_SingletonWithLock< T, DESTRUCTONEXIT, LOCK >::operator* ( void  )
inline

Definition at line 256 of file UT_Singleton.h.

template<typename T, bool DESTRUCTONEXIT = false, class LOCK = UT_Lock>
T* UT_SingletonWithLock< T, DESTRUCTONEXIT, LOCK >::operator-> ( void  )
inline

Definition at line 257 of file UT_Singleton.h.

template<typename T, bool DESTRUCTONEXIT = false, class LOCK = UT_Lock>
void UT_SingletonWithLock< T, DESTRUCTONEXIT, LOCK >::unsafeSet ( T newp)
inline

NOTE: Even though unsafeSet locks, it is still unsafe, because other threads may have called get(), gotten a valid pointer, and be using it when unsafeSet destructs it.

Definition at line 251 of file UT_Singleton.h.


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