add removed tests with NoDestroy plolicy

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@358 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2005-11-13 13:39:15 +00:00
parent 71b6338ca7
commit f10f61534c

View file

@ -41,8 +41,7 @@ class SmallObjectChild : public SmallObjectParent
public: public:
typedef Loki::SingletonHolder< SmallObjectChild, Loki::CreateUsingNew, typedef Loki::SingletonHolder< SmallObjectChild, Loki::CreateUsingNew,
Loki::LongevityLifetime::DieAsSmallObjectChild, Loki::LongevityLifetime::DieAsSmallObjectChild>
LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL >
MySmallSingleton; MySmallSingleton;
/// Returns reference to the singleton. /// Returns reference to the singleton.
@ -86,8 +85,7 @@ class LongLivedSingleton : public LongLivedObject
public: public:
typedef Loki::SingletonHolder< LongLivedSingleton, Loki::CreateUsingNew, typedef Loki::SingletonHolder< LongLivedSingleton, Loki::CreateUsingNew,
Loki::SingletonWithLongevity, Loki::SingletonWithLongevity>
LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL >
MySmallSingleton; MySmallSingleton;
/// Returns reference to the singleton. /// Returns reference to the singleton.
@ -139,8 +137,7 @@ class FollowerSingleton : public MasterObject
public: public:
typedef Loki::SingletonHolder< FollowerSingleton, Loki::CreateUsingNew, typedef Loki::SingletonHolder< FollowerSingleton, Loki::CreateUsingNew,
Loki::FollowIntoDeath::AfterMaster<FollowerSingleton::ObjAllocatorSingleton>::IsDestroyed, Loki::FollowIntoDeath::AfterMaster<FollowerSingleton::ObjAllocatorSingleton>::IsDestroyed>
LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL >
MySmallSingleton; MySmallSingleton;
/// Returns reference to the singleton. /// Returns reference to the singleton.
@ -168,13 +165,97 @@ private:
#endif #endif
// ----------------------------------------------------------------------------
//
// NoDestroy policy
//
// ----------------------------------------------------------------------------
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 ImmortalSingleton : public ImmortalObject
{
public:
typedef Loki::SingletonHolder< ImmortalSingleton, Loki::CreateUsingNew,
Loki::NoDestroy>
MySmallSingleton;
/// Returns reference to the singleton.
inline static ImmortalSingleton & Instance( void )
{
return MySmallSingleton::Instance();
}
ImmortalSingleton( void )
{
cout << "ImmortalSingleton created" << endl;
}
~ImmortalSingleton( void )
{
cout << "~ImmortalSingleton destructor should never get called!" << endl;
}
void DoThat( void )
{
cout << "ImmortalSingleton::DoThat" << endl << endl;
}
private:
char m_stuff[ 16 ];
};
// ----------------------------------------------------------------------------
//
// NoDestroy and SingletonWithLongevity policy combination
//
// ----------------------------------------------------------------------------
class MortalSingleton : public ImmortalObject
{
public:
typedef Loki::SingletonHolder< MortalSingleton, Loki::CreateUsingNew,
Loki::SingletonWithLongevity>
MySmallSingleton;
/// Returns reference to the singleton.
inline static MortalSingleton & Instance( void )
{
return MySmallSingleton::Instance();
}
MortalSingleton( void )
{
cout << "MortalSingleton created" << endl;
}
~MortalSingleton( void )
{
cout << "~MortalSingleton" << endl;
}
void DoThat( void )
{
cout << "MortalSingleton::DoThat" << endl << 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;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// //
// detect memory leaks on MSVC Ide // detect memory leaks on MSVC Ide
// //
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#ifdef _MSC_VER //#define LOKI_MSVC_CHECK_FOR_MEMORY_LEAKS
#ifdef LOKI_MSVC_CHECK_FOR_MEMORY_LEAKS
#include <crtdbg.h> #include <crtdbg.h>
#include <cassert> #include <cassert>
@ -193,8 +274,13 @@ void heap_debug()
// Set flag to the new value // Set flag to the new value
_CrtSetDbgFlag( tmpFlag ); _CrtSetDbgFlag( tmpFlag );
} }
#else
void heap_debug()
{}
#endif #endif
@ -206,39 +292,46 @@ void heap_debug()
int main() int main()
{ {
#ifdef _MSC_VER
heap_debug(); heap_debug();
#endif
cout << endl cout << endl
<< "This program tests the three recommended lifetime policies for Loki's " << endl << "This program tests the lifetime policies for Loki's " << endl
<< "Small-Object Allocator and singleton objects that are derived " << endl << "Small-Object Allocator and singleton objects that are derived " << endl
<< "from SmallObject or SmallValueObject." << endl << "from SmallObject or SmallValueObject." << endl
<< endl << endl
<< "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. LongevityLifetime Parent/Child" << endl << "- LongevityLifetime Parent/Child: SmallObject has" << endl
<< " SmallObject has " << endl << " LongevityLifetime::DieAsSmallObjectParent" << endl
<< " LongevityLifetime::DieAsSmallObjectParent policy " << endl << " policy and the derived Singleton has " << endl
<< " and the derived Singleton has " << endl
<< " LongevityLifetime::DieAsSmallObjectChild" << endl << " LongevityLifetime::DieAsSmallObjectChild" << endl
<< " This is tested by the SmallObjectChild class." << endl << " This is tested by the SmallObjectChild class." << endl
<< endl << endl
<< "2. Both SmallObject and derived Singleton use SingletonWithLongevity policy." << endl << "- Both SmallObject and derived Singleton use" << endl
<< " This is tested by the LongLivedSingleton class." << endl << " SingletonWithLongevity" << endl
<< " policy. This is tested by the LongLivedSingleton class." << endl
<< endl << endl
#if !defined(_MSC_VER) || (_MSC_VER>=1400) #if !defined(_MSC_VER) || (_MSC_VER>=1400)
<< "3. FollowIntoDeath:" << endl << "- FollowIntoDeath: SmallObject has" << endl
<< " SmallObject has " << endl << " FollowIntoDeath::With<LIFETIME>::AsMasterLiftime" << endl
<< " FollowIntoDeath::With<LIFETIME>::AsMasterLiftime policy" << endl << " policy and the derived Singleton has " << endl
<< " and the derived Singleton has " << endl << " FollowIntoDeath::AfterMaster<MASTERSINGLETON>::IsDestroyed" << endl
<< " FollowIntoDeath::AfterMaster<MASTERSINGLETON>::IsDestroyed policy" << endl << " policy. This is tested by the FollowerSingleton class." << endl
<< " This is tested by the FollowerSingleton class." << endl
<< endl << endl
#endif #endif
<< "- Both SmallObject and derived Singleton use" << endl
<< " NoDestroy" << endl
<< " policy. This is tested by the ImmortalSingleton class." << endl
<< " Note: yow will get memory leaks" << endl
<< endl
<< "- SmallObject has"<< endl
<< " NoDestroy" << endl
<< " policy but the derived Singleton has" << endl
<< " SingletonWithLongevity" << endl
<< " policy. This is tested by the MortalSingleton class." << endl
<< " Note: yow will get memory leaks" << endl << 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 policies work." << endl << endl; << "at exit time, then all policies work." << endl << endl;
@ -251,11 +344,23 @@ int main()
FollowerSingleton::Instance().DoThat(); FollowerSingleton::Instance().DoThat();
#endif #endif
#define ENABLE_MEMORY_LEAK
#ifdef ENABLE_MEMORY_LEAK
ImmortalSingleton::Instance().DoThat();
MortalSingleton::Instance().DoThat();
#else
cout << endl;
cout << "ImmortalSingleton and MortalSingleton" << endl;
cout << "are disabled due to the test on memory leaks.";
cout << endl << endl;
#endif
#if defined(__BORLANDC__) || defined(__GNUC__) || defined(_MSC_VER) #if defined(__BORLANDC__) || defined(__GNUC__) || defined(_MSC_VER)
system("pause"); system("pause");
#endif #endif
cout << endl<< endl << "now leaving main" << endl << endl; cout << endl<< endl << "now leaving main" << endl;
cout << "________________________________" << endl << endl;
return 0; return 0;
} }
@ -263,6 +368,9 @@ int main()
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// $Log$ // $Log$
// Revision 1.10 2005/11/13 13:39:15 syntheticpp
// add removed tests with NoDestroy plolicy
//
// Revision 1.9 2005/11/07 12:06:43 syntheticpp // 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 // change lifetime policy DieOrder to a msvc7.1 compilable version. Make this the default lifetime for SmallObject
// //