Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10412


Ignore:
Timestamp:
May 2, 2015, 10:41:10 PM (10 years ago)
Author:
landauf
Message:

fixed crash: apparently these static maps get initialized later now that Scope.cc is in core instead of util. using static getters fixed the issue.

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

Legend:

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

    r10407 r10412  
    3636namespace orxonox
    3737{
    38     std::map<ScopeID::Value, int> ScopeManager::instanceCounts_s;
    39     std::map<ScopeID::Value, std::set<ScopeListener*> > ScopeManager::listeners_s;
     38    /*static*/ std::map<ScopeID::Value, int>& ScopeManager::getInstanceCounts()
     39    {
     40        static std::map<ScopeID::Value, int> instanceCounts;
     41        return instanceCounts;
     42    }
     43    /*static*/ std::map<ScopeID::Value, std::set<ScopeListener*> >& ScopeManager::getListeners()
     44    {
     45        static std::map<ScopeID::Value, std::set<ScopeListener*> > listeners;
     46        return listeners;
     47    }
    4048}
  • code/branches/core7/src/libraries/core/singleton/Scope.h

    r10407 r10412  
    8080
    8181        private:
    82             static std::map<ScopeID::Value, int> instanceCounts_s;                  //!< Counts the number of active instances (>0 means active) for a scope
    83             static std::map<ScopeID::Value, std::set<ScopeListener*> > listeners_s; //!< Stores all listeners for a scope
     82            static std::map<ScopeID::Value, int>& getInstanceCounts();                  //!< Counts the number of active instances (>0 means active) for a scope
     83            static std::map<ScopeID::Value, std::set<ScopeListener*> >& getListeners(); //!< Stores all listeners for a scope
    8484    };
    8585
     
    9898            //! Constructor: Registers the instance.
    9999            ScopeListener(ScopeID::Value scope) : scope_(scope), bActivated_(false)
    100                 { ScopeManager::listeners_s[this->scope_].insert(this); }
     100                { ScopeManager::getListeners()[this->scope_].insert(this); }
    101101            //! Destructor: Unregisters the instance.
    102102            virtual ~ScopeListener()
    103                 { ScopeManager::listeners_s[this->scope_].erase(this); }
     103                { ScopeManager::getListeners()[this->scope_].erase(this); }
    104104
    105105            //! Gets called if the scope is activated
     
    132132                try
    133133                {
    134                     ScopeManager::instanceCounts_s[scope]++;
    135                     assert(ScopeManager::instanceCounts_s[scope] > 0);
    136                     if (ScopeManager::instanceCounts_s[scope] == 1)
     134                    ScopeManager::getInstanceCounts()[scope]++;
     135                    assert(ScopeManager::getInstanceCounts()[scope] > 0);
     136                    if (ScopeManager::getInstanceCounts()[scope] == 1)
    137137                    {
    138138                        Loki::ScopeGuard deactivator = Loki::MakeObjGuard(*this, &Scope::deactivateListeners);
    139                         for (typename std::set<ScopeListener*>::iterator it = ScopeManager::listeners_s[scope].begin(); it != ScopeManager::listeners_s[scope].end(); )
     139                        for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getListeners()[scope].begin(); it != ScopeManager::getListeners()[scope].end(); )
    140140                        {
    141141                            (*it)->activated();
     
    147147                catch (...)
    148148                {
    149                     ScopeManager::instanceCounts_s[scope]--;
     149                    ScopeManager::getInstanceCounts()[scope]--;
    150150                    throw;
    151151                }
     
    159159                orxout(internal_status) << "destroying scope... (" << scope << ")" << endl;
    160160
    161                 ScopeManager::instanceCounts_s[scope]--;
     161                ScopeManager::getInstanceCounts()[scope]--;
    162162
    163163                // This shouldn't happen but just to be sure: check if the count is positive
    164                 assert(ScopeManager::instanceCounts_s[scope] >= 0);
    165                 if (ScopeManager::instanceCounts_s[scope] < 0)
    166                     ScopeManager::instanceCounts_s[scope] = 0;
    167 
    168                 if (ScopeManager::instanceCounts_s[scope] == 0)
     164                assert(ScopeManager::getInstanceCounts()[scope] >= 0);
     165                if (ScopeManager::getInstanceCounts()[scope] < 0)
     166                    ScopeManager::getInstanceCounts()[scope] = 0;
     167
     168                if (ScopeManager::getInstanceCounts()[scope] == 0)
    169169                    this->deactivateListeners();
    170170
     
    175175            void deactivateListeners()
    176176            {
    177                 for (typename std::set<ScopeListener*>::iterator it = ScopeManager::listeners_s[scope].begin(); it != ScopeManager::listeners_s[scope].end(); )
     177                for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getListeners()[scope].begin(); it != ScopeManager::getListeners()[scope].end(); )
    178178                {
    179179                    if ((*it)->bActivated_)
     
    193193            static bool isActive()
    194194            {
    195                 return (ScopeManager::instanceCounts_s[scope] > 0);
     195                return (ScopeManager::getInstanceCounts()[scope] > 0);
    196196            }
    197197    };
Note: See TracChangeset for help on using the changeset viewer.