xoreos  0.0.5
singleton.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 /* Based on ScummVM (<http://scummvm.org>) code, which is released
26  * under the terms of version 2 or later of the GNU General Public
27  * License.
28  *
29  * The original copyright note in ScummVM reads as follows:
30  *
31  * ScummVM is the legal property of its developers, whose names
32  * are too numerous to list here. Please refer to the COPYRIGHT
33  * file distributed with this source distribution.
34  *
35  * This program is free software; you can redistribute it and/or
36  * modify it under the terms of the GNU General Public License
37  * as published by the Free Software Foundation; either version 2
38  * of the License, or (at your option) any later version.
39  *
40  * This program is distributed in the hope that it will be useful,
41  * but WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43  * GNU General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License
46  * along with this program; if not, write to the Free Software
47  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
48  */
49 
50 #ifndef COMMON_SINGLETON_H
51 #define COMMON_SINGLETON_H
52 
53 #include <boost/noncopyable.hpp>
54 
55 namespace Common {
56 
60 template<class T>
61 class Singleton : boost::noncopyable {
62 private:
63  Singleton<T>(const Singleton<T> &);
65 
66  static T *_singleton;
67 
75  //template <class T>
76 #if defined (_WIN32_WCE) || defined (_MSC_VER) || defined (__WINS__)
77 //FIXME evc4 and msvc7 doesn't like it as private member
78 public:
79 #endif
80  static T *makeInstance() {
81  return new T();
82  }
83 
84  static void destroyInstance() {
85  delete _singleton;
86  _singleton = 0;
87  }
88 
89 
90 public:
91  static T& instance() {
92  // TODO: We aren't thread safe. For now we ignore it since the
93  // only thing using this singleton template is the config manager,
94  // and that is first instantiated long before any threads.
95  // TODO: We don't leak, but the destruction order is nevertheless
96  // semi-random. If we use multiple singletons, the destruction
97  // order might become an issue. There are various approaches
98  // to solve that problem, but for now this is sufficient
99  if (!_singleton)
100  _singleton = T::makeInstance();
101  return *_singleton;
102  }
103 
104  static void destroy() {
105  T::destroyInstance();
106  }
107 protected:
109  virtual ~Singleton<T>() { }
110 
111  typedef T SingletonBaseType;
112 };
113 
122 #define DECLARE_SINGLETON(T) \
123  namespace Common { \
124  template<> T *Singleton<T>::_singleton = 0; \
125  } // End of namespace Common
126 
127 } // End of namespace Common
128 
129 #endif // COMMON_SINGLETON_H
Definition: 2dafile.h:39
static T * _singleton
Definition: singleton.h:66
static T * makeInstance()
The default object factory used by the template class Singleton.
Definition: singleton.h:80
Generic template base class for implementing the singleton design pattern.
Definition: singleton.h:61
static void destroyInstance()
Definition: singleton.h:84
Aurora::TwoDARegistry * _singleton
Definition: 2dareg.cpp:35
static void destroy()
Definition: singleton.h:104
static T & instance()
Definition: singleton.h:91
Singleton< T > & operator=(const Singleton< T > &)