2005-10-14 18:48:10 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// The Loki Library
|
|
|
|
|
// Copyright (c) 2005 Richard Sposato
|
2005-11-02 13:58:18 +00:00
|
|
|
|
// Copyright (c) 2005 Peter K<>mmel
|
2005-10-14 18:48:10 +00:00
|
|
|
|
// Permission to use, copy, modify, distribute and sell this software for any
|
|
|
|
|
// purpose is hereby granted without fee, provided that the above copyright
|
|
|
|
|
// notice appear in all copies and that both that copyright notice and this
|
|
|
|
|
// permission notice appear in supporting documentation.
|
|
|
|
|
// The authors make no representations about the
|
|
|
|
|
// suitability of this software for any purpose. It is provided "as is"
|
|
|
|
|
// without express or implied warranty.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
// $Header$
|
|
|
|
|
|
|
|
|
|
|
2005-10-29 10:21:46 +00:00
|
|
|
|
#include "../../include/loki/SmallObj.h"
|
|
|
|
|
#include "../../include/loki/Singleton.h"
|
2005-10-14 18:48:10 +00:00
|
|
|
|
#include <iostream>
|
|
|
|
|
|
2005-11-02 13:58:18 +00:00
|
|
|
|
// define DO_EXTRA_LOKI_TESTS in src/SmallObj.cpp to get
|
|
|
|
|
// a message when a SmallObject is created/deleted.
|
2005-10-14 18:48:10 +00:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
2005-11-02 13:58:18 +00:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// FollowIntoDeath policy
|
|
|
|
|
//
|
2005-10-14 18:48:10 +00:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
typedef Loki::SmallValueObject< LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL,
|
|
|
|
|
LOKI_DEFAULT_CHUNK_SIZE, LOKI_MAX_SMALL_OBJECT_SIZE,
|
2005-11-02 13:58:18 +00:00
|
|
|
|
LOKI_DEFAULT_OBJECT_ALIGNMENT,
|
|
|
|
|
Loki::FollowIntoDeath::With<Loki::DefaultLifetime>::AsMasterLifetime // this is default
|
|
|
|
|
>
|
|
|
|
|
MasterObject;
|
2005-10-14 18:48:10 +00:00
|
|
|
|
|
2005-11-02 13:58:18 +00:00
|
|
|
|
class FollowerSingleton : public MasterObject
|
2005-10-14 18:48:10 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
2005-11-02 13:58:18 +00:00
|
|
|
|
typedef Loki::SingletonHolder< FollowerSingleton, Loki::CreateUsingNew,
|
|
|
|
|
Loki::FollowIntoDeath::AfterMaster<FollowerSingleton::ObjAllocatorSingleton>::IsDestroyed,
|
|
|
|
|
LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL >
|
2005-10-14 18:48:10 +00:00
|
|
|
|
MySmallSingleton;
|
|
|
|
|
|
|
|
|
|
/// Returns reference to the singleton.
|
2005-11-02 13:58:18 +00:00
|
|
|
|
inline static FollowerSingleton & Instance( void )
|
2005-10-14 18:48:10 +00:00
|
|
|
|
{
|
|
|
|
|
return MySmallSingleton::Instance();
|
|
|
|
|
}
|
|
|
|
|
|
2005-11-02 13:58:18 +00:00
|
|
|
|
FollowerSingleton( void )
|
2005-10-14 18:48:10 +00:00
|
|
|
|
{
|
2005-11-02 13:58:18 +00:00
|
|
|
|
cout << "FollowerSingleton created" << endl;
|
2005-10-14 18:48:10 +00:00
|
|
|
|
}
|
2005-11-02 13:58:18 +00:00
|
|
|
|
~FollowerSingleton( void )
|
2005-10-14 18:48:10 +00:00
|
|
|
|
{
|
2005-11-02 13:58:18 +00:00
|
|
|
|
cout << "~FollowerSingleton" << endl;
|
2005-10-14 18:48:10 +00:00
|
|
|
|
}
|
|
|
|
|
void DoThat( void )
|
|
|
|
|
{
|
2005-11-02 13:58:18 +00:00
|
|
|
|
cout << "FollowerSingleton::DoThat" << endl << endl;
|
2005-10-14 18:48:10 +00:00
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
char m_stuff[ 16 ];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2005-11-02 13:58:18 +00:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// DieOrder policy
|
|
|
|
|
//
|
2005-10-14 18:48:10 +00:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
2005-11-02 13:58:18 +00:00
|
|
|
|
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
|
2005-10-14 18:48:10 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
2005-11-02 13:58:18 +00:00
|
|
|
|
typedef Loki::SingletonHolder< DieFirstSingleton, Loki::CreateUsingNew,
|
|
|
|
|
Loki::DieOrder::First,
|
|
|
|
|
LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL >
|
2005-10-14 18:48:10 +00:00
|
|
|
|
MySmallSingleton;
|
|
|
|
|
|
|
|
|
|
/// Returns reference to the singleton.
|
2005-11-02 13:58:18 +00:00
|
|
|
|
inline static DieFirstSingleton & Instance( void )
|
2005-10-14 18:48:10 +00:00
|
|
|
|
{
|
|
|
|
|
return MySmallSingleton::Instance();
|
|
|
|
|
}
|
|
|
|
|
|
2005-11-02 13:58:18 +00:00
|
|
|
|
DieFirstSingleton( void )
|
2005-10-14 18:48:10 +00:00
|
|
|
|
{
|
2005-11-02 13:58:18 +00:00
|
|
|
|
cout << "DieFirstSingleton created" << endl;
|
2005-10-14 18:48:10 +00:00
|
|
|
|
}
|
2005-11-02 13:58:18 +00:00
|
|
|
|
~DieFirstSingleton( void )
|
2005-10-14 18:48:10 +00:00
|
|
|
|
{
|
2005-11-02 13:58:18 +00:00
|
|
|
|
cout << "~DieFirstSingleton" << endl;
|
2005-10-14 18:48:10 +00:00
|
|
|
|
}
|
|
|
|
|
void DoThat( void )
|
|
|
|
|
{
|
2005-11-02 13:58:18 +00:00
|
|
|
|
cout << "DieFirstSingleton::DoThat" << endl << endl;
|
2005-10-14 18:48:10 +00:00
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
char m_stuff[ 16 ];
|
|
|
|
|
};
|
|
|
|
|
|
2005-11-02 13:58:18 +00:00
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// SingletonWithLongevity policy
|
|
|
|
|
//
|
2005-10-14 18:48:10 +00:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
2005-11-02 13:58:18 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
class LongLivedSingleton : public LongLivedObject
|
2005-10-14 18:48:10 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
2005-11-02 13:58:18 +00:00
|
|
|
|
typedef Loki::SingletonHolder< LongLivedSingleton, Loki::CreateUsingNew,
|
|
|
|
|
Loki::SingletonWithLongevity,
|
|
|
|
|
LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL >
|
2005-10-14 18:48:10 +00:00
|
|
|
|
MySmallSingleton;
|
|
|
|
|
|
|
|
|
|
/// Returns reference to the singleton.
|
2005-11-02 13:58:18 +00:00
|
|
|
|
inline static LongLivedSingleton & Instance( void )
|
2005-10-14 18:48:10 +00:00
|
|
|
|
{
|
|
|
|
|
return MySmallSingleton::Instance();
|
|
|
|
|
}
|
|
|
|
|
|
2005-11-02 13:58:18 +00:00
|
|
|
|
LongLivedSingleton( void )
|
2005-10-14 18:48:10 +00:00
|
|
|
|
{
|
2005-11-02 13:58:18 +00:00
|
|
|
|
cout << "LongLivedSingleton created" << endl;
|
2005-10-14 18:48:10 +00:00
|
|
|
|
}
|
2005-11-02 13:58:18 +00:00
|
|
|
|
~LongLivedSingleton( void )
|
2005-10-14 18:48:10 +00:00
|
|
|
|
{
|
2005-11-02 13:58:18 +00:00
|
|
|
|
cout << "~LongLivedSingleton" << endl;
|
2005-10-14 18:48:10 +00:00
|
|
|
|
}
|
|
|
|
|
void DoThat( void )
|
|
|
|
|
{
|
2005-11-02 13:58:18 +00:00
|
|
|
|
cout << "LongLivedSingleton::DoThat" << endl << endl;
|
2005-10-14 18:48:10 +00:00
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
char m_stuff[ 16 ];
|
|
|
|
|
};
|
|
|
|
|
|
2005-11-02 13:58:18 +00:00
|
|
|
|
inline unsigned int GetLongevity( LongLivedSingleton * )
|
2005-10-14 18:48:10 +00:00
|
|
|
|
{
|
|
|
|
|
/// @note Must return a longevity level lower than the one in SmallObj.h.
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2005-11-02 13:58:18 +00:00
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// detect memory leaks on MSVC Ide
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
|
|
|
|
|
#include <crtdbg.h>
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
|
|
void heap_debug()
|
|
|
|
|
{
|
|
|
|
|
int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
|
|
|
|
|
|
|
|
|
|
// Turn on leak-checking bit
|
|
|
|
|
tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
|
|
|
|
|
|
|
|
|
|
//tmpFlag |= _CRTDBG_CHECK_MasterLWMasterYS_DF;
|
|
|
|
|
|
|
|
|
|
// Turn off CRT block checking bit
|
|
|
|
|
tmpFlag &= ~_CRTDBG_CHECK_CRT_DF;
|
|
|
|
|
|
|
|
|
|
// Set flag to the new value
|
|
|
|
|
_CrtSetDbgFlag( tmpFlag );
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// main
|
|
|
|
|
//
|
2005-10-14 18:48:10 +00:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
2005-11-02 13:58:18 +00:00
|
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
heap_debug();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
cout << endl
|
|
|
|
|
<< "This program tests the three recommended 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. FollowIntoDeath:" << endl
|
|
|
|
|
<< " SmallObject has " << endl
|
|
|
|
|
<< " FollowIntoDeath::With<LIFETIME>::AsMasterLiftime policy" << endl
|
|
|
|
|
<< " and the derived Singleton has " << endl
|
|
|
|
|
<< " FollowIntoDeath::After<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 DieOrderSingleton class." << endl
|
|
|
|
|
<< endl
|
|
|
|
|
<< "3. Both SmallObject and derived Singleton use SingletonWithLongevity policy." << endl
|
|
|
|
|
<< " This is tested by the LongLivedSingleton class." << endl
|
|
|
|
|
<< endl << endl
|
|
|
|
|
<< "If this program executes without crashing or asserting" << endl
|
|
|
|
|
<< " at exit time, then all 3 policies work." << endl << endl;
|
|
|
|
|
|
|
|
|
|
FollowerSingleton::Instance().DoThat();
|
|
|
|
|
DieFirstSingleton::Instance().DoThat();
|
2005-10-14 18:48:10 +00:00
|
|
|
|
LongLivedSingleton::Instance().DoThat();
|
2005-11-02 13:58:18 +00:00
|
|
|
|
|
2005-10-30 14:03:23 +00:00
|
|
|
|
system("PAUSE");
|
2005-11-02 13:58:18 +00:00
|
|
|
|
|
|
|
|
|
cout << endl<< endl << "now leaving main" << endl << endl;
|
|
|
|
|
|
2005-10-30 14:03:23 +00:00
|
|
|
|
return 0;
|
2005-10-14 18:48:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// $Log$
|
2005-11-02 13:58:18 +00:00
|
|
|
|
// Revision 1.4 2005/11/02 13:58:18 syntheticpp
|
|
|
|
|
// use new singleton lifetime policies
|
|
|
|
|
//
|
2005-10-30 14:03:23 +00:00
|
|
|
|
// Revision 1.3 2005/10/30 14:03:23 syntheticpp
|
|
|
|
|
// replace tabs space
|
|
|
|
|
//
|
2005-10-29 10:21:46 +00:00
|
|
|
|
// Revision 1.2 2005/10/29 10:21:46 syntheticpp
|
|
|
|
|
// find loki include files without a correct sreach pathand some small fixes
|
|
|
|
|
//
|
2005-10-14 18:48:10 +00:00
|
|
|
|
// Revision 1.1 2005/10/14 18:48:10 rich_sposato
|
|
|
|
|
// Adding SmallSingleton test project to CVS.
|
|
|
|
|
//
|