File: DeletableSingleton.cpp

package info (click to toggle)
libloki 0.1.7-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,608 kB
  • sloc: cpp: 30,475; ansic: 1,974; makefile: 365; php: 316; perl: 108
file content (83 lines) | stat: -rw-r--r-- 2,055 bytes parent folder | download | duplicates (5)
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
////////////////////////////////////////////////////////////////////////////////
// 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.
////////////////////////////////////////////////////////////////////////////////

// $Id: DeletableSingleton.cpp 823 2007-05-08 10:48:40Z lfittl $


// 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;
}