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:
syntheticpp 2005-10-17 08:43:42 +00:00
parent 6c7cedb361
commit a61d7f7127
8 changed files with 354 additions and 8 deletions

View file

@ -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();

View file

@ -8,6 +8,7 @@ SUBTARGETS = \
sub-OrderedStatic \
sub-RegressionTest \
sub-SafeFormat \
sub-Singleton \
sub-SmallObj
all: $(SUBTARGETS)
@ -32,6 +33,12 @@ sub-SafeFormat: FORCE
$(MAKE) -f $(MAKEFILE)
@cd ..
sub-Singleton: FORCE
cd Singleton && \
$(MAKE) -f $(MAKEFILE)-DeletableSingleton && \
$(MAKE) -f $(MAKEFILE)-Phoenix
@cd ..
sub-SmallObj: FORCE
cd SmallObj && \
$(MAKE) -f $(MAKEFILE)

View file

@ -0,0 +1,80 @@
////////////////////////////////////////////////////////////////////////////////
// The Loki Library
// Copyright (c) 2005 by Curtis Krauskopf
// Copyright (c) 2005 by Peter Kuemmel
//
// Code covered by the MIT License
// The authors make no representations about the suitability of this software
// for any purpose. It is provided "as is" without express or implied warranty.
////////////////////////////////////////////////////////////////////////////////
// Show an example of a Loki policy that uses DeletableSingleton.
//
// Expected output:
//
// Log::Log()
// Log singleton instantiated
// Going to manually delete log.
// Log::~Log()
// Log::Log()
// Log reinstantiated.
// Going to terminate program now.
// Log::~Log()
//
#include <iostream>
#include "loki/Singleton.h" // for Loki::SingletonHolder
using namespace std; // okay for small programs
using namespace Loki; // okay for small programs
// A singleton Log object derived from the Example class.
// Its longevity is set by the user on the command line.
//
class Log
{
public:
Log()
{
print("Log::Log()");
};
~Log()
{
print("Log::~Log()");
}
void print(const char *s)
{
cout << s << endl;
}
};
typedef SingletonHolder<Log, CreateUsingNew, DeletableSingleton> log;
class Example
{
public:
void method()
{
cout << "test\n";
}
};
int main(int argc, char *argv[])
{
// Instantiate both singletons by calling them...
log::Instance().print("Log singleton instantiated");
log::Instance().print("Going to manually delete log.");
DeletableSingleton<Log>::GracefulDelete();
log::Instance().print("Log reinstantiated.");
log::Instance().print("Going to terminate program now.");
#if defined(__BORLANDC__) || defined(__GNUC__) || defined(_MSC_VER)
system("pause"); // Stop console window from closing if run from IDE.
#endif
return 0;
}

View file

@ -0,0 +1,32 @@
CPP = g++
CC = gcc
OBJ = DeletableSingleton.o
LINKOBJ = DeletableSingleton.o
CXXINCS = -I./../../include
LIBS = -L../../lib -lloki
CXXFLAGS = $(CXXINCS) -O2 -DNDEBUG
BIN = DeletableSingleton-gcc.exe
RM = rm -f
CHK_DIR_EXISTS= if not exist
MKDIR = mkdir
.PHONY: all all-before all-after clean clean-custom
all: all-before $(BIN) all-after
clean: clean-custom
${RM} $(OBJ) $(BIN)
$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o DeletableSingleton-gcc.exe $(LIBS)
check_tmp:
@$(CHK_DIR_EXISTS) "" $(MKDIR) "tmp"
Factory.o: Factory.cpp
$(CPP) -c DeletableSingleton.cpp -o DeletableSingleton.o $(CXXFLAGS)

32
test/Singleton/Makefile-Phoenix Executable file
View file

@ -0,0 +1,32 @@
CPP = g++
CC = gcc
OBJ = Phoenix.o
LINKOBJ = Phoenix.o
CXXINCS = -I./../../include
LIBS = -L../../lib -lloki
CXXFLAGS = $(CXXINCS) -O2 -DNDEBUG
BIN = Phoenix-gcc.exe
RM = rm -f
CHK_DIR_EXISTS= if not exist
MKDIR = mkdir
.PHONY: all all-before all-after clean clean-custom
all: all-before $(BIN) all-after
clean: clean-custom
${RM} $(OBJ) $(BIN)
$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o Phoenix-gcc.exe $(LIBS)
check_tmp:
@$(CHK_DIR_EXISTS) "" $(MKDIR) "tmp"
Factory.o: Factory.cpp
$(CPP) -c Phoenix.cpp -o Phoenix.o $(CXXFLAGS)

91
test/Singleton/Phoenix.cpp Executable file
View file

@ -0,0 +1,91 @@
////////////////////////////////////////////////////////////////////////////////
// The Loki Library
// Copyright (c) 2005 by Curtis Krauskopf
// Copyright (c) 2005 by Peter Kuemmel
//
// Code covered by the MIT License
// The authors make no representations about the suitability of this software
// for any purpose. It is provided "as is" without express or implied warranty.
////////////////////////////////////////////////////////////////////////////////
// A singleton Log object that resurrects itself after
// it has been automatically destroyed during program
// termination. When a dead-reference to the Log
// singleton is detected, it is resurrected automatically.
//
// Expected output:
// Example c'tor
// Log c'tor
// Log now instantiated.
// Log d'tor
// Example d'tor starting
// Log c'tor
// Log: inside Example d'tor
// Example d'tor finished
//
// The last line of the output only appears when this
// program is compiled with the ATEXIT_FIXED symbol
// defined (see the Loki library and the CUJ article).
//
#include <iostream>
#include "loki/Singleton.h" // for Loki::SingletonHolder
using namespace std; // okay for small programs
using namespace Loki; // okay for small programs
class Log
{
public:
Log()
{
echo("Log c'tor");
}
~Log()
{
echo("Log d'tor");
}
void echo(const char *s)
{
cout << s << endl;
}
};
typedef SingletonHolder<Log, CreateUsingNew, PhoenixSingleton> log;
// A generic example class that stores and echoes a const char.
//
class Example
{
public:
Example()
{
echo("Example c'tor");
}
~Example()
{
echo("Example d'tor starting");
log::Instance().echo("Log: inside Example d'tor");
echo("Example d'tor finished");
}
void echo(const char *s)
{
cout << s << endl;
}
};
int main(int argc, char* argv[])
{
Example *example = new Example();
SetLongevity<Example, void (*)(Example*)>(example, 1, &Loki::Private::Deleter<Example>::Delete);
log::Instance().echo("Log now instantiated.");
#if defined(__BORLANDC__) || defined(__GNUC__) || defined(_MSC_VER)
system("pause"); // Stop console window from closing if run from IDE.
#endif
return 0;
}

31
test/Singleton/make.msvc.bat Executable file
View file

@ -0,0 +1,31 @@
if not exist tmp\ mkdir tmp
:: DeletableSingleton.cpp
cl -c -Zm200 -O2 -DNDEBUG -MT -EHsc -GR -W0 -wd4710 -I"." -I"..\..\include" -Fotmp\ DeletableSingleton.cpp
if not defined LOKI_MSVC_NOLIB (
link /NOLOGO /SUBSYSTEM:CONSOLE /incremental:no /OUT:"DeletableSingleton-msvc.exe" ..\..\lib\loki.lib tmp\DeletableSingleton.obj
) else (
link /NOLOGO /SUBSYSTEM:CONSOLE /incremental:no /OUT:"DeletableSingleton-msvc.exe" tmp\DeletableSingleton.obj ..\..\lib\SmallObj.obj ..\..\lib\Singleton.obj
)
:: Phoenix.cpp
cl -c -Zm200 -O2 -DNDEBUG -MT -EHsc -GR -W0 -wd4710 -I"." -I"..\..\include" -Fotmp\ Phoenix.cpp
if not defined LOKI_MSVC_NOLIB (
link /NOLOGO /SUBSYSTEM:CONSOLE /incremental:no /OUT:"Phoenix-msvc.exe" ..\..\lib\loki.lib tmp\Phoenix.obj
) else (
link /NOLOGO /SUBSYSTEM:CONSOLE /incremental:no /OUT:"Phoenix-msvc.exe" tmp\Phoenix.obj ..\..\lib\SmallObj.obj ..\..\lib\Singleton.obj
)

View file

@ -23,6 +23,10 @@ cd SafeFormat
call make.msvc.bat
cd ..
cd Singleton
call make.msvc.bat
cd ..
cd SmallObj
call make.msvc.bat
cd ..