change lifetime policy DieOrder to a msvc7.1 compilable version. Make this the default lifetime for SmallObject
git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@353 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
parent
4e97accb6a
commit
0fc58a0e86
9 changed files with 363 additions and 398 deletions
|
@ -18,14 +18,14 @@
|
||||||
namespace Loki
|
namespace Loki
|
||||||
{
|
{
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
/// \struct Function
|
/// \struct Function
|
||||||
///
|
///
|
||||||
/// \ingroup FunctorGroup
|
/// \ingroup FunctorGroup
|
||||||
/// Allows a boost/TR1 like usage of Functor.
|
/// Allows a boost/TR1 like usage of Functor.
|
||||||
///
|
///
|
||||||
/// e.g. Functor<int,LOKI_TYPELIST_2(int,int)> becomes Function<int(int,int)>
|
/// e.g. Functor<int,LOKI_TYPELIST_2(int,int)> becomes Function<int(int,int)>
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
template<class R = void()>
|
template<class R = void()>
|
||||||
struct Function : public Functor<R>
|
struct Function : public Functor<R>
|
||||||
{
|
{
|
||||||
|
|
|
@ -426,41 +426,66 @@ namespace Loki
|
||||||
static void OnDeadReference()
|
static void OnDeadReference()
|
||||||
{}
|
{}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <unsigned int Longevity, class T>
|
|
||||||
class SingletonFixedLongevity
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
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 DieOrder
|
/// \defgroup LongevityLifetimeGroup LongevityLifetime
|
||||||
///
|
|
||||||
/// \ingroup LifetimeGroup
|
/// \ingroup LifetimeGroup
|
||||||
/// Lifetime policy to handle lifetime dependencies
|
///
|
||||||
|
/// \namespace LongevityLifetime
|
||||||
|
///
|
||||||
|
/// \ingroup LongevityLifetimeGroup
|
||||||
|
/// \brief In this namespace are special lifetime policies to manage lifetime
|
||||||
|
/// dependencies.
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
struct DieOrder
|
namespace LongevityLifetime
|
||||||
{
|
{
|
||||||
/// \struct Last
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
/// Die after the singleton with the DieOrder::First lifetime
|
/// \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:
|
||||||
|
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
|
||||||
template <class T>
|
template <class T>
|
||||||
struct Last : public SingletonFixedLongevity<0xFFFFFFFF ,T>
|
struct DieLast : SingletonFixedLongevity<0xFFFFFFFF ,T>
|
||||||
{};
|
{};
|
||||||
|
|
||||||
/// \struct First
|
/// \struct DieDirectlyBeforeLast
|
||||||
/// Die before the singleton with the DieOrder::Last lifetime
|
/// \ingroup LongevityLifetimeGroup
|
||||||
|
/// \brief Lifetime is a one less than DieLast: 0xFFFFFFFF-1
|
||||||
template <class T>
|
template <class T>
|
||||||
struct First : public SingletonFixedLongevity<0xFFFFFFFF-1,T>
|
struct DieDirectlyBeforeLast : SingletonFixedLongevity<0xFFFFFFFF-1 ,T>
|
||||||
{};
|
{};
|
||||||
};
|
|
||||||
|
/// \struct DieFirst
|
||||||
|
/// \ingroup LongevityLifetimeGroup
|
||||||
|
/// \brief Shortest possible SingletonWithLongevity lifetime: 0
|
||||||
|
template <class T>
|
||||||
|
struct DieFirst : SingletonFixedLongevity<0,T>
|
||||||
|
{};
|
||||||
|
|
||||||
|
}//namespace LongevityLifetime
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
/// \class FollowIntoDeath
|
/// \class FollowIntoDeath
|
||||||
|
|
|
@ -36,18 +36,8 @@
|
||||||
#define LOKI_DEFAULT_OBJECT_ALIGNMENT 4
|
#define LOKI_DEFAULT_OBJECT_ALIGNMENT 4
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
//#define LOKI_DEFAULT_SMALLOBJ_LIFETIME DefaultLifetime
|
|
||||||
|
|
||||||
#ifndef LOKI_DEFAULT_SMALLOBJ_LIFETIME
|
#ifndef LOKI_DEFAULT_SMALLOBJ_LIFETIME
|
||||||
|
#define LOKI_DEFAULT_SMALLOBJ_LIFETIME LongevityLifetime::DieAsSmallObjectParent
|
||||||
#if defined(_MSC_VER) && (_MSC_VER<1400)
|
|
||||||
// msvc 7.1 is faulty and can't handle the FollowIntoDeath and DieOrder lifetime policies
|
|
||||||
#define LOKI_DEFAULT_SMALLOBJ_LIFETIME NoDestroy
|
|
||||||
#else
|
|
||||||
#define LOKI_DEFAULT_SMALLOBJ_LIFETIME FollowIntoDeath::With<DefaultLifetime>::AsMasterLifetime
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(LOKI_SMALL_OBJECT_USE_NEW_ARRAY) && defined(_MSC_VER)
|
#if defined(LOKI_SMALL_OBJECT_USE_NEW_ARRAY) && defined(_MSC_VER)
|
||||||
|
@ -63,6 +53,28 @@
|
||||||
|
|
||||||
namespace Loki
|
namespace Loki
|
||||||
{
|
{
|
||||||
|
namespace LongevityLifetime
|
||||||
|
{
|
||||||
|
/** @struct DieAsSmallObjectParent
|
||||||
|
@ingroup SmallObjectGroup
|
||||||
|
Lifetime policy to manage lifetime dependencies of
|
||||||
|
SmallObject base and child classes.
|
||||||
|
The Base class should have this lifetime
|
||||||
|
*/
|
||||||
|
template <class T>
|
||||||
|
struct DieAsSmallObjectParent : DieLast<T> {};
|
||||||
|
|
||||||
|
/** @struct DieAsSmallObjectChild
|
||||||
|
@ingroup SmallObjectGroup
|
||||||
|
Lifetime policy to manage lifetime dependencies of
|
||||||
|
SmallObject base and child classes.
|
||||||
|
The Child class should have this lifetime
|
||||||
|
*/
|
||||||
|
template <class T>
|
||||||
|
struct DieAsSmallObjectChild : DieDirectlyBeforeLast<T> {};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
class FixedAllocator;
|
class FixedAllocator;
|
||||||
|
|
||||||
/** @class SmallObjAllocator
|
/** @class SmallObjAllocator
|
||||||
|
@ -540,6 +552,9 @@ namespace Loki
|
||||||
// Nov. 26, 2004: re-implemented by Rich Sposato.
|
// Nov. 26, 2004: re-implemented by Rich Sposato.
|
||||||
//
|
//
|
||||||
// $Log$
|
// $Log$
|
||||||
|
// Revision 1.22 2005/11/07 12:06:43 syntheticpp
|
||||||
|
// change lifetime policy DieOrder to a msvc7.1 compilable version. Make this the default lifetime for SmallObject
|
||||||
|
//
|
||||||
// Revision 1.21 2005/11/05 17:43:55 syntheticpp
|
// Revision 1.21 2005/11/05 17:43:55 syntheticpp
|
||||||
// disable FollowIntoDeath/DieOrder lifetime policies when using the msvc 7.1 compiler, bug article: 839821 'Microsoft has confirmed that this is a problem..'
|
// disable FollowIntoDeath/DieOrder lifetime policies when using the msvc 7.1 compiler, bug article: 839821 'Microsoft has confirmed that this is a problem..'
|
||||||
//
|
//
|
||||||
|
|
|
@ -52,10 +52,8 @@ public:
|
||||||
typedef SingletonHolder
|
typedef SingletonHolder
|
||||||
<
|
<
|
||||||
Factory< AbstractProduct, int >,
|
Factory< AbstractProduct, int >,
|
||||||
CreateUsingNew
|
CreateUsingNew,
|
||||||
#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
Loki::LongevityLifetime::DieAsSmallObjectChild
|
||||||
,FollowIntoDeath::AfterMaster<Functor<>::Impl::ObjAllocatorSingleton>::IsDestroyed
|
|
||||||
#endif
|
|
||||||
>
|
>
|
||||||
PFactoryNull;
|
PFactoryNull;
|
||||||
|
|
||||||
|
@ -70,10 +68,8 @@ typedef SingletonHolder
|
||||||
#else
|
#else
|
||||||
Factory< AbstractProduct, int, Seq< int, int > >,
|
Factory< AbstractProduct, int, Seq< int, int > >,
|
||||||
#endif
|
#endif
|
||||||
CreateUsingNew
|
CreateUsingNew,
|
||||||
#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
Loki::LongevityLifetime::DieAsSmallObjectChild
|
||||||
,FollowIntoDeath::AfterMaster<Functor<>::Impl::ObjAllocatorSingleton>::IsDestroyed
|
|
||||||
#endif
|
|
||||||
>
|
>
|
||||||
PFactory;
|
PFactory;
|
||||||
|
|
||||||
|
@ -171,10 +167,8 @@ SingletonHolder
|
||||||
#else
|
#else
|
||||||
Factory< AbstractProduct, int,Seq<CreateFunctor,int,int> >,
|
Factory< AbstractProduct, int,Seq<CreateFunctor,int,int> >,
|
||||||
#endif
|
#endif
|
||||||
CreateUsingNew
|
CreateUsingNew,
|
||||||
#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
Loki::LongevityLifetime::DieAsSmallObjectChild
|
||||||
,FollowIntoDeath::AfterMaster<Functor<>::Impl::ObjAllocatorSingleton>::IsDestroyed
|
|
||||||
#endif
|
|
||||||
>
|
>
|
||||||
PFactoryFunctorParm;
|
PFactoryFunctorParm;
|
||||||
|
|
||||||
|
@ -259,6 +253,9 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
// $Log$
|
// $Log$
|
||||||
|
// Revision 1.8 2005/11/07 12:06:43 syntheticpp
|
||||||
|
// change lifetime policy DieOrder to a msvc7.1 compilable version. Make this the default lifetime for SmallObject
|
||||||
|
//
|
||||||
// Revision 1.7 2005/11/05 17:43:55 syntheticpp
|
// Revision 1.7 2005/11/05 17:43:55 syntheticpp
|
||||||
// disable FollowIntoDeath/DieOrder lifetime policies when using the msvc 7.1 compiler, bug article: 839821 'Microsoft has confirmed that this is a problem..'
|
// disable FollowIntoDeath/DieOrder lifetime policies when using the msvc 7.1 compiler, bug article: 839821 'Microsoft has confirmed that this is a problem..'
|
||||||
//
|
//
|
||||||
|
|
|
@ -18,8 +18,156 @@
|
||||||
|
|
||||||
using namespace Loki;
|
using namespace Loki;
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// Data object for all singletons
|
||||||
|
//
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
template<template <class> class T, int Nr>
|
||||||
|
struct SingletonDataObject
|
||||||
|
{
|
||||||
|
SingletonDataObject() {std::cout<<"new SingletonDataObject"<<Nr<<"\n\n";}
|
||||||
|
~SingletonDataObject(){std::cout<<"delete SingletonDataObject"<<Nr<<"\n\n";}
|
||||||
|
|
||||||
|
int i[Nr];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// How to use LongevityLifetime policies
|
||||||
|
// DieAsSmallObjectParent/DieAsSmallObjectChild with SmallObjects?
|
||||||
|
//
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// LongevityLifetime::DieAsSmallObjectParent is the default
|
||||||
|
// lifetime of SmallObject template:
|
||||||
|
typedef Loki::SmallObject<>
|
||||||
|
SmallObject_DieAs;
|
||||||
|
|
||||||
|
class MySmallObject_DieAs : public SmallObject_DieAs
|
||||||
|
{};
|
||||||
|
|
||||||
|
typedef SingletonHolder
|
||||||
|
<
|
||||||
|
MySmallObject_DieAs,
|
||||||
|
CreateUsingNew,
|
||||||
|
LongevityLifetime::DieAsSmallObjectChild
|
||||||
|
>
|
||||||
|
Singleton_with_MySmallObject_DieAs;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// How to use LongevityLifetime policies
|
||||||
|
// DieAsSmallObjectParent/DieAsSmallObjectChild with
|
||||||
|
// classes containing a Functor/Function?
|
||||||
|
//
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
|
struct MyFunctionObject_DieAs
|
||||||
|
{
|
||||||
|
MyFunctionObject_DieAs()
|
||||||
|
{
|
||||||
|
functor = Functor<void> (this, &MyFunctionObject_DieAs::f);
|
||||||
|
function = Function< void()>(this, &MyFunctionObject_DieAs::f);
|
||||||
|
}
|
||||||
|
|
||||||
|
void f(){}
|
||||||
|
Functor<void> functor;
|
||||||
|
Function<void()> function;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef SingletonHolder
|
||||||
|
<
|
||||||
|
MyFunctionObject_DieAs,
|
||||||
|
CreateUsingNew,
|
||||||
|
LongevityLifetime::DieAsSmallObjectChild
|
||||||
|
>
|
||||||
|
Singleton_MyFunctionObject_DieAs;
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// Test code for LongevityLifetime Policy
|
||||||
|
//
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
template<template <class> class Lifetime>
|
||||||
|
struct Master_die_first
|
||||||
|
{
|
||||||
|
Master_die_first()
|
||||||
|
{
|
||||||
|
data = &Singleton::Instance();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~Master_die_first(){}
|
||||||
|
|
||||||
|
typedef Master_die_first<Lifetime> ThisType;
|
||||||
|
typedef SingletonDataObject<Lifetime,888> MasterSingleton;
|
||||||
|
typedef SingletonHolder<MasterSingleton,CreateUsingNew,Lifetime> Singleton;
|
||||||
|
|
||||||
|
MasterSingleton* data;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<template <class> class Lifetime>
|
||||||
|
struct Master_die_last
|
||||||
|
{
|
||||||
|
Master_die_last()
|
||||||
|
{
|
||||||
|
data = &Singleton::Instance();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~Master_die_last(){}
|
||||||
|
|
||||||
|
typedef Master_die_last<Lifetime> ThisType;
|
||||||
|
typedef SingletonDataObject<Lifetime,555> MasterSingleton;
|
||||||
|
typedef SingletonHolder<MasterSingleton,CreateUsingNew,Lifetime> Singleton;
|
||||||
|
|
||||||
|
MasterSingleton* data;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef Master_die_first<LongevityLifetime::DieFirst>
|
||||||
|
Master1_die_first;
|
||||||
|
|
||||||
|
typedef Master_die_last<LongevityLifetime::DieLast>
|
||||||
|
Master1_die_last;
|
||||||
|
|
||||||
|
class B1_die_first;
|
||||||
|
class B1_die_last;
|
||||||
|
|
||||||
|
typedef SingletonHolder<B1_die_first,CreateUsingNew,
|
||||||
|
LongevityLifetime::DieFirst
|
||||||
|
> Follower1_Singleton_B1_die_first;
|
||||||
|
|
||||||
|
typedef SingletonHolder<B1_die_last,CreateUsingNew,
|
||||||
|
LongevityLifetime::DieLast
|
||||||
|
> Follower1_Singleton_B1_die_last;
|
||||||
|
|
||||||
|
class B1_die_first : public Master1_die_last
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
B1_die_first(){std::cout<<"new B1_die_first, look for SingletonDataObject555\n\n";}
|
||||||
|
~B1_die_first(){std::cout<<"delete B1_die_first, look for SingletonDataObject555\n\n";}
|
||||||
|
};
|
||||||
|
|
||||||
|
class B1_die_last : public Master1_die_first
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
B1_die_last(){std::cout<<"new B1_die_last, look for SingletonDataObject888\n\n";}
|
||||||
|
~B1_die_last(){std::cout<<"delete B1_die_last, look for SingletonDataObject888\n\n";}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// How to use FollowIntoDeath with SmallObjects?
|
// How to use FollowIntoDeath with SmallObjects?
|
||||||
|
@ -84,93 +232,6 @@ typedef SingletonHolder
|
||||||
>
|
>
|
||||||
Singleton_MyFunctionObject2;
|
Singleton_MyFunctionObject2;
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//
|
|
||||||
// Data object for all singletons
|
|
||||||
//
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
template<template <class> class T, int Nr>
|
|
||||||
struct SingletonDataObject
|
|
||||||
{
|
|
||||||
SingletonDataObject() {std::cout<<"new SingletonDataObject"<<Nr<<"\n\n";}
|
|
||||||
~SingletonDataObject(){std::cout<<"delete SingletonDataObject"<<Nr<<"\n\n";}
|
|
||||||
|
|
||||||
int i[Nr];
|
|
||||||
};
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//
|
|
||||||
// Test code for DieOrder Policy
|
|
||||||
//
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
template<template <class> class Lifetime>
|
|
||||||
struct Master_die_first
|
|
||||||
{
|
|
||||||
Master_die_first()
|
|
||||||
{
|
|
||||||
data = &Singleton::Instance();
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~Master_die_first(){}
|
|
||||||
|
|
||||||
typedef Master_die_first<Lifetime> ThisType;
|
|
||||||
typedef SingletonDataObject<Lifetime,888> MasterSingleton;
|
|
||||||
typedef SingletonHolder<MasterSingleton,CreateUsingNew,Lifetime> Singleton;
|
|
||||||
|
|
||||||
MasterSingleton* data;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<template <class> class Lifetime>
|
|
||||||
struct Master_die_last
|
|
||||||
{
|
|
||||||
Master_die_last()
|
|
||||||
{
|
|
||||||
data = &Singleton::Instance();
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~Master_die_last(){}
|
|
||||||
|
|
||||||
typedef Master_die_last<Lifetime> ThisType;
|
|
||||||
typedef SingletonDataObject<Lifetime,555> MasterSingleton;
|
|
||||||
typedef SingletonHolder<MasterSingleton,CreateUsingNew,Lifetime> Singleton;
|
|
||||||
|
|
||||||
MasterSingleton* data;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef Master_die_first<DieOrder::First>
|
|
||||||
Master1_die_first;
|
|
||||||
|
|
||||||
typedef Master_die_last<DieOrder::Last>
|
|
||||||
Master1_die_last;
|
|
||||||
|
|
||||||
class B1_die_first;
|
|
||||||
class B1_die_last;
|
|
||||||
|
|
||||||
typedef SingletonHolder<B1_die_first,CreateUsingNew,
|
|
||||||
DieOrder::First
|
|
||||||
> Follower1_Singleton_B1_die_first;
|
|
||||||
|
|
||||||
typedef SingletonHolder<B1_die_last,CreateUsingNew,
|
|
||||||
DieOrder::Last
|
|
||||||
> Follower1_Singleton_B1_die_last;
|
|
||||||
|
|
||||||
class B1_die_first : public Master1_die_last
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
B1_die_first(){std::cout<<"new B1_die_first, look for SingletonDataObject555\n\n";}
|
|
||||||
~B1_die_first(){std::cout<<"delete B1_die_first, look for SingletonDataObject555\n\n";}
|
|
||||||
};
|
|
||||||
|
|
||||||
class B1_die_last : public Master1_die_first
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
B1_die_last(){std::cout<<"new B1_die_last, look for SingletonDataObject888\n\n";}
|
|
||||||
~B1_die_last(){std::cout<<"delete B1_die_last, look for SingletonDataObject888\n\n";}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
|
@ -178,6 +239,7 @@ public:
|
||||||
//
|
//
|
||||||
////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
template<template <class> class Lifetime>
|
template<template <class> class Lifetime>
|
||||||
struct Master1
|
struct Master1
|
||||||
{
|
{
|
||||||
|
@ -335,6 +397,7 @@ typedef SingletonHolder<B2_DeletableSingleton,CreateUsingNew,
|
||||||
FollowIntoDeath::AfterMaster<Master2_DeletableSingleton::MasterSingleton>::IsDestroyed
|
FollowIntoDeath::AfterMaster<Master2_DeletableSingleton::MasterSingleton>::IsDestroyed
|
||||||
> Follower2_Singleton_DeletableSingleton;
|
> Follower2_Singleton_DeletableSingleton;
|
||||||
|
|
||||||
|
#endif //#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
|
@ -373,20 +436,27 @@ void heap_debug()
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
|
||||||
MyFunctionObject *f1 = &Singleton_MyFunctionObject1::Instance();
|
|
||||||
MyFunctionObject *f2 = &Singleton_MyFunctionObject2::Instance();
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
heap_debug();
|
heap_debug();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
MySmallObject_DieAs *s_DieAs = &Singleton_with_MySmallObject_DieAs::Instance();
|
||||||
|
MyFunctionObject_DieAs *f_DieAs = &Singleton_MyFunctionObject_DieAs::Instance();
|
||||||
|
|
||||||
std::cout<<"\n";
|
std::cout<<"\n";
|
||||||
|
|
||||||
B1_die_first *first= &Follower1_Singleton_B1_die_first::Instance();
|
B1_die_first *first= &Follower1_Singleton_B1_die_first::Instance();
|
||||||
B1_die_last *last = &Follower1_Singleton_B1_die_last::Instance();
|
B1_die_last *last = &Follower1_Singleton_B1_die_last::Instance();
|
||||||
|
|
||||||
|
|
||||||
|
// test of FollowIntoDeath policy, not supported by msvc 7.1 compiler
|
||||||
|
#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
||||||
|
|
||||||
|
MyFunctionObject *f1 = &Singleton_MyFunctionObject1::Instance();
|
||||||
|
MyFunctionObject *f2 = &Singleton_MyFunctionObject2::Instance();
|
||||||
|
|
||||||
std::cout << "\nMaster1:\n\n";
|
std::cout << "\nMaster1:\n\n";
|
||||||
|
|
||||||
B1_DefaultLifetime *def = &Follower1_Singleton_DefaultLifetime::Instance();
|
B1_DefaultLifetime *def = &Follower1_Singleton_DefaultLifetime::Instance();
|
||||||
|
@ -406,25 +476,21 @@ int main(int argc, char *argv[])
|
||||||
del2->data = &Master2_DeletableSingleton::Singleton::Instance();
|
del2->data = &Master2_DeletableSingleton::Singleton::Instance();
|
||||||
|
|
||||||
// memory leak when code is enabled
|
// memory leak when code is enabled
|
||||||
//#define ENMasterBLE_LEMasterK
|
//#define ENMasterBLE_LEMasterK
|
||||||
#ifdef ENMasterBLE_LEMasterK
|
#ifdef ENMasterBLE_LEMasterK
|
||||||
B1_NoDestroy *no = &Follower1_Singleton_NoDestroy::Instance();
|
B1_NoDestroy *no = &Follower1_Singleton_NoDestroy::Instance();
|
||||||
B2_NoDestroy *no2 = &Follower2_Singleton_NoDestro0::Instance();
|
B2_NoDestroy *no2 = &Follower2_Singleton_NoDestro0::Instance();
|
||||||
no2->data = &Master2_NoDestroy::Singleton::Instance();
|
no2->data = &Master2_NoDestroy::Singleton::Instance();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#endif //#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
||||||
|
|
||||||
|
#if defined(__BORLANDC__) || defined(__GNUC__) || defined(_MSC_VER)
|
||||||
|
system("pause"); // Stop console window from closing if run from IDE.
|
||||||
|
#endif
|
||||||
|
|
||||||
std::cout << "\nnow leaving main \n";
|
std::cout << "\nnow leaving main \n";
|
||||||
std::cout << "________________________________\n\n";
|
std::cout << "________________________________\n\n";
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
std::cout<<"code is disabled because of faulty microsoft compiler 7.1";
|
|
||||||
return 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif //#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
if not exist tmp\ mkdir tmp
|
if not exist tmp\ mkdir tmp
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
:: DeletableSingleton.cpp
|
:: DeletableSingleton.cpp
|
||||||
|
|
||||||
cl -c -Zm200 -O2 -DNDEBUG -MT -EHsc -GR -W0 -wd4710 -I"." -I"..\..\include" -Fotmp\ DeletableSingleton.cpp
|
cl -c -Zm200 -O2 -DNDEBUG -MT -EHsc -GR -W0 -wd4710 -I"." -I"..\..\include" -Fotmp\ DeletableSingleton.cpp
|
||||||
|
@ -16,6 +17,7 @@ link /NOLOGO /SUBSYSTEM:CONSOLE /incremental:no /OUT:"DeletableSingleton-msvc.ex
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
:: Phoenix.cpp
|
:: Phoenix.cpp
|
||||||
|
|
||||||
cl -c -Zm200 -O2 -DNDEBUG -MT -EHsc -GR -W0 -wd4710 -I"." -I"..\..\include" -Fotmp\ Phoenix.cpp
|
cl -c -Zm200 -O2 -DNDEBUG -MT -EHsc -GR -W0 -wd4710 -I"." -I"..\..\include" -Fotmp\ Phoenix.cpp
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
CPP = g++
|
CPP = g++
|
||||||
CC = gcc
|
CC = gcc
|
||||||
OBJ = SmallObjBench.o SmallObj.o
|
OBJ = SmallObjBench.o SmallObj.o Singleton.o
|
||||||
LINKOBJ = SmallObjBench.o SmallObj.o
|
LINKOBJ = SmallObjBench.o SmallObj.o Singleton.o
|
||||||
CXXINCS = -I./../../include -I./../../include/loki
|
CXXINCS = -I./../../include -I./../../include/loki
|
||||||
CXXFLAGS = $(CXXINCS) -DNDEBUG -fexpensive-optimizations -O3
|
CXXFLAGS = $(CXXINCS) -DNDEBUG -fexpensive-optimizations -O3
|
||||||
BIN = main-gcc.exe
|
BIN = main-gcc.exe
|
||||||
|
@ -30,3 +30,6 @@ SmallObjBench.o: SmallObjBench.cpp
|
||||||
|
|
||||||
SmallObj.o: ../../src/SmallObj.cpp
|
SmallObj.o: ../../src/SmallObj.cpp
|
||||||
$(CPP) -c ../../src/SmallObj.cpp -o SmallObj.o $(CXXFLAGS)
|
$(CPP) -c ../../src/SmallObj.cpp -o SmallObj.o $(CXXFLAGS)
|
||||||
|
|
||||||
|
Singleton.o: ../../src/Singleton.cpp
|
||||||
|
$(CPP) -c ../../src/Singleton.cpp -o Singleton.o $(CXXFLAGS)
|
|
@ -18,102 +18,50 @@
|
||||||
#include "../../include/loki/Singleton.h"
|
#include "../../include/loki/Singleton.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
// disable new implementation when using msvc 7.1
|
|
||||||
#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
|
||||||
|
|
||||||
// define DO_EXTRA_LOKI_TESTS in src/SmallObj.cpp to get
|
// define DO_EXTRA_LOKI_TESTS in src/SmallObj.cpp to get
|
||||||
// a message when a SmallObject is created/deleted.
|
// a message when a SmallObject is created/deleted.
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
// FollowIntoDeath policy
|
// LongevityLifetime Parent/Child policies
|
||||||
//
|
//
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
typedef Loki::SmallValueObject< LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL,
|
typedef Loki::SmallValueObject< LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL,
|
||||||
LOKI_DEFAULT_CHUNK_SIZE, LOKI_MAX_SMALL_OBJECT_SIZE,
|
LOKI_DEFAULT_CHUNK_SIZE, LOKI_MAX_SMALL_OBJECT_SIZE,
|
||||||
LOKI_DEFAULT_OBJECT_ALIGNMENT,
|
LOKI_DEFAULT_OBJECT_ALIGNMENT,
|
||||||
Loki::FollowIntoDeath::With<Loki::DefaultLifetime>::AsMasterLifetime
|
Loki::LongevityLifetime::DieAsSmallObjectParent
|
||||||
>
|
>
|
||||||
MasterObject;
|
SmallObjectParent;
|
||||||
|
|
||||||
// this is default so you could also use:
|
class SmallObjectChild : public SmallObjectParent
|
||||||
// typedef Loki::SmallValueObject<> MasterObject;
|
|
||||||
|
|
||||||
class FollowerSingleton : public MasterObject
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
typedef Loki::SingletonHolder< FollowerSingleton, Loki::CreateUsingNew,
|
typedef Loki::SingletonHolder< SmallObjectChild, Loki::CreateUsingNew,
|
||||||
Loki::FollowIntoDeath::AfterMaster<FollowerSingleton::ObjAllocatorSingleton>::IsDestroyed,
|
Loki::LongevityLifetime::DieAsSmallObjectChild,
|
||||||
LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL >
|
LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL >
|
||||||
MySmallSingleton;
|
MySmallSingleton;
|
||||||
|
|
||||||
/// Returns reference to the singleton.
|
/// Returns reference to the singleton.
|
||||||
inline static FollowerSingleton & Instance( void )
|
inline static SmallObjectChild & Instance( void )
|
||||||
{
|
{
|
||||||
return MySmallSingleton::Instance();
|
return MySmallSingleton::Instance();
|
||||||
}
|
}
|
||||||
|
|
||||||
FollowerSingleton( void )
|
SmallObjectChild( void )
|
||||||
{
|
{
|
||||||
cout << "FollowerSingleton created" << endl;
|
cout << "SmallObjectChild created" << endl;
|
||||||
}
|
}
|
||||||
~FollowerSingleton( void )
|
~SmallObjectChild( void )
|
||||||
{
|
{
|
||||||
cout << "~FollowerSingleton" << endl;
|
cout << "~SmallObjectChild" << endl;
|
||||||
}
|
}
|
||||||
void DoThat( void )
|
void DoThat( void )
|
||||||
{
|
{
|
||||||
cout << "FollowerSingleton::DoThat" << endl << endl;
|
cout << "SmallObjectChild::DoThat" << endl << endl;
|
||||||
}
|
|
||||||
private:
|
|
||||||
char m_stuff[ 16 ];
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
|
||||||
//
|
|
||||||
// DieOrder policy
|
|
||||||
//
|
|
||||||
// ----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
typedef Loki::SmallValueObject< LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL,
|
|
||||||
LOKI_DEFAULT_CHUNK_SIZE, LOKI_MAX_SMALL_OBJECT_SIZE,
|
|
||||||
LOKI_DEFAULT_OBJECT_ALIGNMENT,
|
|
||||||
Loki::DieOrder::Last
|
|
||||||
>
|
|
||||||
DieLastObject;
|
|
||||||
|
|
||||||
class DieFirstSingleton : public DieLastObject
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
typedef Loki::SingletonHolder< DieFirstSingleton, Loki::CreateUsingNew,
|
|
||||||
Loki::DieOrder::First,
|
|
||||||
LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL >
|
|
||||||
MySmallSingleton;
|
|
||||||
|
|
||||||
/// Returns reference to the singleton.
|
|
||||||
inline static DieFirstSingleton & Instance( void )
|
|
||||||
{
|
|
||||||
return MySmallSingleton::Instance();
|
|
||||||
}
|
|
||||||
|
|
||||||
DieFirstSingleton( void )
|
|
||||||
{
|
|
||||||
cout << "DieFirstSingleton created" << endl;
|
|
||||||
}
|
|
||||||
~DieFirstSingleton( void )
|
|
||||||
{
|
|
||||||
cout << "~DieFirstSingleton" << endl;
|
|
||||||
}
|
|
||||||
void DoThat( void )
|
|
||||||
{
|
|
||||||
cout << "DieFirstSingleton::DoThat" << endl << endl;
|
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
char m_stuff[ 16 ];
|
char m_stuff[ 16 ];
|
||||||
|
@ -170,6 +118,55 @@ inline unsigned int GetLongevity( LongLivedSingleton * )
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// FollowIntoDeath policy
|
||||||
|
//
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
typedef Loki::SmallValueObject< LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL,
|
||||||
|
LOKI_DEFAULT_CHUNK_SIZE, LOKI_MAX_SMALL_OBJECT_SIZE,
|
||||||
|
LOKI_DEFAULT_OBJECT_ALIGNMENT,
|
||||||
|
Loki::FollowIntoDeath::With<Loki::DefaultLifetime>::AsMasterLifetime
|
||||||
|
>
|
||||||
|
MasterObject;
|
||||||
|
|
||||||
|
|
||||||
|
class FollowerSingleton : public MasterObject
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
typedef Loki::SingletonHolder< FollowerSingleton, Loki::CreateUsingNew,
|
||||||
|
Loki::FollowIntoDeath::AfterMaster<FollowerSingleton::ObjAllocatorSingleton>::IsDestroyed,
|
||||||
|
LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL >
|
||||||
|
MySmallSingleton;
|
||||||
|
|
||||||
|
/// Returns reference to the singleton.
|
||||||
|
inline static FollowerSingleton & Instance( void )
|
||||||
|
{
|
||||||
|
return MySmallSingleton::Instance();
|
||||||
|
}
|
||||||
|
|
||||||
|
FollowerSingleton( void )
|
||||||
|
{
|
||||||
|
cout << "FollowerSingleton created" << endl;
|
||||||
|
}
|
||||||
|
~FollowerSingleton( void )
|
||||||
|
{
|
||||||
|
cout << "~FollowerSingleton" << endl;
|
||||||
|
}
|
||||||
|
void DoThat( void )
|
||||||
|
{
|
||||||
|
cout << "FollowerSingleton::DoThat" << endl << endl;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
char m_stuff[ 16 ];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
|
@ -222,196 +219,53 @@ int main()
|
||||||
<< "Use one of the following lifetime policies" << endl
|
<< "Use one of the following lifetime policies" << endl
|
||||||
<< "to manage the lifetime dependency:" << endl
|
<< "to manage the lifetime dependency:" << endl
|
||||||
<< endl
|
<< endl
|
||||||
<< "1. FollowIntoDeath:" << endl
|
<< "1. LongevityLifetime Parent/Child" << endl
|
||||||
|
<< " SmallObject has " << endl
|
||||||
|
<< " LongevityLifetime::DieAsSmallObjectParent policy " << endl
|
||||||
|
<< " and the derived Singleton has " << endl
|
||||||
|
<< " LongevityLifetime::DieAsSmallObjectChild" << endl
|
||||||
|
<< " This is tested by the SmallObjectChild class." << endl
|
||||||
|
<< endl
|
||||||
|
<< "2. Both SmallObject and derived Singleton use SingletonWithLongevity policy." << endl
|
||||||
|
<< " This is tested by the LongLivedSingleton class." << endl
|
||||||
|
<< endl
|
||||||
|
#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
||||||
|
<< "3. FollowIntoDeath:" << endl
|
||||||
<< " SmallObject has " << endl
|
<< " SmallObject has " << endl
|
||||||
<< " FollowIntoDeath::With<LIFETIME>::AsMasterLiftime policy" << endl
|
<< " FollowIntoDeath::With<LIFETIME>::AsMasterLiftime policy" << endl
|
||||||
<< " and the derived Singleton has " << endl
|
<< " and the derived Singleton has " << endl
|
||||||
<< " FollowIntoDeath::AfterMaster<MASTERSINGLETON>::IsDestroyed policy" << endl
|
<< " FollowIntoDeath::AfterMaster<MASTERSINGLETON>::IsDestroyed policy" << endl
|
||||||
<< " This is tested by the FollowerSingleton class." << endl
|
<< " This is tested by the FollowerSingleton class." << endl
|
||||||
|
|
||||||
<< endl
|
<< endl
|
||||||
<< "2. DieOrder:" << endl
|
#endif
|
||||||
<< " SmallObject has " << endl
|
|
||||||
<< " DieOrder::Last policy " << endl
|
|
||||||
<< " and the derived Singleton has " << endl
|
|
||||||
<< " DieOrder::First" << endl
|
|
||||||
<< " This is tested by the DieFirstSingleton class." << endl
|
|
||||||
<< endl
|
|
||||||
<< "3. Both SmallObject and derived Singleton use SingletonWithLongevity policy." << endl
|
|
||||||
<< " This is tested by the LongLivedSingleton class." << endl
|
|
||||||
<< endl << endl
|
<< endl << endl
|
||||||
<< "If this program executes without crashing or asserting" << endl
|
<< "If this program executes without crashing or asserting" << endl
|
||||||
<< "at exit time, then all 3 policies work." << endl << endl;
|
<< "at exit time, then all policies work." << endl << endl;
|
||||||
|
|
||||||
FollowerSingleton::Instance().DoThat();
|
|
||||||
DieFirstSingleton::Instance().DoThat();
|
SmallObjectChild::Instance().DoThat();
|
||||||
LongLivedSingleton::Instance().DoThat();
|
LongLivedSingleton::Instance().DoThat();
|
||||||
|
|
||||||
system("PAUSE");
|
#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
||||||
|
FollowerSingleton::Instance().DoThat();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__BORLANDC__) || defined(__GNUC__) || defined(_MSC_VER)
|
||||||
|
system("pause");
|
||||||
|
#endif
|
||||||
|
|
||||||
cout << endl<< endl << "now leaving main" << endl << endl;
|
cout << endl<< endl << "now leaving main" << endl << endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
typedef Loki::SmallValueObject< LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL,
|
|
||||||
LOKI_DEFAULT_CHUNK_SIZE, LOKI_MAX_SMALL_OBJECT_SIZE,
|
|
||||||
LOKI_DEFAULT_OBJECT_ALIGNMENT, Loki::SingletonWithLongevity >
|
|
||||||
LongLivedObject;
|
|
||||||
|
|
||||||
typedef Loki::SmallValueObject< LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL,
|
|
||||||
LOKI_DEFAULT_CHUNK_SIZE, LOKI_MAX_SMALL_OBJECT_SIZE,
|
|
||||||
LOKI_DEFAULT_OBJECT_ALIGNMENT, Loki::NoDestroy >
|
|
||||||
ImmortalObject;
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
class LongLivedSingleton : public LongLivedObject
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
typedef Loki::SingletonHolder< LongLivedSingleton, Loki::CreateUsingNew,
|
|
||||||
Loki::SingletonWithLongevity, LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL >
|
|
||||||
MySmallSingleton;
|
|
||||||
|
|
||||||
/// Returns reference to the singleton.
|
|
||||||
inline static LongLivedSingleton & Instance( void )
|
|
||||||
{
|
|
||||||
return MySmallSingleton::Instance();
|
|
||||||
}
|
|
||||||
|
|
||||||
LongLivedSingleton( void )
|
|
||||||
{
|
|
||||||
cout << "LongLivedSingleton" << endl;
|
|
||||||
}
|
|
||||||
~LongLivedSingleton( void )
|
|
||||||
{
|
|
||||||
cout << "~LongLivedSingleton" << endl;
|
|
||||||
}
|
|
||||||
void DoThat( void )
|
|
||||||
{
|
|
||||||
cout << "LongLivedSingleton::DoThat" << endl;
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
char m_stuff[ 16 ];
|
|
||||||
};
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
inline unsigned int GetLongevity( LongLivedSingleton * )
|
|
||||||
{
|
|
||||||
/// @note Must return a longevity level lower than the one in SmallObj.h.
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
class ImmortalSingleton : public ImmortalObject
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
typedef Loki::SingletonHolder< ImmortalSingleton, Loki::CreateUsingNew,
|
|
||||||
Loki::NoDestroy, LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL >
|
|
||||||
MySmallSingleton;
|
|
||||||
|
|
||||||
/// Returns reference to the singleton.
|
|
||||||
inline static ImmortalSingleton & Instance( void )
|
|
||||||
{
|
|
||||||
return MySmallSingleton::Instance();
|
|
||||||
}
|
|
||||||
|
|
||||||
ImmortalSingleton( void )
|
|
||||||
{
|
|
||||||
cout << "ImmortalSingleton" << endl;
|
|
||||||
}
|
|
||||||
~ImmortalSingleton( void )
|
|
||||||
{
|
|
||||||
cout << "~ImmortalSingleton destructor should never get called!" << endl;
|
|
||||||
}
|
|
||||||
void DoThat( void )
|
|
||||||
{
|
|
||||||
cout << "ImmortalSingleton::DoThat" << endl;
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
char m_stuff[ 16 ];
|
|
||||||
};
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
class MortalSingleton : public ImmortalObject
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
typedef Loki::SingletonHolder< MortalSingleton, Loki::CreateUsingNew,
|
|
||||||
Loki::SingletonWithLongevity, LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL >
|
|
||||||
MySmallSingleton;
|
|
||||||
|
|
||||||
/// Returns reference to the singleton.
|
|
||||||
inline static MortalSingleton & Instance( void )
|
|
||||||
{
|
|
||||||
return MySmallSingleton::Instance();
|
|
||||||
}
|
|
||||||
|
|
||||||
MortalSingleton( void )
|
|
||||||
{
|
|
||||||
cout << "MortalSingleton" << endl;
|
|
||||||
}
|
|
||||||
~MortalSingleton( void )
|
|
||||||
{
|
|
||||||
cout << "~MortalSingleton" << endl;
|
|
||||||
}
|
|
||||||
void DoThat( void )
|
|
||||||
{
|
|
||||||
cout << "MortalSingleton::DoThat" << endl;
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
char m_stuff[ 16 ];
|
|
||||||
};
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
inline unsigned int GetLongevity( MortalSingleton * )
|
|
||||||
{
|
|
||||||
/// @note Must return a longevity level lower than the one in SmallObj.h.
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
cout << "This program tests the three recommended combinations of singleton" << endl
|
|
||||||
<< "lifetime policies for Loki's Small-Object Allocator and singleton" << endl
|
|
||||||
<< "objects that are derived from SmallObject or SmallValueObject." << endl << endl
|
|
||||||
<< "Those 3 recommended combinations are:" << endl
|
|
||||||
<< "\t1. Both SmallObject and derived Singleton use NoDestroy policy." << endl
|
|
||||||
<< "\t This is tested by the ImmortalSingleton class." << endl
|
|
||||||
<< "\t2. Both use SingletonWithLongevity policy." << endl
|
|
||||||
<< "\t This is tested by the LongLivedSingleton class." << endl
|
|
||||||
<< "\t3. SmallObject has NoDestroy policy but the derived Singleton has" << endl
|
|
||||||
<< "\t SingletonWithLongevity policy." << endl
|
|
||||||
<< "\t This is tested by the MortalSingleton class." << endl << endl
|
|
||||||
<< "If this program executes without crashing or asserting at exit time," << endl
|
|
||||||
<< "then all 3 combinations work." << endl << endl;
|
|
||||||
MortalSingleton::Instance().DoThat();
|
|
||||||
LongLivedSingleton::Instance().DoThat();
|
|
||||||
ImmortalSingleton::Instance().DoThat();
|
|
||||||
system("PAUSE");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
// $Log$
|
// $Log$
|
||||||
|
// Revision 1.9 2005/11/07 12:06:43 syntheticpp
|
||||||
|
// change lifetime policy DieOrder to a msvc7.1 compilable version. Make this the default lifetime for SmallObject
|
||||||
|
//
|
||||||
// Revision 1.8 2005/11/05 17:43:55 syntheticpp
|
// Revision 1.8 2005/11/05 17:43:55 syntheticpp
|
||||||
// disable FollowIntoDeath/DieOrder lifetime policies when using the msvc 7.1 compiler, bug article: 839821 'Microsoft has confirmed that this is a problem..'
|
// disable FollowIntoDeath/DieOrder lifetime policies when using the msvc 7.1 compiler, bug article: 839821 'Microsoft has confirmed that this is a problem..'
|
||||||
//
|
//
|
||||||
|
|
|
@ -20,11 +20,14 @@ if "%1"=="dll" (
|
||||||
|
|
||||||
@ECHO ON
|
@ECHO ON
|
||||||
|
|
||||||
:: main.cpp
|
|
||||||
|
|
||||||
cl -c -DNDEBUG -Zm200 -O2 -%MTMD% -EHsc -GR -W4 -wd4710 -I"." -I"..\..\include" -I"..\..\include\loki" -Fotmp\ SmallObjBench.cpp ..\..\src\SmallObj.cpp
|
|
||||||
|
|
||||||
link /NOLOGO /SUBSYSTEM:CONSOLE /incremental:no /OUT:%OUT_EXE% tmp\SmallObjBench.obj tmp\SmallObj.obj
|
:: SmallObjBench.cpp
|
||||||
|
|
||||||
|
cl -c -DNDEBUG -Zm200 -O2 -%MTMD% -EHsc -GR -W4 -wd4710 -I"." -I"..\..\include" -I"..\..\include\loki" -Fotmp\ SmallObjBench.cpp ..\..\src\SmallObj.cpp ..\..\src\Singleton.cpp
|
||||||
|
|
||||||
|
link /NOLOGO /SUBSYSTEM:CONSOLE /incremental:no /OUT:%OUT_EXE% tmp\SmallObjBench.obj tmp\SmallObj.obj tmp\Singleton.obj
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
:: SmallSingleton.cpp
|
:: SmallSingleton.cpp
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue