[5640] | 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 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[7401] | 29 | /** |
---|
| 30 | @file |
---|
| 31 | @ingroup SingletonScope |
---|
[10461] | 32 | @brief Declaration of the classes that are needed to use Scopes: orxonox::Scope and orxonox::ScopeListener. |
---|
[7401] | 33 | |
---|
| 34 | @anchor Scope |
---|
| 35 | |
---|
| 36 | A virtual scope can be represented by an instance of class orxonox::Scope. orxonox::Scope<@a scope> is a template |
---|
| 37 | an its template argument defines the name of the virtual scope. See orxonox::ScopeID for an enumeration of the |
---|
| 38 | available values for @a scope. The orxonox::Scope object for a given @a scope can be activated or deactivated. |
---|
[10461] | 39 | Instances of orxonox::ScopeListener can register in orxonox::ScopeMAnager for a given @a scope and will get a |
---|
| 40 | notification if the corresponding orxonox::Scope object changes its state. |
---|
[7401] | 41 | |
---|
| 42 | To avoid multiple instances of orxonox::Scope<@a scope> in different libraries, each instance of orxonox::Scope |
---|
[10407] | 43 | registers in orxonox::ScopeManager, where they are linked statically in the core library. |
---|
[7401] | 44 | |
---|
| 45 | Scopes are usually used to control the creation and destruction of Singletons. |
---|
| 46 | |
---|
| 47 | @see orxonox::Singleton |
---|
| 48 | */ |
---|
| 49 | |
---|
[10407] | 50 | #ifndef __Core_Scope_H__ |
---|
| 51 | #define __Core_Scope_H__ |
---|
[5640] | 52 | |
---|
[10407] | 53 | #include "core/CorePrereqs.h" |
---|
[5929] | 54 | |
---|
[5640] | 55 | #include <cassert> |
---|
[5929] | 56 | #include <map> |
---|
[5640] | 57 | #include <set> |
---|
[7266] | 58 | #include <loki/ScopeGuard.h> |
---|
[5929] | 59 | |
---|
[10407] | 60 | #include "util/Output.h" |
---|
[10461] | 61 | #include "ScopeManager.h" |
---|
[5640] | 62 | |
---|
| 63 | namespace orxonox |
---|
| 64 | { |
---|
| 65 | /** |
---|
[7401] | 66 | @brief ScopeListeners register themselves in the corresponding Scope and wait for notifications. |
---|
| 67 | Notifications are sent if a Scope is activated or deactivated. |
---|
| 68 | |
---|
| 69 | @see See @ref Scope "this description" for details about the interrelationship of Scope, ScopeListener, and ScopeManager. |
---|
[5640] | 70 | */ |
---|
[10407] | 71 | class _CoreExport ScopeListener |
---|
[5640] | 72 | { |
---|
| 73 | template <ScopeID::Value scope> |
---|
| 74 | friend class Scope; |
---|
| 75 | |
---|
| 76 | protected: |
---|
[10460] | 77 | ScopeListener(ScopeID::Value scope) : scope_(scope), bActivated_(false) { } |
---|
| 78 | virtual ~ScopeListener() { } |
---|
[5640] | 79 | |
---|
| 80 | //! Gets called if the scope is activated |
---|
| 81 | virtual void activated() = 0; |
---|
| 82 | //! Gets called if the scope is deactivated |
---|
| 83 | virtual void deactivated() = 0; |
---|
| 84 | |
---|
[10460] | 85 | public: |
---|
| 86 | inline ScopeID::Value getScope() const |
---|
| 87 | { return this->scope_; } |
---|
| 88 | |
---|
[5640] | 89 | private: |
---|
| 90 | ScopeID::Value scope_; //!< Store the scope to unregister on destruction |
---|
[5929] | 91 | bool bActivated_; |
---|
[5640] | 92 | }; |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | @brief A scope for a given template argument is either active or not. |
---|
| 96 | |
---|
| 97 | Objects inheriting from a ScopeListener are registered in a list (different for each scope). |
---|
| 98 | If the scope gets activated or deactivated, all objects in this list are notified. |
---|
[7401] | 99 | |
---|
| 100 | @see See @ref Scope "this description" for details about the interrelationship of Scope, ScopeListener, and ScopeManager. |
---|
[5640] | 101 | */ |
---|
| 102 | template <ScopeID::Value scope> |
---|
| 103 | class Scope |
---|
| 104 | { |
---|
| 105 | public: |
---|
| 106 | //! Constructor: Increases the instance counter and activates the scope if the count went from 0 to 1. Counts >1 don't change anything. |
---|
| 107 | Scope() |
---|
| 108 | { |
---|
[8858] | 109 | orxout(internal_status) << "creating scope... (" << scope << ")" << endl; |
---|
| 110 | |
---|
[5929] | 111 | try |
---|
[5640] | 112 | { |
---|
[10412] | 113 | ScopeManager::getInstanceCounts()[scope]++; |
---|
| 114 | assert(ScopeManager::getInstanceCounts()[scope] > 0); |
---|
| 115 | if (ScopeManager::getInstanceCounts()[scope] == 1) |
---|
[5929] | 116 | { |
---|
| 117 | Loki::ScopeGuard deactivator = Loki::MakeObjGuard(*this, &Scope::deactivateListeners); |
---|
[10412] | 118 | for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getListeners()[scope].begin(); it != ScopeManager::getListeners()[scope].end(); ) |
---|
[5929] | 119 | { |
---|
| 120 | (*it)->activated(); |
---|
| 121 | (*(it++))->bActivated_ = true; |
---|
| 122 | } |
---|
| 123 | deactivator.Dismiss(); |
---|
| 124 | } |
---|
[5640] | 125 | } |
---|
[5929] | 126 | catch (...) |
---|
| 127 | { |
---|
[10412] | 128 | ScopeManager::getInstanceCounts()[scope]--; |
---|
[5929] | 129 | throw; |
---|
| 130 | } |
---|
[8858] | 131 | |
---|
| 132 | orxout(internal_status) << "created scope (" << scope << ")" << endl; |
---|
[5640] | 133 | } |
---|
| 134 | |
---|
| 135 | //! Destructor: Decreases the instance counter and deactivates the scope if the count went from 1 to 0. Counts >0 don't change anything. |
---|
| 136 | ~Scope() |
---|
| 137 | { |
---|
[8858] | 138 | orxout(internal_status) << "destroying scope... (" << scope << ")" << endl; |
---|
| 139 | |
---|
[10412] | 140 | ScopeManager::getInstanceCounts()[scope]--; |
---|
[5640] | 141 | |
---|
| 142 | // This shouldn't happen but just to be sure: check if the count is positive |
---|
[10412] | 143 | assert(ScopeManager::getInstanceCounts()[scope] >= 0); |
---|
| 144 | if (ScopeManager::getInstanceCounts()[scope] < 0) |
---|
| 145 | ScopeManager::getInstanceCounts()[scope] = 0; |
---|
[5640] | 146 | |
---|
[10412] | 147 | if (ScopeManager::getInstanceCounts()[scope] == 0) |
---|
[5929] | 148 | this->deactivateListeners(); |
---|
[8858] | 149 | |
---|
| 150 | orxout(internal_status) << "destroyed scope (" << scope << ")" << endl; |
---|
[5929] | 151 | } |
---|
| 152 | |
---|
[7401] | 153 | //! Deactivates the listeners of this scope in case the scope is destroyed or the construction fails. |
---|
[5929] | 154 | void deactivateListeners() |
---|
| 155 | { |
---|
[10412] | 156 | for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getListeners()[scope].begin(); it != ScopeManager::getListeners()[scope].end(); ) |
---|
[5640] | 157 | { |
---|
[5929] | 158 | if ((*it)->bActivated_) |
---|
| 159 | { |
---|
| 160 | try |
---|
| 161 | { (*it)->deactivated(); } |
---|
| 162 | catch (...) |
---|
[8858] | 163 | { orxout(internal_warning) << "ScopeListener::deactivated() failed! This MUST NOT happen, fix it!" << endl; } |
---|
[5929] | 164 | (*(it++))->bActivated_ = false; |
---|
| 165 | } |
---|
| 166 | else |
---|
| 167 | ++it; |
---|
[5640] | 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | //! Returns true if the scope is active. |
---|
| 172 | static bool isActive() |
---|
| 173 | { |
---|
[10412] | 174 | return (ScopeManager::getInstanceCounts()[scope] > 0); |
---|
[5640] | 175 | } |
---|
| 176 | }; |
---|
| 177 | } |
---|
| 178 | |
---|
[10407] | 179 | #endif /* __Core_Scope_H__ */ |
---|