Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1321


Ignore:
Timestamp:
May 19, 2008, 2:20:56 AM (16 years ago)
Author:
landauf
Message:

changed ConfigValueContainer to use MultiTypeMath::assimilate and fixed a small bug, causing a segfault if the default-value was a vector with size zero.

Location:
code/branches/console/src/core
Files:
2 edited

Legend:

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

    r1313 r1321  
    9999
    100100    /**
     101        @brief Assigns a new value to the config-value of all objects and writes the change into the config-file.
     102        @param input The new value
     103        @return True if the new value was successfully assigned
     104    */
     105    bool ConfigValueContainer::set(const MultiTypeMath& input)
     106    {
     107        if (this->bIsVector_)
     108        {
     109            return this->callFunctionWithIndex(&ConfigValueContainer::set, input.getString());
     110        }
     111        else
     112        {
     113            if (this->tset(input))
     114            {
     115                ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, input.getString(), this->value_.isA(MT_string));
     116                return true;
     117            }
     118        }
     119        return false;
     120    }
     121
     122    /**
     123        @brief Assigns a new value to the config-value of all objects and writes the change into the config-file.
     124        @param index The index in the vector
     125        @param input The new value
     126        @return True if the new value was successfully assigned
     127    */
     128    bool ConfigValueContainer::set(unsigned int index, const MultiTypeMath& input)
     129    {
     130        if (this->bIsVector_)
     131        {
     132            if (this->tset(index, input))
     133            {
     134                ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, index, input.getString(), this->value_.isA(MT_string));
     135                return true;
     136            }
     137        }
     138        else
     139        {
     140            COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
     141        }
     142        return false;
     143    }
     144
     145    /**
     146        @brief Assigns a new value to the config-value of all objects, but doesn't change the config-file (t stands for temporary).
     147        @param input The new value
     148        @return True if the new value was successfully assigned
     149    */
     150    bool ConfigValueContainer::tset(const MultiTypeMath& input)
     151    {
     152        if (this->bIsVector_)
     153        {
     154            return this->callFunctionWithIndex(&ConfigValueContainer::tset, input.getString());
     155        }
     156        else
     157        {
     158            MultiTypeMath temp = this->value_;
     159            if (temp.assimilate(input))
     160            {
     161                this->value_ = temp;
     162
     163                if (this->identifier_)
     164                    this->identifier_->updateConfigValues();
     165
     166                return true;
     167            }
     168        }
     169        return false;
     170    }
     171
     172    /**
     173        @brief Assigns a new value to the config-value of all objects, but doesn't change the config-file (t stands for temporary).
     174        @param index The index in the vector
     175        @param input The new value
     176        @return True if the new value was successfully assigned
     177    */
     178    bool ConfigValueContainer::tset(unsigned int index, const MultiTypeMath& input)
     179    {
     180        if (this->bIsVector_)
     181        {
     182            if (index > MAX_VECTOR_INDEX)
     183            {
     184                COUT(1) << "Error: Index " << index << " is too large." << std::endl;
     185                return false;
     186            }
     187
     188            if (index >= this->valueVector_.size())
     189            {
     190                for (unsigned int i = this->valueVector_.size(); i <= index; i++)
     191                {
     192                    this->valueVector_.push_back(MultiTypeMath());
     193                    ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, i, this->valueVector_[i], this->value_.isA(MT_string));
     194                }
     195            }
     196
     197            MultiTypeMath temp = this->valueVector_[index];
     198            if (temp.assimilate(input))
     199            {
     200                this->valueVector_[index] = temp;
     201
     202                if (this->identifier_)
     203                    this->identifier_->updateConfigValues();
     204
     205                return true;
     206            }
     207        }
     208        else
     209        {
     210            COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
     211        }
     212        return false;
     213    }
     214
     215    /**
    101216        @brief Adds a new entry to the end of the vector.
    102217        @param input The new entry
    103218        @return True if the new entry was successfully added
    104219    */
    105     bool ConfigValueContainer::add(const std::string& input)
     220    bool ConfigValueContainer::add(const MultiTypeMath& input)
    106221    {
    107222        if (this->bIsVector_)
     
    132247            }
    133248            COUT(1) << "Error: Invalid vector-index." << std::endl;
    134         }
    135 
    136         COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
    137         return false;
    138     }
    139 
    140     /**
    141         @brief Assigns a new value to the config-value of all objects and writes the change into the config-file.
    142         @param input The new value
    143         @return True if the new value was successfully assigned
    144     */
    145     bool ConfigValueContainer::set(const std::string& input)
    146     {
    147         if (this->bIsVector_)
    148         {
    149             SubString token(input, " ", "", true, '"', false, '(', ')', false, '\0');
    150             int index = -1;
    151             bool success = false;
    152 
    153             if (token.size() > 0)
    154                 success = ConvertValue(&index, token[0]);
    155 
    156             if (!success || index < 0 || index > MAX_VECTOR_INDEX)
    157             {
    158                 if (!success)
    159                 {
    160                     COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is a vector." << std::endl;
    161                 }
    162                 else
    163                 {
    164                     COUT(1) << "Error: Invalid vector-index." << std::endl;
    165                 }
    166                 return false;
    167             }
    168 
    169             if (token.size() >= 2)
    170                 return this->set(index, token.subSet(1).join());
    171             else
    172                 return this->set(index, "");
    173         }
    174 
    175         bool success = this->tset(input);
    176         ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, input, this->value_.isA(MT_string));
    177         return success;
    178     }
    179 
    180     /**
    181         @brief Assigns a new value to the config-value of all objects and writes the change into the config-file.
    182         @param index The index in the vector
    183         @param input The new value
    184         @return True if the new value was successfully assigned
    185     */
    186     bool ConfigValueContainer::set(unsigned int index, const std::string& input)
    187     {
    188         if (this->bIsVector_)
    189         {
    190             bool success = this->tset(index, input);
    191             ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, index, input, this->value_.isA(MT_string));
    192             return success;
    193         }
    194 
    195         COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
    196         return false;
    197     }
    198 
    199     /**
    200         @brief Assigns a new value to the config-value of all objects, but doesn't change the config-file (t stands for temporary).
    201         @param input The new value
    202         @return True if the new value was successfully assigned
    203     */
    204     bool ConfigValueContainer::tset(const std::string& input)
    205     {
    206         bool success = this->parse(input);
    207         if (this->identifier_)
    208             this->identifier_->updateConfigValues();
    209         return success;
    210     }
    211 
    212     /**
    213         @brief Assigns a new value to the config-value of all objects, but doesn't change the config-file (t stands for temporary).
    214         @param index The index in the vector
    215         @param input The new value
    216         @return True if the new value was successfully assigned
    217     */
    218     bool ConfigValueContainer::tset(unsigned int index, const std::string& input)
    219     {
    220         if (this->bIsVector_)
    221         {
    222             bool success = this->parse(index, input);
    223             if (this->identifier_)
    224                 this->identifier_->updateConfigValues();
    225             return success;
    226249        }
    227250
     
    260283            for (unsigned int i = 0; i < ConfigFileManager::getSingleton()->getVectorSize(this->type_, this->sectionname_, this->varname_); i++)
    261284            {
    262                 this->value_.fromString(ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, i, this->defvalueStringVector_[i], this->value_.isA(MT_string)));
     285                if (i < this->defvalueStringVector_.size())
     286                    this->value_.fromString(ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, i, this->defvalueStringVector_[i], this->value_.isA(MT_string)));
     287                else
     288                    this->value_.fromString(ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, i, MultiTypeMath(), this->value_.isA(MT_string)));
     289
    263290                this->valueVector_.push_back(this->value_);
    264291            }
     
    267294
    268295    /**
    269         @brief Parses a given std::string into a value of the type of the associated variable and assigns it.
    270         @param input The string to convert
    271         @return True if the string was successfully parsed
    272     */
    273     bool ConfigValueContainer::parse(const std::string& input)
    274     {
    275         if (this->bIsVector_)
    276         {
    277             SubString token(input, " ", "", true, '"', false, '(', ')', false, '\0');
    278             int index = -1;
    279             bool success = false;
    280 
    281             if (token.size() > 0)
    282                 success = ConvertValue(&index, token[0]);
    283 
    284             if (!success || index < 0 || index > MAX_VECTOR_INDEX)
    285             {
    286                 if (!success)
    287                 {
    288                     COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is a vector." << std::endl;
    289                 }
    290                 else
    291                 {
    292                     COUT(1) << "Error: Invalid vector-index." << std::endl;
    293                 }
    294                 return false;
    295             }
    296 
    297             if (token.size() >= 2)
    298                 return this->parse(index, token.subSet(1).join());
     296        @brief Calls the given function with parsed index and the parsed argument from the input string.
     297        @param function The function to call
     298        @param input The input string
     299        @return The returnvalue of the functioncall
     300    */
     301    bool ConfigValueContainer::callFunctionWithIndex(bool (ConfigValueContainer::* function) (unsigned int, const MultiTypeMath&), const std::string& input)
     302    {
     303        SubString token(input, " ", SubString::WhiteSpaces, true, '\\', false, '"', false, '(', ')', false, '\0');
     304        int index = -1;
     305        bool success = false;
     306
     307        if (token.size() > 0)
     308            success = ConvertValue(&index, token[0]);
     309
     310        if (!success || index < 0 || index > MAX_VECTOR_INDEX)
     311        {
     312            if (!success)
     313            {
     314                COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is a vector." << std::endl;
     315            }
    299316            else
    300                 return this->parse(index, "");
    301         }
    302 
    303         MultiTypeMath temp = this->value_;
    304         if (temp.fromString(input))
    305         {
    306             this->value_ = temp;
    307             return true;
    308         }
    309         return false;
    310     }
    311 
    312     /**
    313         @brief Parses a given std::string into a value of the type of the associated variable and assigns it.
    314         @param index The index in the vector
    315         @param input The string to convert
    316         @return True if the string was successfully parsed
    317     */
    318     bool ConfigValueContainer::parse(unsigned int index, const std::string& input)
    319     {
    320         if (this->bIsVector_)
    321         {
    322             if (index >= this->valueVector_.size())
    323             {
    324                 for (unsigned int i = this->valueVector_.size(); i <= index; i++)
    325                 {
    326                     this->valueVector_.push_back(MultiTypeMath());
    327                     ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, i, this->valueVector_[i], this->value_.isA(MT_string));
    328                 }
    329             }
    330 
    331             MultiTypeMath temp = this->value_;
    332             if (temp.fromString(input))
    333             {
    334                 this->valueVector_[index] = temp;
    335                 return true;
     317            {
     318                COUT(1) << "Error: Invalid vector-index." << std::endl;
    336319            }
    337320            return false;
    338321        }
    339322
    340         COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
    341         return false;
    342     }
    343 
    344     /**
    345         @brief Parses a given std::string into a value of the type of the associated variable and assigns it.
    346         @param input The string to convert
    347         @param defvalue The default value to assign if the parsing fails
    348         @return True if the string was successfully parsed
    349     */
    350     bool ConfigValueContainer::parse(const std::string& input, const MultiTypeMath& defvalue)
    351     {
    352         if (this->parse(input))
    353             return true;
    354 
    355         this->value_ = defvalue;
    356         return false;
    357     }
    358 
    359     /**
    360         @brief Parses a given std::string into a value of the type of the associated variable and assigns it.
    361         @param index The index in the vector
    362         @param input The string to convert
    363         @param defvalue The default value to assign if the parsing fails
    364         @return True if the string was successfully parsed
    365     */
    366     bool ConfigValueContainer::parse(unsigned int index, const std::string& input, const MultiTypeMath& defvalue)
    367     {
    368         if (this->bIsVector_)
    369         {
    370             if (this->parse(index, input))
    371                 return true;
    372 
    373             this->valueVector_[index] = defvalue;
    374             return false;
    375         }
    376 
    377         COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
    378         return false;
     323        if (token.size() >= 2)
     324            return (this->*function)(index, token.subSet(1).join());
     325        else
     326            return (this->*function)(index, "");
    379327    }
    380328
  • code/branches/console/src/core/ConfigValueContainer.h

    r1313 r1321  
    100100            const std::string& getDescription() const;
    101101
    102             bool add(const std::string& input);
     102            bool set(const MultiTypeMath& input);
     103            bool tset(const MultiTypeMath& input);
     104
     105            bool set(unsigned int index, const MultiTypeMath& input);
     106            bool tset(unsigned int index, const MultiTypeMath& input);
     107            bool add(const MultiTypeMath& input);
    103108            bool remove(unsigned int index);
    104             bool set(const std::string& input);
    105             bool tset(const std::string& input);
     109
    106110            bool reset();
    107111            void update();
    108 
    109             bool parse(const std::string& input);
    110             bool parse(const std::string& input, const MultiTypeMath& defvalue);
    111 
    112             bool set(unsigned int index, const std::string& input);
    113             bool tset(unsigned int index, const std::string& input);
    114             bool parse(unsigned int index, const std::string& input);
    115             bool parse(unsigned int index, const std::string& input, const MultiTypeMath& defvalue);
    116112
    117113            /** @brief Converts the config-value to a string. @return The string */
     
    123119
    124120        private:
     121            bool callFunctionWithIndex(bool (ConfigValueContainer::* function) (unsigned int, const MultiTypeMath&), const std::string& input);
     122
    125123            bool                       bIsVector_;                  //!< True if the container contains a std::vector
    126124
Note: See TracChangeset for help on using the changeset viewer.