00001 #ifndef StreamParser_HPP
00002 #define StreamParser_HPP
00003 
00004 
00005 
00006 
00007 #include <stdexcept>
00008 #include <sstream>
00009 
00013 class BadConversion : public std::runtime_error {
00014  public:
00015    BadConversion(const std::string& s)
00016      : std::runtime_error(s) { }
00017  };
00018  
00019  template<typename T>
00020  inline void convert(const std::string& s, T& x,
00021                      bool failIfLeftoverChars = true)
00022  {
00023    std::istringstream i(s);
00024    char c;
00025    if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
00026      throw BadConversion(s);
00027  }
00028 
00029 #endif