Orxonox  0.0.5 Codename: Arcturus
Singleton.h
Go to the documentation of this file.
1 /*
2  * ORXONOX - the hottest 3D action shooter ever to exist
3  * > www.orxonox.net <
4  *
5  *
6  * License notice:
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * Author:
23  * Reto Grieder
24  * Co-authors:
25  * ...
26  *
27  */
28 
91 #ifndef __Util_Singleton_H__
92 #define __Util_Singleton_H__
93 
94 #include "UtilPrereqs.h"
95 
96 #include <cstring>
97 #include <typeinfo>
98 
99 #include "OrxAssert.h"
100 
101 namespace orxonox
102 {
113  template <class T>
114  class Singleton
115  {
116  public:
118  static T& getInstance()
119  {
120  OrxVerify(T::singletonPtr_s != nullptr, "T=" << typeid(T).name());
121  return *T::singletonPtr_s;
122  }
123 
125  static bool exists()
126  {
127  return (T::singletonPtr_s != nullptr);
128  }
129 
130  protected:
133  {
134  OrxVerify(T::singletonPtr_s == nullptr, "T=" << typeid(T).name());
135  T::singletonPtr_s = static_cast<T*>(this);
136  }
137 
139  virtual ~Singleton()
140  {
141  OrxVerify(T::singletonPtr_s != nullptr, "T=" << typeid(T).name());
142  T::singletonPtr_s = nullptr;
143  }
144 
145  private:
146  // non-copyable:
147  Singleton(const Singleton&) = delete;
148  Singleton& operator=(const Singleton&) = delete;
149  };
150 }
151 
152 #endif /* __Util_Singleton_H__ */
Singleton()
Constructor sets the singleton instance pointer.
Definition: Singleton.h:132
Base for singleton classes.
Definition: Singleton.h:114
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
Shared library macros, enums, constants and forward declarations for the util library ...
Singleton & operator=(const Singleton &)=delete
Definition: InputPrereqs.h:78
static bool exists()
Tells whether the singleton has been created.
Definition: Singleton.h:125
#define OrxVerify(condition, errorMessage)
Works like OrxAssert in debug mode, but also checks the condition in release mode (no abort() trigger...
Definition: OrxAssert.h:78
virtual ~Singleton()
Destructor resets the singleton instance pointer.
Definition: Singleton.h:139
static T & getInstance()
Returns a reference to the singleton instance.
Definition: Singleton.h:118
Declaration of custom assertion facilities