00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef SBUILD_PARSE_VALUE_H
00021 #define SBUILD_PARSE_VALUE_H
00022
00023 #include <sbuild/sbuild-parse-error.h>
00024 #include <sbuild/sbuild-log.h>
00025
00026 #include <string>
00027 #include <sstream>
00028
00029 namespace sbuild
00030 {
00031
00036 class parse_value
00037 {
00038 public:
00040 typedef parse_error error;
00041
00046 parse_value (std::string const& value);
00047
00049 virtual ~parse_value ();
00050
00056 template <typename T>
00057 operator T (void)
00058 {
00059 T tmp;
00060
00061 if (parse(tmp) == false)
00062 {
00063 throw error(parse_error::BAD_VALUE, this->value);
00064 }
00065
00066 return tmp;
00067 }
00068
00069 private:
00075 bool
00076 parse (bool& parsed_value) const;
00077
00083 bool
00084 parse (std::string& parsed_value) const;
00085
00091 template <typename T>
00092 bool
00093 parse (T& parsed_value) const
00094 {
00095 std::istringstream is(this->value);
00096 is.imbue(std::locale("C"));
00097 T tmpval;
00098 if (is >> tmpval)
00099 {
00100 parsed_value = tmpval;
00101 log_debug(DEBUG_NOTICE) << "value=" << parsed_value << std::endl;
00102 return true;
00103 }
00104 log_debug(DEBUG_NOTICE) << "parse error" << std::endl;
00105 return false;
00106 }
00107
00108 std::string value;
00109 };
00110
00111 }
00112
00113 #endif
00114
00115
00116
00117
00118
00119