[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 |
---|
[10458] | 63 | ScopedSingletonWrapper(const std::string& className, ScopeID::Value scope) |
---|
[10460] | 64 | : ScopeListener(scope) |
---|
| 65 | , className_(className) |
---|
[5867] | 66 | { } |
---|
[10458] | 67 | virtual ~ScopedSingletonWrapper() { } |
---|
[7401] | 68 | |
---|
[5867] | 69 | protected: |
---|
[7401] | 70 | const std::string className_; ///< The name of the scoped singleton class that is managed by this object |
---|
[5867] | 71 | }; |
---|
| 72 | |
---|
[7401] | 73 | /** |
---|
[10458] | 74 | @anchor ClassScopedSingletonWrapper |
---|
[7401] | 75 | |
---|
| 76 | @brief Manages a scoped singleton for a given scope. |
---|
| 77 | @param T The managed singleton class |
---|
| 78 | @param scope The scope in which the singleton @a T should be active |
---|
| 79 | @param allowedToFail If true, a specialization of this template is used, that uses try-catch blocks to handle possible failures. |
---|
| 80 | |
---|
| 81 | This class inherits from ScopeListener for the given scope and thus its functions |
---|
| 82 | activated() and deactivated() are called whenever the Scope changes its state. |
---|
| 83 | |
---|
| 84 | If the Scope is activated, a new instance of @a T (which must be a singleton) is created. |
---|
| 85 | If the Scope is deactivated, the singleton is destroyed. |
---|
| 86 | |
---|
| 87 | @see Singleton |
---|
| 88 | */ |
---|
[5877] | 89 | template <class T, ScopeID::Value scope, bool allowedToFail> |
---|
[10460] | 90 | class ClassScopedSingletonWrapper : public ScopedSingletonWrapper |
---|
[5867] | 91 | { |
---|
| 92 | public: |
---|
[10458] | 93 | //! Constructor: Initializes the singleton pointer and passes the scope to ScopedSingletonWrapper and ScopeListener |
---|
| 94 | ClassScopedSingletonWrapper(const std::string& className) |
---|
| 95 | : ScopedSingletonWrapper(className, scope) |
---|
[5867] | 96 | , singletonPtr_(NULL) |
---|
| 97 | { |
---|
| 98 | } |
---|
| 99 | |
---|
[10458] | 100 | ~ClassScopedSingletonWrapper() |
---|
[5867] | 101 | { |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | //! Called if the Scope of the Singleton gets active (creates the instance) |
---|
| 105 | void activated() |
---|
| 106 | { |
---|
| 107 | assert(singletonPtr_ == NULL); |
---|
| 108 | singletonPtr_ = new T(); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | //! Called if the Scope of this Singleton gets deactivated (destroys the instance) |
---|
| 112 | void deactivated() |
---|
| 113 | { |
---|
| 114 | assert(singletonPtr_ != NULL); |
---|
| 115 | this->destroy(singletonPtr_); |
---|
| 116 | singletonPtr_ = NULL; |
---|
| 117 | } |
---|
| 118 | |
---|
[9667] | 119 | //! Destroys the singleton instance - overloaded for Destroyable, calls Destroyable::destroy() |
---|
| 120 | void destroy(Destroyable*) |
---|
[5877] | 121 | { |
---|
| 122 | singletonPtr_->destroy(); |
---|
| 123 | } |
---|
[7401] | 124 | //! Destroys the singleton instance - overloaded for all other pointers, calls delete |
---|
[5877] | 125 | void destroy(void*) |
---|
| 126 | { |
---|
| 127 | delete singletonPtr_; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | private: |
---|
[7401] | 131 | T* singletonPtr_; ///< Unique instance of the singleton class @a T |
---|
[5877] | 132 | }; |
---|
| 133 | |
---|
[7401] | 134 | /** |
---|
[10458] | 135 | @brief This class partially spezializes ClassScopedSingletonWrapper for classes @a T that are allowed to fail. |
---|
[7401] | 136 | @param T The managed singleton class |
---|
| 137 | @param scope The scope in which the singleton @a T should be active |
---|
| 138 | |
---|
[10458] | 139 | Because @a T could fail when being created, this partial spezialization of ClassScopedSingletonWrapper |
---|
[7401] | 140 | uses a try-catch block to handle exceptions. |
---|
| 141 | |
---|
[10458] | 142 | See @ref ClassScopedSingletonWrapper for a full documentation of the basis template. |
---|
[7401] | 143 | */ |
---|
[5877] | 144 | template <class T, ScopeID::Value scope> |
---|
[10460] | 145 | class ClassScopedSingletonWrapper<T, scope, true> : public ScopedSingletonWrapper |
---|
[5877] | 146 | { |
---|
| 147 | public: |
---|
[10458] | 148 | //! Constructor: Initializes the singleton pointer and passes the scope to ScopedSingletonWrapper and ScopeListener |
---|
| 149 | ClassScopedSingletonWrapper(const std::string& className) |
---|
| 150 | : ScopedSingletonWrapper(className, scope) |
---|
[5877] | 151 | , singletonPtr_(NULL) |
---|
| 152 | { |
---|
| 153 | } |
---|
| 154 | |
---|
[10458] | 155 | ~ClassScopedSingletonWrapper() |
---|
[5877] | 156 | { |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | //! Called if the Scope of the Singleton gets active (creates the instance) |
---|
| 160 | void activated() |
---|
| 161 | { |
---|
| 162 | assert(singletonPtr_ == NULL); |
---|
| 163 | try |
---|
| 164 | { singletonPtr_ = new T(); } |
---|
[6417] | 165 | catch (const InitialisationAbortedException& ex) |
---|
[8858] | 166 | { orxout(internal_error) << ex.getDescription() << endl; } |
---|
[5877] | 167 | catch (...) |
---|
[8858] | 168 | { orxout(internal_error) << "Singleton creation failed: " << Exception::handleMessage() << endl; } |
---|
[5877] | 169 | } |
---|
| 170 | |
---|
| 171 | //! Called if the Scope of this Singleton gets deactivated (destroys the instance) |
---|
| 172 | void deactivated() |
---|
| 173 | { |
---|
| 174 | if (singletonPtr_ != NULL) |
---|
| 175 | { |
---|
| 176 | this->destroy(singletonPtr_); |
---|
| 177 | singletonPtr_ = NULL; |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
[9667] | 181 | //! Destroys the singleton instance - overloaded for Destroyable, calls Destroyable::destroy() |
---|
| 182 | void destroy(Destroyable*) |
---|
[5867] | 183 | { |
---|
| 184 | singletonPtr_->destroy(); |
---|
| 185 | } |
---|
[7401] | 186 | //! Destroys the singleton instance - overloaded for void*, calls delete |
---|
[8706] | 187 | void destroy(void*) |
---|
[5867] | 188 | { |
---|
| 189 | delete singletonPtr_; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | private: |
---|
[7401] | 193 | T* singletonPtr_; ///< Unique instance of the singleton class @a T |
---|
[5867] | 194 | }; |
---|
| 195 | } |
---|
| 196 | |
---|
[10458] | 197 | #endif /* __ScopedSingletonWrapper_H__ */ |
---|