HDK
|
#include <UT_Singleton.h>
Public Types | |
typedef UT_Singleton< T, DESTRUCTONEXIT > | Base |
Public Member Functions | |
T & | get () |
template<typename S > | |
T & | get (S s) |
void | unsafeSet (T *newp) |
T & | operator* () |
T * | operator-> () |
Public Member Functions inherited from UT_Singleton< T, DESTRUCTONEXIT > | |
UT_Singleton () | |
~UT_Singleton () | |
template<class LOCK > | |
T & | get (LOCK &lock) |
template<class LOCK , typename S > | |
T & | get (LOCK &lock, S s) |
template<class LOCK > | |
void | unsafeSet (LOCK &lock, T *newp) |
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 234 of file UT_Singleton.h.
typedef UT_Singleton<T, DESTRUCTONEXIT> UT_SingletonWithLock< T, DESTRUCTONEXIT, LOCK >::Base |
Definition at line 237 of file UT_Singleton.h.
|
inline |
Definition at line 239 of file UT_Singleton.h.
|
inline |
Definition at line 245 of file UT_Singleton.h.
|
inline |
Definition at line 258 of file UT_Singleton.h.
|
inline |
Definition at line 259 of file UT_Singleton.h.
|
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 253 of file UT_Singleton.h.