StringConversions.H

Go to the documentation of this file.
00001 /*!@file Util/StringConversions.H Convert to and from std::string */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Util/StringConversions.H $
00035 // $Id: StringConversions.H 11934 2009-11-06 03:03:46Z itti $
00036 //
00037 
00038 #ifndef STRINGCONVERSIONS_H_DEFINED
00039 #define STRINGCONVERSIONS_H_DEFINED
00040 
00041 #include <string>
00042 #include <typeinfo>
00043 
00044 #include "rutz/time.h"  // for rutz::time
00045 
00046 struct in_addr;
00047 
00048 /// Type of exception that is thrown from throwBadConversion
00049 /** The real type of the exception is a derived type that is hidden
00050     from this header file -- that's so that we can encapsulate the
00051     implementation. */
00052 class conversion_error : public std::exception
00053 {
00054 protected:
00055   conversion_error() throw();
00056   virtual ~conversion_error() throw();
00057 
00058 public:
00059   virtual const char* what() const throw() = 0;
00060 
00061   /// Raise an exception for an invalid string conversion to the given type
00062   /** The exception thrown will be derived from conversion_error. */
00063   template <class T>
00064   static inline void raise(const std::string& str,
00065                            const std::string& extrainfo = "")
00066   {
00067     raise_impl(typeid(T), str, extrainfo);
00068   }
00069 
00070 private:
00071   static void raise_impl(const std::type_info& type,
00072                          const std::string& str,
00073                          const std::string& extrainfo);
00074 };
00075 
00076 /*! \defgroup strcvthelp String conversion helpers
00077 
00078    We define a number of string conversion helpers here just so that
00079    we don't require each type that we may want to instantiate into a
00080    ModelParam derivative to define operator<< and operator>>. The main
00081    reason is that this definition requires #including <sstream>
00082    (especially if the class is all-inline like Point2D<int>), which would
00083    largely increase the compile time if we did it for all our simple
00084    types like Dims, Point2D<int>, etc.  But the code below may some day
00085    migrate into proper definitions of operator<< and operator>> for
00086    those data types. */
00087 //@{
00088 
00089 /*! @name Basic type string conversion functions */
00090 //@{
00091 //! int -> string via stringstream
00092 std::string convertToString(const int& val);
00093 //! string -> int via stringstream
00094 void convertFromString(const std::string& str, int& val);
00095 
00096 //! long int -> string via stringstream
00097 std::string convertToString(const long int& val);
00098 //! string -> long int via stringstream
00099 void convertFromString(const std::string& str, long int& val);
00100 
00101 //! long long int -> string via stringstream
00102 std::string convertToString(const long long int& val);
00103 //! string -> long long int via stringstream
00104 void convertFromString(const std::string& str, long long int& val);
00105 
00106 //! short int -> string via stringstream
00107 std::string convertToString(const short int& val);
00108 //! string -> short int via stringstream
00109 void convertFromString(const std::string& str, short int& val);
00110 
00111 //! unsigned int -> string via stringstream
00112 std::string convertToString(const unsigned int& val);
00113 //! string -> unsigned int via stringstream
00114 void convertFromString(const std::string& str, unsigned int& val);
00115 
00116 //! unsigned long int -> string via stringstream
00117 std::string convertToString(const unsigned long int& val);
00118 //! string -> unsigned long int via stringstream
00119 void convertFromString(const std::string& str, unsigned long int& val);
00120 
00121 //! unsigned long long int -> string via stringstream
00122 std::string convertToString(const unsigned long long int& val);
00123 //! string -> unsigned long long int via stringstream
00124 void convertFromString(const std::string& str, unsigned long long int& val);
00125 
00126 //! unsigned short int -> string via stringstream
00127 std::string convertToString(const unsigned short int& val);
00128 //! string -> unsigned short int via stringstream
00129 void convertFromString(const std::string& str, unsigned short int& val);
00130 
00131 //! unsigned char -> string via stringstream
00132 std::string convertToString(const unsigned char& val);
00133 //! string -> unsigned char via stringstream
00134 void convertFromString(const std::string& str, unsigned char& val);
00135 
00136 //! float -> string via stringstream
00137 std::string convertToString(const float& val);
00138 //! string -> float via stringstream
00139 void convertFromString(const std::string& str, float& val);
00140 
00141 //! double -> string via stringstream
00142 std::string convertToString(const double& val);
00143 //! string -> double stringstream
00144 void convertFromString(const std::string& str, double& val);
00145 
00146 //! std::string overload
00147 std::string convertToString(const std::string& val);
00148 //! std::string overload
00149 void convertFromString(const std::string& str, std::string& val);
00150 
00151 //! bool overload: format is "true|false"
00152 std::string convertToString(const bool& val);
00153 //! bool overload
00154 /*! Anything with a first letter 't', T', 'y', 'Y', or '1' will
00155   convert to true; anything with a first letter 'f', F', 'n', 'N', or
00156   '0' will convert to false */
00157 void convertFromString(const std::string& str, bool& val);
00158 
00159 //@}
00160 
00161 /*! @name Overloaded implementations of string conversion functions */
00162 //@{
00163 
00164 //! in_addr overload: format is "<int>.<int>.<int>.<int>"
00165 std::string convertToString(const in_addr& val);
00166 //! in_addr overload: format is "<int>.<int>.<int>.<int>"
00167 void convertFromString(const std::string& str, in_addr& val);
00168 
00169 //! rutz::time overload: format is "<h>:<m>:<s>.<ms>"
00170 std::string convertToString(const rutz::time& val);
00171 //! rutz::time overload: format is "<h>:<m>:<s>.<ms>"
00172 void convertFromString(const std::string& str, rutz::time& val);
00173 
00174 //@}
00175 
00176 /*! @name Easier-syntax functions to convert to/from string */
00177 //@{
00178 
00179 //! converts val into a string and returns the string
00180 template <class T>
00181 inline std::string toStr(const T& val);
00182 
00183 //! converts str into a T value and returns the T value
00184 template <class T>
00185 inline T fromStr(const std::string& str);
00186 //@}
00187 
00188 
00189 // ######################################################################
00190 // inline functions
00191 // ######################################################################
00192 
00193 // ######################################################################
00194 // let this function stay inline so that in case somebody tries to run
00195 // toStr() for a type for which there is no convertToString(), we will
00196 // get an error at compile time instead of at link time
00197 template <class T>
00198 inline std::string toStr(const T& val)
00199 { return convertToString(val); }
00200 
00201 // ######################################################################
00202 // let this function stay inline so that in case somebody tries to run
00203 // fromStr() for a type for which there is no convertFromString(), we
00204 // will get an error at compile time instead of at link time
00205 template <class T>
00206 T fromStr(const std::string& str)
00207 {
00208   T val;
00209   convertFromString(str, val);
00210   return val;
00211 }
00212 
00213 #endif
00214 
00215 // ######################################################################
00216 /* So things look consistent in everyone's emacs... */
00217 /* Local Variables: */
00218 /* indent-tabs-mode: nil */
00219 /* End: */
Generated on Sun May 8 08:42:32 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3