Last change
on this file since 663 was
659,
checked in by nicolasc, 17 years ago
|
- a lot of inlining of getfunctions
- set libaudio to shared (not sure, if related: audioplayer + ingame sound == crash!)
- minor space cleanups
|
File size:
1.2 KB
|
Rev | Line | |
---|
[395] | 1 | #ifndef __STRING2NUMBER_H__ |
---|
| 2 | #define __STRING2NUMBER_H__ |
---|
| 3 | |
---|
| 4 | #include <string> |
---|
| 5 | #include <sstream> |
---|
[659] | 6 | #include <iostream> |
---|
[395] | 7 | |
---|
[659] | 8 | #include "../orxonox/core/Debug.h" |
---|
| 9 | |
---|
[395] | 10 | /** |
---|
[659] | 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 | |
---|
[395] | 24 | template <class T> |
---|
| 25 | class String2Number |
---|
| 26 | { |
---|
[659] | 27 | private: |
---|
| 28 | bool success_; |
---|
[560] | 29 | |
---|
[659] | 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 | } |
---|
[395] | 49 | }; |
---|
| 50 | |
---|
| 51 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.