6#ifndef __qdp_singleton_h__
7#define __qdp_singleton_h__
39 {
return lval += val; }
42 {
return lval -= val; }
45 {
return lval *= val; }
48 {
return lval /= val; }
83 return rhs->longevity_ > lhs->longevity_;
87 unsigned int longevity_;
107 template <
typename T,
typename Destroyer>
118 { destroyer_(pTracked_); }
122 Destroyer destroyer_;
135 template <
typename T,
typename Destroyer>
141 TrackerArray pNewArray =
static_cast<TrackerArray
>(
142 std::realloc(pTrackerArray, elements + 1));
143 if (!pNewArray)
throw std::bad_alloc();
145 LifetimeTracker* p =
new ConcreteLifetimeTracker<T, Destroyer>(
146 pDynObject, longevity, d);
149 pTrackerArray = pNewArray;
152 TrackerArray pos = std::upper_bound(
154 pTrackerArray + elements,
156 LifetimeTracker::Compare);
159 pTrackerArray + elements,
160 pTrackerArray + elements + 1);
194 void* p = std::malloc(
sizeof(T));
234 return new(&staticMemory_) T;
254 { std::atexit(pFun); }
257 {
throw std::logic_error(
"Dead Reference Detected"); }
282 destroyedOnce_ =
true;
293 template <
class T>
bool PhoenixSingleton<T>::destroyedOnce_ =
false;
330 {
throw std::logic_error(
"Dead Reference Detected"); }
357 template <
typename T,
358 template <
class>
class CreationPolicy = CreateUsingNew,
359 template <
class>
class LifetimePolicy = DefaultLifetime,
360 template <
class>
class ThreadingModel = SingleThreaded>
361 class SingletonHolder
368 static void MakeInstance();
369 static void DestroySingleton();
374 SingletonHolder& operator= (
const SingletonHolder&) {}
377 typedef typename ThreadingModel<T*>::VolatileType PtrInstanceType;
378 static PtrInstanceType pInstance_;
379 static bool destroyed_;
387 template <
class>
class C,
388 template <
class>
class L,
389 template <
class>
class M>
390 typename SingletonHolder<T, C, L, M>::PtrInstanceType
391 SingletonHolder<T, C, L, M>::pInstance_=0;
396 template <
class>
class C,
397 template <
class>
class L,
398 template <
class>
class M
400 bool SingletonHolder<T, C, L, M>::destroyed_=
false;
407 template <
class>
class CreationPolicy,
408 template <
class>
class LifetimePolicy,
409 template <
class>
class ThreadingModel>
425 template <
class>
class CreationPolicy,
426 template <
class>
class LifetimePolicy,
427 template <
class>
class ThreadingModel>
429 LifetimePolicy, ThreadingModel>::MakeInstance()
431 typename ThreadingModel<T>::Lock guard;
438 LifetimePolicy<T>::OnDeadReference();
442 LifetimePolicy<T>::ScheduleDestruction(
pInstance_,
448 template <
class>
class CreationPolicy,
449 template <
class>
class L,
450 template <
class>
class M>
451 void SingletonHolder<T, CreationPolicy, L, M>::DestroySingleton()
static void ScheduleDestruction(T *, void(*pFun)())
static void OnDeadReference()
ConcreteLifetimeTracker(T *p, unsigned int longevity, Destroyer d)
~ConcreteLifetimeTracker()
static bool Compare(const LifetimeTracker *lhs, const LifetimeTracker *rhs)
LifetimeTracker(unsigned int x)
virtual ~LifetimeTracker()=0
static IntType AtomicSubtract(volatile IntType &lval, IntType val)
static IntType AtomicIncrement(volatile IntType &lval)
static IntType AtomicDivide(volatile IntType &lval, IntType val)
static void AtomicAssign(volatile IntType &lval, IntType val)
static IntType AtomicDivide(volatile IntType &lval)
static IntType AtomicAdd(volatile IntType &lval, IntType val)
static IntType AtomicMultiply(volatile IntType &lval, IntType val)
static void AtomicAssign(IntType &lval, volatile IntType &val)
static void ScheduleDestruction(T *pObj, void(*pFun)())
static void OnDeadReference()
OLattice< PScalar< PColorMatrix< RComplexFloat, 3 > > > C
TrackerArray pTrackerArray
LifetimeTracker ** TrackerArray
Yet another random number generator.
bool PhoenixSingleton< T >::destroyedOnce_
void SetLongevity(T *pDynObject, unsigned int longevity, Destroyer d=Private::Deleter< T >::Delete)
bool SingletonHolder< T, C, L, M >::destroyed_
SingletonHolder< T, C, L, M >::PtrInstanceType SingletonHolder< T, C, L, M >::pInstance_
static void Destroy(T *p)
static void Destroy(T *p)
static void Destroy(T *p)
static void OnDeadReference()
static void ScheduleDestruction(T *, void(*pFun)())
static void OnDeadReference()
static void ScheduleDestruction(T *, void(*pFun)())
static void Delete(T *pObj)
int(Test::* pMemberFn_)(int)