Changeset 5877 for code/branches/core5/src/libraries
- Timestamp:
- Oct 5, 2009, 1:34:10 AM (15 years ago)
- Location:
- code/branches/core5/src/libraries
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core5/src/libraries/core/Core.cc
r5876 r5877 434 434 { 435 435 // singletons from other libraries 436 ScopedSingletonManager::update (time, ScopeID::Root);436 ScopedSingletonManager::update<ScopeID::Root>(time); 437 437 if (this->bGraphicsLoaded_) 438 438 { … … 442 442 this->guiManager_->update(time); 443 443 // graphics singletons from other libraries 444 ScopedSingletonManager::update (time, ScopeID::Graphics);444 ScopedSingletonManager::update<ScopeID::Graphics>(time); 445 445 } 446 446 // process thread commands -
code/branches/core5/src/libraries/core/ScopedSingletonManager.h
r5867 r5877 34 34 #include <cassert> 35 35 #include <map> 36 #include "util/Exception.h" 36 37 #include "util/Scope.h" 37 38 #include "util/Singleton.h" 38 39 39 #define ManageScopedSingleton(className, scope ) \40 static ClassScopedSingletonManager<className, scope > className##ScopedSingletonManager(#className)40 #define ManageScopedSingleton(className, scope, allowedToFail) \ 41 static ClassScopedSingletonManager<className, scope, allowedToFail> className##ScopedSingletonManager(#className) 41 42 42 43 namespace orxonox … … 53 54 static void removeManager(ScopedSingletonManager* manager); 54 55 55 static void update(const Clock& time, ScopeID::Value scope) 56 template<ScopeID::Value scope> 57 static void update(const Clock& time) 56 58 { 59 assert(Scope<scope>::isActive()); 57 60 for (ManagerMultiMap::iterator it = getManagersByScope().lower_bound(scope); it != getManagersByScope().upper_bound(scope); ++it) 58 61 it->second->update(time); … … 69 72 }; 70 73 71 template <class T, ScopeID::Value scope >74 template <class T, ScopeID::Value scope, bool allowedToFail> 72 75 class ClassScopedSingletonManager : public ScopedSingletonManager, public ScopeListener 73 76 { … … 83 86 ~ClassScopedSingletonManager() 84 87 { 88 assert(singletonPtr_ == NULL); 85 89 ScopedSingletonManager::removeManager(this); 86 90 } … … 101 105 } 102 106 107 void destroy(OrxonoxClass*) 108 { 109 singletonPtr_->destroy(); 110 } 111 void destroy(void*) 112 { 113 delete singletonPtr_; 114 } 115 116 //! Called every frame by the ScopedSingletonManager 117 void update(const Clock& time) 118 { 119 assert(Scope<scope>::isActive()); 120 // assuming T inherits Singleton<T> 121 singletonPtr_->updateSingleton(time); 122 } 123 124 private: 125 T* singletonPtr_; 126 }; 127 128 template <class T, ScopeID::Value scope> 129 class ClassScopedSingletonManager<T, scope, true> : public ScopedSingletonManager, public ScopeListener 130 { 131 public: 132 ClassScopedSingletonManager(const std::string& className) 133 : ScopedSingletonManager(className, scope) 134 , ScopeListener(scope) 135 , singletonPtr_(NULL) 136 { 137 ScopedSingletonManager::addManager(this); 138 } 139 140 ~ClassScopedSingletonManager() 141 { 142 assert(singletonPtr_ == NULL); 143 ScopedSingletonManager::removeManager(this); 144 } 145 146 //! Called if the Scope of the Singleton gets active (creates the instance) 147 void activated() 148 { 149 assert(singletonPtr_ == NULL); 150 try 151 { singletonPtr_ = new T(); } 152 catch (...) 153 { COUT(1) << "Singleton creation failed: " << Exception::handleMessage() << std::endl; } 154 } 155 156 //! Called if the Scope of this Singleton gets deactivated (destroys the instance) 157 void deactivated() 158 { 159 if (singletonPtr_ != NULL) 160 { 161 this->destroy(singletonPtr_); 162 singletonPtr_ = NULL; 163 } 164 } 165 103 166 void destroy(OrxonoxClass* ptr) 104 167 { … … 113 176 void update(const Clock& time) 114 177 { 178 assert(Scope<scope>::isActive()); 115 179 // assuming T inherits Singleton<T> 116 singletonPtr_->updateSingleton(time); 180 if (singletonPtr_ != NULL) 181 singletonPtr_->updateSingleton(time); 117 182 } 118 183 -
code/branches/core5/src/libraries/core/input/KeyBinderManager.cc
r5869 r5877 41 41 { 42 42 KeyBinderManager* KeyBinderManager::singletonPtr_s = 0; 43 ManageScopedSingleton(KeyBinderManager, ScopeID::Graphics );43 ManageScopedSingleton(KeyBinderManager, ScopeID::Graphics, false); 44 44 45 45 KeyBinderManager::KeyBinderManager() -
code/branches/core5/src/libraries/core/input/KeyDetector.cc
r5869 r5877 40 40 std::string KeyDetector::callbackCommand_s = "KeyDetectorKeyPressed"; 41 41 KeyDetector* KeyDetector::singletonPtr_s = 0; 42 ManageScopedSingleton(KeyDetector, ScopeID::Graphics );42 ManageScopedSingleton(KeyDetector, ScopeID::Graphics, false); 43 43 44 44 KeyDetector::KeyDetector() -
code/branches/core5/src/libraries/util/Scope.h
r5867 r5877 35 35 #include <map> 36 36 #include <set> 37 37 38 #include "Debug.h" 39 #include "ScopeGuard.h" 38 40 39 41 namespace orxonox … … 63 65 protected: 64 66 //! Constructor: Registers the instance. 65 ScopeListener(ScopeID::Value scope) : scope_(scope) 67 ScopeListener(ScopeID::Value scope) : scope_(scope), bActivated_(false) 66 68 { ScopeManager::listeners_s[this->scope_].insert(this); } 67 69 //! Destructor: Unregisters the instance. … … 76 78 private: 77 79 ScopeID::Value scope_; //!< Store the scope to unregister on destruction 80 bool bActivated_; 78 81 }; 79 82 … … 91 94 Scope() 92 95 { 93 ScopeManager::instanceCounts_s[scope]++; 94 assert(ScopeManager::instanceCounts_s[scope] > 0); 95 if (ScopeManager::instanceCounts_s[scope] == 1) 96 try 96 97 { 97 for (typename std::set<ScopeListener*>::iterator it = ScopeManager::listeners_s[scope].begin(); it != ScopeManager::listeners_s[scope].end(); ) 98 (*(it++))->activated(); 98 ScopeManager::instanceCounts_s[scope]++; 99 assert(ScopeManager::instanceCounts_s[scope] > 0); 100 if (ScopeManager::instanceCounts_s[scope] == 1) 101 { 102 Loki::ScopeGuard deactivator = Loki::MakeObjGuard(*this, &Scope::deactivateListeners); 103 for (typename std::set<ScopeListener*>::iterator it = ScopeManager::listeners_s[scope].begin(); it != ScopeManager::listeners_s[scope].end(); ) 104 { 105 (*it)->activated(); 106 (*(it++))->bActivated_ = true; 107 } 108 deactivator.Dismiss(); 109 } 110 } 111 catch (...) 112 { 113 ScopeManager::instanceCounts_s[scope]--; 114 throw; 99 115 } 100 116 } … … 111 127 112 128 if (ScopeManager::instanceCounts_s[scope] == 0) 129 this->deactivateListeners(); 130 } 131 132 void deactivateListeners() 133 { 134 for (typename std::set<ScopeListener*>::iterator it = ScopeManager::listeners_s[scope].begin(); it != ScopeManager::listeners_s[scope].end(); ) 113 135 { 114 for (typename std::set<ScopeListener*>::iterator it = ScopeManager::listeners_s[scope].begin(); it != ScopeManager::listeners_s[scope].end(); ) 115 (*(it++))->deactivated(); 136 if ((*it)->bActivated_) 137 { 138 try 139 { (*it)->deactivated(); } 140 catch (...) 141 { COUT(0) << "ScopeListener::deactivated() failed! This MUST NOT happen, fix it!" << std::endl; } 142 (*(it++))->bActivated_ = false; 143 } 144 else 145 ++it; 116 146 } 117 147 }
Note: See TracChangeset
for help on using the changeset viewer.