Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 726


Ignore:
Timestamp:
Dec 30, 2007, 1:50:02 AM (16 years ago)
Author:
landauf
Message:

adopted benschs Convert class and used it in the ConfigValueContainer

Location:
code/branches/FICN/src
Files:
3 edited

Legend:

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

    r724 r726  
    2020 *
    2121 *   Author:
     22 *      Benjamin Grauer
     23 *   Co-authors:
    2224 *      Fabian 'x3n' Landau
    23  *   Co-authors:
    24  *      ...
    25  *
    2625 */
    2726
     
    3736#include <sstream>
    3837
    39 //! The Convert class has some static member functions to convert strings to values and values to strings.
    40 class Convert
     38
     39// DEFAULT CLASS
     40template <typename FromType, typename ToType>
     41class Converter
    4142{
    42   public:
    43     /**
    44         @brief Converts a value of any type to a string.
    45         @param output The string to write the result in
    46         @param input The variable to convert
    47         @return True if the conversion succeded
    48 
    49         @example
    50         float f = 3.14;
    51         std::string output;
    52         bool success = Convert::ToString(output, f);
    53     */
    54     template <typename T>
    55     static bool ToString(std::string* output, T input)
    56     {
    57         std::ostringstream oss;
    58         if (oss << input)
    59         {
    60             (*output) = oss.str();
    61             return true;
    62         }
    63 
    64         return false;
    65     }
    66 
    67     /**
    68         @brief Converts a value of any type to a string and assigns a defaultvalue if the conversion fails.
    69         @param output The string to write the result in
    70         @param input The variable to convert
    71         @param fallbackString The assigned string if the conversion fails.
    72         @return True if the conversion succeeded
    73 
    74         @example
    75         float f = 3.14;
    76         std::string output;
    77         bool success = Convert::ToString(output, f, "0.000000");
    78     */
    79     template <typename T>
    80     static bool ToString(std::string* output, T input, const std::string& fallbackString)
    81     {
    82         if (Convert::ToString(output, input))
    83             return true;
    84 
    85         (*output) = fallbackString;
    86         return false;
    87     }
    88 
    89     /**
    90         @brief Converts a string to a value of any type.
    91         @param output The variable to assign the result to
    92         @param input The string to convert
    93         @return True if the conversion succeeded
    94 
    95         @example
    96         std::string input = "3.14";
    97         float f;
    98         bool success = string2Number(f, input);
    99     */
    100     template <typename T>
    101     static bool FromString(T* output, const std::string& input)
    102     {
    103         std::istringstream iss(input);
    104         if (iss >> (*output))
    105             return true;
    106 
    107         return false;
    108     }
    109 
    110     /**
    111         @brief Converts a string to a value of any type.
    112         @param output The variable to assign the result to
    113         @param input The string to convert
    114         @param fallbackValue The assigned value if the conversion fails
    115         @return True if the conversion succeeded
    116 
    117         @example
    118         std::string input = "3.14";
    119         float f;
    120         bool success = string2Number(f, input, 0.000000);
    121     */
    122     template <typename T>
    123     static bool FromString(T* output, const std::string& input, T fallbackValue)
    124     {
    125         if (Convert::FromString(output, input))
    126             return true;
    127 
    128         (*output) = fallbackValue;
    129         return false;
    130     }
     43 public:
     44  bool operator()(ToType* output, const FromType& input) const
     45  {
     46    return false;
     47  }
    13148};
    13249
     50// PARTIAL SPECIALIZATION TO CONVERT TO STRINGS
     51template<typename FromType>
     52class Converter<FromType, std::string>
     53{
     54 public:
     55  bool operator()(std::string* output, const FromType& input) const
     56  {
     57    std::ostringstream oss;
     58    if (oss << input)
     59    {
     60      (*output) = oss.str();
     61      return true;
     62    }
     63    else
     64      return false;
     65  }
     66};
     67
     68// PARTIAL SPECIALIZATION TO CONVERT FROM STRING
     69template<typename ToType>
     70class Converter<std::string, ToType>
     71{
     72 public:
     73  bool operator()(ToType* output, const std::string& input) const
     74  {
     75    std::istringstream iss(input);
     76    if (iss >> (*output))
     77      return true;
     78    else
     79      return false;
     80  }
     81};
     82
     83// FUNCTION SO WE DO NOT HAVE TO TELL THE COMPILER ABOUT THE TYPE
     84template<typename FromType, typename ToType>
     85static bool ConvertValue(ToType* output, const FromType& input)
     86{
     87  Converter<FromType, ToType> converter;
     88  return converter(output, input);
     89}
     90
     91// THE SAME, BUT WITH DEFAULT VALUE
     92template<typename FromType, typename ToType>
     93static bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback)
     94{
     95  Converter<FromType, ToType> converter;
     96  if (converter(output, input))
     97    return true;
     98
     99  (*output) = fallback;
     100  return false;
     101}
     102
    133103#endif /* _Convert_H__ */
  • code/branches/FICN/src/orxonox/core/ConfigValueContainer.cc

    r725 r726  
    5050        this->type_ = VT_Int;
    5151
    52         Convert::ToString(&this->defvalueString_, defvalue, "0");                   // Try to convert the default-value to a string
     52        ConvertValue(&this->defvalueString_, defvalue, std::string("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         Convert::ToString(&this->defvalueString_, defvalue, "0");                   // Try to convert the default-value to a string
     74        ConvertValue(&this->defvalueString_, defvalue, std::string("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         Convert::ToString(&this->defvalueString_, (int)defvalue, "0");              // Try to convert the default-value to a string
     96        ConvertValue(&this->defvalueString_, (int)defvalue, std::string("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         Convert::ToString(&this->defvalueString_, (unsigned int)defvalue, "0");     // Try to convert the default-value to a string
     118        ConvertValue(&this->defvalueString_, (unsigned int)defvalue, std::string("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         Convert::ToString(&this->defvalueString_, defvalue, "0.000000");            // Try to convert the default-value to a string
     140        ConvertValue(&this->defvalueString_, defvalue, std::string("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         Convert::ToString(&this->defvalueString_, defvalue, "0.000000");            // Try to convert the default-value to a string
     162        ConvertValue(&this->defvalueString_, defvalue, std::string("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         Convert::ToString(&this->defvalueString_, defvalue, "0.000000");            // Try to convert the default-value to a string
     184        ConvertValue(&this->defvalueString_, defvalue, std::string("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 Convert::FromString(&this->value_.value_int_, input, defvalue);
     386        return ConvertValue(&this->value_.value_int_, input, defvalue);
    387387    }
    388388
     
    395395    bool ConfigValueContainer::parseSting(const std::string& input, unsigned int defvalue)
    396396    {
    397         return Convert::FromString(&this->value_.value_uint_, input, defvalue);
     397        return ConvertValue(&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 Convert::FromString(&this->value_.value_int_, input, (int)defvalue);
     409        return ConvertValue(&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 Convert::FromString(&this->value_.value_uint_, input, (unsigned int)defvalue);
     421        return ConvertValue(&this->value_.value_uint_, input, (unsigned int)defvalue);
    422422    }
    423423
     
    430430    bool ConfigValueContainer::parseSting(const std::string& input, float defvalue)
    431431    {
    432         return Convert::FromString(&this->value_.value_float_, input, defvalue);
     432        return ConvertValue(&this->value_.value_float_, input, defvalue);
    433433    }
    434434
     
    441441    bool ConfigValueContainer::parseSting(const std::string& input, double defvalue)
    442442    {
    443         return Convert::FromString(&this->value_.value_double_, input, defvalue);
     443        return ConvertValue(&this->value_.value_double_, input, defvalue);
    444444    }
    445445
     
    452452    bool ConfigValueContainer::parseSting(const std::string& input, long double defvalue)
    453453    {
    454         return Convert::FromString(&this->value_.value_long_double_, input, defvalue);
     454        return ConvertValue(&this->value_.value_long_double_, input, defvalue);
    455455    }
    456456
     
    477477        {
    478478            // Its not a known word - is it a number?
    479             return Convert::FromString(&this->value_.value_bool_, input, defvalue);
     479            return ConvertValue(&this->value_.value_bool_, input, defvalue);
    480480        }
    481481
     
    549549        {
    550550            std::vector<std::string> tokens = tokenize(input.substr(pos1, pos2 - pos1), ",");
    551             if (!Convert::FromString(&this->value_vector2_.x, tokens[0], defvalue.x))
     551            if (!ConvertValue(&this->value_vector2_.x, tokens[0]))
    552552            {
    553553                this->value_vector2_ = defvalue;
    554554                return false;
    555555            }
    556             if (!Convert::FromString(&this->value_vector2_.y, tokens[1], defvalue.y))
     556            if (!ConvertValue(&this->value_vector2_.y, tokens[1]))
    557557            {
    558558                this->value_vector2_ = defvalue;
     
    583583        {
    584584            std::vector<std::string> tokens = tokenize(input.substr(pos1, pos2 - pos1), ",");
    585             if (!Convert::FromString(&this->value_vector3_.x, tokens[0], defvalue.x))
     585            if (!ConvertValue(&this->value_vector3_.x, tokens[0]))
    586586            {
    587587                this->value_vector3_ = defvalue;
    588588                return false;
    589589            }
    590             if (!Convert::FromString(&this->value_vector3_.y, tokens[1], defvalue.y))
     590            if (!ConvertValue(&this->value_vector3_.y, tokens[1]))
    591591            {
    592592                this->value_vector3_ = defvalue;
    593593                return false;
    594594            }
    595             if (!Convert::FromString(&this->value_vector3_.z, tokens[2], defvalue.z))
     595            if (!ConvertValue(&this->value_vector3_.z, tokens[2]))
    596596            {
    597597                this->value_vector3_ = defvalue;
     
    622622        {
    623623            std::vector<std::string> tokens = tokenize(input.substr(pos1, pos2 - pos1), ",");
    624             if (!Convert::FromString(&this->value_colourvalue_.r, tokens[0], defvalue.r))
     624            if (!ConvertValue(&this->value_colourvalue_.r, tokens[0]))
    625625            {
    626626                this->value_colourvalue_ = defvalue;
    627627                return false;
    628628            }
    629             if (!Convert::FromString(&this->value_colourvalue_.g, tokens[1], defvalue.g))
     629            if (!ConvertValue(&this->value_colourvalue_.g, tokens[1]))
    630630            {
    631631                this->value_colourvalue_ = defvalue;
    632632                return false;
    633633            }
    634             if (!Convert::FromString(&this->value_colourvalue_.b, tokens[2], defvalue.b))
     634            if (!ConvertValue(&this->value_colourvalue_.b, tokens[2]))
    635635            {
    636636                this->value_colourvalue_ = defvalue;
    637637                return false;
    638638            }
    639             if (!Convert::FromString(&this->value_colourvalue_.a, tokens[3], defvalue.a))
     639            if (!ConvertValue(&this->value_colourvalue_.a, tokens[3]))
    640640            {
    641641                this->value_colourvalue_ = defvalue;
  • code/branches/FICN/src/orxonox/core/ConfigValueContainer.h

    r717 r726  
    124124            inline ConfigValueContainer& getValue(const char* value)                    { value = this->value_string_.c_str(); return *this; }
    125125            /** @returns the value. @param value This is only needed to determine the right type. */
    126             inline ConfigValueContainer& getValue(Vector2& value)                 { value = this->value_vector2_; return *this; }
     126            inline ConfigValueContainer& getValue(Vector2& value)                       { value = this->value_vector2_; return *this; }
    127127            /** @returns the value. @param value This is only needed to determine the right type. */
    128             inline ConfigValueContainer& getValue(Vector3& value)                 { value = this->value_vector3_; return *this; }
     128            inline ConfigValueContainer& getValue(Vector3& value)                       { value = this->value_vector3_; return *this; }
    129129            /** @returns the value. @param value This is only needed to determine the right type. */
    130             inline ConfigValueContainer& getValue(ColourValue& value)             { value = this->value_colourvalue_; return *this; }
     130            inline ConfigValueContainer& getValue(ColourValue& value)                   { value = this->value_colourvalue_; return *this; }
    131131
    132132            void description(const std::string& description);
Note: See TracChangeset for help on using the changeset viewer.