1: #include "singl_app.h"
2:
3: /**
4: * Arno Wilhelm 2002-02-20
5: *
6: * This is a little testprogamm that shows the cosntruction and usage
7: * of a template for the singleton pattern.
8: * Attention:
9: * There are two ways of using the constructin/destructing the class:
10: *
11: * 1. Create an instance of class CSingleton<MyClass>:
12: * Works similar to an autopointer: When instance is deleted
13: * also MyClass will be deleted.
14: *
15: * e.g.:
16: * typedef CSingleton<MyClass> MySingleton;
17: * MySingleton mySingle;
18: * MyClass * Pointer2MyClass_2 = mySingle.getInstance();
19: * // do something with the pointer
20: *
21: * 2. Never crate an instance of class CSingleton<MyClass>,
22: * but always only use the static getInstance() and destroyInstance() methods.
23: * Warning: You have to destroy the class manually with destroyInstance()!
24: *
25: * e.g.:
26: * typedef CSingleton<MyClass> MySingleton;
27: * MyClass * Pointer2MyClass_2 = MySingleton::getInstance();
28: * // do something with the pointer and delete it afterwards
29: * MySingleton::destroyInstance();
30: *
31: * You also can mix this two ways of constructing/destructing
32: * the singleton class up if you know what you are doing.
33: */
35:
36:
37: typedef CSingleton<MyClass> MySingleton;
38:
39: int main(int argc, char** argv)
40: {
41: argc = 0;
42: argv = 0;
43:
44: MySingleton mySingle;
45:
46: MyClass * Pointer2MyClass_1 = mySingle.getInstance();
47: Pointer2MyClass_1->print();
48:
49: MyClass * Pointer2MyClass_2 = mySingle.getInstance();
50: Pointer2MyClass_2->set_outstring( "Second pointer was here!" );
51: Pointer2MyClass_2->print();
52: Pointer2MyClass_1->print();
53:
54: // Another way to get a pointer to my class.
55: // Only use it when you already have constructed a MySingleton class
56: // or destroy the MySingleton class manually with MySingleton::destroyInstance();
57: MyClass * Pointer2MyClass_3 = MySingleton::getInstance();
58: Pointer2MyClass_2->set_outstring( "Third pointer was here!" );
59: Pointer2MyClass_3->print();
60: Pointer2MyClass_1->print();
61:
62: return 0;
63: }
64:
65: