Last change
on this file since 637 was
592,
checked in by nicolasc, 17 years ago
|
added engineglow particle effect - based of treibwerk
other various changes
|
File size:
1.1 KB
|
Rev | Line | |
---|
[395] | 1 | #ifndef __STRING2NUMBER_H__ |
---|
| 2 | #define __STRING2NUMBER_H__ |
---|
| 3 | |
---|
| 4 | #include <string> |
---|
| 5 | #include <sstream> |
---|
[560] | 6 | #include <iostream> |
---|
| 7 | |
---|
| 8 | #include "../orxonox/core/Debug.h" |
---|
[395] | 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 | * |
---|
[592] | 17 | * @author Nicolas Perrenoud<nicolape@ee.ethz.ch> |
---|
[395] | 18 | * |
---|
| 19 | * @example |
---|
[560] | 20 | * float f; |
---|
[395] | 21 | * String2Number<float>(f, std::string(" 123.45 ")); |
---|
| 22 | */ |
---|
| 23 | template <class T> |
---|
| 24 | class String2Number |
---|
| 25 | { |
---|
| 26 | private: |
---|
| 27 | bool success_; |
---|
| 28 | public: |
---|
| 29 | /** |
---|
| 30 | * Constructor |
---|
| 31 | * |
---|
| 32 | * First value is the target variable, the second vector is the |
---|
[560] | 33 | * string where the number is taken from, the third parameter |
---|
[395] | 34 | * should be one of std::hex, std::dec or std::oct (dec is default value) |
---|
| 35 | */ |
---|
[507] | 36 | inline String2Number(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&) = std::dec, int haltOnError=1) |
---|
[395] | 37 | { |
---|
| 38 | std::istringstream iss(s); |
---|
| 39 | success_ = !(iss >> f >> t).fail(); |
---|
[560] | 40 | |
---|
[395] | 41 | if (!success_ && haltOnError==1) |
---|
| 42 | { |
---|
[560] | 43 | COUT(1) << "Error: Conversion from string to number in \"" << s << "\" failed." << std::endl; |
---|
[395] | 44 | exit(1); |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | }; |
---|
| 48 | |
---|
| 49 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.