#ifndef _String2Number_H__ #define _String2Number_H__ #include #include #include #include "orxonox/core/Debug.h" /** * String to number conversion * * This class converts a number inside a std::string * into a numeric type number (int,float,double) * Number in string can be decimal, hexadecimal or octal * * @author Nicolas Perrenoud * * @example * float f; * String2Number(f, std::string(" 123.45 ")); */ template class String2Number { private: bool success_; public: /** * Constructor * * First value is the target variable, the second vector is the * string where the number is taken from, the third parameter * should be one of std::hex, std::dec or std::oct (dec is default value) */ inline String2Number(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&) = std::dec, int haltOnError=1) { std::istringstream iss(s); success_ = !(iss >> f >> t).fail(); if (!success_ && haltOnError==1) { COUT(1) << "Error: Conversion from string to number in \"" << s << "\" failed." << std::endl; exit(1); } } }; #endif /* _String2Number_H__ */