ximol/str_cast.hpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002  *                                                                           *
00003  * library XiMoL                                                             *
00004  * Copyright (C) 2002, 2003, 2004 Florent Tournois                           *
00005  *                                                                           *
00006  * This library is free software; you can redistribute it and/or             *
00007  * modify it under the terms of the GNU Lesser General Public                *
00008  * License as published by the Free Software Foundation; either              *
00009  * version 2.1 of the License, or (at your option) any later version.        *
00010  *                                                                           *
00011  * This library is distributed in the hope that it will be useful,           *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00014  * Lesser General Public License for more details.                           *
00015  *                                                                           *
00016  * You should have received a copy of the GNU Lesser General Public          *
00017  * License along with this library; if not, write to the Free Software       *
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA   *
00019  *                                                                           *
00020 \*****************************************************************************/
00021 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00022 /** \file 
00023         \brief cast to string.
00024 
00025     \author Tournois Florent
00026         \version 1.0
00027 
00028     $Id: str_cast.hpp,v 1.9 2004/03/04 18:45:29 tournois Exp $
00029     $Log: str_cast.hpp,v $
00030     Revision 1.9  2004/03/04 18:45:29  tournois
00031     Compatibility with gcc.
00032 
00033     Revision 1.8  2004/03/03 22:05:30  tournois
00034     Add a short roadmap.
00035     Add BOOST_NO_STD_WSTRING for gcc.
00036 
00037     Revision 1.7  2004/02/22 10:27:32  tournois
00038     Add some doc.
00039 
00040     Revision 1.6  2004/02/22 09:54:19  tournois
00041     Change years on the copyright.
00042 
00043     Revision 1.5  2004/02/09 12:41:25  tournois
00044     Fix bug about error message.
00045     Add a read_optionnal_space before the stag read.
00046 
00047     Revision 1.4  2004/01/18 11:40:58  tournois
00048     Add the pattern facet.
00049 
00050     Revision 1.3  2003/12/09 19:57:27  tournois
00051     Fix some bugs about attributes classes.
00052 
00053     Revision 1.2  2003/11/27 15:31:55  hfp
00054     partially adapted to vc6.
00055 
00056     Revision 1.1  2003/11/18 18:54:51  tournois
00057     Add str_cast and drop the transformation.hpp file.
00058 
00059 
00060   */
00061 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00062 #ifndef XIMOL_STR_CAST_HPP_
00063 #define XIMOL_STR_CAST_HPP_
00064 
00065 #include <ximol/namespace_defs.hpp>
00066 #include <ximol/export_defs.hpp>
00067 #include <ximol/typedefs.hpp>
00068 
00069 
00070 XIMOL_BEGIN_NAMESPACE
00071 
00072 template < typename Char_dest, typename Char_source > struct converter
00073 {
00074     static Char_dest get(Char_source);
00075     static ::std::basic_string<Char_dest> get(const ::std::basic_string<Char_source> & wstr);
00076 };
00077 
00078 template <> struct XIMOL_EXPORT converter<char, wchar_t>
00079 {
00080     static char get(wchar_t);
00081     static ::std::string get(const ::std::wstring & wstr);
00082 };
00083 
00084 template <> struct XIMOL_EXPORT converter<char, char>
00085 {
00086     static char get(char);
00087     static ::std::string get(const ::std::string & str);
00088 };
00089 
00090 template <> struct XIMOL_EXPORT converter<wchar_t, char>
00091 {
00092     static wchar_t get(char);
00093     static ::std::wstring get(const ::std::string & str);
00094 };
00095 
00096 template <> struct XIMOL_EXPORT converter<wchar_t, wchar_t>
00097 {
00098     static wchar_t get(wchar_t);
00099     static ::std::wstring get(const ::std::wstring & wstr);
00100 };
00101 
00102 // full generic: undefined
00103 template<typename T> struct str {};
00104 
00105 // specialized for string
00106 template<> struct str< ::std::string >
00107 {
00108         template<typename T>
00109         static ::std::string cast(const T& t)
00110         {
00111                 ::std::ostringstream ss;
00112                 ss << t;
00113                 return ss.str();
00114         };
00115 
00116         static ::std::string cast(const ::std::string & t)              { return converter<char,char>::get(t); };
00117         static ::std::string cast(const ::std::wstring & t)             { return converter<char,wchar_t>::get(t); };
00118 
00119         static ::std::string cast(const char * t)                               { return converter<char,char>::get(t); };
00120         static ::std::string cast(const wchar_t * t)                    { return converter<char,wchar_t>::get(t); };
00121 
00122         static ::std::string cast(char t)                                               { return ::std::string(1,converter<char,char>::get(t)); };
00123         static ::std::string cast(wchar_t t)                                    { return ::std::string(1,converter<char,wchar_t>::get(t)); };
00124 };
00125 
00126 // specialized for const string
00127 template<> struct str< ::std::string const>: public str< ::std::string> {};
00128 template<> struct str<char const *>: public str< ::std::string> {};
00129 template<> struct str<char const []>: public str< ::std::string> {};
00130 
00131 template<> struct str< ::std::wstring>
00132 {
00133         template<typename T>
00134         static ::std::wstring cast(const T& t)
00135         {
00136                 ::std::ostringstream ss;
00137                 ss << t;
00138                 return converter<wchar_t, char>::get(ss.str());
00139         };
00140 
00141         static ::std::wstring cast(const ::std::string & t)             { return converter<wchar_t,char>::get(t); };
00142         static ::std::wstring cast(const ::std::wstring & t)    { return converter<wchar_t,wchar_t>::get(t); };
00143 
00144         static ::std::wstring cast(const char * t)                              { return converter<wchar_t,char>::get(t); };
00145         static ::std::wstring cast(const wchar_t * t)                   { return converter<wchar_t,wchar_t>::get(t); };
00146 
00147         static ::std::wstring cast(char t)                                              { return ::std::wstring(1,converter<wchar_t,char>::get(t)); };
00148         static ::std::wstring cast(wchar_t t)                                   { return ::std::wstring(1,converter<wchar_t,wchar_t>::get(t)); };
00149 };
00150 
00151 // specialized for const wstring
00152 template<> struct str< ::std::wstring const>: public str< ::std::wstring> {};
00153 template<> struct str<wchar_t const *>: public str< ::std::wstring> {};
00154 template<> struct str<wchar_t const []>: public str< ::std::wstring> {};
00155 
00156 XIMOL_END_NAMESPACE
00157 
00158 #endif // #ifndef XIMOL_STR_CAST_HPP_


Donate to the XiMoL project SourceForge.net Logo If you have any questions about XiMoL, you could write to tournois@users.sourceforge.net.