Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Singleton.h
Go to the documentation of this file.
1 //
2 //C++ Template: Singleton<T>
3 //
4 #ifndef __SINGLETON_H__INCLUDED__
5 #define __SINGLETON_H__INCLUDED__
6 
7 namespace InitializationFunctors
8 {
9 template < typename T >
11 {
12 public:
13  T*operator()() const
14  {
15  return new T();
16  }
17 };
18 };
19 
32 template < typename T, typename INIT = InitializationFunctors::DefaultConstructor< T > >
33 class Singleton
34 {
35 protected:
36  static T *_singletonInstance;
37 
38  static void initializeSingleton()
39  {
40  if (_singletonInstance != 0)
41  delete _singletonInstance;
42  //GCC 3.3 errors out if you do '(INIT())()'
43  INIT singletonConstructor;
44  _singletonInstance = singletonConstructor();
45  }
46 
47  static void deinitializeSingleton()
48  {
50  }
51 
53  {
54  if (_singletonInstance == this)
56  }
57 
58 public:
59  static T * getSingleton()
60  {
61  if (_singletonInstance == 0)
63  return _singletonInstance;
64  }
65 };
66 
67 #endif //__SINGLETON_H__INCLUDED__
68