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:
syntheticpp 2005-11-07 12:06:43 +00:00
parent 4e97accb6a
commit 0fc58a0e86
9 changed files with 363 additions and 398 deletions

View file

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

View file

@ -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..'
//

View file

@ -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