blob: 86210ca06ccce93200bdca1626c84d3d603994c2 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_UTIL_STRING_TO_NUM_H_
2#define AOS_UTIL_STRING_TO_NUM_H_
Daniel Pettib6c885b2014-09-12 10:04:28 -07003
4#include <sstream>
5#include <string>
6
7namespace aos {
8namespace util {
9
Daniel Petti5aa29792014-12-27 17:48:07 -050010// Converts a string into a specified numeric type. If it can't be converted
Daniel Pettib6c885b2014-09-12 10:04:28 -070011// completely or at all, or if the converted number would overflow the
Daniel Petti5aa29792014-12-27 17:48:07 -050012// specified type, it returns false.
Daniel Pettib6c885b2014-09-12 10:04:28 -070013template<typename T>
Daniel Petti5aa29792014-12-27 17:48:07 -050014inline bool StringToNumber(const ::std::string &input, T *out_num) {
Daniel Pettib6c885b2014-09-12 10:04:28 -070015 ::std::istringstream stream(input);
16 stream >> *out_num;
17
18 if (stream.fail() || !stream.eof()) {
19 return false;
20 }
21
22 return true;
23}
24
25
26} // util
27} // aos
28
29#endif