Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 698 for code/branches/FICN


Ignore:
Timestamp:
Dec 27, 2007, 5:49:03 AM (17 years ago)
Author:
landauf
Message:

hopefully replaced all static pre-main variables with more secure wrapper functions. and now i'll sleep.

Location:
code/branches/FICN/src/orxonox/core
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • code/branches/FICN/src/orxonox/core/ConfigValueContainer.cc

    r682 r698  
    3636namespace orxonox
    3737{
    38     std::list<std::string>* ConfigValueContainer::configFileLines_s = 0; // Set the static member variable configFileLines_s to zero
    39     bool ConfigValueContainer::readConfigFile_s = false;                 // Set the static member variable readConfigFile_s to false
    40 
    4138    /**
    4239        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
     
    438435    {
    439436        // Read the file if needed
    440         if (!ConfigValueContainer::readConfigFile_s)
     437        if (!ConfigValueContainer::finishedReadingConfigFile())
    441438            ConfigValueContainer::readConfigFile(CONFIGFILEPATH);
    442439
     
    450447        bool success = false;
    451448        std::list<std::string>::iterator it1;
    452         for(it1 = ConfigValueContainer::configFileLines_s->begin(); it1 != ConfigValueContainer::configFileLines_s->end(); ++it1)
     449        for(it1 = ConfigValueContainer::getConfigFileLines().begin(); it1 != ConfigValueContainer::getConfigFileLines().end(); ++it1)
    453450        {
    454451            // Don't try to parse comments
     
    464461                // Iterate through all lines in the section
    465462                std::list<std::string>::iterator it2;
    466                 for(it2 = ++it1; it2 != ConfigValueContainer::configFileLines_s->end(); ++it2)
     463                for(it2 = ++it1; it2 != ConfigValueContainer::getConfigFileLines().end(); ++it2)
    467464                {
    468465                    // Don't try to parse comments
     
    495492                    {
    496493                        // The next section startet, so our line isn't yet in the file - now we add it and safe the file
    497                         this->configFileLine_ = this->configFileLines_s->insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalueString_);
     494                        this->configFileLine_ = this->getConfigFileLines().insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalueString_);
    498495                        ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    499496                        success = true;
     
    515512                {
    516513                    // Looks like we found the right section, but the file ended without containing our variable - so we add it and safe the file
    517                     this->configFileLine_ = this->configFileLines_s->insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalueString_);
     514                    this->configFileLine_ = this->getConfigFileLines().insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalueString_);
    518515                    ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    519516                    success = true;
     
    527524        {
    528525            // We obviously didn't found the right section, so we'll create it
    529             this->configFileLines_s->push_back("[" + this->classname_ + "]");                   // Create the section
    530             this->configFileLines_s->push_back(this->varname_ + "=" + this->defvalueString_);   // Create the line
    531             this->configFileLine_ = --this->configFileLines_s->end();                           // Set the pointer to the last element
     526            this->getConfigFileLines().push_back("[" + this->classname_ + "]");                   // Create the section
     527            this->getConfigFileLines().push_back(this->varname_ + "=" + this->defvalueString_);   // Create the line
     528            this->configFileLine_ = --this->getConfigFileLines().end();                           // Set the pointer to the last element
    532529            success = true;
    533             this->configFileLines_s->push_back("");                                             // Add an empty line - this is needed for the algorithm in the searchConfigFileLine-function
     530            this->getConfigFileLines().push_back("");                                             // Add an empty line - this is needed for the algorithm in the searchConfigFileLine-function
    534531            ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);                              // Save the changed config-file
    535532        }
     
    601598
    602599    /**
     600        @returns a list, containing all entrys in the config-file.
     601    */
     602    std::list<std::string>& ConfigValueContainer::getConfigFileLines()
     603    {
     604        // This is done to avoid problems while executing this code before main()
     605        static std::list<std::string> configFileLinesStaticReference = std::list<std::string>();
     606        return configFileLinesStaticReference;
     607    }
     608
     609    /**
     610        @brief Returns true if the ConfigFile is read and stored into the ConfigFile-lines-list.
     611        @param finished This is used to change the state
     612        @return True if the ConfigFile is read and stored into the ConfigFile-lines-list
     613    */
     614    bool ConfigValueContainer::finishedReadingConfigFile(bool finished)
     615    {
     616        // This is done to avoid problems while executing this code before main()
     617        static bool finishedReadingConfigFileStaticVariable = false;
     618
     619        if (finished)
     620            finishedReadingConfigFileStaticVariable = true;
     621
     622        return finishedReadingConfigFileStaticVariable;
     623    }
     624
     625    /**
    603626        @brief Reads the config-file and stores the lines in a list.
    604627        @param filename The name of the config-file
     
    606629    void ConfigValueContainer::readConfigFile(const std::string& filename)
    607630    {
    608         ConfigValueContainer::readConfigFile_s = true;
    609 
    610         // Create the list if needed
    611         if (!ConfigValueContainer::configFileLines_s)
    612             ConfigValueContainer::configFileLines_s = new std::list<std::string>;
     631        ConfigValueContainer::finishedReadingConfigFile(true);
    613632
    614633        // This creates the file if it's not existing
     
    627646        {
    628647            file.getline(line, 1024);
    629             ConfigValueContainer::configFileLines_s->push_back(line);
     648            ConfigValueContainer::getConfigFileLines().push_back(line);
    630649//            std::cout << "### ->" << line << "<- : empty: " << isEmpty(line) << " comment: " << isComment(line) << std::endl;
    631650        }
    632651
    633652        // The last line is useless
    634         ConfigValueContainer::configFileLines_s->pop_back();
     653        ConfigValueContainer::getConfigFileLines().pop_back();
    635654
    636655        // Add an empty line to the end of the file if needed
    637656        // this is needed for the algorithm in the searchConfigFileLine-function
    638         if ((ConfigValueContainer::configFileLines_s->size() > 0) && !isEmpty(*ConfigValueContainer::configFileLines_s->rbegin()))
     657        if ((ConfigValueContainer::getConfigFileLines().size() > 0) && !isEmpty(*ConfigValueContainer::getConfigFileLines().rbegin()))
    639658        {
    640659//            std::cout << "### newline added" << std::endl;
    641             ConfigValueContainer::configFileLines_s->push_back("");
     660            ConfigValueContainer::getConfigFileLines().push_back("");
    642661        }
    643662
     
    652671    {
    653672        // Make sure we stored the config-file in the list
    654         if (!ConfigValueContainer::readConfigFile_s)
     673        if (!ConfigValueContainer::finishedReadingConfigFile())
    655674            ConfigValueContainer::readConfigFile(filename);
    656675
     
    661680        // Iterate through the list an write the lines into the file
    662681        std::list<std::string>::iterator it;
    663         for(it = ConfigValueContainer::configFileLines_s->begin(); it != ConfigValueContainer::configFileLines_s->end(); ++it)
     682        for(it = ConfigValueContainer::getConfigFileLines().begin(); it != ConfigValueContainer::getConfigFileLines().end(); ++it)
    664683        {
    665684            file << (*it) << std::endl;
  • code/branches/FICN/src/orxonox/core/ConfigValueContainer.h

    r682 r698  
    8686            ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::ColourValue defvalue);
    8787
     88            static std::list<std::string>& getConfigFileLines();
     89            static bool finishedReadingConfigFile(bool finished = false);
    8890            void setConfigFileEntyToDefault();
    8991            void searchConfigFileLine();
     
    143145
    144146            std::list<std::string>::iterator configFileLine_;   //!< An iterator, pointing to the entry of the variable in the config-file
    145             static std::list<std::string>* configFileLines_s;   //!< A list, containing all entrys in the config-file
    146             static bool readConfigFile_s;                       //!< True if the config-file is read and stored in the list
    147147
    148148            enum VariableType
  • code/branches/FICN/src/orxonox/core/Factory.cc

    r682 r698  
    104104    Factory* Factory::getFactoryPointer()
    105105    {
    106       static Factory theOneAndOnlyInstance = Factory();
    107       return &theOneAndOnlyInstance;
     106      static Factory theOneAndOnlyFactoryInstance = Factory();
     107      return &theOneAndOnlyFactoryInstance;
    108108    }
    109109}
  • code/branches/FICN/src/orxonox/core/Identifier.cc

    r696 r698  
    3838    // ###       Identifier        ###
    3939    // ###############################
    40     int Identifier::hierarchyCreatingCounter_s = 0; // Set the static member variable hierarchyCreatingCounter_s to zero
    41     unsigned int Identifier::classIDcounter_s = 0;  // Set the static member variable classIDcounter_s to zero
     40    int Identifier::hierarchyCreatingCounter_s = 0; // Set the static member variable hierarchyCreatingCounter_s to zero (this static member variable is ok: it's used in main(), not before)
    4241
    4342    /**
     
    5049
    5150        this->children_ = new IdentifierList;
    52         this->classID_ = Identifier::classIDcounter_s++;
     51
     52        // Use a static variable because the classID gets created before main() and that's why we should avoid static member variables
     53        static unsigned int classIDcounter_s = 0;
     54        this->classID_ = classIDcounter_s++;
    5355    }
    5456
  • code/branches/FICN/src/orxonox/core/Identifier.h

    r696 r698  
    137137                { this->configValues_[varname] = container; }
    138138
    139             std::map<std::string, Identifier*>& getIdentifierMap();
     139            static std::map<std::string, Identifier*>& getIdentifierMap();
    140140
    141141        private:
     
    171171            bool bCreatedOneObject_;                                    //!< True if at least one object of the given type was created (used to determine the need of storing the parents)
    172172            static int hierarchyCreatingCounter_s;                      //!< Bigger than zero if at least one Identifier stores its parents (its an int instead of a bool to avoid conflicts with multithreading)
    173             static unsigned int classIDcounter_s;                       //!< The number of existing Identifiers
    174173            unsigned int classID_;                                      //!< The network ID to identify a class through the network
    175174            std::map<std::string, ConfigValueContainer*> configValues_; //!< A map to link the string of configurable variables with their ConfigValueContainer
     
    197196        private:
    198197            ClassIdentifier();
    199             ClassIdentifier(const ClassIdentifier<T>& identifier) {} // don't copy
    200             ~ClassIdentifier();
     198            ClassIdentifier(const ClassIdentifier<T>& identifier) {}    // don't copy
     199            ~ClassIdentifier() {}                                       // don't delete
    201200
    202201            ObjectList<T>* objects_;    //!< The ObjectList, containing all objects of type T
     
    211210    ClassIdentifier<T>::ClassIdentifier()
    212211    {
    213         this->objects_ = new ObjectList<T>;
     212        this->objects_ = ObjectList<T>::getList();
    214213        this->bSetName_ = false;
    215     }
    216 
    217     /**
    218         @brief Destructor: Deletes the ObjectList, sets the singleton-pointer to zero.
    219     */
    220     template <class T>
    221     ClassIdentifier<T>::~ClassIdentifier()
    222     {
    223         delete this->objects_;
    224214    }
    225215
  • code/branches/FICN/src/orxonox/core/ObjectList.h

    r682 r698  
    8282    {
    8383        public:
     84            static ObjectList<T>* getList();
     85
     86            ObjectListElement<T>* add(T* object);
     87//            void remove(OrxonoxClass* object, bool bIterateForwards = true);
     88
     89            /** @returns the first element in the list */
     90            inline static Iterator<T> start()
     91                { return Iterator<T>(getList()->first_); }
     92
     93            /** @returns the last element in the list */
     94            inline static Iterator<T> end()
     95                { return Iterator<T>(getList()->last_); }
     96
     97            ObjectListElement<T>* first_;       //!< The first element in the list
     98            ObjectListElement<T>* last_;        //!< The last element in the list
     99
     100        private:
    84101            ObjectList();
    85102            ~ObjectList();
    86             ObjectListElement<T>* add(T* object);
    87 //            void remove(OrxonoxClass* object, bool bIterateForwards = true);
    88 
    89             /** @returns the first element in the list */
    90             inline static Iterator<T> start()
    91                 { return Iterator<T>(pointer_s->first_); }
    92 
    93             /** @returns the last element in the list */
    94             inline static Iterator<T> end()
    95                 { return Iterator<T>(pointer_s->last_); }
    96 
    97             ObjectListElement<T>* first_;       //!< The first element in the list
    98             ObjectListElement<T>* last_;        //!< The last element in the list
    99 
    100         private:
    101             static ObjectList<T>* pointer_s;    //!< A static pointer to the last created list (different for all T)
    102103    };
    103 
    104     template <class T>
    105     ObjectList<T>* ObjectList<T>::pointer_s = 0; // Set the static member variable pointer_s to zero
    106104
    107105    /**
     
    113111        this->first_ = 0;
    114112        this->last_ = 0;
    115 
    116         // ObjectLists are only created by Identifiers and therefore only one ObjectList of each T will exist.
    117         // Thats why pointer_s is in fact a pointer to the only ObjectList for a type, which makes it almost a singleton.
    118         this->pointer_s = this;
    119113    }
    120114
     
    132126            this->first_ = temp;
    133127        }
     128    }
     129
     130    /**
     131        @returns a pointer to the only existing instance for the given class T.
     132    */
     133    template <class T>
     134    ObjectList<T>* ObjectList<T>::getList()
     135    {
     136        static ObjectList<T> theOnlyObjectListObjectForClassT = ObjectList<T>();
     137        return &theOnlyObjectListObjectForClassT;
    134138    }
    135139
Note: See TracChangeset for help on using the changeset viewer.