Conversion.h

Go to the documentation of this file.
00001 
00005 /* Copyright (C) 2002, Bernd Speiser */
00006 /* This file is part of BSUtilities.
00007 
00008 BSUtilities is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 BSUtilities is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017   
00018 You should have received a copy of the GNU General Public License
00019 along with this program; if not, write to the Free Software
00020 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00021 02111-1307, USA.
00022 */
00023 
00024 #ifndef _Conversion_h
00025 #define _Conversion_h
00026 
00027 #include "BSUtilitiesError.h"
00028 
00029 #include <string>
00030 #include <iostream>
00031 #include <sstream>
00032 
00034 namespace BSUtilities {
00035 
00037 
00039 class ConversionFailure : public BSUtilitiesError
00040 {
00041 private:
00043   static const std::string CONVERSIONFAILURE;
00044 
00045 public:
00047 
00049   ConversionFailure (void) : BSUtilitiesError (CONVERSIONFAILURE) {}
00050 
00052 
00054   ConversionFailure (const std::string message)
00055                                          : BSUtilitiesError (message) {}
00056 };
00057 
00058 
00060 template<class T>
00061   class Conversion  
00062   {  
00063     public:  
00064     
00065      static std::string to_string (const T value);  
00066     
00067      static const char * to_Cstring (const T value);  
00068     
00069      static T string_to (const std::string valuestring);  
00070     
00071      static T Cstring_to (const char *valuestring);  
00072     
00073   };  
00074 
00075 /* definition of inline functions */
00076 
00077 template<class T>
00078   inline std::string Conversion<T>::to_string (const T value) 
00079                  {std::ostringstream os; os << value; return os.str ();}
00080 
00081 template<class T>
00082   inline const char * Conversion<T>::to_Cstring (const T value) 
00083                                   {return (to_string (value).c_str ());}
00084 
00085 template<class T>
00086   T Conversion<T>::string_to (const std::string valuestring)
00087   {
00088     std::istringstream is (valuestring);
00089 
00090     T value;
00091 
00092     is >> value;
00093     is >> value;
00094 
00095     if (!is.eof ())
00096       {throw ConversionFailure ();}
00097 
00098     else
00099       return value;
00100   }
00101 
00102 template<class T>
00103   inline T Conversion<T>::Cstring_to (const char *valuestring) 
00104           {std::string value (valuestring); return (string_to (value));}
00105 
00106 }
00107 
00108 #endif /* _Conversion_h */

Generated on Sun Jul 26 16:51:58 2009 for BSUtilities by  doxygen 1.5.3