![]() |
QxOrm
1.4.5
C++ Object Relational Mapping library
|
00001 /**************************************************************************** 00002 ** 00003 ** https://www.qxorm.com/ 00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com) 00005 ** 00006 ** This file is part of the QxOrm library 00007 ** 00008 ** This software is provided 'as-is', without any express or implied 00009 ** warranty. In no event will the authors be held liable for any 00010 ** damages arising from the use of this software 00011 ** 00012 ** Commercial Usage 00013 ** Licensees holding valid commercial QxOrm licenses may use this file in 00014 ** accordance with the commercial license agreement provided with the 00015 ** Software or, alternatively, in accordance with the terms contained in 00016 ** a written agreement between you and Lionel Marty 00017 ** 00018 ** GNU General Public License Usage 00019 ** Alternatively, this file may be used under the terms of the GNU 00020 ** General Public License version 3.0 as published by the Free Software 00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the 00022 ** packaging of this file. Please review the following information to 00023 ** ensure the GNU General Public License version 3.0 requirements will be 00024 ** met : http://www.gnu.org/copyleft/gpl.html 00025 ** 00026 ** If you are unsure which license is appropriate for your use, or 00027 ** if you have questions regarding the use of this file, please contact : 00028 ** contact@qxorm.com 00029 ** 00030 ****************************************************************************/ 00031 00032 #ifndef _QX_CONVERT_IMPL_H_ 00033 #define _QX_CONVERT_IMPL_H_ 00034 00035 #ifdef _MSC_VER 00036 #pragma once 00037 #endif 00038 00039 #include <QxConvert/QxConvert.h> 00040 #include <QxCommon/QxBool.h> 00041 00042 #include <QxDao/QxDateNeutral.h> 00043 #include <QxDao/QxTimeNeutral.h> 00044 #include <QxDao/QxDateTimeNeutral.h> 00045 #include <QxDao/QxSqlQuery.h> 00046 #include <QxDao/IxPersistable.h> 00047 00048 #include <QxCollection/QxCollection.h> 00049 00050 #include <QxRegister/QxClass.h> 00051 00052 #include <QxSerialize/QxArchive.h> 00053 #include <QxSerialize/QxSerializeQDataStream.h> 00054 #include <QxSerialize/QDataStream/QxSerializeQDataStream_all_include.h> 00055 #include <QxSerialize/QJson/QxSerializeQJson_qx_registered_class.h> 00056 00057 #include <QxValidator/QxInvalidValue.h> 00058 #include <QxValidator/QxInvalidValueX.h> 00059 00060 #include <QxTraits/is_smart_ptr.h> 00061 #include <QxTraits/is_container.h> 00062 #include <QxTraits/is_qx_registered.h> 00063 #include <QxTraits/is_qt_variant_compatible.h> 00064 #include <QxTraits/get_class_name_primitive.h> 00065 #include <QxTraits/construct_ptr.h> 00066 #include <QxTraits/generic_container.h> 00067 00068 #define QX_STR_CVT_QDATE_FORMAT "yyyyMMdd" 00069 #define QX_STR_CVT_QTIME_FORMAT "hhmmsszzz" 00070 #define QX_STR_CVT_QDATETIME_FORMAT "yyyyMMddhhmmsszzz" 00071 00072 #ifdef _QX_ENABLE_BOOST_SERIALIZATION 00073 #if _QX_SERIALIZE_POLYMORPHIC 00074 #define QX_CVT_DEFAULT_ARCHIVE qx::serialization::polymorphic_xml 00075 #elif _QX_SERIALIZE_XML 00076 #define QX_CVT_DEFAULT_ARCHIVE qx::serialization::xml 00077 #elif _QX_SERIALIZE_TEXT 00078 #define QX_CVT_DEFAULT_ARCHIVE qx::serialization::text 00079 #elif _QX_SERIALIZE_BINARY 00080 #define QX_CVT_DEFAULT_ARCHIVE qx::serialization::binary 00081 #endif // _QX_SERIALIZE_XML 00082 #else // _QX_ENABLE_BOOST_SERIALIZATION 00083 #define QX_CVT_DEFAULT_ARCHIVE qx::serialization::qt 00084 #endif // _QX_ENABLE_BOOST_SERIALIZATION 00085 00086 #define QX_CVT_USING_ARCHIVE_IMPL(className) \ 00087 namespace qx { namespace cvt { namespace detail { \ 00088 template <> struct QxConvert_ToString< className > { \ 00089 static inline QString toString(const className & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) \ 00090 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::to_string(t); } }; \ 00091 template <> struct QxConvert_FromString< className > { \ 00092 static inline qx_bool fromString(const QString & s, className & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) \ 00093 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::from_string(t, s); } }; \ 00094 template <> struct QxConvert_ToVariant< className > { \ 00095 static inline QVariant toVariant(const className & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) \ 00096 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::to_string(t); } }; \ 00097 template <> struct QxConvert_FromVariant< className > { \ 00098 static inline qx_bool fromVariant(const QVariant & v, className & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) \ 00099 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); QString s = v.toString(); return QX_CVT_DEFAULT_ARCHIVE::from_string(t, s); } }; \ 00100 } } } // namespace qx::cvt::detail 00101 00102 namespace qx { 00103 namespace cvt { 00104 namespace detail { 00105 namespace helper { 00106 00107 struct QxConvertHelper_Generic { }; 00108 struct QxConvertHelper_Ptr { }; 00109 struct QxConvertHelper_Registered { }; 00110 struct QxConvertHelper_Persistable { }; 00111 struct QxConvertHelper_Container { }; 00112 struct QxConvertHelper_Enum { }; 00113 00114 inline bool checkConvertQVariantToString(const QVariant & v) { return ((v.type() == QVariant::List) || (v.type() == QVariant::Map) || (v.type() == QVariant::Hash) || (v.type() == QVariant::StringList)); } 00115 00116 } // namespace helper 00117 00118 template <typename T, typename H> 00119 struct QxConvertHelper_ToString 00120 { 00121 static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00122 { qAssertMsg(false, "qx::cvt::detail::QxConvertHelper_ToString", "template must be specialized"); Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QString(); } 00123 }; 00124 00125 template <typename T, typename H> 00126 struct QxConvertHelper_FromString 00127 { 00128 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00129 { qAssertMsg(false, "qx::cvt::detail::QxConvertHelper_FromString", "template must be specialized"); Q_UNUSED(s); Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return qx_bool(); } 00130 }; 00131 00132 template <typename T, typename H> 00133 struct QxConvertHelper_ToVariant 00134 { 00135 static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00136 { qAssertMsg(false, "qx::cvt::detail::QxConvertHelper_ToVariant", "template must be specialized"); Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QVariant(); } 00137 }; 00138 00139 template <typename T, typename H> 00140 struct QxConvertHelper_FromVariant 00141 { 00142 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00143 { qAssertMsg(false, "qx::cvt::detail::QxConvertHelper_FromVariant", "template must be specialized"); Q_UNUSED(v); Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return qx_bool(); } 00144 }; 00145 00146 #ifndef _QX_NO_JSON 00147 00148 template <typename T, typename H> 00149 struct QxConvertHelper_ToJson 00150 { 00151 static inline QJsonValue toJson(const T & t, const QString & format) 00152 { qAssertMsg(false, "qx::cvt::detail::QxConvertHelper_ToJson", "template must be specialized"); Q_UNUSED(t); Q_UNUSED(format); return QJsonValue(); } 00153 }; 00154 00155 template <typename T, typename H> 00156 struct QxConvertHelper_FromJson 00157 { 00158 static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format) 00159 { qAssertMsg(false, "qx::cvt::detail::QxConvertHelper_FromJson", "template must be specialized"); Q_UNUSED(j); Q_UNUSED(t); Q_UNUSED(format); return qx_bool(); } 00160 }; 00161 00162 #endif // _QX_NO_JSON 00163 00164 template <typename T> 00165 struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Generic> 00166 { 00167 00168 enum { qx_need_to_specialize_template_convert_to_string_from_string = 0 }; 00169 00170 static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00171 { 00172 Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); 00173 static_assert(qx_need_to_specialize_template_convert_to_string_from_string, "qx_need_to_specialize_template_convert_to_string_from_string"); // If a compilation error occurred here : you have to specialize template 'qx::cvt::detail::QxConvert_ToString< MyType >' 00174 return QString(); 00175 } 00176 00177 }; 00178 00179 template <typename T> 00180 struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Generic> 00181 { 00182 00183 enum { qx_need_to_specialize_template_convert_to_string_from_string = 0 }; 00184 00185 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00186 { 00187 Q_UNUSED(s); Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); 00188 static_assert(qx_need_to_specialize_template_convert_to_string_from_string, "qx_need_to_specialize_template_convert_to_string_from_string"); // If a compilation error occurred here : you have to specialize template 'qx::cvt::detail::QxConvert_FromString< MyType >' 00189 return qx_bool(true); 00190 } 00191 00192 }; 00193 00194 template <typename T> 00195 struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Generic> 00196 { 00197 00198 static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00199 { return cvtQVariant<qx::trait::is_qt_variant_compatible<T>::value, 0>::toVariant(t, format, index, ctx); } 00200 00201 private: 00202 00203 template <bool isQVariantCompatible /* = false */, int dummy> 00204 struct cvtQVariant 00205 { static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) { return qx::cvt::to_string(t, format, index, ctx); }; }; 00206 00207 template <int dummy> 00208 struct cvtQVariant<true, dummy> 00209 { static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QVariant(t); }; }; 00210 00211 }; 00212 00213 template <typename T> 00214 struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Generic> 00215 { 00216 00217 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00218 { return cvtQVariant<qx::trait::is_qt_variant_compatible<T>::value, 0>::fromVariant(v, t, format, index, ctx); } 00219 00220 private: 00221 00222 template <bool isQVariantCompatible /* = false */, int dummy> 00223 struct cvtQVariant 00224 { static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) { return qx::cvt::from_string(v.toString(), t, format, index, ctx); }; }; 00225 00226 template <int dummy> 00227 struct cvtQVariant<true, dummy> 00228 { static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); t = v.value<T>(); return qx_bool(true); }; }; 00229 00230 }; 00231 00232 #ifndef _QX_NO_JSON 00233 00234 template <typename T> 00235 struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Generic> 00236 { 00237 static inline QJsonValue toJson(const T & t, const QString & format) 00238 { return QJsonValue::fromVariant(qx::cvt::detail::QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Generic>::toVariant(t, format, -1, qx::cvt::context::e_no_context)); } 00239 }; 00240 00241 template <typename T> 00242 struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Generic> 00243 { 00244 static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format) 00245 { QVariant v = j.toVariant(); return qx::cvt::detail::QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Generic>::fromVariant(v, t, format, -1, qx::cvt::context::e_no_context); } 00246 }; 00247 00248 #endif // _QX_NO_JSON 00249 00250 template <typename T> 00251 struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Persistable> 00252 { 00253 00254 enum { qx_need_to_specialize_template_convert_to_string_from_string = 0 }; 00255 00256 static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00257 { 00258 Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); 00259 #ifndef _QX_NO_JSON 00260 return t.toJson(format); 00261 #else // _QX_NO_JSON 00262 static_assert(qx_need_to_specialize_template_convert_to_string_from_string, "qx_need_to_specialize_template_convert_to_string_from_string"); // If a compilation error occurred here : you have to specialize template 'qx::cvt::detail::QxConvert_ToString< MyType >' 00263 return QString(); 00264 #endif // _QX_NO_JSON 00265 } 00266 00267 }; 00268 00269 template <typename T> 00270 struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Persistable> 00271 { 00272 00273 enum { qx_need_to_specialize_template_convert_to_string_from_string = 0 }; 00274 00275 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00276 { 00277 Q_UNUSED(s); Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); 00278 #ifndef _QX_NO_JSON 00279 return t.fromJson(s, format); 00280 #else // _QX_NO_JSON 00281 static_assert(qx_need_to_specialize_template_convert_to_string_from_string, "qx_need_to_specialize_template_convert_to_string_from_string"); // If a compilation error occurred here : you have to specialize template 'qx::cvt::detail::QxConvert_FromString< MyType >' 00282 return qx_bool(true); 00283 #endif // _QX_NO_JSON 00284 } 00285 00286 }; 00287 00288 template <typename T> 00289 struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Persistable> 00290 { 00291 00292 enum { qx_need_to_specialize_template_convert_to_variant_from_variant = 0 }; 00293 00294 static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00295 { 00296 Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); 00297 #ifndef _QX_NO_JSON 00298 return QVariant(t.toJson(format)); 00299 #else // _QX_NO_JSON 00300 static_assert(qx_need_to_specialize_template_convert_to_variant_from_variant, "qx_need_to_specialize_template_convert_to_variant_from_variant"); // If a compilation error occurred here : you have to specialize template 'qx::cvt::detail::QxConvert_ToVariant< MyType >' 00301 return QVariant(); 00302 #endif // _QX_NO_JSON 00303 } 00304 00305 }; 00306 00307 template <typename T> 00308 struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Persistable> 00309 { 00310 00311 enum { qx_need_to_specialize_template_convert_to_variant_from_variant = 0 }; 00312 00313 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00314 { 00315 Q_UNUSED(v); Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); 00316 #ifndef _QX_NO_JSON 00317 return t.fromJson(v.toString(), format); 00318 #else // _QX_NO_JSON 00319 static_assert(qx_need_to_specialize_template_convert_to_variant_from_variant, "qx_need_to_specialize_template_convert_to_variant_from_variant"); // If a compilation error occurred here : you have to specialize template 'qx::cvt::detail::QxConvert_FromVariant< MyType >' 00320 return qx_bool(true); 00321 #endif // _QX_NO_JSON 00322 } 00323 00324 }; 00325 00326 #ifndef _QX_NO_JSON 00327 00328 template <typename T> 00329 struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Persistable> 00330 { 00331 static inline QJsonValue toJson(const T & t, const QString & format) 00332 { const qx::IxPersistable * p = static_cast<const qx::IxPersistable *>(& t); return qx::cvt::to_json((* p), format); } 00333 }; 00334 00335 template <typename T> 00336 struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Persistable> 00337 { 00338 static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format) 00339 { qx::IxPersistable * p = static_cast<qx::IxPersistable *>(& t); return qx::cvt::from_json(j, (* p), format); } 00340 }; 00341 00342 #endif // _QX_NO_JSON 00343 00344 template <typename T> 00345 struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Ptr> 00346 { 00347 static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00348 { return (t ? qx::cvt::to_string((* t), format, index, ctx) : ""); } 00349 }; 00350 00351 template <typename T> 00352 struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Ptr> 00353 { 00354 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00355 { if (! t) { qx::trait::construct_ptr<T>::get(t); }; return (t ? qx::cvt::from_string(s, (* t), format, index, ctx) : qx_bool(false)); } 00356 }; 00357 00358 template <typename T> 00359 struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Ptr> 00360 { 00361 static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00362 { return (t ? qx::cvt::to_variant((* t), format, index, ctx) : QVariant()); } 00363 }; 00364 00365 template <typename T> 00366 struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Ptr> 00367 { 00368 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00369 { if (! t && ! v.isNull()) { qx::trait::construct_ptr<T>::get(t); }; return (t ? qx::cvt::from_variant(v, (* t), format, index, ctx) : qx_bool(false)); } 00370 }; 00371 00372 #ifndef _QX_NO_JSON 00373 00374 template <typename T> 00375 struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Ptr> 00376 { 00377 static inline QJsonValue toJson(const T & t, const QString & format) 00378 { return (t ? qx::cvt::to_json((* t), format) : QJsonValue()); } 00379 }; 00380 00381 template <typename T> 00382 struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Ptr> 00383 { 00384 static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format) 00385 { if (! t && ! j.isNull()) { qx::trait::construct_ptr<T>::get(t); }; return (t ? qx::cvt::from_json(j, (* t), format) : qx_bool(false)); } 00386 }; 00387 00388 #endif // _QX_NO_JSON 00389 00390 template <typename T> 00391 struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Registered> 00392 { 00393 static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00394 { 00395 if (ctx != qx::cvt::context::e_serialize_registered) 00396 { qx::IxDataMember * pId = qx::QxClass<T>::getSingleton()->getDataMemberX()->getId_WithDaoStrategy(); return (pId ? pId->toVariant((& t), format, index, ctx).toString() : QString()); } 00397 return qx::serialization::qt::to_string<T>(t); 00398 } 00399 }; 00400 00401 template <typename T> 00402 struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Registered> 00403 { 00404 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00405 { 00406 if (ctx != qx::cvt::context::e_serialize_registered) 00407 { qx::IxDataMember * pId = qx::QxClass<T>::getSingleton()->getDataMemberX()->getId_WithDaoStrategy(); QVariant tmp(s); return (pId ? pId->fromVariant((& t), tmp, format, index, ctx) : qx_bool(false)); } 00408 return qx::serialization::qt::from_string<T>(t, s); 00409 } 00410 }; 00411 00412 template <typename T> 00413 struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Registered> 00414 { 00415 static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00416 { 00417 if (ctx != qx::cvt::context::e_serialize_registered) 00418 { qx::IxDataMember * pId = qx::QxClass<T>::getSingleton()->getDataMemberX()->getId_WithDaoStrategy(); return (pId ? pId->toVariant((& t), format, index, ctx) : QVariant()); } 00419 return qx::serialization::qt::to_byte_array<T>(t); 00420 } 00421 }; 00422 00423 template <typename T> 00424 struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Registered> 00425 { 00426 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00427 { 00428 if (ctx != qx::cvt::context::e_serialize_registered) 00429 { qx::IxDataMember * pId = qx::QxClass<T>::getSingleton()->getDataMemberX()->getId_WithDaoStrategy(); return (pId ? pId->fromVariant((& t), v, format, index, ctx) : qx_bool(false)); } 00430 return qx::serialization::qt::from_byte_array<T>(t, v.toByteArray()); 00431 } 00432 }; 00433 00434 #ifndef _QX_NO_JSON 00435 00436 template <typename T> 00437 struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Registered> 00438 { 00439 static inline QJsonValue toJson(const T & t, const QString & format) 00440 { return qx::cvt::detail::QxSerializeJsonRegistered<T>::save(t, format); } 00441 }; 00442 00443 template <typename T> 00444 struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Registered> 00445 { 00446 static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format) 00447 { return qx::cvt::detail::QxSerializeJsonRegistered<T>::load(j, t, format); } 00448 }; 00449 00450 #endif // _QX_NO_JSON 00451 00452 template <typename T> 00453 struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Container> 00454 { 00455 static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00456 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::to_string(t); } 00457 }; 00458 00459 template <typename T> 00460 struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Container> 00461 { 00462 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00463 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::from_string(t, s); } 00464 }; 00465 00466 template <typename T> 00467 struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Container> 00468 { 00469 static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00470 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::to_string(t); } 00471 }; 00472 00473 template <typename T> 00474 struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Container> 00475 { 00476 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00477 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::from_string(t, v.toString()); } 00478 }; 00479 00480 #ifndef _QX_NO_JSON 00481 00482 template <typename T> 00483 struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Container> 00484 { 00485 static inline QJsonValue toJson(const T & t, const QString & format) 00486 { return QJsonValue::fromVariant(qx::cvt::detail::QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Container>::toVariant(t, format, -1, qx::cvt::context::e_no_context)); } 00487 }; 00488 00489 template <typename T> 00490 struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Container> 00491 { 00492 static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format) 00493 { QVariant v = j.toVariant(); return qx::cvt::detail::QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Container>::fromVariant(v, t, format, -1, qx::cvt::context::e_no_context); } 00494 }; 00495 00496 #endif // _QX_NO_JSON 00497 00498 template <typename T> 00499 struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Enum> 00500 { 00501 static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00502 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QString::number(static_cast<long>(t)); } 00503 }; 00504 00505 template <typename T> 00506 struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Enum> 00507 { 00508 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00509 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); bool bOk = false; t = static_cast<T>(static_cast<long>(s.toLongLong(& bOk))); return bOk; } 00510 }; 00511 00512 template <typename T> 00513 struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Enum> 00514 { 00515 static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00516 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QVariant(static_cast<qlonglong>(t)); } 00517 }; 00518 00519 template <typename T> 00520 struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Enum> 00521 { 00522 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00523 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); bool bOk = false; t = static_cast<T>(static_cast<long>(v.toLongLong(& bOk))); return bOk; } 00524 }; 00525 00526 #ifndef _QX_NO_JSON 00527 00528 template <typename T> 00529 struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Enum> 00530 { 00531 static inline QJsonValue toJson(const T & t, const QString & format) 00532 { Q_UNUSED(format); return QJsonValue(static_cast<int>(t)); } 00533 }; 00534 00535 template <typename T> 00536 struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Enum> 00537 { 00538 static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format) 00539 { Q_UNUSED(format); t = static_cast<T>(static_cast<long>(qRound(j.toDouble()))); return qx_bool(true); } 00540 }; 00541 00542 #endif // _QX_NO_JSON 00543 00544 template <typename T> 00545 struct QxConvertHelper 00546 { 00547 00548 private: 00549 00550 typedef typename std::conditional< qx::trait::is_ix_persistable<T>::value, qx::cvt::detail::helper::QxConvertHelper_Persistable, qx::cvt::detail::helper::QxConvertHelper_Generic >::type type_str_cvt_helper_0; 00551 typedef typename std::conditional< std::is_pointer<T>::value, qx::cvt::detail::helper::QxConvertHelper_Ptr, type_str_cvt_helper_0 >::type type_str_cvt_helper_1; 00552 typedef typename std::conditional< qx::trait::is_smart_ptr<T>::value, qx::cvt::detail::helper::QxConvertHelper_Ptr, type_str_cvt_helper_1 >::type type_str_cvt_helper_2; 00553 typedef typename std::conditional< qx::trait::is_container<T>::value, qx::cvt::detail::helper::QxConvertHelper_Container, type_str_cvt_helper_2 >::type type_str_cvt_helper_3; 00554 typedef typename std::conditional< qx::trait::is_qx_registered<T>::value, qx::cvt::detail::helper::QxConvertHelper_Registered, type_str_cvt_helper_3 >::type type_str_cvt_helper_4; 00555 typedef typename std::conditional< std::is_enum<T>::value, qx::cvt::detail::helper::QxConvertHelper_Enum, type_str_cvt_helper_4 >::type type_str_cvt_helper_5; 00556 00557 public: 00558 00559 typedef typename QxConvertHelper<T>::type_str_cvt_helper_5 type; 00560 00561 }; 00562 00563 template <typename T> 00564 struct QxConvert_ToString 00565 { 00566 static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00567 { return qx::cvt::detail::QxConvertHelper_ToString<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::toString(t, format, index, ctx); } 00568 }; 00569 00570 template <typename T> 00571 struct QxConvert_FromString 00572 { 00573 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00574 { return qx::cvt::detail::QxConvertHelper_FromString<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::fromString(s, t, format, index, ctx); } 00575 }; 00576 00577 template <typename T> 00578 struct QxConvert_ToVariant 00579 { 00580 static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00581 { return qx::cvt::detail::QxConvertHelper_ToVariant<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::toVariant(t, format, index, ctx); } 00582 }; 00583 00584 template <typename T> 00585 struct QxConvert_FromVariant 00586 { 00587 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00588 { return qx::cvt::detail::QxConvertHelper_FromVariant<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::fromVariant(v, t, format, index, ctx); } 00589 }; 00590 00591 #ifndef _QX_NO_JSON 00592 00593 template <typename T> 00594 struct QxConvert_ToJson 00595 { 00596 static inline QJsonValue toJson(const T & t, const QString & format) 00597 { return qx::cvt::detail::QxConvertHelper_ToJson<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::toJson(t, format); } 00598 }; 00599 00600 template <typename T> 00601 struct QxConvert_FromJson 00602 { 00603 static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format) 00604 { return qx::cvt::detail::QxConvertHelper_FromJson<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::fromJson(j, t, format); } 00605 }; 00606 00607 #endif // _QX_NO_JSON 00608 00609 } // namespace detail 00610 } // namespace cvt 00611 } // namespace qx 00612 00613 #include "../../inl/QxConvert/QxConvert_WithIndex.inl" 00614 #include "../../inl/QxConvert/QxConvert_ToString.inl" 00615 #include "../../inl/QxConvert/QxConvert_FromString.inl" 00616 #include "../../inl/QxConvert/QxConvert_ToVariant.inl" 00617 #include "../../inl/QxConvert/QxConvert_FromVariant.inl" 00618 #include "../../inl/QxConvert/QxConvert_ToJson.inl" 00619 #include "../../inl/QxConvert/QxConvert_FromJson.inl" 00620 #include "../../inl/QxConvert/QxConvert_Qt.inl" 00621 00622 #endif // _QX_CONVERT_IMPL_H_