src/rcrt/loaders/conversion.hpp

Go to the documentation of this file.
00001 #ifndef CONVERSION_HPP_
00002 #define CONVERSION_HPP_
00003 
00004 
00005 #include <iostream>
00006 #include <sstream>
00007 #include <string>
00008 #include <typeinfo>
00009 #include <stdexcept>
00010 
00011 namespace rcrt
00012 {
00013  
00014 class BadConversion : public std::runtime_error
00015 {
00016 public:
00017         BadConversion(const std::string& s) : std::runtime_error(s)
00018      { }
00019 };
00020  
00021 template<typename T>
00022 inline std::string toString(const T& x)
00023 {
00024         std::ostringstream o;
00025         if (!(o << x))
00026                 throw BadConversion(std::string("toString(")
00027                          + typeid(x).name() + ")");
00028         return o.str();
00029 }
00030 
00031 template<typename T>
00032 inline void convert(const std::string& s, T& x,
00033                      bool failIfLeftoverChars = true)
00034 {
00035         std::istringstream i(s);
00036         char c;
00037         if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
00038                 throw BadConversion(std::string("convert(")+s+std::string(", ")+
00039                                 typeid(x).name()+std::string(")"));
00040 }
00041 
00042 template<typename T> inline T convertTo(const std::string& s,
00043                 bool failIfLeftoverChars = true) {
00044         T x;
00045         convert(s, x, failIfLeftoverChars);
00046         return x;
00047 }
00048 
00049 }
00050 
00051 #endif /*CONVERSION_HPP_*/

Generated on Thu Jan 31 19:26:19 2008 for RenderingCompetitionRayTracer by  doxygen 1.5.3