[Orxonox-commit 5802] r10462 - in code/branches/core7/src/libraries/core: . singleton

landauf at orxonox.net landauf at orxonox.net
Sun May 24 23:42:17 CEST 2015


Author: landauf
Date: 2015-05-24 23:42:16 +0200 (Sun, 24 May 2015)
New Revision: 10462

Modified:
   code/branches/core7/src/libraries/core/Core.cc
   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
   code/branches/core7/src/libraries/core/singleton/ScopedSingletonIncludes.cc
Log:
some refactoring in ScopeManager. made it a singleton and added functions.

Modified: code/branches/core7/src/libraries/core/Core.cc
===================================================================
--- code/branches/core7/src/libraries/core/Core.cc	2015-05-24 21:29:21 UTC (rev 10461)
+++ code/branches/core7/src/libraries/core/Core.cc	2015-05-24 21:42:16 UTC (rev 10462)
@@ -142,6 +142,7 @@
             }
         }
 
+        // TODO: initialize ScopeManager here
         // TODO: initialize CommandLineParser here
         // TODO: initialize ConsoleCommandManager here
         ModuleInstance::getCurrentModuleInstance()->loadAllStaticallyInitializedInstances();

Modified: code/branches/core7/src/libraries/core/singleton/Scope.h
===================================================================
--- code/branches/core7/src/libraries/core/singleton/Scope.h	2015-05-24 21:29:21 UTC (rev 10461)
+++ code/branches/core7/src/libraries/core/singleton/Scope.h	2015-05-24 21:42:16 UTC (rev 10462)
@@ -110,12 +110,12 @@
 
                 try
                 {
-                    ScopeManager::getInstanceCounts()[scope]++;
-                    assert(ScopeManager::getInstanceCounts()[scope] > 0);
-                    if (ScopeManager::getInstanceCounts()[scope] == 1)
+                    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::getListeners()[scope].begin(); it != ScopeManager::getListeners()[scope].end(); )
+                        for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getInstance().getListeners(scope).begin(); it != ScopeManager::getInstance().getListeners(scope).end(); )
                         {
                             (*it)->activated();
                             (*(it++))->bActivated_ = true;
@@ -125,7 +125,7 @@
                 }
                 catch (...)
                 {
-                    ScopeManager::getInstanceCounts()[scope]--;
+                    ScopeManager::getInstance().getInstanceCount(scope)--;
                     throw;
                 }
 
@@ -137,14 +137,14 @@
             {
                 orxout(internal_status) << "destroying scope... (" << scope << ")" << endl;
 
-                ScopeManager::getInstanceCounts()[scope]--;
+                ScopeManager::getInstance().getInstanceCount(scope)--;
 
                 // This shouldn't happen but just to be sure: check if the count is positive
-                assert(ScopeManager::getInstanceCounts()[scope] >= 0);
-                if (ScopeManager::getInstanceCounts()[scope] < 0)
-                    ScopeManager::getInstanceCounts()[scope] = 0;
+                assert(ScopeManager::getInstance().getInstanceCount(scope) >= 0);
+                if (ScopeManager::getInstance().getInstanceCount(scope) < 0)
+                    ScopeManager::getInstance().getInstanceCount(scope) = 0;
 
-                if (ScopeManager::getInstanceCounts()[scope] == 0)
+                if (ScopeManager::getInstance().getInstanceCount(scope) == 0)
                     this->deactivateListeners();
 
                 orxout(internal_status) << "destroyed scope (" << scope << ")" << endl;
@@ -153,7 +153,7 @@
             //! Deactivates the listeners of this scope in case the scope is destroyed or the construction fails.
             void deactivateListeners()
             {
-                for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getListeners()[scope].begin(); it != ScopeManager::getListeners()[scope].end(); )
+                for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getInstance().getListeners(scope).begin(); it != ScopeManager::getInstance().getListeners(scope).end(); )
                 {
                     if ((*it)->bActivated_)
                     {
@@ -171,7 +171,7 @@
             //! Returns true if the scope is active.
             static bool isActive()
             {
-                return (ScopeManager::getInstanceCounts()[scope] > 0);
+                return (ScopeManager::getInstance().getInstanceCount(scope) > 0);
             }
     };
 }

Modified: code/branches/core7/src/libraries/core/singleton/ScopeManager.cc
===================================================================
--- code/branches/core7/src/libraries/core/singleton/ScopeManager.cc	2015-05-24 21:29:21 UTC (rev 10461)
+++ code/branches/core7/src/libraries/core/singleton/ScopeManager.cc	2015-05-24 21:42:16 UTC (rev 10462)
@@ -31,18 +31,25 @@
     @brief Static linkage of the two maps in orxonox::ScopeManager.
 */
 
+#include "ScopeManager.h"
+
 #include "Scope.h"
 
 namespace orxonox
 {
-    /*static*/ std::map<ScopeID::Value, int>& ScopeManager::getInstanceCounts()
+    /* static */ ScopeManager& ScopeManager::getInstance()
     {
-        static std::map<ScopeID::Value, int> instanceCounts;
-        return instanceCounts;
+        static ScopeManager instance;
+        return instance;
     }
-    /*static*/ std::map<ScopeID::Value, std::set<ScopeListener*> >& ScopeManager::getListeners()
+
+    void ScopeManager::addListener(ScopeListener* listener)
     {
-        static std::map<ScopeID::Value, std::set<ScopeListener*> > listeners;
-        return listeners;
+        this->listeners_[listener->getScope()].insert(listener);
     }
+
+    void ScopeManager::removeListener(ScopeListener* listener)
+    {
+        this->listeners_[listener->getScope()].erase(listener);
+    }
 }

Modified: code/branches/core7/src/libraries/core/singleton/ScopeManager.h
===================================================================
--- code/branches/core7/src/libraries/core/singleton/ScopeManager.h	2015-05-24 21:29:21 UTC (rev 10461)
+++ code/branches/core7/src/libraries/core/singleton/ScopeManager.h	2015-05-24 21:42:16 UTC (rev 10462)
@@ -53,13 +53,20 @@
     */
     class _CoreExport ScopeManager
     {
-        template <ScopeID::Value scope>
-        friend class Scope;
-        friend class StaticallyInitializedScopedSingletonWrapper;
+        public:
+            static ScopeManager& getInstance();
 
+            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:
-            static std::map<ScopeID::Value, int>& getInstanceCounts();                  //!< Counts the number of active instances (>0 means active) for a scope
-            static std::map<ScopeID::Value, std::set<ScopeListener*> >& getListeners(); //!< Stores all listeners for a scope
+            std::map<ScopeID::Value, int> instanceCounts_;                  //!< Counts the number of active instances (>0 means active) for a scope
+            std::map<ScopeID::Value, std::set<ScopeListener*> > listeners_; //!< Stores all listeners for a scope
     };
 }
 

Modified: code/branches/core7/src/libraries/core/singleton/ScopedSingletonIncludes.cc
===================================================================
--- code/branches/core7/src/libraries/core/singleton/ScopedSingletonIncludes.cc	2015-05-24 21:29:21 UTC (rev 10461)
+++ code/branches/core7/src/libraries/core/singleton/ScopedSingletonIncludes.cc	2015-05-24 21:42:16 UTC (rev 10462)
@@ -32,11 +32,11 @@
 {
     void StaticallyInitializedScopedSingletonWrapper::load()
     {
-        ScopeManager::getListeners()[this->wrapper_->getScope()].insert(this->wrapper_);
+        ScopeManager::getInstance().addListener(this->wrapper_);
     }
 
     void StaticallyInitializedScopedSingletonWrapper::unload()
     {
-        ScopeManager::getListeners()[this->wrapper_->getScope()].erase(this->wrapper_);
+        ScopeManager::getInstance().removeListener(this->wrapper_);
     }
 }




More information about the Orxonox-commit mailing list