1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
////////////////////////////////////////////////////////////////////////////////
// The Loki Library
// Copyright (c) 2005 Richard Sposato
// Copyright (c) 2005 Peter Kmmel
// 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.
////////////////////////////////////////////////////////////////////////////////
// $Id: SmallObjSingleton.cpp 761 2006-10-17 20:48:18Z syntheticpp $
#include <loki/SmallObj.h>
#include <loki/Singleton.h>
#include <iostream>
// define DO_EXTRA_LOKI_TESTS in src/SmallObj.cpp to get
// a message when a SmallObject is created/deleted.
using namespace std;
// ----------------------------------------------------------------------------
//
// LongevityLifetime Parent/Child policies
//
// ----------------------------------------------------------------------------
typedef Loki::SmallValueObject< LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL,
512, 32, 4, Loki::LongevityLifetime::DieAsSmallObjectParent
>
SmallObjectParent;
class SmallObjectChild : public SmallObjectParent
{
public:
typedef Loki::SingletonHolder< SmallObjectChild, Loki::CreateUsingNew,
Loki::LongevityLifetime::DieAsSmallObjectChild>
MySmallSingleton;
/// Returns reference to the singleton.
inline static SmallObjectChild & Instance( void )
{
return MySmallSingleton::Instance();
}
SmallObjectChild( void )
{
cout << "SmallObjectChild created" << endl;
}
~SmallObjectChild( void )
{
cout << "~SmallObjectChild" << endl;
}
void DoThat( void )
{
cout << "SmallObjectChild::DoThat" << endl << endl;
}
private:
char m_stuff[ 16 ];
};
// ----------------------------------------------------------------------------
//
// SingletonWithLongevity policy
//
// ----------------------------------------------------------------------------
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
{
public:
typedef Loki::SingletonHolder< LongLivedSingleton, Loki::CreateUsingNew,
Loki::SingletonWithLongevity>
MySmallSingleton;
/// Returns reference to the singleton.
inline static LongLivedSingleton & Instance( void )
{
return MySmallSingleton::Instance();
}
LongLivedSingleton( void )
{
cout << "LongLivedSingleton created" << endl;
}
~LongLivedSingleton( void )
{
cout << "~LongLivedSingleton" << endl;
}
void DoThat( void )
{
cout << "LongLivedSingleton::DoThat" << endl << 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;
}
#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>
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
// ----------------------------------------------------------------------------
//
// 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
//
// ----------------------------------------------------------------------------
//#define LOKI_MSVC_CHECK_FOR_MEMORY_LEAKS
#ifdef LOKI_MSVC_CHECK_FOR_MEMORY_LEAKS
#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 );
}
#else
void heap_debug()
{}
#endif
// ----------------------------------------------------------------------------
//
// main
//
// ----------------------------------------------------------------------------
int main()
{
heap_debug();
cout << 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
<< "- 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
<< "- 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)
<< "- FollowIntoDeath: SmallObject has" << endl
<< " FollowIntoDeath::With<LIFETIME>::AsMasterLiftime" << endl
<< " policy and the derived Singleton has " << endl
<< " FollowIntoDeath::AfterMaster<MASTERSINGLETON>::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;
SmallObjectChild::Instance().DoThat();
LongLivedSingleton::Instance().DoThat();
#if !defined(_MSC_VER) || (_MSC_VER>=1400)
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(_MSC_VER)
system("PAUSE");
#endif
cout << endl<< endl << "now leaving main" << endl;
cout << "________________________________" << endl << endl;
return 0;
}
// ----------------------------------------------------------------------------
|