00001 /* *\ 00002 ** Copyright (C) 2009-2010 ** 00003 ** Dominik Charousset < Dominik.Charousset (at) haw-hamburg.de > ** 00004 ** ** 00005 ** MMM MMMMMM MMMMMMMMM MMMMMMM MMM MMM ** 00006 ** MMMMM MMMMMMMM MMMMMMMMM MMMMMMMM MMM MMMMM ** 00007 ** MMM MMM MMM MMMM MMM MMM MMM MMM MMM ** 00008 ** MMM MMM MMM MMMMMMMMM MMM MMM MMM MMM MMM ** 00009 ** MMMMMMMMMMM MMMM MMM MMMM MMM MMM MMM MMMMMMMMMMM ** 00010 ** MMM MMM MMMMMMMM MMMMMMMMM MMMMMMMM MMM MMM MMM ** 00011 ** MMM MMM MMMMMM MMMMMMMMM MMMMMMM MMM MMM MMM ** 00012 ** ** 00013 ** ** 00014 ** This file is part of the acedia library. ** 00015 ** ** 00016 ** This library is free software: you can redistribute it and/or modify ** 00017 ** it under the terms of the GNU Lesser General Public License as published ** 00018 ** by the Free Software Foundation, either version 3 of the License, or ** 00019 ** (at your option) any later version. ** 00020 ** ** 00021 ** This library is distributed in the hope that it will be useful, ** 00022 ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** 00023 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** 00024 ** GNU Lesser General Public License for more details. ** 00025 ** ** 00026 ** You should have received a copy of the GNU Lesser General Public License ** 00027 ** along with acedia. If not, see <http://www.gnu.org/licenses/>. ** 00028 \* */ 00029 00030 #ifndef ACEDIA_TO_STRING_CONVERTER_H 00031 #define ACEDIA_TO_STRING_CONVERTER_H 00032 00033 #include <sstream> 00034 #include <boost/cstdint.hpp> 00035 00036 #include "metaclass.hpp" 00037 #include "metaclassimpl.hpp" 00038 00039 #include "acedia_string.hpp" 00040 00041 #include "acedia_util.hpp" 00042 00043 namespace acedia 00044 { 00045 00046 class Tuple; 00047 00048 String tupleToString(MetaClass *mc, const Tuple &tuple, bool verbose); 00049 00057 template<typename T> 00058 struct ToStringConverter 00059 { 00060 static String toString(const T& value, bool verbose = false) 00061 { 00062 MetaClass* mc = details::MetaClassImpl<T>::instance(); 00063 if (mc->isCaseTuple()) 00064 { 00065 return tupleToString(mc,reinterpret_cast<const Tuple &>(value), 00066 verbose); 00067 } 00068 else if (util::IsCaseClass<T>::VALUE) 00069 { 00070 // do only return the class name 00071 return mc->name(); 00072 } 00073 if (verbose) 00074 { 00075 String res = mc->name(); 00076 res += "("; 00077 res += util::AsString<T>::get(value); 00078 res += ")"; 00079 return res; 00080 } 00081 return util::AsString<T>::get(value); 00082 // return "-?-"; 00083 } 00084 }; 00085 00086 /* 00087 template<typename T> 00088 inline String defaultToStringImpl(const T &value, bool verbose) 00089 { 00090 std::ostringstream ostr; 00091 if (verbose) 00092 { 00093 MetaClass *mc = details::MetaClassImpl<T>::instance(); 00094 ostr << mc->name().const_str(); 00095 ostr << "(" << value << ")"; 00096 } 00097 else ostr << value; 00098 return ostr.str(); 00099 } 00100 00101 template<> 00102 struct ToStringConverter<bool> 00103 { 00104 static String toString(const bool &value, bool verbose = false) 00105 { 00106 verbose = verbose; // suppress compiler warning 00107 return value ? "true" : "false"; 00108 } 00109 }; 00110 00111 template<> 00112 struct ToStringConverter<boost::int8_t> 00113 { 00114 static String toString(const boost::int8_t &value, bool verbose = false) 00115 { 00116 return defaultToStringImpl(value, verbose); 00117 } 00118 }; 00119 00120 template<> 00121 struct ToStringConverter<boost::uint8_t> 00122 { 00123 static String toString(const boost::uint8_t &value, bool verbose = false) 00124 { 00125 return defaultToStringImpl(value, verbose); 00126 } 00127 }; 00128 00129 template<> struct ToStringConverter<boost::int16_t> 00130 { 00131 static String toString(const boost::int16_t &value, bool verbose = false) 00132 { 00133 return defaultToStringImpl(value, verbose); 00134 } 00135 }; 00136 00137 template<> struct ToStringConverter<boost::uint16_t> 00138 { 00139 static String toString(const boost::uint16_t &value, bool verbose = false) 00140 { 00141 return defaultToStringImpl(value, verbose); 00142 } 00143 }; 00144 00145 template<> struct ToStringConverter<boost::int32_t> 00146 { 00147 static String toString(const boost::int32_t &value, bool verbose = false) 00148 { 00149 return defaultToStringImpl(value, verbose); 00150 } 00151 }; 00152 00153 template<> struct ToStringConverter<boost::uint32_t> 00154 { 00155 static String toString(const boost::uint32_t &value, bool verbose = false) 00156 { 00157 return defaultToStringImpl(value, verbose); 00158 } 00159 }; 00160 00161 template<> struct ToStringConverter<boost::int64_t> 00162 { 00163 static String toString(const boost::int64_t &value, bool verbose = false) 00164 { 00165 return defaultToStringImpl(value, verbose); 00166 } 00167 }; 00168 00169 template<> struct ToStringConverter<boost::uint64_t> 00170 { 00171 static String toString(const boost::uint64_t &value, bool verbose = false) 00172 { 00173 return defaultToStringImpl(value, verbose); 00174 } 00175 }; 00176 00177 template<> struct ToStringConverter<float> 00178 { 00179 static String toString(const float &value, bool verbose = false) 00180 { 00181 return defaultToStringImpl(value, verbose); 00182 } 00183 }; 00184 00185 template<> struct ToStringConverter<double> 00186 { 00187 static String toString(const double &value, bool verbose = false) 00188 { 00189 return defaultToStringImpl(value, verbose); 00190 } 00191 }; 00192 */ 00193 00194 template<> 00195 struct ToStringConverter<String> 00196 { 00197 // String output is never verbose 00198 static String toString(const String &value, bool) 00199 { 00200 String res; 00201 res.reserve(value.size() + 2); 00202 res += '"'; 00203 res += value; 00204 res += '"'; 00205 return res; 00206 } 00207 }; 00208 00209 } // namespace acedia 00210 00211 #endif