Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00024
00025 #ifndef ICL_CORE_CONFIG_UTIL_H_INCLUDED
00026 #define ICL_CORE_CONFIG_UTIL_H_INCLUDED
00027
00028 #include <iostream>
00029 #include <iomanip>
00030 #include <sstream>
00031 #include <stdexcept>
00032 #include <boost/algorithm/string.hpp>
00033
00034 namespace icl_core {
00035 namespace config {
00037 namespace impl {
00038
00042 template <typename T, typename U>
00043 T hexical_cast(U input)
00044 {
00045 std::stringstream interpreter;
00046 interpreter.setf(std::ios::fmtflags(), std::ios::basefield);
00047 interpreter << input;
00048 T result;
00049 interpreter >> result;
00050 return result;
00051 }
00052
00059 template <typename U>
00060 bool bool_cast(U input)
00061 {
00062 std::stringstream interpreter;
00063 interpreter.setf(std::ios::fmtflags(), std::ios::basefield);
00064 interpreter << input;
00065 std::string result;
00066 interpreter >> result;
00067 boost::algorithm::to_lower(result);
00068 if (result == "1" || result == "yes" || result == "true")
00069 {
00070 return true;
00071 }
00072 else
00073 {
00074 return false;
00075 }
00076 }
00077
00086 template <typename U>
00087 bool strict_bool_cast(U input)
00088 {
00089 std::stringstream interpreter;
00090 interpreter.setf(std::ios::fmtflags(), std::ios::basefield);
00091 interpreter << input;
00092 std::string result;
00093 interpreter >> result;
00094 boost::algorithm::to_lower(result);
00095 if (result == "1" || result == "yes" || result == "true")
00096 {
00097 return true;
00098 }
00099 else if (result == "0" || result == "no" || result == "false")
00100 {
00101 return false;
00102 }
00103 else
00104 {
00105 throw std::invalid_argument("Not a boolean value: " + result);
00106 }
00107 }
00108
00109 }
00110 }
00111 }
00112
00113 #endif