[3364] | 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 | |
---|
[7401] | 29 | /** |
---|
| 30 | @defgroup SingletonScope Singletons and Scope |
---|
| 31 | @ingroup Util |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | @file |
---|
| 36 | @ingroup SingletonScope |
---|
| 37 | @brief Definition of the Singleton template that is used as base class for classes that allow only one instance. |
---|
| 38 | |
---|
| 39 | @anchor SingletonExample |
---|
| 40 | |
---|
| 41 | Classes that inherit from orxonox::Singleton follow the singleton pattern and thus |
---|
| 42 | allow only one instance of the class to exist. This istance is stored in a static |
---|
| 43 | variable called @c singletonPtr_s. orxonox::Singleton will access this variable, but |
---|
| 44 | it must be implemented in the deriving class. |
---|
| 45 | |
---|
| 46 | Example: |
---|
| 47 | @code |
---|
| 48 | class TestSingleton : public Singleton<TestSingleton> // inherit from Singleton, pass the own class as template argument |
---|
| 49 | { |
---|
| 50 | friend class Singleton<TestSingleton>; // friend declaration so Singleton can access singletonPtr_s |
---|
| 51 | |
---|
| 52 | public: |
---|
| 53 | TestSingleton(); // public constructor because we may want to manage this singleton |
---|
[10624] | 54 | // with an orxonox::ScopedSingletonWrapper |
---|
[7401] | 55 | virtual ~TestSingleton(); // public destructor |
---|
| 56 | |
---|
| 57 | void testFunction(); // put your functions here |
---|
| 58 | |
---|
| 59 | private: |
---|
| 60 | int testValue_; // put your variables here |
---|
| 61 | |
---|
| 62 | static TestSingleton* singletonPtr_s; // static singleton instance pointer, used by the Singleton template |
---|
| 63 | }; |
---|
| 64 | @endcode |
---|
| 65 | |
---|
| 66 | And don't forget to initialize the static singleton pointer in the source (*.cc) %file: |
---|
| 67 | @code |
---|
[10765] | 68 | TestSingleton* TestSingleton::singletonPtr_s = nullptr; |
---|
[7401] | 69 | @endcode |
---|
| 70 | |
---|
| 71 | If a class inherits from orxonox::Singleton, it also inherits its functions. The most important |
---|
| 72 | function is orxonox::Singleton::getInstance() which returns a reference to the only instance |
---|
| 73 | of the singleton. |
---|
| 74 | |
---|
| 75 | Example: |
---|
| 76 | @code |
---|
| 77 | TestSingleton::TestSingleton() // implement the constructor |
---|
| 78 | { |
---|
| 79 | this->testValue_ = 15; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | void TestSingleton::testFunction() // implement testFunction |
---|
| 83 | { |
---|
[8858] | 84 | orxout() << "My value is " << this->testValue_ << endl; |
---|
[7401] | 85 | } |
---|
| 86 | |
---|
| 87 | TestSingleton::getInstance().testFunction(); // prints "My value is 15" |
---|
| 88 | @endcode |
---|
| 89 | */ |
---|
| 90 | |
---|
[3364] | 91 | #ifndef __Util_Singleton_H__ |
---|
| 92 | #define __Util_Singleton_H__ |
---|
| 93 | |
---|
| 94 | #include "UtilPrereqs.h" |
---|
[7163] | 95 | |
---|
| 96 | #include <cstring> |
---|
[10624] | 97 | #include <typeinfo> |
---|
[3364] | 98 | |
---|
[10624] | 99 | #include "OrxAssert.h" |
---|
| 100 | |
---|
[3364] | 101 | namespace orxonox |
---|
| 102 | { |
---|
| 103 | /** |
---|
| 104 | @brief |
---|
| 105 | Base for singleton classes. |
---|
| 106 | |
---|
| 107 | Usage: |
---|
[7401] | 108 | Inherit publicly from Singleton<MyClass> and provide access to MyClass::singletonPtr_s. |
---|
[3366] | 109 | This can easily be done with a friend declaration. |
---|
[7401] | 110 | |
---|
| 111 | See @ref SingletonExample "this example" for an exemplary implementation. |
---|
[3364] | 112 | */ |
---|
| 113 | template <class T> |
---|
| 114 | class Singleton |
---|
| 115 | { |
---|
| 116 | public: |
---|
| 117 | //! Returns a reference to the singleton instance |
---|
[6751] | 118 | static T& getInstance() |
---|
[3364] | 119 | { |
---|
[10765] | 120 | OrxVerify(T::singletonPtr_s != nullptr, "T=" << typeid(T).name()); |
---|
[3366] | 121 | return *T::singletonPtr_s; |
---|
[3364] | 122 | } |
---|
| 123 | |
---|
[6536] | 124 | //! Tells whether the singleton has been created |
---|
[6751] | 125 | static bool exists() |
---|
[6536] | 126 | { |
---|
[10765] | 127 | return (T::singletonPtr_s != nullptr); |
---|
[6536] | 128 | } |
---|
| 129 | |
---|
[3364] | 130 | protected: |
---|
[3366] | 131 | //! Constructor sets the singleton instance pointer |
---|
[3364] | 132 | Singleton() |
---|
| 133 | { |
---|
[10765] | 134 | OrxVerify(T::singletonPtr_s == nullptr, "T=" << typeid(T).name()); |
---|
[3366] | 135 | T::singletonPtr_s = static_cast<T*>(this); |
---|
[3364] | 136 | } |
---|
[3366] | 137 | |
---|
[7401] | 138 | //! Destructor resets the singleton instance pointer |
---|
[7904] | 139 | virtual ~Singleton() |
---|
[3364] | 140 | { |
---|
[10765] | 141 | OrxVerify(T::singletonPtr_s != nullptr, "T=" << typeid(T).name()); |
---|
| 142 | T::singletonPtr_s = nullptr; |
---|
[3364] | 143 | } |
---|
| 144 | |
---|
| 145 | private: |
---|
| 146 | Singleton(const Singleton& rhs); //!< Don't use (undefined) |
---|
| 147 | }; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | #endif /* __Util_Singleton_H__ */ |
---|