00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __string_cast_hpp
00021 #define __string_cast_hpp
00022
00023 #include <sstream>
00024 #include <stdexcept>
00025 #include <string>
00026
00027 namespace MynahSA {
00028
00029 class StringCastException : std::logic_error {
00030 public:
00031 StringCastException(const std::string& str) : std::logic_error(str.c_str()) {
00032 }
00033 StringCastException(const char* c) : std::logic_error(c) {
00034 }
00035 };
00036
00037 template<class T>
00038 T StringCast(const char* c) {
00039 std::ostringstream os;
00040 os << c;
00041 std::istringstream is(os.str());
00042 T rv;
00043 is >> rv;
00044 if (!is) {
00045 throw StringCastException("Error in string conversion");
00046 }
00047 return rv;
00048 }
00049
00050 template<class T>
00051 T StringCast(const std::string& c) {
00052 std::ostringstream os;
00053 os << c;
00054 std::istringstream is(os.str());
00055 T rv;
00056 is >> rv;
00057 if (!is) {
00058 throw StringCastException("Error in string conversion");
00059 }
00060 return rv;
00061 }
00062 };
00063
00064 #endif
00065