Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 12, 2008, 10:42:15 PM (16 years ago)
Author:
rgrieder
Message:
  • removed conversion functions in String.h
  • you can use convertToString and convertFromString from Convert.h as shortcuts
  • added "true", "false", "on", "off", "yes" and "no" as boolean values to the Converter
  • Conversion bool —> string newly gives "true" or "false"
  • HUDOverlay can deal with the aspect ratio, e.g. circles stay circles when resizing
  • the rest isn't yet finished: svn save ;)
Location:
code/branches/hud/src/util
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/hud/src/util/Convert.h

    r1588 r1595  
    4343#include "SubString.h"
    4444#include "MultiTypeMath.h"
     45#include "String.h"
    4546
    4647// disable annoying warning about forcing value to boolean
     
    320321    }
    321322};
     323
     324// convert from string Shortcut
     325template <class ToType>
     326ToType convertFromString(std::string str)
     327{
     328  return getConvertedValue<std::string, ToType>(str);
     329}
    322330
    323331
     
    411419////////////////////
    412420
     421// bool to std::string
     422template <>
     423struct ConverterSpecialized<bool, std::string, _Explicit_>
     424{
     425    enum { specialized = true };
     426    static bool convert(std::string* output, const bool& input)
     427    {
     428        if (input)
     429          *output = "true";
     430        else
     431          *output = "false";
     432        return false;
     433    }
     434};
     435
    413436// Vector2 to std::string
    414437template <>
     
    501524////////////////////
    502525
     526// std::string to bool
     527template <>
     528struct ConverterSpecialized<std::string, bool, _Explicit_>
     529{
     530    enum { specialized = true };
     531    static bool convert(bool* output, const std::string& input)
     532    {
     533        std::string stripped = getLowercase(removeTrailingWhitespaces(input));
     534        if (stripped == "true" || stripped == "on" || stripped == "yes")
     535        {
     536          *output = true;
     537          return true;
     538        }
     539        else if (stripped == "false" || stripped == "off" || stripped == "no")
     540        {
     541          *output = false;
     542          return true;
     543        }
     544
     545        std::istringstream iss(input);
     546        if (iss >> (*output))
     547            return true;
     548        else
     549            return false;
     550    }
     551};
     552
    503553// std::string to Vector2
    504554template <>
  • code/branches/hud/src/util/String.h

    r1505 r1595  
    7070_UtilExport unsigned int getNextCommentPosition(const std::string& str, unsigned int start = 0);
    7171
    72 //! The Convert class has some static member functions to convert strings to values and values to strings.
    73 class _UtilExport Convert
    74 {
    75     public:
    76         /**
    77             @brief Converts a value of any type to a string.
    78             @param output The string to write the result in
    79             @param input The variable to convert
    80             @return True if the conversion succeded
    81 
    82             @example
    83             float f = 3.14;
    84             std::string output;
    85             bool success = Convert::ToString(&output, f);
    86         */
    87         template <typename T>
    88         static bool ToString(std::string* output, T input)
    89         {
    90             std::ostringstream oss;
    91             if (oss << input)
    92             {
    93                 (*output) = oss.str();
    94                 return true;
    95             }
    96 
    97             return false;
    98         }
    99 
    100         /**
    101             @brief Converts a value of any type to a string and assigns a defaultvalue if the conversion fails.
    102             @param output The string to write the result in
    103             @param input The variable to convert
    104             @param fallbackString The assigned string if the conversion fails.
    105             @return True if the conversion succeeded
    106 
    107             @example
    108             float f = 3.14;
    109             std::string output;
    110             bool success = Convert::ToString(&output, f, "0.000000");
    111         */
    112         template <typename T>
    113         static bool ToString(std::string* output, T input, const std::string& fallbackString)
    114         {
    115             if (Convert::ToString(output, input))
    116                 return true;
    117 
    118             (*output) = fallbackString;
    119             return false;
    120         }
    121 
    122         /**
    123             @brief Converts a string to a value of any type.
    124             @param output The variable to assign the result to
    125             @param input The string to convert
    126             @return True if the conversion succeeded
    127 
    128             @example
    129             std::string input = "3.14";
    130             float f;
    131             bool success = string2Number(&f, input);
    132         */
    133         template <typename T>
    134         static bool FromString(T* output, const std::string& input)
    135         {
    136             std::istringstream iss(input);
    137             if (iss >> (*output))
    138                 return true;
    139 
    140             return false;
    141         }
    142 
    143         /**
    144             @brief Converts a string to a value of any type.
    145             @param output The variable to assign the result to
    146             @param input The string to convert
    147             @param fallbackValue The assigned value if the conversion fails
    148             @return True if the conversion succeeded
    149 
    150             @example
    151             std::string input = "3.14";
    152             float f;
    153             bool success = string2Number(&f, input, 0.000000);
    154         */
    155         template <typename T>
    156         static bool FromString(T* output, const std::string& input, T fallbackValue)
    157         {
    158             if (Convert::FromString(output, input))
    159                 return true;
    160 
    161             (*output) = fallbackValue;
    162             return false;
    163         }
    164 };
    165 
    16672#endif /* _Util_String_H__ */
Note: See TracChangeset for help on using the changeset viewer.