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 <string>
00024
00025 #include <boost/format.hpp>
00026
00027 #include "sbuild-error.h"
00028 #include "sbuild-log.h"
00029
00030 namespace sbuild
00031 {
00032
00037 class parse_value
00038 {
00039 public:
00041 typedef runtime_error_custom<parse_value> error;
00042
00047 parse_value (std::string const& value);
00048
00050 virtual ~parse_value ();
00051
00057 template <typename T>
00058 operator T (void)
00059 {
00060 T tmp;
00061
00062 if (parse(tmp) == false)
00063 {
00064 boost::format fmt("Could not parse value \"%1%\"");
00065 fmt % this->value;
00066 throw error(fmt);
00067 }
00068
00069 return tmp;
00070 }
00071
00072 private:
00078 bool
00079 parse (bool& parsed_value) const;
00080
00086 bool
00087 parse (std::string& parsed_value) const;
00088
00094 template <typename T>
00095 bool
00096 parse (T& parsed_value) const
00097 {
00098 std::istringstream is(this->value);
00099 is.imbue(std::locale("C"));
00100 T tmpval;
00101 if (is >> tmpval)
00102 {
00103 parsed_value = tmpval;
00104 log_debug(DEBUG_NOTICE) << "value=" << parsed_value << std::endl;
00105 return true;
00106 }
00107 log_debug(DEBUG_NOTICE) << "parse error" << std::endl;
00108 return false;
00109 }
00110
00111 std::string value;
00112 };
00113
00114 }
00115
00116 #endif
00117
00118
00119
00120
00121
00122