Changeset 10463 for code/branches/core7/src/libraries
- Timestamp:
- May 25, 2015, 12:00:14 AM (10 years ago)
- Location:
- code/branches/core7/src/libraries/core/singleton
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core7/src/libraries/core/singleton/Scope.h
r10462 r10463 39 39 Instances of orxonox::ScopeListener can register in orxonox::ScopeMAnager for a given @a scope and will get a 40 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::Scope43 registers in orxonox::ScopeManager, where they are linked statically in the core library.44 41 45 42 Scopes are usually used to control the creation and destruction of Singletons. … … 104 101 { 105 102 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.103 //! Constructor: activate the listeners. 107 104 Scope() 108 105 { 109 106 orxout(internal_status) << "creating scope... (" << scope << ")" << endl; 110 107 111 try 108 Loki::ScopeGuard deactivator = Loki::MakeObjGuard(*this, &Scope::deactivateScope); 109 ScopeManager::getInstance().addScope(scope); 110 for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getInstance().getListeners(scope).begin(); it != ScopeManager::getInstance().getListeners(scope).end(); ) 112 111 { 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 } 112 (*it)->activated(); 113 (*(it++))->bActivated_ = true; 125 114 } 126 catch (...) 127 { 128 ScopeManager::getInstance().getInstanceCount(scope)--; 129 throw; 130 } 115 deactivator.Dismiss(); 131 116 132 117 orxout(internal_status) << "created scope (" << scope << ")" << endl; 133 118 } 134 119 135 //! Destructor: Decreases the instance counter and deactivates the scope if the count went from 1 to 0. Counts >0 don't change anything.120 //! Destructor: deactivate the listeners. 136 121 ~Scope() 137 122 { 138 123 orxout(internal_status) << "destroying scope... (" << scope << ")" << endl; 139 124 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(); 125 this->deactivateScope(); 149 126 150 127 orxout(internal_status) << "destroyed scope (" << scope << ")" << endl; … … 152 129 153 130 //! Deactivates the listeners of this scope in case the scope is destroyed or the construction fails. 154 void deactivate Listeners()131 void deactivateScope() 155 132 { 133 ScopeManager::getInstance().removeScope(scope); 156 134 for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getInstance().getListeners(scope).begin(); it != ScopeManager::getInstance().getListeners(scope).end(); ) 157 135 { … … 172 150 static bool isActive() 173 151 { 174 return (ScopeManager::getInstance().getInstanceCount(scope) > 0);152 return ScopeManager::getInstance().isActive(scope); 175 153 } 176 154 }; -
code/branches/core7/src/libraries/core/singleton/ScopeManager.cc
r10462 r10463 44 44 } 45 45 46 void ScopeManager::addScope(ScopeID::Value scope) 47 { 48 this->activeScopes_.insert(scope); 49 } 50 51 void ScopeManager::removeScope(ScopeID::Value scope) 52 { 53 this->activeScopes_.erase(scope); 54 } 55 56 bool ScopeManager::isActive(ScopeID::Value scope) 57 { 58 return this->activeScopes_.find(scope) != this->activeScopes_.end(); 59 } 60 46 61 void ScopeManager::addListener(ScopeListener* listener) 47 62 { -
code/branches/core7/src/libraries/core/singleton/ScopeManager.h
r10462 r10463 57 57 static ScopeManager& getInstance(); 58 58 59 void addScope(ScopeID::Value scope); 60 void removeScope(ScopeID::Value scope); 61 bool isActive(ScopeID::Value scope); 62 59 63 void addListener(ScopeListener* listener); 60 64 void removeListener(ScopeListener* listener); 61 65 62 inline int& getInstanceCount(ScopeID::Value scope)63 { return this->instanceCounts_[scope]; }64 66 inline std::set<ScopeListener*>& getListeners(ScopeID::Value scope) 65 67 { return this->listeners_[scope]; } 66 68 67 69 private: 68 std:: map<ScopeID::Value, int> instanceCounts_; //!< Counts the number of active instances (>0 means active) for a scope70 std::set<ScopeID::Value> activeScopes_; 69 71 std::map<ScopeID::Value, std::set<ScopeListener*> > listeners_; //!< Stores all listeners for a scope 70 72 };
Note: See TracChangeset
for help on using the changeset viewer.