2002-08-11 05:49:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// The Loki Library
|
|
|
|
// Copyright (c) 2001 by Andrei Alexandrescu
|
|
|
|
// This code accompanies the book:
|
|
|
|
// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
|
|
|
|
// Patterns Applied". Copyright (c) 2001. Addison-Wesley.
|
|
|
|
// Permission to use, copy, modify, distribute and sell this software for any
|
|
|
|
// purpose is hereby granted without fee, provided that the above copyright
|
|
|
|
// notice appear in all copies and that both that copyright notice and this
|
|
|
|
// permission notice appear in supporting documentation.
|
|
|
|
// The author or Addison-Wesley Longman make no representations about the
|
|
|
|
// suitability of this software for any purpose. It is provided "as is"
|
|
|
|
// without express or implied warranty.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2005-09-26 07:33:05 +00:00
|
|
|
#ifndef LOKI_SINGLETON_INC_
|
|
|
|
#define LOKI_SINGLETON_INC_
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
#include "Threads.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <new>
|
|
|
|
|
2003-12-11 16:48:26 +00:00
|
|
|
#ifdef _MSC_VER
|
2005-09-26 07:33:05 +00:00
|
|
|
#define LOKI_C_CALLING_CONVENTION_QUALIFIER __cdecl
|
2003-12-11 16:48:26 +00:00
|
|
|
#else
|
2005-09-26 07:33:05 +00:00
|
|
|
#define LOKI_C_CALLING_CONVENTION_QUALIFIER
|
2003-12-11 16:48:26 +00:00
|
|
|
#endif
|
|
|
|
|
2005-07-26 14:33:30 +00:00
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
namespace Loki
|
|
|
|
{
|
2005-09-26 07:33:05 +00:00
|
|
|
typedef void (LOKI_C_CALLING_CONVENTION_QUALIFIER *atexit_pfn_t)();
|
2003-12-11 16:48:26 +00:00
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
namespace Private
|
|
|
|
{
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class LifetimeTracker
|
|
|
|
// Helper class for SetLongevity
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class LifetimeTracker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LifetimeTracker(unsigned int x) : longevity_(x)
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~LifetimeTracker() = 0;
|
|
|
|
|
|
|
|
static bool Compare(const LifetimeTracker* lhs,
|
|
|
|
const LifetimeTracker* rhs)
|
|
|
|
{
|
2005-10-05 09:08:42 +00:00
|
|
|
return lhs->longevity_ > rhs->longevity_;
|
2002-08-11 05:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned int longevity_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Definition required
|
|
|
|
inline LifetimeTracker::~LifetimeTracker() {}
|
|
|
|
|
|
|
|
// Helper data
|
|
|
|
typedef LifetimeTracker** TrackerArray;
|
|
|
|
extern TrackerArray pTrackerArray;
|
|
|
|
extern unsigned int elements;
|
|
|
|
|
|
|
|
// Helper destroyer function
|
|
|
|
template <typename T>
|
|
|
|
struct Deleter
|
|
|
|
{
|
2005-10-05 09:08:42 +00:00
|
|
|
typedef void (*Type)(T*);
|
2002-08-11 05:49:45 +00:00
|
|
|
static void Delete(T* pObj)
|
|
|
|
{ delete pObj; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Concrete lifetime tracker for objects of type T
|
|
|
|
template <typename T, typename Destroyer>
|
|
|
|
class ConcreteLifetimeTracker : public LifetimeTracker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ConcreteLifetimeTracker(T* p,unsigned int longevity, Destroyer d)
|
|
|
|
: LifetimeTracker(longevity)
|
|
|
|
, pTracked_(p)
|
|
|
|
, destroyer_(d)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~ConcreteLifetimeTracker()
|
|
|
|
{ destroyer_(pTracked_); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* pTracked_;
|
|
|
|
Destroyer destroyer_;
|
|
|
|
};
|
|
|
|
|
2005-09-26 07:33:05 +00:00
|
|
|
void LOKI_C_CALLING_CONVENTION_QUALIFIER AtExitFn(); // declaration needed below
|
2002-08-11 05:49:45 +00:00
|
|
|
} // namespace Private
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// function template SetLongevity
|
|
|
|
// Assigns an object a longevity; ensures ordered destructions of objects
|
|
|
|
// registered thusly during the exit sequence of the application
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template <typename T, typename Destroyer>
|
|
|
|
void SetLongevity(T* pDynObject, unsigned int longevity,
|
2005-10-05 09:08:42 +00:00
|
|
|
Destroyer d)
|
2002-08-11 05:49:45 +00:00
|
|
|
{
|
|
|
|
using namespace Private;
|
|
|
|
|
|
|
|
TrackerArray pNewArray = static_cast<TrackerArray>(
|
|
|
|
std::realloc(pTrackerArray,
|
|
|
|
sizeof(*pTrackerArray) * (elements + 1)));
|
|
|
|
if (!pNewArray) throw std::bad_alloc();
|
|
|
|
|
|
|
|
// Delayed assignment for exception safety
|
|
|
|
pTrackerArray = pNewArray;
|
|
|
|
|
|
|
|
LifetimeTracker* p = new ConcreteLifetimeTracker<T, Destroyer>(
|
|
|
|
pDynObject, longevity, d);
|
|
|
|
|
|
|
|
// Insert a pointer to the object into the queue
|
|
|
|
TrackerArray pos = std::upper_bound(
|
|
|
|
pTrackerArray,
|
|
|
|
pTrackerArray + elements,
|
|
|
|
p,
|
|
|
|
LifetimeTracker::Compare);
|
|
|
|
std::copy_backward(
|
|
|
|
pos,
|
|
|
|
pTrackerArray + elements,
|
|
|
|
pTrackerArray + elements + 1);
|
|
|
|
*pos = p;
|
|
|
|
++elements;
|
|
|
|
|
|
|
|
// Register a call to AtExitFn
|
|
|
|
std::atexit(Private::AtExitFn);
|
|
|
|
}
|
|
|
|
|
2005-10-05 09:08:42 +00:00
|
|
|
template <typename T>
|
|
|
|
void SetLongevity(T* pDynObject, unsigned int longevity,
|
|
|
|
typename Private::Deleter<T>::Type d = Private::Deleter<T>::Delete)
|
|
|
|
{
|
|
|
|
SetLongevity<T, typename Private::Deleter<T>::Type>(pDynObject, longevity, d);
|
|
|
|
}
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template CreateUsingNew
|
|
|
|
// Implementation of the CreationPolicy used by SingletonHolder
|
|
|
|
// Creates objects using a straight call to the new operator
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template <class T> struct CreateUsingNew
|
|
|
|
{
|
|
|
|
static T* Create()
|
|
|
|
{ return new T; }
|
|
|
|
|
|
|
|
static void Destroy(T* p)
|
|
|
|
{ delete p; }
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template CreateUsingNew
|
|
|
|
// Implementation of the CreationPolicy used by SingletonHolder
|
|
|
|
// Creates objects using a call to std::malloc, followed by a call to the
|
|
|
|
// placement new operator
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template <class T> struct CreateUsingMalloc
|
|
|
|
{
|
|
|
|
static T* Create()
|
|
|
|
{
|
|
|
|
void* p = std::malloc(sizeof(T));
|
|
|
|
if (!p) return 0;
|
|
|
|
return new(p) T;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Destroy(T* p)
|
|
|
|
{
|
|
|
|
p->~T();
|
|
|
|
std::free(p);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template CreateStatic
|
|
|
|
// Implementation of the CreationPolicy used by SingletonHolder
|
|
|
|
// Creates an object in static memory
|
|
|
|
// Implementation is slightly nonportable because it uses the MaxAlign trick
|
|
|
|
// (an union of all types to ensure proper memory alignment). This trick is
|
|
|
|
// nonportable in theory but highly portable in practice.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template <class T> struct CreateStatic
|
|
|
|
{
|
2003-02-27 20:09:08 +00:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER >= 1300
|
|
|
|
#pragma warning( push )
|
|
|
|
// alignment of a member was sensitive to packing
|
|
|
|
#pragma warning( disable : 4121 )
|
|
|
|
#endif // _MSC_VER
|
2002-08-11 05:49:45 +00:00
|
|
|
union MaxAlign
|
|
|
|
{
|
|
|
|
char t_[sizeof(T)];
|
|
|
|
short int shortInt_;
|
|
|
|
int int_;
|
|
|
|
long int longInt_;
|
|
|
|
float float_;
|
|
|
|
double double_;
|
|
|
|
long double longDouble_;
|
|
|
|
struct Test;
|
|
|
|
int Test::* pMember_;
|
|
|
|
int (Test::*pMemberFn_)(int);
|
|
|
|
};
|
2003-02-27 20:09:08 +00:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER >= 1300
|
|
|
|
#pragma warning( pop )
|
|
|
|
#endif // _MSC_VER
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
static T* Create()
|
|
|
|
{
|
|
|
|
static MaxAlign staticMemory_;
|
|
|
|
return new(&staticMemory_) T;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Destroy(T* p)
|
|
|
|
{
|
|
|
|
p->~T();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template DefaultLifetime
|
|
|
|
// Implementation of the LifetimePolicy used by SingletonHolder
|
|
|
|
// Schedules an object's destruction as per C++ rules
|
|
|
|
// Forwards to std::atexit
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct DefaultLifetime
|
|
|
|
{
|
2003-12-11 16:48:26 +00:00
|
|
|
static void ScheduleDestruction(T*, atexit_pfn_t pFun)
|
2002-08-11 05:49:45 +00:00
|
|
|
{ std::atexit(pFun); }
|
|
|
|
|
|
|
|
static void OnDeadReference()
|
|
|
|
{ throw std::logic_error("Dead Reference Detected"); }
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template PhoenixSingleton
|
|
|
|
// Implementation of the LifetimePolicy used by SingletonHolder
|
|
|
|
// Schedules an object's destruction as per C++ rules, and it allows object
|
|
|
|
// recreation by not throwing an exception from OnDeadReference
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class PhoenixSingleton
|
|
|
|
{
|
|
|
|
public:
|
2003-12-11 16:48:26 +00:00
|
|
|
static void ScheduleDestruction(T*, atexit_pfn_t pFun)
|
2002-08-11 05:49:45 +00:00
|
|
|
{
|
|
|
|
#ifndef ATEXIT_FIXED
|
|
|
|
if (!destroyedOnce_)
|
|
|
|
#endif
|
|
|
|
std::atexit(pFun);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void OnDeadReference()
|
|
|
|
{
|
|
|
|
#ifndef ATEXIT_FIXED
|
|
|
|
destroyedOnce_ = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
#ifndef ATEXIT_FIXED
|
|
|
|
static bool destroyedOnce_;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifndef ATEXIT_FIXED
|
|
|
|
template <class T> bool PhoenixSingleton<T>::destroyedOnce_ = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template Adapter
|
|
|
|
// Helper for SingletonWithLongevity below
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
namespace Private
|
|
|
|
{
|
|
|
|
template <class T>
|
|
|
|
struct Adapter
|
|
|
|
{
|
|
|
|
void operator()(T*) { return pFun_(); }
|
2003-12-11 16:48:26 +00:00
|
|
|
atexit_pfn_t pFun_;
|
2002-08-11 05:49:45 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template SingletonWithLongevity
|
|
|
|
// Implementation of the LifetimePolicy used by SingletonHolder
|
|
|
|
// Schedules an object's destruction in order of their longevities
|
|
|
|
// Assumes a visible function GetLongevity(T*) that returns the longevity of the
|
|
|
|
// object
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class SingletonWithLongevity
|
|
|
|
{
|
|
|
|
public:
|
2003-12-11 16:48:26 +00:00
|
|
|
static void ScheduleDestruction(T* pObj, atexit_pfn_t pFun)
|
2002-08-11 05:49:45 +00:00
|
|
|
{
|
|
|
|
Private::Adapter<T> adapter = { pFun };
|
|
|
|
SetLongevity(pObj, GetLongevity(pObj), adapter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void OnDeadReference()
|
|
|
|
{ throw std::logic_error("Dead Reference Detected"); }
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template NoDestroy
|
|
|
|
// Implementation of the LifetimePolicy used by SingletonHolder
|
|
|
|
// Never destroys the object
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct NoDestroy
|
|
|
|
{
|
2005-07-24 17:28:27 +00:00
|
|
|
static void ScheduleDestruction(T*, atexit_pfn_t)
|
2002-08-11 05:49:45 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
static void OnDeadReference()
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template SingletonHolder
|
|
|
|
// Provides Singleton amenities for a type T
|
|
|
|
// To protect that type from spurious instantiations, you have to protect it
|
|
|
|
// yourself.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template
|
|
|
|
<
|
|
|
|
typename T,
|
|
|
|
template <class> class CreationPolicy = CreateUsingNew,
|
|
|
|
template <class> class LifetimePolicy = DefaultLifetime,
|
2005-09-26 07:33:05 +00:00
|
|
|
template <class> class ThreadingModel = LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL
|
2002-08-11 05:49:45 +00:00
|
|
|
>
|
|
|
|
class SingletonHolder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static T& Instance();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Helpers
|
|
|
|
static void MakeInstance();
|
2005-09-26 07:33:05 +00:00
|
|
|
static void LOKI_C_CALLING_CONVENTION_QUALIFIER DestroySingleton();
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
// Protection
|
|
|
|
SingletonHolder();
|
|
|
|
|
|
|
|
// Data
|
|
|
|
typedef typename ThreadingModel<T*>::VolatileType PtrInstanceType;
|
|
|
|
static PtrInstanceType pInstance_;
|
|
|
|
static bool destroyed_;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// SingletonHolder's data
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template
|
|
|
|
<
|
|
|
|
class T,
|
|
|
|
template <class> class C,
|
|
|
|
template <class> class L,
|
|
|
|
template <class> class M
|
|
|
|
>
|
|
|
|
typename SingletonHolder<T, C, L, M>::PtrInstanceType
|
|
|
|
SingletonHolder<T, C, L, M>::pInstance_;
|
|
|
|
|
|
|
|
template
|
|
|
|
<
|
|
|
|
class T,
|
|
|
|
template <class> class C,
|
|
|
|
template <class> class L,
|
|
|
|
template <class> class M
|
|
|
|
>
|
|
|
|
bool SingletonHolder<T, C, L, M>::destroyed_;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// SingletonHolder::Instance
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template
|
|
|
|
<
|
|
|
|
class T,
|
|
|
|
template <class> class CreationPolicy,
|
|
|
|
template <class> class LifetimePolicy,
|
|
|
|
template <class> class ThreadingModel
|
|
|
|
>
|
|
|
|
inline T& SingletonHolder<T, CreationPolicy,
|
|
|
|
LifetimePolicy, ThreadingModel>::Instance()
|
|
|
|
{
|
|
|
|
if (!pInstance_)
|
|
|
|
{
|
|
|
|
MakeInstance();
|
|
|
|
}
|
|
|
|
return *pInstance_;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// SingletonHolder::MakeInstance (helper for Instance)
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template
|
|
|
|
<
|
|
|
|
class T,
|
|
|
|
template <class> class CreationPolicy,
|
|
|
|
template <class> class LifetimePolicy,
|
|
|
|
template <class> class ThreadingModel
|
|
|
|
>
|
|
|
|
void SingletonHolder<T, CreationPolicy,
|
|
|
|
LifetimePolicy, ThreadingModel>::MakeInstance()
|
|
|
|
{
|
2005-07-27 17:33:22 +00:00
|
|
|
typename ThreadingModel<SingletonHolder>::Lock guard;
|
2002-08-11 05:49:45 +00:00
|
|
|
(void)guard;
|
|
|
|
|
|
|
|
if (!pInstance_)
|
|
|
|
{
|
|
|
|
if (destroyed_)
|
|
|
|
{
|
|
|
|
LifetimePolicy<T>::OnDeadReference();
|
|
|
|
destroyed_ = false;
|
|
|
|
}
|
|
|
|
pInstance_ = CreationPolicy<T>::Create();
|
|
|
|
LifetimePolicy<T>::ScheduleDestruction(pInstance_,
|
|
|
|
&DestroySingleton);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template
|
|
|
|
<
|
|
|
|
class T,
|
|
|
|
template <class> class CreationPolicy,
|
|
|
|
template <class> class L,
|
|
|
|
template <class> class M
|
|
|
|
>
|
2005-09-26 07:33:05 +00:00
|
|
|
void LOKI_C_CALLING_CONVENTION_QUALIFIER
|
2003-12-11 16:48:26 +00:00
|
|
|
SingletonHolder<T, CreationPolicy, L, M>::DestroySingleton()
|
2002-08-11 05:49:45 +00:00
|
|
|
{
|
|
|
|
assert(!destroyed_);
|
|
|
|
CreationPolicy<T>::Destroy(pInstance_);
|
|
|
|
pInstance_ = 0;
|
|
|
|
destroyed_ = true;
|
|
|
|
}
|
|
|
|
} // namespace Loki
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Change log:
|
|
|
|
// May 21, 2001: Correct the volatile qualifier - credit due to Darin Adler
|
|
|
|
// June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!!
|
|
|
|
// January 08, 2002: Fixed bug in call to realloc - credit due to Nigel Gent and
|
|
|
|
// Eike Petersen
|
|
|
|
// March 08, 2002: moved the assignment to pTrackerArray in SetLongevity to fix
|
|
|
|
// exception safety issue. Credit due to Kari Hoijarvi
|
|
|
|
// May 09, 2002: Fixed bug in Compare that caused longevities to act backwards.
|
|
|
|
// Credit due to Scott McDonald.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#endif // SINGLETON_INC_
|