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
|
|
|
|
2006-01-16 19:05:09 +00:00
|
|
|
// $Header$
|
|
|
|
|
2006-02-27 19:59:20 +00:00
|
|
|
#include "LokiExport.h"
|
2002-08-11 05:49:45 +00:00
|
|
|
#include "Threads.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <new>
|
2005-11-01 11:11:52 +00:00
|
|
|
#include <vector>
|
2005-11-12 15:14:08 +00:00
|
|
|
#include <list>
|
2005-11-13 14:32:03 +00:00
|
|
|
#include <memory>
|
2005-11-12 15:14:08 +00:00
|
|
|
|
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-11-02 20:01:11 +00:00
|
|
|
/// \defgroup SingletonGroup Singleton
|
|
|
|
/// \defgroup CreationGroup Creation policies
|
|
|
|
/// \ingroup SingletonGroup
|
|
|
|
/// \defgroup LifetimeGroup Lifetime policies
|
|
|
|
/// \ingroup SingletonGroup
|
2006-05-20 10:23:07 +00:00
|
|
|
/// The lifetimes of the singleton.
|
|
|
|
/// \par Special lifetime for SmallObjects
|
|
|
|
/// When the holded object is a Small(Value)Object or the holded object
|
|
|
|
/// uses objects which are or inherit from Small(Value)Object
|
|
|
|
/// then you can't use the default lifetime: you must use the lifetime
|
|
|
|
/// \code Loki::LongevityLifetime::DieAsSmallObjectChild \endcode
|
|
|
|
/// Be aware of this when you use Loki::Factory, Loki::Functor, or Loki::Function.
|
|
|
|
|
2005-11-02 20:01:11 +00:00
|
|
|
|
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
|
|
|
|
{
|
2006-02-27 19:59:20 +00:00
|
|
|
|
|
|
|
#ifndef LOKI_MAKE_DLL
|
2005-11-12 15:14:08 +00:00
|
|
|
void LOKI_C_CALLING_CONVENTION_QUALIFIER AtExitFn(); // declaration needed below
|
2006-02-27 19:59:20 +00:00
|
|
|
#else
|
2006-02-28 10:31:01 +00:00
|
|
|
void LOKI_EXPORT AtExitFn();
|
2006-02-27 19:59:20 +00:00
|
|
|
#endif
|
2005-11-12 15:14:08 +00:00
|
|
|
|
|
|
|
class LifetimeTracker;
|
|
|
|
|
|
|
|
#define LOKI_ENABLE_NEW_SETLONGLIVITY_HELPER_DATA_IMPL
|
|
|
|
#ifdef LOKI_ENABLE_NEW_SETLONGLIVITY_HELPER_DATA_IMPL
|
|
|
|
|
|
|
|
// Helper data
|
|
|
|
// std::list because of the inserts
|
|
|
|
typedef std::list<LifetimeTracker*> TrackerArray;
|
2006-02-27 19:59:20 +00:00
|
|
|
extern LOKI_EXPORT TrackerArray* pTrackerArray;
|
2005-11-12 15:14:08 +00:00
|
|
|
#else
|
|
|
|
// Helper data
|
|
|
|
typedef LifetimeTracker** TrackerArray;
|
|
|
|
extern TrackerArray pTrackerArray;
|
|
|
|
extern unsigned int elements;
|
|
|
|
#endif
|
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class LifetimeTracker
|
|
|
|
// Helper class for SetLongevity
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
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 destroyer function
|
|
|
|
template <typename T>
|
|
|
|
struct Deleter
|
|
|
|
{
|
2005-10-17 08:43: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_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Private
|
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-02 20:01:11 +00:00
|
|
|
/// \ingroup LifetimeGroup
|
|
|
|
///
|
|
|
|
/// Assigns an object a longevity; ensures ordered destructions of objects
|
|
|
|
/// registered thusly during the exit sequence of the application
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-12 15:14:08 +00:00
|
|
|
|
|
|
|
#ifdef LOKI_ENABLE_NEW_SETLONGLIVITY_HELPER_DATA_IMPL
|
|
|
|
|
|
|
|
template <typename T, typename Destroyer>
|
|
|
|
void SetLongevity(T* pDynObject, unsigned int longevity,
|
|
|
|
Destroyer d)
|
|
|
|
{
|
|
|
|
using namespace Private;
|
|
|
|
|
|
|
|
// manage lifetime of stack manually
|
|
|
|
if(pTrackerArray==0)
|
|
|
|
pTrackerArray = new TrackerArray;
|
|
|
|
|
2005-11-13 14:32:03 +00:00
|
|
|
// automatically delete the ConcreteLifetimeTracker object when a exception is thrown
|
|
|
|
std::auto_ptr<LifetimeTracker>
|
|
|
|
p( new ConcreteLifetimeTracker<T, Destroyer>(pDynObject, longevity, d) );
|
2005-11-12 15:14:08 +00:00
|
|
|
|
|
|
|
// Find correct position
|
|
|
|
TrackerArray::iterator pos = std::upper_bound(
|
|
|
|
pTrackerArray->begin(),
|
|
|
|
pTrackerArray->end(),
|
2005-11-13 14:32:03 +00:00
|
|
|
p.get(),
|
2005-11-12 15:14:08 +00:00
|
|
|
LifetimeTracker::Compare);
|
|
|
|
|
2005-11-13 14:32:03 +00:00
|
|
|
// Insert the pointer to the ConcreteLifetimeTracker object into the queue
|
|
|
|
pTrackerArray->insert(pos, p.get());
|
|
|
|
|
|
|
|
// nothing has thrown: don't delete the ConcreteLifetimeTracker object
|
|
|
|
p.release();
|
|
|
|
|
2005-11-12 15:14:08 +00:00
|
|
|
// Register a call to AtExitFn
|
|
|
|
std::atexit(Private::AtExitFn);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
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);
|
2005-11-12 15:14:08 +00:00
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
// 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-11-12 15:14:08 +00:00
|
|
|
#endif
|
|
|
|
|
2005-10-17 08:43:42 +00:00
|
|
|
template <typename T>
|
2005-10-05 09:08:42 +00:00
|
|
|
void SetLongevity(T* pDynObject, unsigned int longevity,
|
2005-10-17 08:43:42 +00:00
|
|
|
typename Private::Deleter<T>::Type d = Private::Deleter<T>::Delete)
|
2005-10-05 09:08:42 +00:00
|
|
|
{
|
2005-10-17 08:43:42 +00:00
|
|
|
SetLongevity<T, typename Private::Deleter<T>::Type>(pDynObject, longevity, d);
|
|
|
|
}
|
2005-11-02 20:01:11 +00:00
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-02 20:01:11 +00:00
|
|
|
/// \struct CreateUsingNew
|
|
|
|
///
|
|
|
|
/// \ingroup CreationGroup
|
|
|
|
/// Implementation of the CreationPolicy used by SingletonHolder
|
|
|
|
/// Creates objects using a straight call to the new operator
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2002-08-11 05:49:45 +00:00
|
|
|
template <class T> struct CreateUsingNew
|
|
|
|
{
|
|
|
|
static T* Create()
|
|
|
|
{ return new T; }
|
|
|
|
|
|
|
|
static void Destroy(T* p)
|
|
|
|
{ delete p; }
|
|
|
|
};
|
|
|
|
|
2006-01-16 20:10:51 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-01-16 19:56:30 +00:00
|
|
|
/// \struct CreateUsing
|
|
|
|
///
|
|
|
|
/// \ingroup CreationGroup
|
|
|
|
/// Implementation of the CreationPolicy used by SingletonHolder
|
|
|
|
/// Creates objects using a custom allocater.
|
2006-01-16 20:10:51 +00:00
|
|
|
/// Usage: e.g. CreateUsing<std::allocator>::Allocator
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
template<template<class> class Alloc>
|
|
|
|
struct CreateUsing
|
|
|
|
{
|
|
|
|
template <class T>
|
|
|
|
struct Allocator
|
|
|
|
{
|
|
|
|
static Alloc<T> allocator;
|
|
|
|
|
|
|
|
static T* Create()
|
|
|
|
{
|
|
|
|
return new (allocator.allocate(1)) T;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Destroy(T* p)
|
|
|
|
{
|
|
|
|
//allocator.destroy(p);
|
|
|
|
p->~T();
|
|
|
|
allocator.deallocate(p,1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2005-11-02 20:01:11 +00:00
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-02 20:01:11 +00:00
|
|
|
/// \struct CreateUsingMalloc
|
|
|
|
///
|
|
|
|
/// \ingroup CreationGroup
|
|
|
|
/// Implementation of the CreationPolicy used by SingletonHolder
|
|
|
|
/// Creates objects using a call to std::malloc, followed by a call to the
|
|
|
|
/// placement new operator
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2002-08-11 05:49:45 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2005-11-02 20:01:11 +00:00
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-02 20:01:11 +00:00
|
|
|
/// \struct CreateStatic
|
|
|
|
///
|
|
|
|
/// \ingroup CreationGroup
|
|
|
|
/// 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.
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2002-08-11 05:49:45 +00:00
|
|
|
template <class T> struct CreateStatic
|
|
|
|
{
|
2005-10-17 08:43:42 +00:00
|
|
|
|
2005-10-05 09:57:37 +00:00
|
|
|
#ifdef _MSC_VER
|
2003-02-27 20:09:08 +00:00
|
|
|
#pragma warning( push )
|
|
|
|
#pragma warning( disable : 4121 )
|
2005-10-05 09:57:37 +00:00
|
|
|
// alignment of a member was sensitive to packing
|
2003-02-27 20:09:08 +00:00
|
|
|
#endif // _MSC_VER
|
2005-10-05 09:57:37 +00:00
|
|
|
|
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);
|
|
|
|
};
|
2005-10-05 09:57:37 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2003-02-27 20:09:08 +00:00
|
|
|
#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();
|
|
|
|
}
|
|
|
|
};
|
2005-11-02 20:01:11 +00:00
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-02 20:01:11 +00:00
|
|
|
/// \struct DefaultLifetime
|
|
|
|
///
|
|
|
|
/// \ingroup LifetimeGroup
|
|
|
|
/// Implementation of the LifetimePolicy used by SingletonHolder
|
|
|
|
/// Schedules an object's destruction as per C++ rules
|
|
|
|
/// Forwards to std::atexit
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2002-08-11 05:49:45 +00:00
|
|
|
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"); }
|
|
|
|
};
|
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-02 20:01:11 +00:00
|
|
|
/// \struct PhoenixSingleton
|
|
|
|
///
|
|
|
|
/// \ingroup LifetimeGroup
|
|
|
|
/// 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
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2002-08-11 05:49:45 +00:00
|
|
|
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
|
2005-10-17 08:43:42 +00:00
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:55:20 +00:00
|
|
|
// Copyright (c) 2004 by Curtis Krauskopf - curtis@decompile.com
|
2005-11-02 20:01:11 +00:00
|
|
|
///
|
|
|
|
/// \struct DeletableSingleton
|
|
|
|
///
|
|
|
|
/// \ingroup LifetimeGroup
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// \par Usage:
|
|
|
|
/// The singleton can be deleted manually:
|
|
|
|
///
|
|
|
|
/// DeletableSingleton<MyClass>::GracefulDelete();
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-10-17 08:43:42 +00:00
|
|
|
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()
|
|
|
|
{
|
|
|
|
}
|
2005-11-02 20:01:11 +00:00
|
|
|
/// delete singleton object manually
|
2005-10-17 08:43:42 +00:00
|
|
|
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;
|
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template Adapter
|
|
|
|
// Helper for SingletonWithLongevity below
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-02 20:01:11 +00:00
|
|
|
/// \struct SingletonWithLongevity
|
|
|
|
///
|
|
|
|
/// \ingroup LifetimeGroup
|
|
|
|
/// 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.
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2002-08-11 05:49:45 +00:00
|
|
|
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"); }
|
|
|
|
};
|
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-02 20:01:11 +00:00
|
|
|
/// \struct NoDestroy
|
|
|
|
///
|
|
|
|
/// \ingroup LifetimeGroup
|
|
|
|
/// Implementation of the LifetimePolicy used by SingletonHolder
|
|
|
|
/// Never destroys the object
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2002-08-11 05:49:45 +00:00
|
|
|
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()
|
|
|
|
{}
|
|
|
|
};
|
2005-11-02 20:01:11 +00:00
|
|
|
|
2005-11-07 12:06:43 +00:00
|
|
|
|
2005-11-02 20:01:11 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-07 12:06:43 +00:00
|
|
|
/// \defgroup LongevityLifetimeGroup LongevityLifetime
|
2005-11-02 20:01:11 +00:00
|
|
|
/// \ingroup LifetimeGroup
|
2005-11-07 12:06:43 +00:00
|
|
|
///
|
|
|
|
/// \namespace LongevityLifetime
|
|
|
|
///
|
|
|
|
/// \ingroup LongevityLifetimeGroup
|
|
|
|
/// \brief In this namespace are special lifetime policies to manage lifetime
|
|
|
|
/// dependencies.
|
2005-11-02 20:01:11 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-07 12:06:43 +00:00
|
|
|
namespace LongevityLifetime
|
2005-11-01 11:11:52 +00:00
|
|
|
{
|
2005-11-07 12:06:43 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// \struct SingletonFixedLongevity
|
|
|
|
///
|
|
|
|
/// \ingroup LongevityLifetimeGroup
|
|
|
|
/// Add your own lifetimes into the namespace 'LongevityLifetime'
|
|
|
|
/// with your prefered lifetime by adding a struct like this:
|
|
|
|
///
|
|
|
|
/// template<class T>
|
|
|
|
/// struct MyLifetime : SingletonFixedLongevity< MyLifetimeNumber ,T> {}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <unsigned int Longevity, class T>
|
|
|
|
class SingletonFixedLongevity
|
|
|
|
{
|
|
|
|
public:
|
2006-01-22 00:44:17 +00:00
|
|
|
virtual ~SingletonFixedLongevity() {}
|
|
|
|
|
2005-11-07 12:06:43 +00:00
|
|
|
static void ScheduleDestruction(T* pObj, atexit_pfn_t pFun)
|
|
|
|
{
|
|
|
|
Private::Adapter<T> adapter = { pFun };
|
|
|
|
SetLongevity(pObj, Longevity , adapter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void OnDeadReference()
|
|
|
|
{ throw std::logic_error("Dead Reference Detected"); }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \struct DieLast
|
|
|
|
/// \ingroup LongevityLifetimeGroup
|
|
|
|
/// \brief Longest possible SingletonWithLongevity lifetime: 0xFFFFFFFF
|
2005-11-01 11:11:52 +00:00
|
|
|
template <class T>
|
2005-11-07 12:06:43 +00:00
|
|
|
struct DieLast : SingletonFixedLongevity<0xFFFFFFFF ,T>
|
2005-11-01 11:11:52 +00:00
|
|
|
{};
|
2005-11-02 20:01:11 +00:00
|
|
|
|
2005-11-07 12:06:43 +00:00
|
|
|
/// \struct DieDirectlyBeforeLast
|
|
|
|
/// \ingroup LongevityLifetimeGroup
|
|
|
|
/// \brief Lifetime is a one less than DieLast: 0xFFFFFFFF-1
|
2005-11-01 11:11:52 +00:00
|
|
|
template <class T>
|
2005-11-07 12:06:43 +00:00
|
|
|
struct DieDirectlyBeforeLast : SingletonFixedLongevity<0xFFFFFFFF-1 ,T>
|
2005-11-01 11:11:52 +00:00
|
|
|
{};
|
2005-11-07 12:06:43 +00:00
|
|
|
|
|
|
|
/// \struct DieFirst
|
|
|
|
/// \ingroup LongevityLifetimeGroup
|
|
|
|
/// \brief Shortest possible SingletonWithLongevity lifetime: 0
|
|
|
|
template <class T>
|
|
|
|
struct DieFirst : SingletonFixedLongevity<0,T>
|
|
|
|
{};
|
|
|
|
|
|
|
|
}//namespace LongevityLifetime
|
2005-11-01 11:11:52 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
/// \class FollowIntoDeath
|
2005-11-02 20:01:11 +00:00
|
|
|
///
|
|
|
|
/// \ingroup LifetimeGroup
|
|
|
|
///
|
|
|
|
/// Lifetime policyfor the SingletonHolder tempalte.
|
|
|
|
/// Followers will die after the master dies Followers will not die, if
|
2005-11-03 12:43:55 +00:00
|
|
|
/// - master never dies (NoDestroy policy)
|
|
|
|
/// - master never created
|
|
|
|
/// - master dies not in the function registered with atexit
|
2005-11-03 12:56:58 +00:00
|
|
|
/// - master dies not by a call of a the atexit registerd function (DeletableSingleton::GracefulDelete)
|
2005-11-02 20:01:11 +00:00
|
|
|
///
|
|
|
|
/// \par Usage:
|
|
|
|
///
|
2005-11-03 12:43:55 +00:00
|
|
|
/// Lifetimes of the master and the follower singletons, e.g. with a M and a F class:
|
|
|
|
/// \code SingletonHolder< M , FollowIntoDeath::With<DefaultLifetime>::AsMasterLifetime > MasterSingleton; \endcode
|
|
|
|
/// \code SingletonHolder< F , CreateUsingNew, FollowIntoDeath::AfterMaster< MasterSingleton >::IsDestroyed > FollowerSingleton \endcode
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-02 20:01:11 +00:00
|
|
|
class FollowIntoDeath
|
2005-11-01 11:11:52 +00:00
|
|
|
{
|
|
|
|
template<class T>
|
|
|
|
class Followers
|
|
|
|
{
|
|
|
|
typedef std::vector<atexit_pfn_t> Container;
|
|
|
|
typedef typename Container::iterator iterator;
|
|
|
|
static Container* followers_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void Init()
|
|
|
|
{
|
|
|
|
static bool done = false;
|
|
|
|
if(!done)
|
|
|
|
{
|
|
|
|
followers_ = new Container;
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AddFollower(atexit_pfn_t ae)
|
|
|
|
{
|
|
|
|
Init();
|
|
|
|
followers_->push_back(ae);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DestroyFollowers()
|
|
|
|
{
|
|
|
|
Init();
|
|
|
|
for(iterator it = followers_->begin();it != followers_->end();++it)
|
|
|
|
(*it)();
|
|
|
|
delete followers_;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2005-11-02 20:01:11 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
/// \struct With
|
|
|
|
/// Template for the master
|
|
|
|
/// \param Lifetime Lifetime policy for the master
|
2005-11-01 11:11:52 +00:00
|
|
|
template<template <class> class Lifetime>
|
|
|
|
struct With
|
|
|
|
{
|
2005-11-02 20:01:11 +00:00
|
|
|
/// \struct AsMasterLifetime
|
|
|
|
/// Policy for master
|
2005-11-01 11:11:52 +00:00
|
|
|
template<class Master>
|
|
|
|
struct AsMasterLifetime
|
|
|
|
{
|
|
|
|
static void ScheduleDestruction(Master* pObj, atexit_pfn_t pFun)
|
|
|
|
{
|
|
|
|
Followers<Master>::Init();
|
|
|
|
Lifetime<Master>::ScheduleDestruction(pObj, pFun);
|
|
|
|
|
|
|
|
// use same policy for the followers and force a new
|
|
|
|
// template instantiation, this adds a additional atexit entry
|
|
|
|
// does not work with SetLonlevity, but there you can control
|
|
|
|
// the lifetime with the GetLongevity function.
|
|
|
|
Lifetime<Followers<Master> >::ScheduleDestruction(0,Followers<Master>::DestroyFollowers);
|
|
|
|
}
|
2005-11-02 20:01:11 +00:00
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
static void OnDeadReference()
|
|
|
|
{
|
|
|
|
throw std::logic_error("Dead Reference Detected");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2005-11-02 20:01:11 +00:00
|
|
|
/// \struct AfterMaster
|
|
|
|
/// Template for the follower
|
|
|
|
/// \param Master Master to follow into death
|
2005-11-01 11:11:52 +00:00
|
|
|
template<class Master>
|
|
|
|
struct AfterMaster
|
|
|
|
{
|
2005-11-02 20:01:11 +00:00
|
|
|
/// \struct IsDestroyed
|
|
|
|
/// Policy for followers
|
2005-11-01 11:11:52 +00:00
|
|
|
template<class F>
|
|
|
|
struct IsDestroyed
|
|
|
|
{
|
2005-11-02 13:58:03 +00:00
|
|
|
static void ScheduleDestruction(F*, atexit_pfn_t pFun)
|
2005-11-01 11:11:52 +00:00
|
|
|
{
|
|
|
|
Followers<Master>::AddFollower(pFun);
|
|
|
|
}
|
2005-11-02 20:01:11 +00:00
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
static void OnDeadReference()
|
|
|
|
{
|
|
|
|
throw std::logic_error("Dead Reference Detected");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
typename FollowIntoDeath::Followers<T>::Container*
|
|
|
|
FollowIntoDeath::Followers<T>::followers_ = 0;
|
2005-11-02 20:01:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-02 20:01:11 +00:00
|
|
|
/// \class SingletonHolder
|
|
|
|
///
|
|
|
|
/// \ingroup SingletonGroup
|
|
|
|
///
|
|
|
|
/// Provides Singleton amenities for a type T
|
|
|
|
/// To protect that type from spurious instantiations,
|
|
|
|
/// you have to protect it yourself.
|
|
|
|
///
|
|
|
|
/// \param CreationPolicy Creation policy, default: CreateUsingNew
|
|
|
|
/// \param LifetimePolicy Lifetime policy, default: DefaultLifetime,
|
|
|
|
/// \param ThreadingModel Threading policy,
|
|
|
|
/// default: LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2002-08-11 05:49:45 +00:00
|
|
|
template
|
|
|
|
<
|
|
|
|
typename T,
|
|
|
|
template <class> class CreationPolicy = CreateUsingNew,
|
|
|
|
template <class> class LifetimePolicy = DefaultLifetime,
|
2006-01-22 00:44:17 +00:00
|
|
|
template <class, class> class ThreadingModel = LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL,
|
2006-01-22 13:37:33 +00:00
|
|
|
class MutexPolicy = LOKI_DEFAULT_MUTEX
|
2002-08-11 05:49:45 +00:00
|
|
|
>
|
|
|
|
class SingletonHolder
|
|
|
|
{
|
|
|
|
public:
|
2005-11-02 20:01:11 +00:00
|
|
|
|
2006-02-28 10:30:17 +00:00
|
|
|
/// Type of the singleton object
|
|
|
|
typedef T ObjectType;
|
|
|
|
|
2005-11-02 20:01:11 +00:00
|
|
|
/// Returns a reference to singleton object
|
2002-08-11 05:49:45 +00:00
|
|
|
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
|
2006-01-22 00:44:17 +00:00
|
|
|
typedef typename ThreadingModel<T*,MutexPolicy>::VolatileType PtrInstanceType;
|
2002-08-11 05:49:45 +00:00
|
|
|
static PtrInstanceType pInstance_;
|
|
|
|
static bool destroyed_;
|
|
|
|
};
|
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// SingletonHolder's data
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
template
|
|
|
|
<
|
|
|
|
class T,
|
|
|
|
template <class> class C,
|
|
|
|
template <class> class L,
|
2006-01-22 00:44:17 +00:00
|
|
|
template <class, class> class M,
|
|
|
|
class X
|
2002-08-11 05:49:45 +00:00
|
|
|
>
|
2006-01-22 00:44:17 +00:00
|
|
|
typename SingletonHolder<T, C, L, M, X>::PtrInstanceType
|
|
|
|
SingletonHolder<T, C, L, M, X>::pInstance_;
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
template
|
|
|
|
<
|
|
|
|
class T,
|
|
|
|
template <class> class C,
|
|
|
|
template <class> class L,
|
2006-01-22 00:44:17 +00:00
|
|
|
template <class, class> class M,
|
|
|
|
class X
|
2002-08-11 05:49:45 +00:00
|
|
|
>
|
2006-01-22 00:44:17 +00:00
|
|
|
bool SingletonHolder<T, C, L, M, X>::destroyed_;
|
2002-08-11 05:49:45 +00:00
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// SingletonHolder::Instance
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
template
|
|
|
|
<
|
|
|
|
class T,
|
|
|
|
template <class> class CreationPolicy,
|
|
|
|
template <class> class LifetimePolicy,
|
2006-01-22 00:44:17 +00:00
|
|
|
template <class, class> class ThreadingModel,
|
|
|
|
class MutexPolicy
|
2002-08-11 05:49:45 +00:00
|
|
|
>
|
|
|
|
inline T& SingletonHolder<T, CreationPolicy,
|
2006-01-22 00:44:17 +00:00
|
|
|
LifetimePolicy, ThreadingModel, MutexPolicy>::Instance()
|
2002-08-11 05:49:45 +00:00
|
|
|
{
|
|
|
|
if (!pInstance_)
|
|
|
|
{
|
|
|
|
MakeInstance();
|
|
|
|
}
|
|
|
|
return *pInstance_;
|
|
|
|
}
|
|
|
|
|
2005-11-01 11:11:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// SingletonHolder::MakeInstance (helper for Instance)
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
template
|
|
|
|
<
|
|
|
|
class T,
|
|
|
|
template <class> class CreationPolicy,
|
|
|
|
template <class> class LifetimePolicy,
|
2006-01-22 00:44:17 +00:00
|
|
|
template <class, class> class ThreadingModel,
|
|
|
|
class MutexPolicy
|
2002-08-11 05:49:45 +00:00
|
|
|
>
|
|
|
|
void SingletonHolder<T, CreationPolicy,
|
2006-01-22 00:44:17 +00:00
|
|
|
LifetimePolicy, ThreadingModel, MutexPolicy>::MakeInstance()
|
2002-08-11 05:49:45 +00:00
|
|
|
{
|
2006-01-22 00:44:17 +00:00
|
|
|
typename ThreadingModel<SingletonHolder,MutexPolicy>::Lock guard;
|
2002-08-11 05:49:45 +00:00
|
|
|
(void)guard;
|
|
|
|
|
|
|
|
if (!pInstance_)
|
|
|
|
{
|
|
|
|
if (destroyed_)
|
|
|
|
{
|
2005-10-17 08:43:42 +00:00
|
|
|
destroyed_ = false;
|
2002-08-11 05:49:45 +00:00
|
|
|
LifetimePolicy<T>::OnDeadReference();
|
|
|
|
}
|
|
|
|
pInstance_ = CreationPolicy<T>::Create();
|
|
|
|
LifetimePolicy<T>::ScheduleDestruction(pInstance_,
|
|
|
|
&DestroySingleton);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template
|
|
|
|
<
|
|
|
|
class T,
|
|
|
|
template <class> class CreationPolicy,
|
|
|
|
template <class> class L,
|
2006-01-22 00:44:17 +00:00
|
|
|
template <class, class> class M,
|
|
|
|
class X
|
2002-08-11 05:49:45 +00:00
|
|
|
>
|
2005-09-26 07:33:05 +00:00
|
|
|
void LOKI_C_CALLING_CONVENTION_QUALIFIER
|
2006-01-22 00:44:17 +00:00
|
|
|
SingletonHolder<T, CreationPolicy, L, M, X>::DestroySingleton()
|
2002-08-11 05:49:45 +00:00
|
|
|
{
|
|
|
|
assert(!destroyed_);
|
|
|
|
CreationPolicy<T>::Destroy(pInstance_);
|
|
|
|
pInstance_ = 0;
|
|
|
|
destroyed_ = true;
|
|
|
|
}
|
2006-02-28 10:30:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// \class Singleton
|
|
|
|
///
|
|
|
|
/// \ingroup SingletonGroup
|
2006-02-28 10:31:01 +00:00
|
|
|
///
|
|
|
|
/// Convenience template to implement a getter function for a singleton object.
|
2006-02-28 10:30:17 +00:00
|
|
|
/// Often needed in a shared library which hosts singletons.
|
2006-02-28 10:31:01 +00:00
|
|
|
///
|
|
|
|
/// \par Usage
|
|
|
|
///
|
|
|
|
/// see test/SingletonDll
|
2006-02-28 10:30:17 +00:00
|
|
|
///
|
2006-02-28 10:31:01 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-02-28 10:30:17 +00:00
|
|
|
|
2006-09-15 06:30:16 +00:00
|
|
|
#ifndef LOKI_SINGLETON_EXPORT
|
|
|
|
#define LOKI_SINGLETON_EXPORT
|
|
|
|
#endif
|
|
|
|
|
2006-02-28 10:31:01 +00:00
|
|
|
template<class T>
|
2006-09-15 06:30:16 +00:00
|
|
|
class LOKI_SINGLETON_EXPORT Singleton
|
2006-02-28 10:31:01 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static T& Instance();
|
|
|
|
};
|
2006-02-28 10:30:17 +00:00
|
|
|
|
2006-03-15 08:47:19 +00:00
|
|
|
} // namespace Loki
|
|
|
|
|
|
|
|
|
|
|
|
/// \def LOKI_SINGLETON_INSTANCE_DEFINITION(SHOLDER)
|
|
|
|
/// Convenience macro for the definition of the static Instance member function
|
|
|
|
/// Put this macro called with a SingletonHolder typedef into your cpp file.
|
|
|
|
|
|
|
|
#define LOKI_SINGLETON_INSTANCE_DEFINITION(SHOLDER) \
|
|
|
|
namespace Loki \
|
2006-06-19 12:39:09 +00:00
|
|
|
{ \
|
|
|
|
template<> \
|
|
|
|
SHOLDER::ObjectType& Singleton<SHOLDER::ObjectType>::Instance() \
|
|
|
|
{ \
|
2006-03-15 08:47:19 +00:00
|
|
|
return SHOLDER::Instance(); \
|
2006-06-19 12:39:09 +00:00
|
|
|
} \
|
2006-03-15 08:47:19 +00:00
|
|
|
}
|
2006-02-28 10:30:17 +00:00
|
|
|
|
|
|
|
|
2006-10-17 19:30:16 +00:00
|
|
|
#endif // end file guardian
|
2002-08-11 05:49:45 +00:00
|
|
|
|