File: DeletableSingleton.cpp

package info (click to toggle)
libloki 0.1.5-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,888 kB
  • ctags: 5,535
  • sloc: cpp: 22,174; ansic: 1,955; makefile: 359; php: 316; perl: 108
file content (80 lines) | stat: -rwxr-xr-x 2,069 bytes parent folder | download
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
////////////////////////////////////////////////////////////////////////////////
// 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.
////////////////////////////////////////////////////////////////////////////////

// Show an example of a Loki policy that uses DeletableSingleton.
//
// Expected output:
//
//    LogClass::LogClass()
//    LogClass singleton instantiated
//    Going to manually delete LogBook.
//    LogClass::~LogClass()
//    LogClass::LogClass()
//    LogClass reinstantiated.
//    Going to terminate program now.
//    LogClass::~LogClass()
//

#include <iostream>
#include <loki/Singleton.h>   // for Loki::SingletonHolder

using namespace std;   // okay for small programs
using namespace Loki;  // okay for small programs

// A singleton LogClass object derived from the Example class.
// Its longevity is set by the user on the command line.
//
class LogClass
{
public:
    LogClass()
    {
        print("LogClass::LogClass()");
    };
    ~LogClass()
    {
        print("LogClass::~LogClass()");
    }
    void print(const char *s)
    {
        cout << s << endl;
    }
};

typedef SingletonHolder<LogClass, CreateUsingNew, DeletableSingleton> LogBook;

class Example
{
public:
    void method()
    {
        cout << "test\n";
    }
};


int main()
{
    // Instantiate both singletons by calling them...
    LogBook::Instance().print("LogClass singleton instantiated");
    LogBook::Instance().print("Going to manually delete LogBook.");

    DeletableSingleton<LogClass>::GracefulDelete();

    LogBook::Instance().print("LogClass reinstantiated.");
    LogBook::Instance().print("Going to terminate program now.");

#if defined(__BORLANDC__) || defined(_MSC_VER)
    system("PAUSE");
#endif

    return 0;
}