Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Singleton.hh
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8 
9  BIAS is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation; either version 2.1 of the License, or
12  (at your option) any later version.
13 
14  BIAS is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with BIAS; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
22 #ifndef __Singleton_hh__
23 #define __Singleton_hh__
24 
25 #include <bias_config.h>
26 
27 #include <Base/Debug/Error.hh>
28 #include <Base/Debug/Exception.hh>
29 
30 #include <cstdlib>
31 #include <typeinfo>
32 
33 #ifdef BIAS_HAVE_PTHREADS
34 # include <pthread.h>
35 #endif
36 
37 namespace BIAS {
38 
39 #if defined BIAS_DEBUG && ! defined WIN32
40  /** This variable is defined in Singleton.cpp and steers the debug output of
41  all Singleton classes */
42  extern bool SingletonDebugOutput_;
43 #endif
44 
45 
46  /** @class Singleton
47  @brief Simple singleton implementation for multithreaded applications
48 
49  To use this template to create a singleton of class MyClass do the following:
50 
51  /code
52 
53  #include <Base/Common/Singleton.hh>
54 
55  class MyClass :public Singleton<MyClass> {
56  public:
57  // some functionality of class MyClass
58  void foo();
59 
60  ....
61 
62  // constructor must be protected to let parent class access, but not
63  //ordinary users
64  protected:
65  // make sure that class MyClass cannot be used as non-singleton by
66  // declaring constructors and copy operator private
67  MyClass();
68  MyClass(const &MyClass);
69  MyClass& operator=(const &MyClass);
70  };
71 
72  /// exemplary usage
73  MyClassSingleton::GetInstance()->foo();
74 
75  /endcode
76 
77  The code wraps a previous defined class MyClass as a singleton. The Singleton
78  class exposes a single function GetInstance() returning a pointer to MyClass.
79  This function creates the single instance of MyClass if it is not already
80  present and registers the DeleteInstance_() function with the atexit()
81  function from the stdlib resulting in automatic destruction of the
82  singleton when the application terminates.
83 
84  @author woelk 10/2007 */
85  template <class T>
86  class Singleton
87  {
88  public:
89  static T* GetInstance();
90 
91  protected:
92 
93  Singleton();
94  Singleton(const Singleton<T>& t);
95  ~Singleton();
96  Singleton<T>& operator=(const Singleton& t);
97 
98  static T* Instance_;
99 
100 #ifdef BIAS_HAVE_PTHREADS
101  static pthread_mutex_t *Mutex_();
102 #endif
103 
104  static void DeleteInstance_();
105  };
106 
107 
108  /////////////////////////////////////////////////////////////////////////
109  /// static members
110  /////////////////////////////////////////////////////////////////////////
111 
112  template <class T>
113  T *Singleton<T>::Instance_ = NULL;
114 
115  /////////////////////////////////////////////////////////////////////////
116  // implementation
117  /////////////////////////////////////////////////////////////////////////
118 
119  template <class T>
121  {}
122 
123 
124  template <class T>
126  { BIASABORT; }
127 
128 
129  template <class T>
131  {}
132 
133 
134  template <class T>
136  { BIASABORT; return *this; }
137 
138 
139 #ifdef BIAS_HAVE_PTHREADS
140  // handle the required static mutex in function to avoid the
141  // "initialization desaster" for static variables
142  template <class T>
143  pthread_mutex_t *Singleton<T>::Mutex_()
144  {
145  static pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;
146  return &my_mutex;
147  }
148 #endif //BIAS_HAVE_PTHREAD
149 
150 
151  template <class T>
153  {
154 #ifdef BIAS_HAVE_PTHREADS
155  pthread_mutex_lock(Singleton<T>::Mutex_());
156 #endif
157  if (Singleton<T>::Instance_ == NULL){
158  Singleton<T>::Instance_ = new T;
159 #if defined BIAS_DEBUG && ! defined WIN32
161  std::cout<<"created Singleton<"<<typeid(T).name() <<">\n";
162  }
163 #endif
164  // register a function handler which is called when the application exits
165 #ifndef WIN32
166  /*if (std::atexit( &Singleton<T>::DeleteInstance_ )!=0){
167  BIASERR("cannot set exit handler for singleton: "
168  <<typeid(T).name()<<std::endl);
169  BEXCEPTION("cannot set exit handler for singleton: "
170  <<typeid(T).name());
171  } else {
172 #ifdef BIAS_DEBUG
173  if (SingletonDebugOutput_){
174  std::cout<<"registered exit handler for "<<typeid(T).name()<<std::endl;
175  }
176 #endif
177  }*/
178 #endif //WIN32
179  }
180 #ifdef BIAS_HAVE_PTHREADS
181  pthread_mutex_unlock(Singleton<T>::Mutex_());
182 #endif
184  }
185 
186 
187  template <class T>
189  {
190 #ifdef BIAS_HAVE_PTHREADS
191  pthread_mutex_lock(Singleton<T>::Mutex_());
192 #endif
193  if (Singleton<T>::Instance_ != NULL){
194 #if defined BIAS_DEBUG && ! defined WIN32
196  std::cout<<"deleting Singleton<"<<typeid(T).name() <<"> ... "
197  <<std::flush;
198  }
199 #endif
202 #if defined BIAS_DEBUG && ! defined WIN32
204  std::cout<<"OK\n";
205  }
206 #endif
207  }
208 #ifdef BIAS_HAVE_PTHREADS
209  pthread_mutex_unlock(Singleton<T>::Mutex_());
210 #endif
211  }
212 
213 
214 }
215 
216 
217 #endif // __Singleton_hh__
static pthread_mutex_t * Mutex_()
Definition: Singleton.hh:143
static void DeleteInstance_()
Definition: Singleton.hh:188
static T * GetInstance()
Definition: Singleton.hh:152
Simple singleton implementation for multithreaded applications.
Definition: Singleton.hh:86
Singleton< T > & operator=(const Singleton &t)
Definition: Singleton.hh:135
static T * Instance_
static members
Definition: Singleton.hh:98
bool SingletonDebugOutput_
This variable is defined in Singleton.cpp and steers the debug output of all Singleton classes...
Definition: Singleton.cpp:27