Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/misc/String2Number.h @ 667

Last change on this file since 667 was 667, checked in by landauf, 16 years ago
  • expanded the String2Number.h file
  • changed the SetConfigValue macro
  • changed the S2N and N2S conversion in the ConfigValueContainer
  • added unsigned int, char, unsigned char, float (additionally to double) and const char* (additionally to std::string) to the ConfigValueContainer
File size: 3.4 KB
Line 
1#ifndef __STRING2NUMBER_H__
2#define __STRING2NUMBER_H__
3
4#include <string>
5#include <sstream>
6#include <iostream>
7
8#include "../orxonox/core/Debug.h"
9
10/**
11 * String to number conversion
12 *
13 * This class converts a number inside a std::string
14 * into a numeric type number (int,float,double)
15 * Number in string can be decimal, hexadecimal or octal
16 *
17 * @author Nicolas Perrenoud<nicolape@ee.ethz.ch>
18 *
19 * @example
20 * float f;
21 * String2Number<float>(f, std::string(" 123.45 "));
22 */
23
24template <class T>
25class String2Number
26{
27  private:
28  bool success_;
29
30  public:
31  /**
32   * Constructor
33   *
34   * First value is the target variable, the second vector is the
35   * string where the number is taken from, the third parameter
36   * should be one of std::hex, std::dec or std::oct (dec is default value)
37   */
38  inline String2Number(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&) =  std::dec, int haltOnError=1)
39  {
40    std::istringstream iss(s);
41    success_ = !(iss >> f >> t).fail();
42
43    if (!success_ && haltOnError==1)
44    {
45      COUT(1) << "Error: Conversion from string to number in \"" << s << "\" failed." << std::endl;
46      exit(1);
47    }
48  }
49};
50
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*/
60template <typename T>
61const std::string& number2String(T variable)
62{
63    std::ostringstream oss;
64    if (oss << variable)
65        return *(new std::string(oss.str()));
66
67    return *(new 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*/
79template <typename T>
80const std::string& number2String(T variable, const std::string& fallbackString)
81{
82    std::cout << "N2Sa: " << variable << std::endl;
83
84    std::ostringstream oss;
85    if (oss << variable)
86    {
87    std::cout << "N2Sb: " << oss.str() << std::endl;
88        return *(new std::string(oss.str()));
89    }
90
91    return fallbackString;
92}
93
94/**
95    @brief Converts a string to a number of any type.
96    @param variable The variable wherein the value gets written
97    @return True if the conversion was successful
98
99    @example
100    float f;
101    bool success = string2Number(f, "3.14");
102*/
103template <typename T>
104bool string2Number(T& variable, const std::string& valueString)
105{
106    std::istringstream iss(valueString);
107    if (iss >> variable)
108        return true;
109
110    return false;
111}
112
113/**
114    @brief Converts a string to a number of any type.
115    @param variable The variable wherein the value gets written
116    @param fallbackValue The assigned value if the conversion fails
117    @return True if the conversion was successful
118
119    @example
120    float f;
121    bool success = string2Number(f, "3.14", 0.000000);
122*/
123template <typename T>
124bool string2Number(T& variable, const std::string& valueString, T fallbackValue)
125{
126    std::cout << "S2Na: " << valueString << std::endl;
127    std::istringstream iss(valueString);
128    if (iss >> variable)
129    {
130    std::cout << "S2Nb: " << variable << std::endl;
131        return true;
132    }
133
134    variable = fallbackValue;
135    return false;
136}
137
138#endif
Note: See TracBrowser for help on using the repository browser.