stringify.hh
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef PALUDIS_GUARD_PALUDIS_STRINGIFY_HH
00021 #define PALUDIS_GUARD_PALUDIS_STRINGIFY_HH 1
00022
00023 #include <paludis/util/attributes.hh>
00024 #include <paludis/util/validated-fwd.hh>
00025 #include <tr1/memory>
00026 #include <sstream>
00027 #include <string>
00028
00029 #ifdef PALUDIS_HAVE_CONCEPTS
00030 # include <concepts>
00031 #endif
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 namespace paludis
00044 {
00045 #ifdef PALUDIS_HAVE_CONCEPTS
00046 auto concept IsStreamStringifiable<typename T_>
00047 {
00048 requires ! std::Dereferenceable<T_>;
00049 std::ostream & operator<< (std::ostream &, const T_ &);
00050 };
00051
00052 auto concept IsStringifiable<typename T_>
00053 {
00054 std::string stringify(const T_ &);
00055 };
00056 #endif
00057
00058 #ifndef PALUDIS_HAVE_CONCEPTS
00059
00060
00061
00062
00063
00064 namespace stringify_internals
00065 {
00066
00067
00068
00069
00070
00071 template <typename T_>
00072 struct CheckType
00073 {
00074
00075 enum { value = 0 } Value;
00076 };
00077
00078
00079
00080
00081
00082
00083
00084 template <typename T_>
00085 struct CheckType<T_ *>
00086 {
00087 };
00088
00089
00090
00091
00092
00093
00094
00095 template <typename T_>
00096 struct CheckType<std::tr1::shared_ptr<T_> >
00097 {
00098 };
00099
00100
00101
00102
00103
00104
00105
00106 template <>
00107 struct CheckType<char *>
00108 {
00109
00110 enum { value = 0 } Value;
00111 };
00112 }
00113 #endif
00114
00115 template <typename T_> inline std::string stringify(const T_ & item);
00116
00117 namespace stringify_internals
00118 {
00119
00120
00121
00122
00123
00124
00125
00126 template <typename T_>
00127 #ifdef PALUDIS_HAVE_CONCEPTS
00128 requires IsStreamStringifiable<T_>
00129 #endif
00130 std::string
00131 real_stringify(const T_ & item)
00132 {
00133 #ifndef PALUDIS_HAVE_CONCEPTS
00134
00135 int check_for_stringifying_silly_things
00136 PALUDIS_ATTRIBUTE((unused)) = CheckType<T_>::value;
00137 #endif
00138
00139 std::ostringstream s;
00140 s << item;
00141 return s.str();
00142 }
00143
00144 inline std::string
00145 real_stringify(const std::string & item)
00146 {
00147 return item;
00148 }
00149
00150 inline std::string
00151 real_stringify(const char & item)
00152 {
00153 return std::string(1, item);
00154 }
00155
00156 inline std::string
00157 real_stringify(const unsigned char & item)
00158 {
00159 return std::string(1, item);
00160 }
00161
00162 inline std::string
00163 real_stringify(const bool & item)
00164 {
00165 return item ? "true" : "false";
00166 }
00167
00168 inline std::string
00169 real_stringify(const char * const item)
00170 {
00171 return std::string(item);
00172 }
00173
00174 template <typename D_, typename V_, bool c_, typename C_>
00175 inline std::string
00176 real_stringify(const Validated<D_, V_, c_, C_> & v)
00177 {
00178 return stringify(v.data());
00179 }
00180 }
00181
00182
00183
00184
00185
00186
00187
00188 template <typename T_>
00189 inline std::string
00190 stringify(const T_ & item)
00191 {
00192 return stringify_internals::real_stringify(item);
00193 }
00194 }
00195
00196 #endif