add DeletableSingleton policy and examples from Curtis Krauskopf's CUJ article 'Creating Dynamic Singletons & the Loki Library' - www.decompile.com
git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@307 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
parent
6c7cedb361
commit
a61d7f7127
8 changed files with 354 additions and 8 deletions
|
@ -71,7 +71,7 @@ namespace Loki
|
|||
template <typename T>
|
||||
struct Deleter
|
||||
{
|
||||
typedef void (*Type)(T*);
|
||||
typedef void (*Type)(T*);
|
||||
static void Delete(T* pObj)
|
||||
{ delete pObj; }
|
||||
};
|
||||
|
@ -138,13 +138,13 @@ namespace Loki
|
|||
std::atexit(Private::AtExitFn);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename T>
|
||||
void SetLongevity(T* pDynObject, unsigned int longevity,
|
||||
typename Private::Deleter<T>::Type d = Private::Deleter<T>::Delete)
|
||||
typename Private::Deleter<T>::Type d = Private::Deleter<T>::Delete)
|
||||
{
|
||||
SetLongevity<T, typename Private::Deleter<T>::Type>(pDynObject, longevity, d);
|
||||
}
|
||||
|
||||
SetLongevity<T, typename Private::Deleter<T>::Type>(pDynObject, longevity, d);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// class template CreateUsingNew
|
||||
// Implementation of the CreationPolicy used by SingletonHolder
|
||||
|
@ -194,7 +194,7 @@ namespace Loki
|
|||
|
||||
template <class T> struct CreateStatic
|
||||
{
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning( push )
|
||||
#pragma warning( disable : 4121 )
|
||||
|
@ -283,7 +283,76 @@ namespace Loki
|
|||
#ifndef ATEXIT_FIXED
|
||||
template <class T> bool PhoenixSingleton<T>::destroyedOnce_ = false;
|
||||
#endif
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// class template DeletableSingleton
|
||||
// Copyright (c) 2004 by Curtis Krauskopf - curtis@decompile.com
|
||||
//
|
||||
// A DeletableSingleton allows the instantiated singleton to be
|
||||
// destroyed at any time. The singleton can be reinstantiated at
|
||||
// any time, even during program termination.
|
||||
// If the singleton exists when the program terminates, it will
|
||||
// be automatically deleted.
|
||||
//
|
||||
// The singleton can be deleted manually:
|
||||
// DeletableSingleton<MyClass>::GracefulDelete();
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <class T>
|
||||
class DeletableSingleton
|
||||
{
|
||||
public:
|
||||
|
||||
static void ScheduleDestruction(T*, atexit_pfn_t pFun)
|
||||
{
|
||||
static bool firstPass = true;
|
||||
isDead = false;
|
||||
deleter = pFun;
|
||||
if (firstPass || needCallback)
|
||||
{
|
||||
std::atexit(atexitCallback);
|
||||
firstPass = false;
|
||||
needCallback = false;
|
||||
}
|
||||
}
|
||||
|
||||
static void OnDeadReference()
|
||||
{
|
||||
}
|
||||
|
||||
static void GracefulDelete()
|
||||
{
|
||||
if (isDead)
|
||||
return;
|
||||
isDead = true;
|
||||
deleter();
|
||||
}
|
||||
|
||||
protected:
|
||||
static atexit_pfn_t deleter;
|
||||
static bool isDead;
|
||||
static bool needCallback;
|
||||
|
||||
static void atexitCallback()
|
||||
{
|
||||
#ifdef ATEXIT_FIXED
|
||||
needCallback = true;
|
||||
#else
|
||||
needCallback = false;
|
||||
#endif
|
||||
GracefulDelete();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
atexit_pfn_t DeletableSingleton<T>::deleter = 0;
|
||||
|
||||
template <class T>
|
||||
bool DeletableSingleton<T>::isDead = true;
|
||||
|
||||
template <class T>
|
||||
bool DeletableSingleton<T>::needCallback = true;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// class template Adapter
|
||||
// Helper for SingletonWithLongevity below
|
||||
|
@ -435,7 +504,7 @@ namespace Loki
|
|||
{
|
||||
if (destroyed_)
|
||||
{
|
||||
destroyed_ = false;
|
||||
destroyed_ = false;
|
||||
LifetimePolicy<T>::OnDeadReference();
|
||||
}
|
||||
pInstance_ = CreationPolicy<T>::Create();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue