Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 721


Ignore:
Timestamp:
Dec 29, 2007, 3:14:39 AM (16 years ago)
Author:
landauf
Message:

added Convert.h (greetings to bensch) and changed the corresponding lines in the ConfigValueContainer.

Location:
code/branches/FICN/src
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/FICN/src/misc/String2Number.h

    r714 r721  
    4949};
    5050
    51 
    52 /**
    53     @brief Converts a number of any type to a string.
    54     @param variable The variable, containing the number.
    55 
    56     @example
    57     float f = 3.14;
    58     std::string output = number2String(f);
    59 */
    60 template <typename T>
    61 const std::string number2String(T variable)
    62 {
    63     std::ostringstream oss;
    64     if (oss << variable)
    65         return std::string(oss.str());
    66 
    67     return std::string("");
    68 }
    69 
    70 /**
    71     @brief Converts a number of any type to a string and assigns a defaultvalue if the conversion fails.
    72     @param variable The variable, containing the number.
    73     @param fallbackString The assigned string if the conversion fails.
    74 
    75     @example
    76     float f = 3.14;
    77     std::string output = number2String(f, "0.000000");
    78 */
    79 template <typename T>
    80 const std::string number2String(T variable, const std::string& fallbackString)
    81 {
    82     std::ostringstream oss;
    83     if (oss << variable)
    84         return std::string(oss.str());
    85 
    86     return fallbackString;
    87 }
    88 
    89 /**
    90     @brief Converts a string to a number of any type.
    91     @param variable The variable wherein the value gets written
    92     @return True if the conversion was successful
    93 
    94     @example
    95     float f;
    96     bool success = string2Number(f, "3.14");
    97 */
    98 template <typename T>
    99 bool string2Number(T& variable, const std::string& valueString)
    100 {
    101     std::istringstream iss(valueString);
    102     if (iss >> variable)
    103         return true;
    104 
    105     return false;
    106 }
    107 
    108 /**
    109     @brief Converts a string to a number of any type.
    110     @param variable The variable wherein the value gets written
    111     @param fallbackValue The assigned value if the conversion fails
    112     @return True if the conversion was successful
    113 
    114     @example
    115     float f;
    116     bool success = string2Number(f, "3.14", 0.000000);
    117 */
    118 template <typename T>
    119 bool string2Number(T& variable, const std::string& valueString, T fallbackValue)
    120 {
    121     std::istringstream iss(valueString);
    122     if (iss >> variable)
    123         return true;
    124 
    125     variable = fallbackValue;
    126     return false;
    127 }
    128 
    12951#endif /* _String2Number_H__ */
  • code/branches/FICN/src/orxonox/core/ConfigValueContainer.cc

    r717 r721  
    2929
    3030#include "misc/Tokenizer.h"
    31 #include "misc/String2Number.h"
     31#include "misc/Convert.h"
    3232#include "ConfigValueContainer.h"
    3333
     
    5050        this->type_ = VT_Int;
    5151
    52         this->defvalueString_ = number2String(defvalue, "0");                       // Try to convert the default-value to a string
     52        Convert::ToString(this->defvalueString_, defvalue, "0");                    // Try to convert the default-value to a string
    5353        this->searchConfigFileLine();                                               // Search the entry in the config-file
    5454
     
    7272        this->type_ = VT_uInt;
    7373
    74         this->defvalueString_ = number2String(defvalue, "0");                       // Try to convert the default-value to a string
     74        Convert::ToString(this->defvalueString_, defvalue, "0");                    // Try to convert the default-value to a string
    7575        this->searchConfigFileLine();                                               // Search the entry in the config-file
    7676
     
    9494        this->type_ = VT_Char;
    9595
    96         this->defvalueString_ = number2String((int)defvalue, "0");                  // Try to convert the default-value to a string
     96        Convert::ToString(this->defvalueString_, (int)defvalue, "0");               // Try to convert the default-value to a string
    9797        this->searchConfigFileLine();                                               // Search the entry in the config-file
    9898
     
    116116        this->type_ = VT_uChar;
    117117
    118         this->defvalueString_ = number2String((unsigned int)defvalue, "0");         // Try to convert the default-value to a string
     118        Convert::ToString(this->defvalueString_, (unsigned int)defvalue, "0");      // Try to convert the default-value to a string
    119119        this->searchConfigFileLine();                                               // Search the entry in the config-file
    120120
     
    138138        this->type_ = VT_Float;
    139139
    140         this->defvalueString_ = number2String(defvalue, "0.000000");                // Try to convert the default-value to a string
     140        Convert::ToString(this->defvalueString_, defvalue, "0.000000");             // Try to convert the default-value to a string
    141141        this->searchConfigFileLine();                                               // Search the entry in the config-file
    142142
     
    160160        this->type_ = VT_Double;
    161161
    162         this->defvalueString_ = number2String(defvalue, "0.000000");                // Try to convert the default-value to a string
     162        Convert::ToString(this->defvalueString_, defvalue, "0.000000");             // Try to convert the default-value to a string
    163163        this->searchConfigFileLine();                                               // Search the entry in the config-file
    164164
     
    182182        this->type_ = VT_LongDouble;
    183183
    184         this->defvalueString_ = number2String(defvalue, "0.000000");                // Try to convert the default-value to a string
     184        Convert::ToString(this->defvalueString_, defvalue, "0.000000");             // Try to convert the default-value to a string
    185185        this->searchConfigFileLine();                                               // Search the entry in the config-file
    186186
     
    384384    bool ConfigValueContainer::parseSting(const std::string& input, int defvalue)
    385385    {
    386         return string2Number(this->value_.value_int_, input, defvalue);
     386        return Convert::FromString(this->value_.value_int_, input, defvalue);
    387387    }
    388388
     
    395395    bool ConfigValueContainer::parseSting(const std::string& input, unsigned int defvalue)
    396396    {
    397         return string2Number(this->value_.value_uint_, input, defvalue);
     397        return Convert::FromString(this->value_.value_uint_, input, defvalue);
    398398    }
    399399
     
    407407    {
    408408        // I used value_int_ instead of value_char_ to avoid number <-> char confusion in the config-file
    409         return string2Number(this->value_.value_int_, input, (int)defvalue);
     409        return Convert::FromString(this->value_.value_int_, input, (int)defvalue);
    410410    }
    411411
     
    419419    {
    420420        // I used value_uint_ instead of value_uchar_ to avoid number <-> char confusion in the config-file
    421         return string2Number(this->value_.value_uint_, input, (unsigned int)defvalue);
     421        return Convert::FromString(this->value_.value_uint_, input, (unsigned int)defvalue);
    422422    }
    423423
     
    430430    bool ConfigValueContainer::parseSting(const std::string& input, float defvalue)
    431431    {
    432         return string2Number(this->value_.value_float_, input, defvalue);
     432        return Convert::FromString(this->value_.value_float_, input, defvalue);
    433433    }
    434434
     
    441441    bool ConfigValueContainer::parseSting(const std::string& input, double defvalue)
    442442    {
    443         return string2Number(this->value_.value_double_, input, defvalue);
     443        return Convert::FromString(this->value_.value_double_, input, defvalue);
    444444    }
    445445
     
    452452    bool ConfigValueContainer::parseSting(const std::string& input, long double defvalue)
    453453    {
    454         return string2Number(this->value_.value_long_double_, input, defvalue);
     454        return Convert::FromString(this->value_.value_long_double_, input, defvalue);
    455455    }
    456456
     
    477477        {
    478478            // Its not a known word - is it a number?
    479             return string2Number(this->value_.value_bool_, input, defvalue);
     479            return Convert::FromString(this->value_.value_bool_, input, defvalue);
    480480        }
    481481
     
    549549        {
    550550            std::vector<std::string> tokens = tokenize(input.substr(pos1, pos2 - pos1), ",");
    551             if (!string2Number(this->value_vector2_.x, tokens[0], defvalue.x))
     551            if (!Convert::FromString(this->value_vector2_.x, tokens[0], defvalue.x))
    552552            {
    553553                this->value_vector2_ = defvalue;
    554554                return false;
    555555            }
    556             if (!string2Number(this->value_vector2_.y, tokens[1], defvalue.y))
     556            if (!Convert::FromString(this->value_vector2_.y, tokens[1], defvalue.y))
    557557            {
    558558                this->value_vector2_ = defvalue;
     
    583583        {
    584584            std::vector<std::string> tokens = tokenize(input.substr(pos1, pos2 - pos1), ",");
    585             if (!string2Number(this->value_vector3_.x, tokens[0], defvalue.x))
     585            if (!Convert::FromString(this->value_vector3_.x, tokens[0], defvalue.x))
    586586            {
    587587                this->value_vector3_ = defvalue;
    588588                return false;
    589589            }
    590             if (!string2Number(this->value_vector3_.y, tokens[1], defvalue.y))
     590            if (!Convert::FromString(this->value_vector3_.y, tokens[1], defvalue.y))
    591591            {
    592592                this->value_vector3_ = defvalue;
    593593                return false;
    594594            }
    595             if (!string2Number(this->value_vector3_.z, tokens[2], defvalue.z))
     595            if (!Convert::FromString(this->value_vector3_.z, tokens[2], defvalue.z))
    596596            {
    597597                this->value_vector3_ = defvalue;
     
    622622        {
    623623            std::vector<std::string> tokens = tokenize(input.substr(pos1, pos2 - pos1), ",");
    624             if (!string2Number(this->value_colourvalue_.r, tokens[0], defvalue.r))
     624            if (!Convert::FromString(this->value_colourvalue_.r, tokens[0], defvalue.r))
    625625            {
    626626                this->value_colourvalue_ = defvalue;
    627627                return false;
    628628            }
    629             if (!string2Number(this->value_colourvalue_.g, tokens[1], defvalue.g))
     629            if (!Convert::FromString(this->value_colourvalue_.g, tokens[1], defvalue.g))
    630630            {
    631631                this->value_colourvalue_ = defvalue;
    632632                return false;
    633633            }
    634             if (!string2Number(this->value_colourvalue_.b, tokens[2], defvalue.b))
     634            if (!Convert::FromString(this->value_colourvalue_.b, tokens[2], defvalue.b))
    635635            {
    636636                this->value_colourvalue_ = defvalue;
    637637                return false;
    638638            }
    639             if (!string2Number(this->value_colourvalue_.a, tokens[3], defvalue.a))
     639            if (!Convert::FromString(this->value_colourvalue_.a, tokens[3], defvalue.a))
    640640            {
    641641                this->value_colourvalue_ = defvalue;
Note: See TracChangeset for help on using the changeset viewer.