00001
00002
00003 #ifndef AITOOLS_UTIL_CONVERSION_HPP
00004 #define AITOOLS_UTIL_CONVERSION_HPP
00005
00006 #include <string>
00007 #include <sstream>
00008
00009 #include "util/exception.hpp"
00010
00014 namespace aitools {
00015 namespace util {
00016
00017 template <typename T>
00018 bool
00019 to_number(const std::string& value, T& number)
00020 {
00021 return std::istringstream(value) >> number;
00022 }
00023
00024 template <typename T>
00025 const T
00026 to_number(const std::string& value)
00027 {
00028 T number;
00029 if (!to_number<T>(value, number))
00030 throw_invalid_argument("to_number failed", value);
00031 return number;
00032 }
00033
00034 template <typename T>
00035 std::string
00036 to_string(T value)
00037 {
00038 std::ostringstream oss;
00039 oss << value;
00040 return oss.str();
00041 }
00042
00043 }
00044
00045 using util::to_number;
00046 using util::to_string;
00047
00048 }
00049
00050 #endif // AITOOLS_UTIL_CONVERSION_HPP