Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10463


Ignore:
Timestamp:
May 25, 2015, 12:00:14 AM (9 years ago)
Author:
landauf
Message:

removed unnecessary instance counts. creation of scopes is strictly controlled (in Core.cc)

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

Legend:

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

    r10462 r10463  
    3939Instances of orxonox::ScopeListener can register in orxonox::ScopeMAnager for a given @a scope and will get a
    4040notification if the corresponding orxonox::Scope object changes its state.
    41 
    42 To avoid multiple instances of orxonox::Scope<@a scope> in different libraries, each instance of orxonox::Scope
    43 registers in orxonox::ScopeManager, where they are linked statically in the core library.
    4441
    4542Scopes are usually used to control the creation and destruction of Singletons.
     
    104101    {
    105102        public:
    106             //! Constructor: Increases the instance counter and activates the scope if the count went from 0 to 1. Counts >1 don't change anything.
     103            //! Constructor: activate the listeners.
    107104            Scope()
    108105            {
    109106                orxout(internal_status) << "creating scope... (" << scope << ")" << endl;
    110107
    111                 try
     108                Loki::ScopeGuard deactivator = Loki::MakeObjGuard(*this, &Scope::deactivateScope);
     109                ScopeManager::getInstance().addScope(scope);
     110                for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getInstance().getListeners(scope).begin(); it != ScopeManager::getInstance().getListeners(scope).end(); )
    112111                {
    113                     ScopeManager::getInstance().getInstanceCount(scope)++;
    114                     assert(ScopeManager::getInstance().getInstanceCount(scope) > 0);
    115                     if (ScopeManager::getInstance().getInstanceCount(scope) == 1)
    116                     {
    117                         Loki::ScopeGuard deactivator = Loki::MakeObjGuard(*this, &Scope::deactivateListeners);
    118                         for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getInstance().getListeners(scope).begin(); it != ScopeManager::getInstance().getListeners(scope).end(); )
    119                         {
    120                             (*it)->activated();
    121                             (*(it++))->bActivated_ = true;
    122                         }
    123                         deactivator.Dismiss();
    124                     }
     112                    (*it)->activated();
     113                    (*(it++))->bActivated_ = true;
    125114                }
    126                 catch (...)
    127                 {
    128                     ScopeManager::getInstance().getInstanceCount(scope)--;
    129                     throw;
    130                 }
     115                deactivator.Dismiss();
    131116
    132117                orxout(internal_status) << "created scope (" << scope << ")" << endl;
    133118            }
    134119
    135             //! Destructor: Decreases the instance counter and deactivates the scope if the count went from 1 to 0. Counts >0 don't change anything.
     120            //! Destructor: deactivate the listeners.
    136121            ~Scope()
    137122            {
    138123                orxout(internal_status) << "destroying scope... (" << scope << ")" << endl;
    139124
    140                 ScopeManager::getInstance().getInstanceCount(scope)--;
    141 
    142                 // This shouldn't happen but just to be sure: check if the count is positive
    143                 assert(ScopeManager::getInstance().getInstanceCount(scope) >= 0);
    144                 if (ScopeManager::getInstance().getInstanceCount(scope) < 0)
    145                     ScopeManager::getInstance().getInstanceCount(scope) = 0;
    146 
    147                 if (ScopeManager::getInstance().getInstanceCount(scope) == 0)
    148                     this->deactivateListeners();
     125                this->deactivateScope();
    149126
    150127                orxout(internal_status) << "destroyed scope (" << scope << ")" << endl;
     
    152129
    153130            //! Deactivates the listeners of this scope in case the scope is destroyed or the construction fails.
    154             void deactivateListeners()
     131            void deactivateScope()
    155132            {
     133                ScopeManager::getInstance().removeScope(scope);
    156134                for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getInstance().getListeners(scope).begin(); it != ScopeManager::getInstance().getListeners(scope).end(); )
    157135                {
     
    172150            static bool isActive()
    173151            {
    174                 return (ScopeManager::getInstance().getInstanceCount(scope) > 0);
     152                return ScopeManager::getInstance().isActive(scope);
    175153            }
    176154    };
  • code/branches/core7/src/libraries/core/singleton/ScopeManager.cc

    r10462 r10463  
    4444    }
    4545
     46    void ScopeManager::addScope(ScopeID::Value scope)
     47    {
     48        this->activeScopes_.insert(scope);
     49    }
     50
     51    void ScopeManager::removeScope(ScopeID::Value scope)
     52    {
     53        this->activeScopes_.erase(scope);
     54    }
     55
     56    bool ScopeManager::isActive(ScopeID::Value scope)
     57    {
     58        return this->activeScopes_.find(scope) != this->activeScopes_.end();
     59    }
     60
    4661    void ScopeManager::addListener(ScopeListener* listener)
    4762    {
  • code/branches/core7/src/libraries/core/singleton/ScopeManager.h

    r10462 r10463  
    5757            static ScopeManager& getInstance();
    5858
     59            void addScope(ScopeID::Value scope);
     60            void removeScope(ScopeID::Value scope);
     61            bool isActive(ScopeID::Value scope);
     62
    5963            void addListener(ScopeListener* listener);
    6064            void removeListener(ScopeListener* listener);
    6165
    62             inline int& getInstanceCount(ScopeID::Value scope)
    63                 { return this->instanceCounts_[scope]; }
    6466            inline std::set<ScopeListener*>& getListeners(ScopeID::Value scope)
    6567                { return this->listeners_[scope]; }
    6668
    6769        private:
    68             std::map<ScopeID::Value, int> instanceCounts_;                  //!< Counts the number of active instances (>0 means active) for a scope
     70            std::set<ScopeID::Value> activeScopes_;
    6971            std::map<ScopeID::Value, std::set<ScopeListener*> > listeners_; //!< Stores all listeners for a scope
    7072    };
Note: See TracChangeset for help on using the changeset viewer.