Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 12, 2008, 3:34:55 PM (17 years ago)
Author:
landauf
Message:

extracted all config-value related macros from CoreIncludes.h and moved them to ConfigValueIncludes.h.

ConfigValueContainer can now handle std::vector<x> where 'x' is is any type supported by MultiTypeMath (all primitives, pointer, string, vector2, vector3, quaternion, colourvalue, radian, degree).

the vectors size is currently limited to 256 elements. this is just a practical limit, it can be raised if it's necessary. the reason for the limit is: you can add new elements to a vector by simply typing 'set classname varname index value' into the console or adding a new entry in the config-file. if 'index' is bigger than the vectors size, all elements up to 'index' are inserted. if the user accidentally enters a big number, he could end up with >4*109 elements in his config-file, resulting in 10-100gb on the hdd and a completely filled memory. and that's not exactly what i want ;)

File:
1 edited

Legend:

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

    r1023 r1030  
    3535#include "ConfigValueContainer.h"
    3636#include "Language.h"
     37#include "util/SubString.h"
     38#include "util/Convert.h"
     39
     40#define MAX_VECTOR_INDEX 255 // to avoid up to 4*10^9 vector entries in the config file after accidentally using a wrong argument
    3741
    3842
     
    4145    /**
    4246        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
    43         @param value This is only needed to determine the right type.
    44         @param classname The name of the class the variable belongs to
     47        @param type The type of the corresponding config-file
     48        @param identifier The identifier of the class the variable belongs to
    4549        @param varname The name of the variable
    4650        @param defvalue The default-value
    4751    */
    48     ConfigValueContainer::ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, MultiTypeMath defvalue)
     52    ConfigValueContainer::ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const MultiTypeMath& defvalue)
    4953    {
    5054        this->type_ = type;
     
    5559        this->value_ = defvalue;
    5660        this->bAddedDescription_ = false;
     61        this->bIsVector_ = false;
    5762
    5863        this->defvalueString_ = defvalue.toString();
    5964        this->update();
     65    }
     66
     67    /**
     68        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
     69        @param type The type of the corresponding config-file
     70        @param identifier The identifier of the class the variable belongs to
     71        @param varname The name of the variable
     72        @param defvalue The default-value
     73    */
     74    ConfigValueContainer::ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const std::vector<MultiTypeMath>& defvalue)
     75    {
     76        this->type_ = type;
     77        this->identifier_ = identifier;
     78        this->sectionname_ = identifier->getName();
     79        this->varname_ = varname;
     80
     81        this->valueVector_ = defvalue;
     82        this->bAddedDescription_ = false;
     83        this->bIsVector_ = true;
     84
     85        if (defvalue.size() > 0)
     86            this->value_ = defvalue[0];
     87
     88        for (unsigned int i = 0; i < defvalue.size(); i++)
     89            ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, i, defvalue[i].toString());
     90
     91        for (unsigned int i = 0; i < defvalue.size(); i++)
     92            this->defvalueStringVector_.push_back(defvalue[i].toString());
     93
     94        this->update();
     95    }
     96
     97    /**
     98        @brief Adds a new entry to the end of the vector.
     99        @param input The new entry
     100        @return True if the new entry was successfully added
     101    */
     102    bool ConfigValueContainer::add(const std::string& input)
     103    {
     104        if (this->bIsVector_)
     105            return this->set(this->valueVector_.size(), input);
     106
     107        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
     108        return false;
     109    }
     110
     111    /**
     112        @brief Removes an existing entry from the vector.
     113        @param index The index of the entry
     114        @return True if the entry was removed
     115    */
     116    bool ConfigValueContainer::remove(unsigned int index)
     117    {
     118        if (this->bIsVector_)
     119        {
     120            if (index < this->valueVector_.size())
     121            {
     122                this->valueVector_.erase(this->valueVector_.begin() + index);
     123                for (unsigned int i = index; i < this->valueVector_.size(); i++)
     124                    ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, i, this->valueVector_[i]);
     125                ConfigFileManager::getSingleton()->deleteVectorEntries(this->type_, this->sectionname_, this->varname_, this->valueVector_.size());
     126
     127                return true;
     128            }
     129            COUT(1) << "Error: Invalid vector-index." << std::endl;
     130        }
     131
     132        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
     133        return false;
    60134    }
    61135
     
    67141    bool ConfigValueContainer::set(const std::string& input)
    68142    {
     143        if (this->bIsVector_)
     144        {
     145            SubString token(input, " ", "", true, '"', false, '(', ')', false, '\0');
     146            int index = -1;
     147            bool success = false;
     148
     149            if (token.size() > 0)
     150                success = ConvertValue(&index, token[0]);
     151
     152            if (!success || index < 0 || index > MAX_VECTOR_INDEX)
     153            {
     154                if (!success)
     155                    COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is a vector." << std::endl;
     156                else
     157                    COUT(1) << "Error: Invalid vector-index." << std::endl;
     158                return false;
     159            }
     160
     161            if (token.size() >= 2)
     162                return this->set(index, token.subSet(1).join());
     163            else
     164                return this->set(index, "");
     165        }
     166
    69167        bool success = this->tset(input);
    70         this->setLineInConfigFile(input);
     168        ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, input);
    71169        return success;
     170    }
     171
     172    /**
     173        @brief Assigns a new value to the config-value of all objects and writes the change into the config-file.
     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::set(unsigned int index, const std::string& input)
     179    {
     180        if (this->bIsVector_)
     181        {
     182            bool success = this->tset(index, input);
     183            ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, index, input);
     184            return success;
     185        }
     186
     187        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
     188        return false;
    72189    }
    73190
     
    86203
    87204    /**
     205        @brief Assigns a new value to the config-value of all objects, but doesn't change the config-file (t stands for temporary).
     206        @param index The index in the vector
     207        @param input The new value
     208        @return True if the new value was successfully assigned
     209    */
     210    bool ConfigValueContainer::tset(unsigned int index, const std::string& input)
     211    {
     212        if (this->bIsVector_)
     213        {
     214            bool success = this->parse(index, input);
     215            if (this->identifier_)
     216                this->identifier_->updateConfigValues();
     217            return success;
     218        }
     219
     220        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
     221        return false;
     222    }
     223
     224    /**
    88225        @brief Sets the value of the variable back to the default value and resets the config-file entry.
    89226    */
    90227    bool ConfigValueContainer::reset()
    91228    {
    92         return this->set(this->defvalueString_);
     229        if (!this->bIsVector_)
     230            return this->set(this->defvalueString_);
     231        else
     232        {
     233            bool success = true;
     234            for (unsigned int i = 0; i < this->defvalueStringVector_.size(); i++)
     235                if (!this->set(i, this->defvalueStringVector_[i]))
     236                    success = false;
     237            ConfigFileManager::getSingleton()->deleteVectorEntries(this->type_, this->sectionname_, this->varname_, this->defvalueStringVector_.size());
     238            return success;
     239        }
    93240    }
    94241
     
    98245    void ConfigValueContainer::update()
    99246    {
    100         this->value_.fromString(ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, this->defvalueString_));
     247        if (!this->bIsVector_)
     248            this->value_.fromString(ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, this->defvalueString_));
     249        else
     250        {
     251            this->valueVector_.clear();
     252            for (unsigned int i = 0; i < ConfigFileManager::getSingleton()->getVectorSize(this->type_, this->sectionname_, this->varname_); i++)
     253            {
     254                this->value_.fromString(ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, i, this->defvalueStringVector_[i]));
     255                this->valueVector_.push_back(this->value_);
     256            }
     257        }
    101258    }
    102259
     
    108265    bool ConfigValueContainer::parse(const std::string& input)
    109266    {
     267        if (this->bIsVector_)
     268        {
     269            SubString token(input, " ", "", true, '"', false, '(', ')', false, '\0');
     270            int index = -1;
     271            bool success = false;
     272
     273            if (token.size() > 0)
     274                success = ConvertValue(&index, token[0]);
     275
     276            if (!success || index < 0 || index > MAX_VECTOR_INDEX)
     277                return false;
     278
     279            if (token.size() >= 2)
     280                return this->parse(index, token.subSet(1).join());
     281            else
     282                return this->parse(index, "");
     283        }
     284
    110285        MultiTypeMath temp = this->value_;
    111286        if (temp.fromString(input))
     
    114289            return true;
    115290        }
     291        return false;
     292    }
     293
     294    /**
     295        @brief Parses a given std::string into a value of the type of the associated variable and assigns it.
     296        @param index The index in the vector
     297        @param input The string to convert
     298        @return True if the string was successfully parsed
     299    */
     300    bool ConfigValueContainer::parse(unsigned int index, const std::string& input)
     301    {
     302        if (this->bIsVector_)
     303        {
     304            if (index >= this->valueVector_.size())
     305            {
     306                for (unsigned int i = this->valueVector_.size(); i <= index; i++)
     307                {
     308                    this->valueVector_.push_back(MultiTypeMath());
     309                    ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, i, this->valueVector_[i]);
     310                }
     311            }
     312
     313            MultiTypeMath temp = this->value_;
     314            if (temp.fromString(input))
     315            {
     316                this->valueVector_[index] = temp;
     317                return true;
     318            }
     319            return false;
     320        }
     321
     322        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
    116323        return false;
    117324    }
     
    125332    bool ConfigValueContainer::parse(const std::string& input, const MultiTypeMath& defvalue)
    126333    {
    127         MultiTypeMath temp = defvalue;
    128         if (temp.fromString(input))
    129         {
    130             this->value_ = temp;
     334        if (this->parse(input))
    131335            return true;
    132         }
    133         else
    134         {
    135             this->value_ = defvalue;
     336
     337        this->value_ = defvalue;
     338        return false;
     339    }
     340
     341    /**
     342        @brief Parses a given std::string into a value of the type of the associated variable and assigns it.
     343        @param index The index in the vector
     344        @param input The string to convert
     345        @param defvalue The default value to assign if the parsing fails
     346        @return True if the string was successfully parsed
     347    */
     348    bool ConfigValueContainer::parse(unsigned int index, const std::string& input, const MultiTypeMath& defvalue)
     349    {
     350        if (this->bIsVector_)
     351        {
     352            if (this->parse(index, input))
     353                return true;
     354
     355            this->valueVector_[index] = defvalue;
    136356            return false;
    137357        }
    138     }
    139 
    140     /**
    141         @brief Sets the corresponding entry in the config-file to a given value.
    142     */
    143     void ConfigValueContainer::setLineInConfigFile(const std::string& input)
    144     {
    145         ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, input);
    146     }
    147 
    148     /**
    149         @brief Sets the corresponding entry in the config-file back to the default value.
    150     */
    151     void ConfigValueContainer::resetLineInConfigFile()
    152     {
    153         this->setLineInConfigFile(this->value_.toString());
     358
     359        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
     360        return false;
    154361    }
    155362
Note: See TracChangeset for help on using the changeset viewer.