Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/xml/String2Number.h @ 395

Last change on this file since 395 was 395, checked in by nicolasc, 16 years ago

c/p string2number from xml

File size: 1.1 KB
Line 
1#ifndef __STRING2NUMBER_H__
2#define __STRING2NUMBER_H__
3
4#include <string>
5#include <sstream>
6#include <iostream>
7
8/**
9* String to number conversion
10*
11* This class converts a number inside a std::string
12* into a numeric type number (int,float,double)
13* Number in string can be decimal, hexadecimal or octal
14*
15* @autor Nicolas Perrenoud<nicolape@ee.ethz.ch>
16*
17* @example
18* float f;
19* String2Number<float>(f, std::string(" 123.45 "));
20*/
21template <class T>
22class String2Number
23{
24private:
25        bool success_;
26public:
27        /**
28        * Constructor
29        *
30        * First value is the target variable, the second vector is the
31        * string where the number is taken from, the third parameter
32        * should be one of std::hex, std::dec or std::oct (dec is default value)
33        */
34        String2Number(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&) =  std::dec, int haltOnError=1)
35        {
36          std::istringstream iss(s);
37          success_ = !(iss >> f >> t).fail();
38         
39          if (!success_ && haltOnError==1)
40          {
41                std::cout << "Conversion error from string to number in \"" << s << "\"" << std::endl;
42                exit(1);
43          }
44        }
45};
46
47#endif
Note: See TracBrowser for help on using the repository browser.