[Orxonox-commit 5857] r10517 - code/branches/core7/src/libraries/core/singleton
landauf at orxonox.net
landauf at orxonox.net
Sun May 31 10:54:39 CEST 2015
Author: landauf
Date: 2015-05-31 10:54:39 +0200 (Sun, 31 May 2015)
New Revision: 10517
Modified:
code/branches/core7/src/libraries/core/singleton/ScopeManager.cc
code/branches/core7/src/libraries/core/singleton/ScopeManager.h
Log:
activating a scopelistener right after registering may not be the best idea after all because some other stuff (e.g. an identifier) may not yet be registered if this happens during static initialization. added helper function instead (not yet used, but this will soon happen)
Modified: code/branches/core7/src/libraries/core/singleton/ScopeManager.cc
===================================================================
--- code/branches/core7/src/libraries/core/singleton/ScopeManager.cc 2015-05-31 08:38:09 UTC (rev 10516)
+++ code/branches/core7/src/libraries/core/singleton/ScopeManager.cc 2015-05-31 08:54:39 UTC (rev 10517)
@@ -63,14 +63,10 @@
void ScopeManager::addListener(ScopeListener* listener, ScopeID::Value scope)
{
this->listeners_[scope].insert(listener);
- if (this->isActive(scope))
- this->activateListener(listener);
}
void ScopeManager::removeListener(ScopeListener* listener, ScopeID::Value scope)
{
- if (this->isActive(scope))
- this->deactivateListener(listener);
this->listeners_[scope].erase(listener);
}
@@ -83,7 +79,8 @@
void ScopeManager::deactivateListenersForScope(ScopeID::Value scope)
{
for (std::set<ScopeListener*>::iterator it = this->listeners_[scope].begin(); it != this->listeners_[scope].end(); ++it)
- this->deactivateListener(*it);
+ if ((*it)->bActivated_)
+ this->deactivateListener(*it);
}
void ScopeManager::activateListener(ScopeListener* listener)
@@ -94,13 +91,31 @@
void ScopeManager::deactivateListener(ScopeListener* listener)
{
- if (listener->bActivated_)
+ try
+ { listener->deactivated(); }
+ catch (...)
+ { orxout(internal_warning) << "ScopeListener::deactivated() failed! This MUST NOT happen, fix it!" << endl; }
+ listener->bActivated_ = false;
+ }
+
+ void ScopeManager::updateListeners()
+ {
+ std::map<ScopeID::Value, std::set<ScopeListener*> >::iterator it1;
+ for (it1 = this->listeners_.begin(); it1 != this->listeners_.end(); ++it1)
{
- try
- { listener->deactivated(); }
- catch (...)
- { orxout(internal_warning) << "ScopeListener::deactivated() failed! This MUST NOT happen, fix it!" << endl; }
- listener->bActivated_ = false;
+ const ScopeID::Value& scope = it1->first;
+ const std::set<ScopeListener*>& listeners = it1->second;
+
+ bool scopeIsActive = this->isActive(scope);
+ for (std::set<ScopeListener*>::const_iterator it2 = listeners.begin(); it2 != listeners.end(); ++it2)
+ {
+ ScopeListener* listener = (*it2);
+
+ if (scopeIsActive && !listener->bActivated_)
+ this->activateListener(listener);
+ else if (!scopeIsActive && listener->bActivated_)
+ this->deactivateListener(listener);
+ }
}
}
}
Modified: code/branches/core7/src/libraries/core/singleton/ScopeManager.h
===================================================================
--- code/branches/core7/src/libraries/core/singleton/ScopeManager.h 2015-05-31 08:38:09 UTC (rev 10516)
+++ code/branches/core7/src/libraries/core/singleton/ScopeManager.h 2015-05-31 08:54:39 UTC (rev 10517)
@@ -63,11 +63,17 @@
/** Returns true if this scope is active */
bool isActive(ScopeID::Value scope);
- /** Registers a listener for the given scope. If the scope is already active, the listener is activate immediately. */
+ /** Registers a listener for the given scope. */
void addListener(ScopeListener* listener, ScopeID::Value scope);
- /** Unregisters a listener for the given scope. If the scope is still active, the listener is deactivated before removal. */
+ /** Unregisters a listener for the given scope. */
void removeListener(ScopeListener* listener, ScopeID::Value scope);
+ /**
+ * Checks for all listeners if their activity matches the activity of the scope.
+ * If not, listeners are activated or deactivated depending on their state.
+ */
+ void updateListeners();
+
private:
void activateListenersForScope(ScopeID::Value scope);
void deactivateListenersForScope(ScopeID::Value scope);
More information about the Orxonox-commit
mailing list