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
|
@ -427,6 +427,29 @@ namespace Loki
|
|||
{}
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// \defgroup LongevityLifetimeGroup LongevityLifetime
|
||||
/// \ingroup LifetimeGroup
|
||||
///
|
||||
/// \namespace LongevityLifetime
|
||||
///
|
||||
/// \ingroup LongevityLifetimeGroup
|
||||
/// \brief In this namespace are special lifetime policies to manage lifetime
|
||||
/// dependencies.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
namespace LongevityLifetime
|
||||
{
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// \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
|
||||
{
|
||||
|
@ -441,26 +464,28 @@ namespace Loki
|
|||
{ throw std::logic_error("Dead Reference Detected"); }
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// \struct DieOrder
|
||||
///
|
||||
/// \ingroup LifetimeGroup
|
||||
/// Lifetime policy to handle lifetime dependencies
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
struct DieOrder
|
||||
{
|
||||
/// \struct Last
|
||||
/// Die after the singleton with the DieOrder::First lifetime
|
||||
/// \struct DieLast
|
||||
/// \ingroup LongevityLifetimeGroup
|
||||
/// \brief Longest possible SingletonWithLongevity lifetime: 0xFFFFFFFF
|
||||
template <class T>
|
||||
struct Last : public SingletonFixedLongevity<0xFFFFFFFF ,T>
|
||||
struct DieLast : SingletonFixedLongevity<0xFFFFFFFF ,T>
|
||||
{};
|
||||
|
||||
/// \struct First
|
||||
/// Die before the singleton with the DieOrder::Last lifetime
|
||||
/// \struct DieDirectlyBeforeLast
|
||||
/// \ingroup LongevityLifetimeGroup
|
||||
/// \brief Lifetime is a one less than DieLast: 0xFFFFFFFF-1
|
||||
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
|
||||
|
|
|
@ -36,18 +36,8 @@
|
|||
#define LOKI_DEFAULT_OBJECT_ALIGNMENT 4
|
||||
#endif
|
||||
|
||||
|
||||
//#define LOKI_DEFAULT_SMALLOBJ_LIFETIME DefaultLifetime
|
||||
|
||||
#ifndef LOKI_DEFAULT_SMALLOBJ_LIFETIME
|
||||
|
||||
#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
|
||||
|
||||
#define LOKI_DEFAULT_SMALLOBJ_LIFETIME LongevityLifetime::DieAsSmallObjectParent
|
||||
#endif
|
||||
|
||||
#if defined(LOKI_SMALL_OBJECT_USE_NEW_ARRAY) && defined(_MSC_VER)
|
||||
|
@ -63,6 +53,28 @@
|
|||
|
||||
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 SmallObjAllocator
|
||||
|
@ -540,6 +552,9 @@ namespace Loki
|
|||
// Nov. 26, 2004: re-implemented by Rich Sposato.
|
||||
//
|
||||
// $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
|
||||
// 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
|
||||
<
|
||||
Factory< AbstractProduct, int >,
|
||||
CreateUsingNew
|
||||
#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
||||
,FollowIntoDeath::AfterMaster<Functor<>::Impl::ObjAllocatorSingleton>::IsDestroyed
|
||||
#endif
|
||||
CreateUsingNew,
|
||||
Loki::LongevityLifetime::DieAsSmallObjectChild
|
||||
>
|
||||
PFactoryNull;
|
||||
|
||||
|
@ -70,10 +68,8 @@ typedef SingletonHolder
|
|||
#else
|
||||
Factory< AbstractProduct, int, Seq< int, int > >,
|
||||
#endif
|
||||
CreateUsingNew
|
||||
#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
||||
,FollowIntoDeath::AfterMaster<Functor<>::Impl::ObjAllocatorSingleton>::IsDestroyed
|
||||
#endif
|
||||
CreateUsingNew,
|
||||
Loki::LongevityLifetime::DieAsSmallObjectChild
|
||||
>
|
||||
PFactory;
|
||||
|
||||
|
@ -171,10 +167,8 @@ SingletonHolder
|
|||
#else
|
||||
Factory< AbstractProduct, int,Seq<CreateFunctor,int,int> >,
|
||||
#endif
|
||||
CreateUsingNew
|
||||
#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
||||
,FollowIntoDeath::AfterMaster<Functor<>::Impl::ObjAllocatorSingleton>::IsDestroyed
|
||||
#endif
|
||||
CreateUsingNew,
|
||||
Loki::LongevityLifetime::DieAsSmallObjectChild
|
||||
>
|
||||
PFactoryFunctorParm;
|
||||
|
||||
|
@ -259,6 +253,9 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
|
||||
// $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
|
||||
// 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;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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)
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// How to use FollowIntoDeath with SmallObjects?
|
||||
|
@ -84,93 +232,6 @@ typedef SingletonHolder
|
|||
>
|
||||
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>
|
||||
struct Master1
|
||||
{
|
||||
|
@ -335,6 +397,7 @@ typedef SingletonHolder<B2_DeletableSingleton,CreateUsingNew,
|
|||
FollowIntoDeath::AfterMaster<Master2_DeletableSingleton::MasterSingleton>::IsDestroyed
|
||||
> Follower2_Singleton_DeletableSingleton;
|
||||
|
||||
#endif //#if !defined(_MSC_VER) || (_MSC_VER>=1400)
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
|
@ -374,19 +437,26 @@ void heap_debug()
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
MyFunctionObject *f1 = &Singleton_MyFunctionObject1::Instance();
|
||||
MyFunctionObject *f2 = &Singleton_MyFunctionObject2::Instance();
|
||||
|
||||
#ifdef _MSC_VER
|
||||
heap_debug();
|
||||
#endif
|
||||
|
||||
|
||||
MySmallObject_DieAs *s_DieAs = &Singleton_with_MySmallObject_DieAs::Instance();
|
||||
MyFunctionObject_DieAs *f_DieAs = &Singleton_MyFunctionObject_DieAs::Instance();
|
||||
|
||||
std::cout<<"\n";
|
||||
|
||||
B1_die_first *first= &Follower1_Singleton_B1_die_first::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";
|
||||
|
||||
B1_DefaultLifetime *def = &Follower1_Singleton_DefaultLifetime::Instance();
|
||||
|
@ -406,25 +476,21 @@ int main(int argc, char *argv[])
|
|||
del2->data = &Master2_DeletableSingleton::Singleton::Instance();
|
||||
|
||||
// memory leak when code is enabled
|
||||
//#define ENMasterBLE_LEMasterK
|
||||
//#define ENMasterBLE_LEMasterK
|
||||
#ifdef ENMasterBLE_LEMasterK
|
||||
B1_NoDestroy *no = &Follower1_Singleton_NoDestroy::Instance();
|
||||
B2_NoDestroy *no2 = &Follower2_Singleton_NoDestro0::Instance();
|
||||
no2->data = &Master2_NoDestroy::Singleton::Instance();
|
||||
#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 << "________________________________\n\n";
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
||||
:: 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
|
||||
|
||||
cl -c -Zm200 -O2 -DNDEBUG -MT -EHsc -GR -W0 -wd4710 -I"." -I"..\..\include" -Fotmp\ Phoenix.cpp
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
CPP = g++
|
||||
CC = gcc
|
||||
OBJ = SmallObjBench.o SmallObj.o
|
||||
LINKOBJ = SmallObjBench.o SmallObj.o
|
||||
OBJ = SmallObjBench.o SmallObj.o Singleton.o
|
||||
LINKOBJ = SmallObjBench.o SmallObj.o Singleton.o
|
||||
CXXINCS = -I./../../include -I./../../include/loki
|
||||
CXXFLAGS = $(CXXINCS) -DNDEBUG -fexpensive-optimizations -O3
|
||||
BIN = main-gcc.exe
|
||||
|
@ -30,3 +30,6 @@ SmallObjBench.o: SmallObjBench.cpp
|
|||
|
||||
SmallObj.o: ../../src/SmallObj.cpp
|
||||
$(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 <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
|
||||
// a message when a SmallObject is created/deleted.
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
//
|
||||
// FollowIntoDeath policy
|
||||
// LongevityLifetime Parent/Child policies
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
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
|
||||
Loki::LongevityLifetime::DieAsSmallObjectParent
|
||||
>
|
||||
MasterObject;
|
||||
SmallObjectParent;
|
||||
|
||||
// this is default so you could also use:
|
||||
// typedef Loki::SmallValueObject<> MasterObject;
|
||||
|
||||
class FollowerSingleton : public MasterObject
|
||||
class SmallObjectChild : public SmallObjectParent
|
||||
{
|
||||
public:
|
||||
|
||||
typedef Loki::SingletonHolder< FollowerSingleton, Loki::CreateUsingNew,
|
||||
Loki::FollowIntoDeath::AfterMaster<FollowerSingleton::ObjAllocatorSingleton>::IsDestroyed,
|
||||
typedef Loki::SingletonHolder< SmallObjectChild, Loki::CreateUsingNew,
|
||||
Loki::LongevityLifetime::DieAsSmallObjectChild,
|
||||
LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL >
|
||||
MySmallSingleton;
|
||||
|
||||
/// Returns reference to the singleton.
|
||||
inline static FollowerSingleton & Instance( void )
|
||||
inline static SmallObjectChild & Instance( void )
|
||||
{
|
||||
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 )
|
||||
{
|
||||
cout << "FollowerSingleton::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;
|
||||
cout << "SmallObjectChild::DoThat" << endl << endl;
|
||||
}
|
||||
private:
|
||||
char m_stuff[ 16 ];
|
||||
|
@ -170,6 +118,55 @@ inline unsigned int GetLongevity( LongLivedSingleton * )
|
|||
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
|
||||
<< "to manage the lifetime dependency:" << 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
|
||||
<< " FollowIntoDeath::With<LIFETIME>::AsMasterLiftime policy" << endl
|
||||
<< " and the derived Singleton has " << endl
|
||||
<< " FollowIntoDeath::AfterMaster<MASTERSINGLETON>::IsDestroyed policy" << endl
|
||||
<< " This is tested by the FollowerSingleton class." << endl
|
||||
|
||||
<< endl
|
||||
<< "2. DieOrder:" << endl
|
||||
<< " 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
|
||||
#endif
|
||||
<< endl << 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();
|
||||
|
||||
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;
|
||||
|
||||
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$
|
||||
// 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
|
||||
// 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
|
||||
|
||||
:: 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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue