Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 675 was 675, checked in by landauf, 16 years ago

removed debug output

File size: 3.2 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::ostringstream oss;
83    if (oss << variable)
84        return *(new 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*/
98template <typename T>
99bool 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*/
118template <typename T>
119bool 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
129#endif
Note: See TracBrowser for help on using the repository browser.