2005-10-17 08:43:42 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// The Loki Library
|
|
|
|
// Copyright (c) 2005 by Curtis Krauskopf
|
|
|
|
// Copyright (c) 2005 by Peter Kuemmel
|
|
|
|
//
|
|
|
|
// Code covered by the MIT License
|
|
|
|
// The authors make no representations about the suitability of this software
|
|
|
|
// for any purpose. It is provided "as is" without express or implied warranty.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2006-10-17 20:36:13 +00:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
2005-10-24 12:13:07 +00:00
|
|
|
// A singleton LogClass object that resurrects itself after
|
2005-10-17 08:43:42 +00:00
|
|
|
// it has been automatically destroyed during program
|
2005-10-24 12:13:07 +00:00
|
|
|
// termination. When a dead-reference to the LogClass
|
2005-10-17 08:43:42 +00:00
|
|
|
// singleton is detected, it is resurrected automatically.
|
|
|
|
//
|
|
|
|
// Expected output:
|
|
|
|
// Example c'tor
|
2005-10-24 12:13:07 +00:00
|
|
|
// LogClass c'tor
|
|
|
|
// LogClass now instantiated.
|
|
|
|
// LogClass d'tor
|
2005-10-17 08:43:42 +00:00
|
|
|
// Example d'tor starting
|
2005-10-24 12:13:07 +00:00
|
|
|
// LogClass c'tor
|
|
|
|
// LogClass: inside Example d'tor
|
2005-10-17 08:43:42 +00:00
|
|
|
// Example d'tor finished
|
|
|
|
//
|
|
|
|
// The last line of the output only appears when this
|
|
|
|
// program is compiled with the ATEXIT_FIXED symbol
|
|
|
|
// defined (see the Loki library and the CUJ article).
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <iostream>
|
2006-01-05 00:23:44 +00:00
|
|
|
#include <loki/Singleton.h> // for Loki::SingletonHolder
|
2005-10-17 08:43:42 +00:00
|
|
|
|
|
|
|
using namespace std; // okay for small programs
|
|
|
|
using namespace Loki; // okay for small programs
|
|
|
|
|
2005-10-24 12:13:07 +00:00
|
|
|
class LogClass
|
2005-10-17 08:43:42 +00:00
|
|
|
{
|
|
|
|
public:
|
2005-10-24 12:13:07 +00:00
|
|
|
LogClass()
|
2005-10-17 08:43:42 +00:00
|
|
|
{
|
2005-10-24 12:13:07 +00:00
|
|
|
echo("LogClass c'tor");
|
2005-10-17 08:43:42 +00:00
|
|
|
}
|
2005-10-24 12:13:07 +00:00
|
|
|
~LogClass()
|
2005-10-17 08:43:42 +00:00
|
|
|
{
|
2005-10-24 12:13:07 +00:00
|
|
|
echo("LogClass d'tor");
|
2005-10-17 08:43:42 +00:00
|
|
|
}
|
|
|
|
void echo(const char *s)
|
|
|
|
{
|
|
|
|
cout << s << endl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2005-10-24 12:13:07 +00:00
|
|
|
typedef SingletonHolder<LogClass, CreateUsingNew, PhoenixSingleton> LogBook;
|
2005-10-17 08:43:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
// A generic example class that stores and echoes a const char.
|
|
|
|
//
|
|
|
|
class Example
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Example()
|
|
|
|
{
|
|
|
|
echo("Example c'tor");
|
|
|
|
}
|
|
|
|
~Example()
|
|
|
|
{
|
|
|
|
echo("Example d'tor starting");
|
2005-10-24 12:13:07 +00:00
|
|
|
LogBook::Instance().echo("LogClass: inside Example d'tor");
|
2005-10-17 08:43:42 +00:00
|
|
|
echo("Example d'tor finished");
|
|
|
|
}
|
|
|
|
void echo(const char *s)
|
|
|
|
{
|
|
|
|
cout << s << endl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2011-10-03 05:03:10 +00:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
|
|
|
|
2005-10-17 08:43:42 +00:00
|
|
|
Example *example = new Example();
|
|
|
|
SetLongevity<Example, void (*)(Example*)>(example, 1, &Loki::Private::Deleter<Example>::Delete);
|
2005-10-24 12:13:07 +00:00
|
|
|
LogBook::Instance().echo("LogClass now instantiated.");
|
2005-10-17 08:43:42 +00:00
|
|
|
|
2006-01-04 23:54:30 +00:00
|
|
|
#if defined(__BORLANDC__) || defined(_MSC_VER)
|
|
|
|
system("PAUSE");
|
2005-10-17 08:43:42 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|