[Orxonox-commit 5803] r10463 - code/branches/core7/src/libraries/core/singleton
landauf at orxonox.net
landauf at orxonox.net
Mon May 25 00:00:14 CEST 2015
Author: landauf
Date: 2015-05-25 00:00:14 +0200 (Mon, 25 May 2015)
New Revision: 10463
Modified:
code/branches/core7/src/libraries/core/singleton/Scope.h
code/branches/core7/src/libraries/core/singleton/ScopeManager.cc
code/branches/core7/src/libraries/core/singleton/ScopeManager.h
Log:
removed unnecessary instance counts. creation of scopes is strictly controlled (in Core.cc)
Modified: code/branches/core7/src/libraries/core/singleton/Scope.h
===================================================================
--- code/branches/core7/src/libraries/core/singleton/Scope.h 2015-05-24 21:42:16 UTC (rev 10462)
+++ code/branches/core7/src/libraries/core/singleton/Scope.h 2015-05-24 22:00:14 UTC (rev 10463)
@@ -39,9 +39,6 @@
Instances of orxonox::ScopeListener can register in orxonox::ScopeMAnager for a given @a scope and will get a
notification if the corresponding orxonox::Scope object changes its state.
-To avoid multiple instances of orxonox::Scope<@a scope> in different libraries, each instance of orxonox::Scope
-registers in orxonox::ScopeManager, where they are linked statically in the core library.
-
Scopes are usually used to control the creation and destruction of Singletons.
@see orxonox::Singleton
@@ -103,56 +100,37 @@
class Scope
{
public:
- //! Constructor: Increases the instance counter and activates the scope if the count went from 0 to 1. Counts >1 don't change anything.
+ //! Constructor: activate the listeners.
Scope()
{
orxout(internal_status) << "creating scope... (" << scope << ")" << endl;
- try
+ Loki::ScopeGuard deactivator = Loki::MakeObjGuard(*this, &Scope::deactivateScope);
+ ScopeManager::getInstance().addScope(scope);
+ for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getInstance().getListeners(scope).begin(); it != ScopeManager::getInstance().getListeners(scope).end(); )
{
- ScopeManager::getInstance().getInstanceCount(scope)++;
- assert(ScopeManager::getInstance().getInstanceCount(scope) > 0);
- if (ScopeManager::getInstance().getInstanceCount(scope) == 1)
- {
- Loki::ScopeGuard deactivator = Loki::MakeObjGuard(*this, &Scope::deactivateListeners);
- for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getInstance().getListeners(scope).begin(); it != ScopeManager::getInstance().getListeners(scope).end(); )
- {
- (*it)->activated();
- (*(it++))->bActivated_ = true;
- }
- deactivator.Dismiss();
- }
+ (*it)->activated();
+ (*(it++))->bActivated_ = true;
}
- catch (...)
- {
- ScopeManager::getInstance().getInstanceCount(scope)--;
- throw;
- }
+ deactivator.Dismiss();
orxout(internal_status) << "created scope (" << scope << ")" << endl;
}
- //! Destructor: Decreases the instance counter and deactivates the scope if the count went from 1 to 0. Counts >0 don't change anything.
+ //! Destructor: deactivate the listeners.
~Scope()
{
orxout(internal_status) << "destroying scope... (" << scope << ")" << endl;
- ScopeManager::getInstance().getInstanceCount(scope)--;
+ this->deactivateScope();
- // This shouldn't happen but just to be sure: check if the count is positive
- assert(ScopeManager::getInstance().getInstanceCount(scope) >= 0);
- if (ScopeManager::getInstance().getInstanceCount(scope) < 0)
- ScopeManager::getInstance().getInstanceCount(scope) = 0;
-
- if (ScopeManager::getInstance().getInstanceCount(scope) == 0)
- this->deactivateListeners();
-
orxout(internal_status) << "destroyed scope (" << scope << ")" << endl;
}
//! Deactivates the listeners of this scope in case the scope is destroyed or the construction fails.
- void deactivateListeners()
+ void deactivateScope()
{
+ ScopeManager::getInstance().removeScope(scope);
for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getInstance().getListeners(scope).begin(); it != ScopeManager::getInstance().getListeners(scope).end(); )
{
if ((*it)->bActivated_)
@@ -171,7 +149,7 @@
//! Returns true if the scope is active.
static bool isActive()
{
- return (ScopeManager::getInstance().getInstanceCount(scope) > 0);
+ return ScopeManager::getInstance().isActive(scope);
}
};
}
Modified: code/branches/core7/src/libraries/core/singleton/ScopeManager.cc
===================================================================
--- code/branches/core7/src/libraries/core/singleton/ScopeManager.cc 2015-05-24 21:42:16 UTC (rev 10462)
+++ code/branches/core7/src/libraries/core/singleton/ScopeManager.cc 2015-05-24 22:00:14 UTC (rev 10463)
@@ -43,6 +43,21 @@
return instance;
}
+ void ScopeManager::addScope(ScopeID::Value scope)
+ {
+ this->activeScopes_.insert(scope);
+ }
+
+ void ScopeManager::removeScope(ScopeID::Value scope)
+ {
+ this->activeScopes_.erase(scope);
+ }
+
+ bool ScopeManager::isActive(ScopeID::Value scope)
+ {
+ return this->activeScopes_.find(scope) != this->activeScopes_.end();
+ }
+
void ScopeManager::addListener(ScopeListener* listener)
{
this->listeners_[listener->getScope()].insert(listener);
Modified: code/branches/core7/src/libraries/core/singleton/ScopeManager.h
===================================================================
--- code/branches/core7/src/libraries/core/singleton/ScopeManager.h 2015-05-24 21:42:16 UTC (rev 10462)
+++ code/branches/core7/src/libraries/core/singleton/ScopeManager.h 2015-05-24 22:00:14 UTC (rev 10463)
@@ -56,16 +56,18 @@
public:
static ScopeManager& getInstance();
+ void addScope(ScopeID::Value scope);
+ void removeScope(ScopeID::Value scope);
+ bool isActive(ScopeID::Value scope);
+
void addListener(ScopeListener* listener);
void removeListener(ScopeListener* listener);
- inline int& getInstanceCount(ScopeID::Value scope)
- { return this->instanceCounts_[scope]; }
inline std::set<ScopeListener*>& getListeners(ScopeID::Value scope)
{ return this->listeners_[scope]; }
private:
- std::map<ScopeID::Value, int> instanceCounts_; //!< Counts the number of active instances (>0 means active) for a scope
+ std::set<ScopeID::Value> activeScopes_;
std::map<ScopeID::Value, std::set<ScopeListener*> > listeners_; //!< Stores all listeners for a scope
};
}
More information about the Orxonox-commit
mailing list