Debian Codegraph @

CodeLite/singleton.h

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2008 by Eran Ifrah
5 // file name : singleton.h
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25 #ifndef SINGLETON_H
26 #define SINGLETON_H
27
28 /**
29 * A template class that implements the Singleton pattern.
30 *
31 * \date 08-23-2006
32 * \author eran
33 */
34 template <class T> class Singleton
35 {
36 static T* ms_instance;
37 public:
38 /**
39 * Static method to access the only pointer of this instance.
40 * \return a pointer to the only instance of this
41 */
42 static T* Get();
43
44 /**
45 * Release resources.
46 */
47 static void Free();
48
49 protected:
50 /**
51 * Default constructor.
52 */
53 Singleton();
54
55 /**
56 * Destructor.
57 */
58 virtual ~Singleton();
59 };
60 template <class T> T* Singleton<T>::ms_instance = 0;
61
62 template <class T> Singleton<T>::Singleton()
63 {
64 }
65
66 template <class T> Singleton<T>::~Singleton()
67 {
68 }
69
70 template <class T> T* Singleton<T>::Get()
71 {
72 if(!ms_instance)
73 ms_instance = new T();
74 return ms_instance;
75 }
76
77 template <class T> void Singleton<T>::Free()
78 {
79 if( ms_instance )
80 {
81 delete ms_instance;
82 ms_instance = 0;
83 }
84 }
85
86 #endif // SINGLETON_H