[5867] | 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 | @file |
---|
| 31 | @ingroup SingletonScope |
---|
[10459] | 32 | @brief Definition of orxonox::ScopedSingletonWrapper and orxonox::ClassScopedSingletonWrapper. |
---|
[7401] | 33 | |
---|
[10458] | 34 | ScopedSingletonWrapper is used to create and destroy Singletons that belong to |
---|
[7401] | 35 | a given Scope. For each one of these singletons, the macro ManageScopedSingleton() |
---|
[10458] | 36 | has to be called to register the wrapper with orxonox::ScopeManager. |
---|
[7401] | 37 | |
---|
| 38 | See @ref SingletonExample "this code" for an example. |
---|
| 39 | |
---|
| 40 | @see orxonox::Singleton |
---|
| 41 | @see orxonox::Scope |
---|
| 42 | */ |
---|
| 43 | |
---|
[10458] | 44 | #ifndef __ScopedSingletonWrapper_H__ |
---|
| 45 | #define __ScopedSingletonWrapper_H__ |
---|
[5867] | 46 | |
---|
[10407] | 47 | #include "core/CorePrereqs.h" |
---|
[5867] | 48 | |
---|
| 49 | #include <cassert> |
---|
[5877] | 50 | #include "util/Exception.h" |
---|
[5867] | 51 | #include "util/Singleton.h" |
---|
[10407] | 52 | #include "Scope.h" |
---|
[5867] | 53 | |
---|
| 54 | namespace orxonox |
---|
| 55 | { |
---|
[7401] | 56 | /** |
---|
[10458] | 57 | @brief Base class of ClassScopedSingletonWrapper. |
---|
[7401] | 58 | */ |
---|
[10460] | 59 | class _CoreExport ScopedSingletonWrapper : public ScopeListener |
---|
[5867] | 60 | { |
---|
| 61 | public: |
---|
[7401] | 62 | /// Constructor: Initializes all the values |
---|
[10514] | 63 | ScopedSingletonWrapper(const std::string& className) |
---|
| 64 | : className_(className) |
---|
[5867] | 65 | { } |
---|
[11071] | 66 | virtual ~ScopedSingletonWrapper() = default; |
---|
[7401] | 67 | |
---|
[5867] | 68 | protected: |
---|
[7401] | 69 | const std::string className_; ///< The name of the scoped singleton class that is managed by this object |
---|
[5867] | 70 | }; |
---|
| 71 | |
---|
[7401] | 72 | /** |
---|
[10458] | 73 | @anchor ClassScopedSingletonWrapper |
---|
[7401] | 74 | |
---|
[10514] | 75 | @brief Manages a scoped singleton |
---|
[7401] | 76 | @param T The managed singleton class |
---|
| 77 | @param allowedToFail If true, a specialization of this template is used, that uses try-catch blocks to handle possible failures. |
---|
| 78 | |
---|
[10514] | 79 | This class inherits from ScopeListener and is registered for a scope in ScopeManager and thus its functions |
---|
[7401] | 80 | activated() and deactivated() are called whenever the Scope changes its state. |
---|
| 81 | |
---|
| 82 | If the Scope is activated, a new instance of @a T (which must be a singleton) is created. |
---|
| 83 | If the Scope is deactivated, the singleton is destroyed. |
---|
| 84 | |
---|
| 85 | @see Singleton |
---|
| 86 | */ |
---|
[10514] | 87 | template <class T, bool allowedToFail> |
---|
[10460] | 88 | class ClassScopedSingletonWrapper : public ScopedSingletonWrapper |
---|
[5867] | 89 | { |
---|
| 90 | public: |
---|
[10458] | 91 | ClassScopedSingletonWrapper(const std::string& className) |
---|
[10514] | 92 | : ScopedSingletonWrapper(className) |
---|
[11071] | 93 | , singletonPtr_(nullptr) |
---|
[5867] | 94 | { |
---|
| 95 | } |
---|
| 96 | |
---|
[10458] | 97 | ~ClassScopedSingletonWrapper() |
---|
[5867] | 98 | { |
---|
[10528] | 99 | if (singletonPtr_) |
---|
| 100 | this->destroy(singletonPtr_); |
---|
[5867] | 101 | } |
---|
| 102 | |
---|
| 103 | //! Called if the Scope of the Singleton gets active (creates the instance) |
---|
[11071] | 104 | virtual void activated() override |
---|
[5867] | 105 | { |
---|
[11071] | 106 | assert(singletonPtr_ == nullptr); |
---|
[5867] | 107 | singletonPtr_ = new T(); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | //! Called if the Scope of this Singleton gets deactivated (destroys the instance) |
---|
[11071] | 111 | virtual void deactivated() override |
---|
[5867] | 112 | { |
---|
[11071] | 113 | assert(singletonPtr_ != nullptr); |
---|
[5867] | 114 | this->destroy(singletonPtr_); |
---|
[11071] | 115 | singletonPtr_ = nullptr; |
---|
[5867] | 116 | } |
---|
| 117 | |
---|
[9667] | 118 | //! Destroys the singleton instance - overloaded for Destroyable, calls Destroyable::destroy() |
---|
| 119 | void destroy(Destroyable*) |
---|
[5877] | 120 | { |
---|
| 121 | singletonPtr_->destroy(); |
---|
| 122 | } |
---|
[7401] | 123 | //! Destroys the singleton instance - overloaded for all other pointers, calls delete |
---|
[5877] | 124 | void destroy(void*) |
---|
| 125 | { |
---|
| 126 | delete singletonPtr_; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | private: |
---|
[7401] | 130 | T* singletonPtr_; ///< Unique instance of the singleton class @a T |
---|
[5877] | 131 | }; |
---|
| 132 | |
---|
[7401] | 133 | /** |
---|
[10458] | 134 | @brief This class partially spezializes ClassScopedSingletonWrapper for classes @a T that are allowed to fail. |
---|
[7401] | 135 | @param T The managed singleton class |
---|
| 136 | |
---|
[10458] | 137 | Because @a T could fail when being created, this partial spezialization of ClassScopedSingletonWrapper |
---|
[7401] | 138 | uses a try-catch block to handle exceptions. |
---|
| 139 | |
---|
[10458] | 140 | See @ref ClassScopedSingletonWrapper for a full documentation of the basis template. |
---|
[7401] | 141 | */ |
---|
[10514] | 142 | template <class T> |
---|
| 143 | class ClassScopedSingletonWrapper<T, true> : public ScopedSingletonWrapper |
---|
[5877] | 144 | { |
---|
| 145 | public: |
---|
[10458] | 146 | ClassScopedSingletonWrapper(const std::string& className) |
---|
[10514] | 147 | : ScopedSingletonWrapper(className) |
---|
[11071] | 148 | , singletonPtr_(nullptr) |
---|
[5877] | 149 | { |
---|
| 150 | } |
---|
| 151 | |
---|
[10458] | 152 | ~ClassScopedSingletonWrapper() |
---|
[5877] | 153 | { |
---|
[10528] | 154 | if (singletonPtr_) |
---|
| 155 | this->destroy(singletonPtr_); |
---|
[5877] | 156 | } |
---|
| 157 | |
---|
| 158 | //! Called if the Scope of the Singleton gets active (creates the instance) |
---|
[11071] | 159 | virtual void activated() override |
---|
[5877] | 160 | { |
---|
[11071] | 161 | assert(singletonPtr_ == nullptr); |
---|
[5877] | 162 | try |
---|
| 163 | { singletonPtr_ = new T(); } |
---|
[6417] | 164 | catch (const InitialisationAbortedException& ex) |
---|
[8858] | 165 | { orxout(internal_error) << ex.getDescription() << endl; } |
---|
[5877] | 166 | catch (...) |
---|
[8858] | 167 | { orxout(internal_error) << "Singleton creation failed: " << Exception::handleMessage() << endl; } |
---|
[5877] | 168 | } |
---|
| 169 | |
---|
| 170 | //! Called if the Scope of this Singleton gets deactivated (destroys the instance) |
---|
[11071] | 171 | virtual void deactivated() override |
---|
[5877] | 172 | { |
---|
[11071] | 173 | if (singletonPtr_ != nullptr) |
---|
[5877] | 174 | { |
---|
| 175 | this->destroy(singletonPtr_); |
---|
[11071] | 176 | singletonPtr_ = nullptr; |
---|
[5877] | 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
[9667] | 180 | //! Destroys the singleton instance - overloaded for Destroyable, calls Destroyable::destroy() |
---|
| 181 | void destroy(Destroyable*) |
---|
[5867] | 182 | { |
---|
| 183 | singletonPtr_->destroy(); |
---|
| 184 | } |
---|
[7401] | 185 | //! Destroys the singleton instance - overloaded for void*, calls delete |
---|
[8706] | 186 | void destroy(void*) |
---|
[5867] | 187 | { |
---|
| 188 | delete singletonPtr_; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | private: |
---|
[7401] | 192 | T* singletonPtr_; ///< Unique instance of the singleton class @a T |
---|
[5867] | 193 | }; |
---|
| 194 | } |
---|
| 195 | |
---|
[10458] | 196 | #endif /* __ScopedSingletonWrapper_H__ */ |
---|