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 | |
---|
29 | /** |
---|
30 | @file |
---|
31 | @ingroup SingletonScope |
---|
32 | @brief Declaration of the classes that are needed to use Scopes: orxonox::Scope and orxonox::ScopeListener. |
---|
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. |
---|
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. |
---|
41 | |
---|
42 | To avoid multiple instances of orxonox::Scope<@a scope> in different libraries, each instance of orxonox::Scope |
---|
43 | registers in orxonox::ScopeManager, where they are linked statically in the core library. |
---|
44 | |
---|
45 | Scopes are usually used to control the creation and destruction of Singletons. |
---|
46 | |
---|
47 | @see orxonox::Singleton |
---|
48 | */ |
---|
49 | |
---|
50 | #ifndef __Core_Scope_H__ |
---|
51 | #define __Core_Scope_H__ |
---|
52 | |
---|
53 | #include "core/CorePrereqs.h" |
---|
54 | |
---|
55 | #include <cassert> |
---|
56 | #include <map> |
---|
57 | #include <set> |
---|
58 | #include <loki/ScopeGuard.h> |
---|
59 | |
---|
60 | #include "util/Output.h" |
---|
61 | #include "ScopeManager.h" |
---|
62 | |
---|
63 | namespace orxonox |
---|
64 | { |
---|
65 | /** |
---|
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. |
---|
70 | */ |
---|
71 | class _CoreExport ScopeListener |
---|
72 | { |
---|
73 | template <ScopeID::Value scope> |
---|
74 | friend class Scope; |
---|
75 | |
---|
76 | protected: |
---|
77 | ScopeListener(ScopeID::Value scope) : scope_(scope), bActivated_(false) { } |
---|
78 | virtual ~ScopeListener() { } |
---|
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 | |
---|
85 | public: |
---|
86 | inline ScopeID::Value getScope() const |
---|
87 | { return this->scope_; } |
---|
88 | |
---|
89 | private: |
---|
90 | ScopeID::Value scope_; //!< Store the scope to unregister on destruction |
---|
91 | bool bActivated_; |
---|
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. |
---|
99 | |
---|
100 | @see See @ref Scope "this description" for details about the interrelationship of Scope, ScopeListener, and ScopeManager. |
---|
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 | { |
---|
109 | orxout(internal_status) << "creating scope... (" << scope << ")" << endl; |
---|
110 | |
---|
111 | try |
---|
112 | { |
---|
113 | ScopeManager::getInstance().getInstanceCount(scope)++; |
---|
114 | assert(ScopeManager::getInstance().getInstanceCount(scope) > 0); |
---|
115 | if (ScopeManager::getInstance().getInstanceCount(scope) == 1) |
---|
116 | { |
---|
117 | Loki::ScopeGuard deactivator = Loki::MakeObjGuard(*this, &Scope::deactivateListeners); |
---|
118 | for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getInstance().getListeners(scope).begin(); it != ScopeManager::getInstance().getListeners(scope).end(); ) |
---|
119 | { |
---|
120 | (*it)->activated(); |
---|
121 | (*(it++))->bActivated_ = true; |
---|
122 | } |
---|
123 | deactivator.Dismiss(); |
---|
124 | } |
---|
125 | } |
---|
126 | catch (...) |
---|
127 | { |
---|
128 | ScopeManager::getInstance().getInstanceCount(scope)--; |
---|
129 | throw; |
---|
130 | } |
---|
131 | |
---|
132 | orxout(internal_status) << "created scope (" << scope << ")" << endl; |
---|
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 | { |
---|
138 | orxout(internal_status) << "destroying scope... (" << scope << ")" << endl; |
---|
139 | |
---|
140 | ScopeManager::getInstance().getInstanceCount(scope)--; |
---|
141 | |
---|
142 | // This shouldn't happen but just to be sure: check if the count is positive |
---|
143 | assert(ScopeManager::getInstance().getInstanceCount(scope) >= 0); |
---|
144 | if (ScopeManager::getInstance().getInstanceCount(scope) < 0) |
---|
145 | ScopeManager::getInstance().getInstanceCount(scope) = 0; |
---|
146 | |
---|
147 | if (ScopeManager::getInstance().getInstanceCount(scope) == 0) |
---|
148 | this->deactivateListeners(); |
---|
149 | |
---|
150 | orxout(internal_status) << "destroyed scope (" << scope << ")" << endl; |
---|
151 | } |
---|
152 | |
---|
153 | //! Deactivates the listeners of this scope in case the scope is destroyed or the construction fails. |
---|
154 | void deactivateListeners() |
---|
155 | { |
---|
156 | for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getInstance().getListeners(scope).begin(); it != ScopeManager::getInstance().getListeners(scope).end(); ) |
---|
157 | { |
---|
158 | if ((*it)->bActivated_) |
---|
159 | { |
---|
160 | try |
---|
161 | { (*it)->deactivated(); } |
---|
162 | catch (...) |
---|
163 | { orxout(internal_warning) << "ScopeListener::deactivated() failed! This MUST NOT happen, fix it!" << endl; } |
---|
164 | (*(it++))->bActivated_ = false; |
---|
165 | } |
---|
166 | else |
---|
167 | ++it; |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | //! Returns true if the scope is active. |
---|
172 | static bool isActive() |
---|
173 | { |
---|
174 | return (ScopeManager::getInstance().getInstanceCount(scope) > 0); |
---|
175 | } |
---|
176 | }; |
---|
177 | } |
---|
178 | |
---|
179 | #endif /* __Core_Scope_H__ */ |
---|