diff --git a/test/SmallObj/SmallSingleton.cpp b/test/SmallObj/SmallSingleton.cpp index f659fb8..607b878 100644 --- a/test/SmallObj/SmallSingleton.cpp +++ b/test/SmallObj/SmallSingleton.cpp @@ -41,8 +41,7 @@ class SmallObjectChild : public SmallObjectParent public: typedef Loki::SingletonHolder< SmallObjectChild, Loki::CreateUsingNew, - Loki::LongevityLifetime::DieAsSmallObjectChild, - LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL > + Loki::LongevityLifetime::DieAsSmallObjectChild> MySmallSingleton; /// Returns reference to the singleton. @@ -86,8 +85,7 @@ class LongLivedSingleton : public LongLivedObject public: typedef Loki::SingletonHolder< LongLivedSingleton, Loki::CreateUsingNew, - Loki::SingletonWithLongevity, - LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL > + Loki::SingletonWithLongevity> MySmallSingleton; /// Returns reference to the singleton. @@ -139,8 +137,7 @@ class FollowerSingleton : public MasterObject public: typedef Loki::SingletonHolder< FollowerSingleton, Loki::CreateUsingNew, - Loki::FollowIntoDeath::AfterMaster::IsDestroyed, - LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL > + Loki::FollowIntoDeath::AfterMaster::IsDestroyed> MySmallSingleton; /// Returns reference to the singleton. @@ -168,13 +165,97 @@ private: #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 // // ---------------------------------------------------------------------------- -#ifdef _MSC_VER +//#define LOKI_MSVC_CHECK_FOR_MEMORY_LEAKS +#ifdef LOKI_MSVC_CHECK_FOR_MEMORY_LEAKS #include #include @@ -193,8 +274,13 @@ void heap_debug() // Set flag to the new value _CrtSetDbgFlag( tmpFlag ); - } + +#else + +void heap_debug() +{} + #endif @@ -206,39 +292,46 @@ void heap_debug() int main() { - -#ifdef _MSC_VER heap_debug(); -#endif 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 << "from SmallObject or SmallValueObject." << endl << endl << "Use one of the following lifetime policies" << endl << "to manage the lifetime dependency:" << endl << endl - << "1. LongevityLifetime Parent/Child" << endl - << " SmallObject has " << endl - << " LongevityLifetime::DieAsSmallObjectParent policy " << endl - << " and the derived Singleton has " << endl + << "- LongevityLifetime Parent/Child: SmallObject has" << endl + << " LongevityLifetime::DieAsSmallObjectParent" << endl + << " policy 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 + << "- Both SmallObject and derived Singleton use" << endl + << " SingletonWithLongevity" << endl + << " policy. This is tested by the LongLivedSingleton class." << endl << endl #if !defined(_MSC_VER) || (_MSC_VER>=1400) - << "3. FollowIntoDeath:" << endl - << " SmallObject has " << endl - << " FollowIntoDeath::With::AsMasterLiftime policy" << endl - << " and the derived Singleton has " << endl - << " FollowIntoDeath::AfterMaster::IsDestroyed policy" << endl - << " This is tested by the FollowerSingleton class." << endl - + << "- FollowIntoDeath: SmallObject has" << endl + << " FollowIntoDeath::With::AsMasterLiftime" << endl + << " policy and the derived Singleton has " << endl + << " FollowIntoDeath::AfterMaster::IsDestroyed" << endl + << " policy. This is tested by the FollowerSingleton class." << endl << endl #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 << "If this program executes without crashing or asserting" << endl << "at exit time, then all policies work." << endl << endl; @@ -251,11 +344,23 @@ int main() FollowerSingleton::Instance().DoThat(); #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) system("pause"); #endif - cout << endl<< endl << "now leaving main" << endl << endl; + cout << endl<< endl << "now leaving main" << endl; + cout << "________________________________" << endl << endl; return 0; } @@ -263,6 +368,9 @@ int main() // ---------------------------------------------------------------------------- // $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 // change lifetime policy DieOrder to a msvc7.1 compilable version. Make this the default lifetime for SmallObject //