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 | |
---|
29 | #ifndef __ScopedSingletonManager_H__ |
---|
30 | #define __ScopedSingletonManager_H__ |
---|
31 | |
---|
32 | #include "CorePrereqs.h" |
---|
33 | |
---|
34 | #include <cassert> |
---|
35 | #include <map> |
---|
36 | #include "util/Scope.h" |
---|
37 | #include "util/Singleton.h" |
---|
38 | |
---|
39 | #define ManageScopedSingleton(className, scope) \ |
---|
40 | static ClassScopedSingletonManager<className, scope> className##ScopedSingletonManager(#className) |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | class _CoreExport ScopedSingletonManager |
---|
45 | { |
---|
46 | public: |
---|
47 | ScopedSingletonManager(const std::string& className, ScopeID::Value scope) |
---|
48 | : className_(className) |
---|
49 | , scope_(scope) |
---|
50 | { } |
---|
51 | virtual ~ScopedSingletonManager() { } |
---|
52 | static void addManager(ScopedSingletonManager* manager); |
---|
53 | static void removeManager(ScopedSingletonManager* manager); |
---|
54 | |
---|
55 | static void update(const Clock& time, ScopeID::Value scope) |
---|
56 | { |
---|
57 | for (ManagerMultiMap::iterator it = getManagersByScope().lower_bound(scope); it != getManagersByScope().upper_bound(scope); ++it) |
---|
58 | it->second->update(time); |
---|
59 | } |
---|
60 | virtual void update(const Clock& time) = 0; |
---|
61 | |
---|
62 | static std::map<std::string, ScopedSingletonManager*>& getManagers(); |
---|
63 | typedef std::multimap<ScopeID::Value, ScopedSingletonManager*> ManagerMultiMap; |
---|
64 | static ManagerMultiMap& getManagersByScope(); |
---|
65 | |
---|
66 | protected: |
---|
67 | const std::string className_; |
---|
68 | const ScopeID::Value scope_; |
---|
69 | }; |
---|
70 | |
---|
71 | template <class T, ScopeID::Value scope> |
---|
72 | class ClassScopedSingletonManager : public ScopedSingletonManager, public ScopeListener |
---|
73 | { |
---|
74 | public: |
---|
75 | ClassScopedSingletonManager(const std::string& className) |
---|
76 | : ScopedSingletonManager(className, scope) |
---|
77 | , ScopeListener(scope) |
---|
78 | , singletonPtr_(NULL) |
---|
79 | { |
---|
80 | ScopedSingletonManager::addManager(this); |
---|
81 | } |
---|
82 | |
---|
83 | ~ClassScopedSingletonManager() |
---|
84 | { |
---|
85 | ScopedSingletonManager::removeManager(this); |
---|
86 | } |
---|
87 | |
---|
88 | //! Called if the Scope of the Singleton gets active (creates the instance) |
---|
89 | void activated() |
---|
90 | { |
---|
91 | assert(singletonPtr_ == NULL); |
---|
92 | singletonPtr_ = new T(); |
---|
93 | } |
---|
94 | |
---|
95 | //! Called if the Scope of this Singleton gets deactivated (destroys the instance) |
---|
96 | void deactivated() |
---|
97 | { |
---|
98 | assert(singletonPtr_ != NULL); |
---|
99 | this->destroy(singletonPtr_); |
---|
100 | singletonPtr_ = NULL; |
---|
101 | } |
---|
102 | |
---|
103 | void destroy(OrxonoxClass* ptr) |
---|
104 | { |
---|
105 | singletonPtr_->destroy(); |
---|
106 | } |
---|
107 | void destroy(void* ptr) |
---|
108 | { |
---|
109 | delete singletonPtr_; |
---|
110 | } |
---|
111 | |
---|
112 | //! Called every frame by the ScopedSingletonManager |
---|
113 | void update(const Clock& time) |
---|
114 | { |
---|
115 | // assuming T inherits Singleton<T> |
---|
116 | singletonPtr_->updateSingleton(time); |
---|
117 | } |
---|
118 | |
---|
119 | private: |
---|
120 | T* singletonPtr_; |
---|
121 | }; |
---|
122 | } |
---|
123 | |
---|
124 | #endif /* __ScopedSingletonManager_H__ */ |
---|