utility_stream.h

00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Rick L. Vinyard, Jr.                            *
00003  *   rvinyard@cs.nmsu.edu                                                  *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation version 2.1.                *
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 Lesser General Public      *
00015  *   License along with this library; if not, write to the                 *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
00018  ***************************************************************************/
00019 #ifndef IDIOSKOPOSUTILITY_STREAM_H
00020 #define IDIOSKOPOSUTILITY_STREAM_H
00021 
00022 #include <sstream>
00023 #include <vector>
00024 #include <idioskopos/array.h>
00025 
00026 namespace Idioskopos {
00027 
00028   // TODO Switch to a name registration rather than these overloaded functions
00029 
00046 template <typename T>
00047   inline Glib::ustring get_xml(const T& t, int indent=-1) {
00048     std::ostringstream sout;
00049     for (int i=0; i < indent; i++ ) sout << "  ";
00050     sout << "<value type=\""
00051          << typeid(T).name() << "\"/>";
00052     return sout.str();
00053   }
00054 
00055 template <typename T,typename A>
00056   inline Glib::ustring get_xml_vector( const std::vector<T,A>& a, int indent=-1 ) {
00057     std::ostringstream sout;
00058     int i;
00059     size_t n;
00060     for (i=0; i < indent; i++ ) sout << "  ";
00061     sout << "<vector type=\""
00062         << typeid(T).name() << "\" size=\""
00063         << a.size() << "\">";
00064     for (n=0; n < a.size(); n++) {
00065       if ( indent >= 0 ) sout << "\n";
00066       sout << get_xml( a[n], (indent>=0)?indent+1:indent );
00067     }
00068     if (indent >= 0 ) sout << "\n";
00069     for (i=0; i < indent; i++ ) sout << "  ";
00070     sout << "</vector>";
00071     return sout.str();
00072   }
00073 
00074 template <typename T, size_t N>
00075   inline Glib::ustring get_xml( const IdioskoposArray<T,N>& a, int indent=-1 ) {
00076     std::ostringstream sout;
00077     int i;
00078     size_t n;
00079     for (i=0; i < indent; i++ ) sout << "  ";
00080     sout << "<array type=\""
00081          << typeid(T).name() << "\" size=\""
00082          << N << "\">";
00083     for (n=0; n < N; n++) {
00084       if ( indent >= 0 ) sout << "\n";
00085       sout << get_xml( a[n], (indent>=0)?indent+1:indent );
00086     }
00087     if (indent >= 0 ) sout << "\n";
00088     for (i=0; i < indent; i++ ) sout << "  ";
00089     sout << "</array>";
00090     return sout.str();
00091   }
00092 
00093 
00099 #define IDIOSKOPOS_XML( Type, TypeString )                                           \
00100 inline Glib::ustring get_xml(const Type& t, int indent=-1) {                         \
00101   std::ostringstream out;                                                            \
00102   for (int i=0; i < indent; i++ )                                                    \
00103       out << "  ";                                                                   \
00104   out << "<value type=\""                                                            \
00105       << TypeString << "\">"                                                         \
00106       << t << "</value>";                                                            \
00107   return out.str();                                                                  \
00108   }                                                                                  \
00109                                                                                      \
00110 template <size_t N>                                                                  \
00111   inline Glib::ustring get_xml( const IdioskoposArray<Type,N>& a, int indent=-1 ) {  \
00112     std::ostringstream sout;                                                         \
00113     int i;                                                                           \
00114     size_t n;                                                                        \
00115     for (i=0; i < indent; i++ ) sout << "  ";                                        \
00116     sout << "<array type=\""                                                         \
00117          << TypeString << "\" size=\""                                               \
00118          << N << "\">";                                                              \
00119     for (n=0; n < N; n++) {                                                          \
00120       if ( indent >= 0 ) sout << "\n";                                               \
00121       sout << get_xml( a[n], (indent>=0)?indent+1:indent );                          \
00122     }                                                                                \
00123     if (indent >= 0 ) sout << "\n";                                                  \
00124     for (i=0; i < indent; i++ ) sout << "  ";                                        \
00125     sout << "</array>";                                                              \
00126     return sout.str();                                                               \
00127   }                                                                                  \
00128                                                                                      \
00129 template <typename A>                                                                \
00130 inline Glib::ustring get_xml_vector( const std::vector<Type,A>& a, int indent=-1 ) { \
00131   std::ostringstream sout;                                                           \
00132   int i;                                                                             \
00133   size_t n;                                                                          \
00134   for (i=0; i < indent; i++ ) sout << "  ";                                          \
00135   sout << "<vector type=\""                                                          \
00136        << TypeString << "\" size=\""                                                 \
00137        << a.size() << "\">";                                                         \
00138   for (n = 0; n < a.size(); n++) {                                                   \
00139     if ( indent >= 0 ) sout << "\n";                                                 \
00140     sout << get_xml( a[n], (indent>=0)?indent+1:indent );                            \
00141   }                                                                                  \
00142   if (indent >= 0 ) sout << "\n";                                                    \
00143   for (i=0; i < indent; i++ ) sout << "  ";                                          \
00144   sout << "</vector>";                                                               \
00145   return sout.str();                                                                 \
00146  }
00147 
00148 
00149 template<typename T>
00150   inline Glib::ustring get_value(const T& t) {
00151     return Glib::ustring();
00152   }
00153 
00158 #define IDIOSKOPOS_GET_VALUE( Type )             \
00159 inline Glib::ustring get_value(const Type& t) {  \
00160   std::ostringstream sout;                       \
00161   sout << t;                                     \
00162   return sout.str();                             \
00163   }
00164 
00165 
00166 template<typename T>
00167 inline void set_value(const Glib::ustring& s, T& t) {
00168   return;
00169 }
00170 
00175 #define IDIOSKOPOS_SET_VALUE( Type )                        \
00176   inline void set_value(const Glib::ustring& s, Type& t) {  \
00177   std::istringstream sin(s);                                \
00178   sin >> t;                                                 \
00179   return;                                                   \
00180 }
00181 
00186 #define IDIOSKOPOS_STREAMABLE( Type, TypeString )  \
00187   IDIOSKOPOS_XML( Type, TypeString )               \
00188   IDIOSKOPOS_GET_VALUE(Type )                      \
00189   IDIOSKOPOS_SET_VALUE(Type )
00190 
00191 IDIOSKOPOS_STREAMABLE(float,"float");
00192 IDIOSKOPOS_STREAMABLE(double,"double");
00193 IDIOSKOPOS_STREAMABLE(long double,"long double");
00194 IDIOSKOPOS_STREAMABLE(short int,"short int");
00195 IDIOSKOPOS_STREAMABLE(unsigned short int,"unsigned short int");
00196 IDIOSKOPOS_STREAMABLE(int,"int");
00197 IDIOSKOPOS_STREAMABLE(unsigned int,"unsigned int");
00198 IDIOSKOPOS_STREAMABLE(long int,"long int");
00199 IDIOSKOPOS_STREAMABLE(unsigned long int,"unsigned long int");
00200 IDIOSKOPOS_STREAMABLE(char,"char");
00201 IDIOSKOPOS_STREAMABLE(unsigned char,"unsigned char");
00202 IDIOSKOPOS_STREAMABLE(bool,"bool");
00203 IDIOSKOPOS_STREAMABLE(std::string,"std::string");
00204 IDIOSKOPOS_STREAMABLE(Glib::ustring,"Glib::ustring");
00205 
00206 }
00207 
00208 #endif
00209 

Generated on Thu Jan 11 00:26:41 2007 by  doxygen 1.5.1