Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

stringcast.hpp

00001 /************************************************************************************
00002  *    This file is part of the MynahSA streaming and archiving toolkit              *
00003  *    Copyright (C) 2006 Mynah-Software Ltd. All Rights Reserved.                   *
00004  *                                                                                  *
00005  *    This program is free software; you can redistribute it and/or modify          *
00006  *    it under the terms of the GNU General Public License, version 2               *
00007  *    as published by the Free Software Foundation.                                 *
00008  *                                                                                  *
00009  *    This program is distributed in the hope that it will be useful,               *
00010  *    but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00011  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
00012  *    GNU General Public License for more details.                                  *
00013  *                                                                                  *
00014  *    You should have received a copy of the GNU General Public License along       *
00015  *    with this program; if not, write to the Free Software Foundation, Inc.,       *
00016  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                   *
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