Conversions.h

Go to the documentation of this file.
00001 
00011 #ifndef __CONVERSIONS__H__
00012 #define __CONVERSIONS__H__
00013 
00014 #include <wx/wx.h> /* wxString -> std::string convesions */
00015 
00016 #include <iostream>
00017 #include <sstream>
00018 #include <iomanip>
00019 #include <string>
00020 #include <limits>
00021 
00025 namespace Conversions {
00030         template<typename T> inline std::string ToString(const T & value) {
00031                 std::ostringstream out;
00032                 out << value;
00033                 return out.str();
00034         }
00035 
00037         template<> inline std::string ToString<bool>(const bool & value) {
00038                 std::ostringstream out;
00039                 out << std::boolalpha << value;
00040                 return out.str();
00041         }
00042 
00044         template<> inline std::string ToString<double>(const double & value) {
00045                 const int sigDigits = std::numeric_limits<double>::digits10;
00046                 std::ostringstream out;
00047                 out << std::setprecision(sigDigits) << value;
00048                 return out.str();
00049         }
00050 
00052         template<> inline std::string ToString<float>(const float & value) {
00053                 const int sigDigits = std::numeric_limits<float>::digits10;
00054                 std::ostringstream out;
00055                 out << std::setprecision(sigDigits) << value;
00056                 return out.str();
00057         }
00058 
00060         template<> inline std::string ToString<long double>(const long double & value) {
00061                 const int sigDigits = std::numeric_limits<long double>::digits10;
00062                 std::ostringstream out;
00063                 out << std::setprecision(sigDigits) << value;
00064                 return out.str();
00065         }
00066 
00067         // Only for optimalization reasons - if there is someone trying to
00068         // convert std::string into std::string...
00070         template<> inline std::string ToString<std::string>(const std::string & value) {
00071                 return std::string(value);
00072         }
00073 
00074         // Only for optimalization reasons - if there is someone trying to
00075         // convert char * into std::string...
00076         typedef char * PtrToChar;
00078         template<> inline std::string ToString<PtrToChar>(const PtrToChar & value) {
00079                 return std::string(value);
00080         }
00081 
00082         // Only for optimalization reasons - if there is someone trying to
00083         // convert char into std::string...
00085         template<> inline std::string ToString<char>(const char & value) {
00086                 return std::string(1, value);
00087         }
00088 
00098         template<typename N> inline bool StringToNumber(const std::string & str,
00099                 N & number, std::ios_base & (* format)(std::ios_base &) = std::dec) {
00100                 return !(std::istringstream(str) >> format >> number).fail();
00101         }
00102 
00103 
00104         // wxString to std::string and vice versa
00105 
00112         inline wxString string2wxString(const std::string & str) {
00113                 wxMBConvUTF8 * wxconv = new wxMBConvUTF8();
00114                 wxString transformed(wxconv->cMB2WC(str.c_str()), wxConvUTF8);
00115                 delete wxconv;
00116                 return transformed;
00117         }
00118 
00123         inline std::string wxString2string(const wxString & str) {
00124                 return std::string(str.mb_str(wxConvUTF8));
00125         }
00126 }
00127 
00128 #endif /* #ifndef __CONVERSIONS__H__ */
00129 
00130 /* End of file Conversions.h */

Generated on Sun Apr 29 11:46:10 2007 for IPP/ICP2007 by  doxygen 1.4.7