Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 24, 2015, 11:42:16 PM (10 years ago)
Author:
landauf
Message:

some refactoring in ScopeManager. made it a singleton and added functions.

Location:
code/branches/core7/src/libraries/core/singleton
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core7/src/libraries/core/singleton/Scope.h

    r10461 r10462  
    111111                try
    112112                {
    113                     ScopeManager::getInstanceCounts()[scope]++;
    114                     assert(ScopeManager::getInstanceCounts()[scope] > 0);
    115                     if (ScopeManager::getInstanceCounts()[scope] == 1)
     113                    ScopeManager::getInstance().getInstanceCount(scope)++;
     114                    assert(ScopeManager::getInstance().getInstanceCount(scope) > 0);
     115                    if (ScopeManager::getInstance().getInstanceCount(scope) == 1)
    116116                    {
    117117                        Loki::ScopeGuard deactivator = Loki::MakeObjGuard(*this, &Scope::deactivateListeners);
    118                         for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getListeners()[scope].begin(); it != ScopeManager::getListeners()[scope].end(); )
     118                        for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getInstance().getListeners(scope).begin(); it != ScopeManager::getInstance().getListeners(scope).end(); )
    119119                        {
    120120                            (*it)->activated();
     
    126126                catch (...)
    127127                {
    128                     ScopeManager::getInstanceCounts()[scope]--;
     128                    ScopeManager::getInstance().getInstanceCount(scope)--;
    129129                    throw;
    130130                }
     
    138138                orxout(internal_status) << "destroying scope... (" << scope << ")" << endl;
    139139
    140                 ScopeManager::getInstanceCounts()[scope]--;
     140                ScopeManager::getInstance().getInstanceCount(scope)--;
    141141
    142142                // This shouldn't happen but just to be sure: check if the count is positive
    143                 assert(ScopeManager::getInstanceCounts()[scope] >= 0);
    144                 if (ScopeManager::getInstanceCounts()[scope] < 0)
    145                     ScopeManager::getInstanceCounts()[scope] = 0;
     143                assert(ScopeManager::getInstance().getInstanceCount(scope) >= 0);
     144                if (ScopeManager::getInstance().getInstanceCount(scope) < 0)
     145                    ScopeManager::getInstance().getInstanceCount(scope) = 0;
    146146
    147                 if (ScopeManager::getInstanceCounts()[scope] == 0)
     147                if (ScopeManager::getInstance().getInstanceCount(scope) == 0)
    148148                    this->deactivateListeners();
    149149
     
    154154            void deactivateListeners()
    155155            {
    156                 for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getListeners()[scope].begin(); it != ScopeManager::getListeners()[scope].end(); )
     156                for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getInstance().getListeners(scope).begin(); it != ScopeManager::getInstance().getListeners(scope).end(); )
    157157                {
    158158                    if ((*it)->bActivated_)
     
    172172            static bool isActive()
    173173            {
    174                 return (ScopeManager::getInstanceCounts()[scope] > 0);
     174                return (ScopeManager::getInstance().getInstanceCount(scope) > 0);
    175175            }
    176176    };
  • code/branches/core7/src/libraries/core/singleton/ScopeManager.cc

    r10461 r10462  
    3232*/
    3333
     34#include "ScopeManager.h"
     35
    3436#include "Scope.h"
    3537
    3638namespace orxonox
    3739{
    38     /*static*/ std::map<ScopeID::Value, int>& ScopeManager::getInstanceCounts()
     40    /* static */ ScopeManager& ScopeManager::getInstance()
    3941    {
    40         static std::map<ScopeID::Value, int> instanceCounts;
    41         return instanceCounts;
     42        static ScopeManager instance;
     43        return instance;
    4244    }
    43     /*static*/ std::map<ScopeID::Value, std::set<ScopeListener*> >& ScopeManager::getListeners()
     45
     46    void ScopeManager::addListener(ScopeListener* listener)
    4447    {
    45         static std::map<ScopeID::Value, std::set<ScopeListener*> > listeners;
    46         return listeners;
     48        this->listeners_[listener->getScope()].insert(listener);
     49    }
     50
     51    void ScopeManager::removeListener(ScopeListener* listener)
     52    {
     53        this->listeners_[listener->getScope()].erase(listener);
    4754    }
    4855}
  • code/branches/core7/src/libraries/core/singleton/ScopeManager.h

    r10461 r10462  
    5454    class _CoreExport ScopeManager
    5555    {
    56         template <ScopeID::Value scope>
    57         friend class Scope;
    58         friend class StaticallyInitializedScopedSingletonWrapper;
     56        public:
     57            static ScopeManager& getInstance();
     58
     59            void addListener(ScopeListener* listener);
     60            void removeListener(ScopeListener* listener);
     61
     62            inline int& getInstanceCount(ScopeID::Value scope)
     63                { return this->instanceCounts_[scope]; }
     64            inline std::set<ScopeListener*>& getListeners(ScopeID::Value scope)
     65                { return this->listeners_[scope]; }
    5966
    6067        private:
    61             static std::map<ScopeID::Value, int>& getInstanceCounts();                  //!< Counts the number of active instances (>0 means active) for a scope
    62             static std::map<ScopeID::Value, std::set<ScopeListener*> >& getListeners(); //!< Stores all listeners for a scope
     68            std::map<ScopeID::Value, int> instanceCounts_;                  //!< Counts the number of active instances (>0 means active) for a scope
     69            std::map<ScopeID::Value, std::set<ScopeListener*> > listeners_; //!< Stores all listeners for a scope
    6370    };
    6471}
  • code/branches/core7/src/libraries/core/singleton/ScopedSingletonIncludes.cc

    r10460 r10462  
    3333    void StaticallyInitializedScopedSingletonWrapper::load()
    3434    {
    35         ScopeManager::getListeners()[this->wrapper_->getScope()].insert(this->wrapper_);
     35        ScopeManager::getInstance().addListener(this->wrapper_);
    3636    }
    3737
    3838    void StaticallyInitializedScopedSingletonWrapper::unload()
    3939    {
    40         ScopeManager::getListeners()[this->wrapper_->getScope()].erase(this->wrapper_);
     40        ScopeManager::getInstance().removeListener(this->wrapper_);
    4141    }
    4242}
Note: See TracChangeset for help on using the changeset viewer.