[Orxonox-commit 5853] r10513 - code/branches/core7/src/libraries/core/singleton

landauf at orxonox.net landauf at orxonox.net
Sun May 31 10:14:13 CEST 2015


Author: landauf
Date: 2015-05-31 10:14:12 +0200 (Sun, 31 May 2015)
New Revision: 10513

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:
refactoring: moved code from Scope.h into ScopeManager

Modified: code/branches/core7/src/libraries/core/singleton/Scope.h
===================================================================
--- code/branches/core7/src/libraries/core/singleton/Scope.h	2015-05-30 20:59:13 UTC (rev 10512)
+++ code/branches/core7/src/libraries/core/singleton/Scope.h	2015-05-31 08:14:12 UTC (rev 10513)
@@ -67,8 +67,7 @@
     */
     class _CoreExport ScopeListener
     {
-        template <ScopeID::Value scope>
-        friend class Scope;
+        friend class ScopeManager;
 
         protected:
             ScopeListener(ScopeID::Value scope) : scope_(scope), bActivated_(false) { }
@@ -107,11 +106,6 @@
 
                 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(); )
-                {
-                    (*it)->activated();
-                    (*(it++))->bActivated_ = true;
-                }
                 deactivator.Dismiss();
 
                 orxout(internal_status) << "created scope (" << scope << ")" << endl;
@@ -131,19 +125,6 @@
             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_)
-                    {
-                        try
-                            { (*it)->deactivated(); }
-                        catch (...)
-                            { orxout(internal_warning) << "ScopeListener::deactivated() failed! This MUST NOT happen, fix it!" << endl; }
-                        (*(it++))->bActivated_ = false;
-                    }
-                    else
-                        ++it;
-                }
             }
 
             //! Returns true if the scope is active.

Modified: code/branches/core7/src/libraries/core/singleton/ScopeManager.cc
===================================================================
--- code/branches/core7/src/libraries/core/singleton/ScopeManager.cc	2015-05-30 20:59:13 UTC (rev 10512)
+++ code/branches/core7/src/libraries/core/singleton/ScopeManager.cc	2015-05-31 08:14:12 UTC (rev 10513)
@@ -28,7 +28,7 @@
 
 /**
     @file
-    @brief Static linkage of the two maps in orxonox::ScopeManager.
+    @brief Implementation of orxonox::ScopeManager.
 */
 
 #include "ScopeManager.h"
@@ -46,11 +46,13 @@
     void ScopeManager::addScope(ScopeID::Value scope)
     {
         this->activeScopes_.insert(scope);
+        this->activateListenersForScope(scope);
     }
 
     void ScopeManager::removeScope(ScopeID::Value scope)
     {
         this->activeScopes_.erase(scope);
+        this->deactivateListenersForScope(scope);
     }
 
     bool ScopeManager::isActive(ScopeID::Value scope)
@@ -67,4 +69,32 @@
     {
         this->listeners_[listener->getScope()].erase(listener);
     }
+
+    void ScopeManager::activateListenersForScope(ScopeID::Value scope)
+    {
+        for (typename std::set<ScopeListener*>::iterator it = this->listeners_[scope].begin(); it != this->listeners_[scope].end(); ++it)
+            this->activateListener(*it);
+    }
+
+    void ScopeManager::deactivateListenersForScope(ScopeID::Value scope)
+    {
+        for (typename std::set<ScopeListener*>::iterator it = this->listeners_[scope].begin(); it != this->listeners_[scope].end(); ++it)
+            if ((*it)->bActivated_)
+                this->deactivateListener(*it);
+    }
+
+    void ScopeManager::activateListener(ScopeListener* listener)
+    {
+        listener->activated();
+        listener->bActivated_ = true;
+    }
+
+    void ScopeManager::deactivateListener(ScopeListener* listener)
+    {
+        try
+            { listener->deactivated(); }
+        catch (...)
+            { orxout(internal_warning) << "ScopeListener::deactivated() failed! This MUST NOT happen, fix it!" << endl; }
+        listener->bActivated_ = false;
+    }
 }

Modified: code/branches/core7/src/libraries/core/singleton/ScopeManager.h
===================================================================
--- code/branches/core7/src/libraries/core/singleton/ScopeManager.h	2015-05-30 20:59:13 UTC (rev 10512)
+++ code/branches/core7/src/libraries/core/singleton/ScopeManager.h	2015-05-31 08:14:12 UTC (rev 10513)
@@ -56,17 +56,25 @@
         public:
             static ScopeManager& getInstance();
 
+            /** Adds a scope and activates all listeners which are registered for this scope */
             void addScope(ScopeID::Value scope);
+            /** Removes a scope and deactivates all listeners which are registered for this scope */
             void removeScope(ScopeID::Value scope);
+            /** Returns true if this scope is active */
             bool isActive(ScopeID::Value scope);
 
+            /** Registers a listener for the given scope. */
             void addListener(ScopeListener* listener);
+            /** Unregisters a listener for the given scope. */
             void removeListener(ScopeListener* listener);
 
-            inline std::set<ScopeListener*>& getListeners(ScopeID::Value scope)
-                { return this->listeners_[scope]; }
+        private:
+            void activateListenersForScope(ScopeID::Value scope);
+            void deactivateListenersForScope(ScopeID::Value scope);
 
-        private:
+            void activateListener(ScopeListener* listener);
+            void deactivateListener(ScopeListener* listener);
+
             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