ANNA Suite  2020b
Multipurpose development suite for Telco applications
Singleton.hpp
Go to the documentation of this file.
1 // ANNA - Anna is Not Nothingness Anymore //
2 // //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
4 // //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7 
8 
9 #ifndef anna_core_Singleton_hpp
10 #define anna_core_Singleton_hpp
11 
12 #include <typeinfo>
13 
14 #include <anna/core/mt/Mutex.hpp>
15 #include <anna/core/mt/Guard.hpp>
16 
17 namespace anna {
18 
76 template <class T> class Singleton {
77 public:
81  static T& instantiate() { return *alloc(true); }
82 
88  static void release() { alloc(false); }
89 
90 private:
91  static T* alloc(const bool allocate) {
92  static Mutex mutex;
93  static T* result(NULL);
94 
95  if(allocate == true) {
96  if(result == NULL) {
97  Guard guard(mutex, "Singleton<T>::allocate");
98 
99  if(result == NULL)
100  result = new T;
101  }
102  } else {
103  if(result != NULL) {
104  Guard guard(mutex, "Singleton<T>::deallocate");
105 
106  if(result != NULL) {
107  delete result;
108  result = NULL;
109  }
110  }
111  }
112 
113  return result;
114  }
115 };
116 
117 } //namespace anna
118 
119 #endif
120 
Definition: Singleton.hpp:76
Definition: app.hpp:12
Definition: Guard.hpp:35
static void release()
Definition: Singleton.hpp:88
Definition: Mutex.hpp:41
static T & instantiate()
Definition: Singleton.hpp:81