![]() |
QxOrm
1.5.1
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 #if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0)) 00103 #define QX_JSON_DATE_TIME_FORMAT Qt::ISODateWithMs 00104 #define QX_JSON_DATE_TIME_FORMAT_SIZE 23 // yyyy-MM-ddTHH:mm:ss.zzz 00105 #else // (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0)) 00106 #define QX_JSON_DATE_TIME_FORMAT Qt::ISODate 00107 #define QX_JSON_DATE_TIME_FORMAT_SIZE 19 // yyyy-MM-ddTHH:mm:ss 00108 #endif // (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0)) 00109 00110 namespace qx { 00111 namespace cvt { 00112 namespace detail { 00113 namespace helper { 00114 00115 struct QxConvertHelper_Generic { }; 00116 struct QxConvertHelper_Ptr { }; 00117 struct QxConvertHelper_Registered { }; 00118 struct QxConvertHelper_Persistable { }; 00119 struct QxConvertHelper_Container { }; 00120 struct QxConvertHelper_Enum { }; 00121 00122 inline bool checkConvertQVariantToString(const QVariant & v) 00123 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) 00124 { int type = v.typeId(); return ((type == qMetaTypeId<QVariantList>()) || (type == qMetaTypeId<QVariantMap>()) || (type == qMetaTypeId<QVariantHash>()) || (type == qMetaTypeId<QStringList>())); } 00125 #else // (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) 00126 { QVariant::Type type = v.type(); return ((type == QVariant::List) || (type == QVariant::Map) || (type == QVariant::Hash) || (type == QVariant::StringList)); } 00127 #endif // (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) 00128 00129 } // namespace helper 00130 00131 template <typename T, typename H> 00132 struct QxConvertHelper_ToString 00133 { 00134 static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00135 { 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(); } 00136 }; 00137 00138 template <typename T, typename H> 00139 struct QxConvertHelper_FromString 00140 { 00141 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00142 { 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(); } 00143 }; 00144 00145 template <typename T, typename H> 00146 struct QxConvertHelper_ToVariant 00147 { 00148 static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00149 { 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(); } 00150 }; 00151 00152 template <typename T, typename H> 00153 struct QxConvertHelper_FromVariant 00154 { 00155 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00156 { 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(); } 00157 }; 00158 00159 #ifndef _QX_NO_JSON 00160 00161 template <typename T, typename H> 00162 struct QxConvertHelper_ToJson 00163 { 00164 static inline QJsonValue toJson(const T & t, const QString & format) 00165 { qAssertMsg(false, "qx::cvt::detail::QxConvertHelper_ToJson", "template must be specialized"); Q_UNUSED(t); Q_UNUSED(format); return QJsonValue(); } 00166 }; 00167 00168 template <typename T, typename H> 00169 struct QxConvertHelper_FromJson 00170 { 00171 static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format) 00172 { qAssertMsg(false, "qx::cvt::detail::QxConvertHelper_FromJson", "template must be specialized"); Q_UNUSED(j); Q_UNUSED(t); Q_UNUSED(format); return qx_bool(); } 00173 }; 00174 00175 #endif // _QX_NO_JSON 00176 00177 template <typename T> 00178 struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Generic> 00179 { 00180 00181 enum { qx_need_to_specialize_template_convert_to_string_from_string = 0 }; 00182 00183 static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00184 { 00185 Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); 00186 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 >' 00187 return QString(); 00188 } 00189 00190 }; 00191 00192 template <typename T> 00193 struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Generic> 00194 { 00195 00196 enum { qx_need_to_specialize_template_convert_to_string_from_string = 0 }; 00197 00198 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00199 { 00200 Q_UNUSED(s); Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); 00201 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 >' 00202 return qx_bool(true); 00203 } 00204 00205 }; 00206 00207 template <typename T> 00208 struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Generic> 00209 { 00210 00211 static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00212 { return cvtQVariant<qx::trait::is_qt_variant_compatible<T>::value, 0>::toVariant(t, format, index, ctx); } 00213 00214 private: 00215 00216 template <bool isQVariantCompatible /* = false */, int dummy> 00217 struct cvtQVariant 00218 { 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); }; }; 00219 00220 template <int dummy> 00221 struct cvtQVariant<true, dummy> 00222 { 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); }; }; 00223 00224 }; 00225 00226 template <typename T> 00227 struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Generic> 00228 { 00229 00230 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00231 { return cvtQVariant<qx::trait::is_qt_variant_compatible<T>::value, 0>::fromVariant(v, t, format, index, ctx); } 00232 00233 private: 00234 00235 template <bool isQVariantCompatible /* = false */, int dummy> 00236 struct cvtQVariant 00237 { 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); }; }; 00238 00239 template <int dummy> 00240 struct cvtQVariant<true, dummy> 00241 { 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); }; }; 00242 00243 }; 00244 00245 #ifndef _QX_NO_JSON 00246 00247 template <typename T> 00248 struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Generic> 00249 { 00250 static inline QJsonValue toJson(const T & t, const QString & format) 00251 { 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)); } 00252 }; 00253 00254 template <typename T> 00255 struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Generic> 00256 { 00257 static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format) 00258 { 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); } 00259 }; 00260 00261 #endif // _QX_NO_JSON 00262 00263 template <typename T> 00264 struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Persistable> 00265 { 00266 00267 enum { qx_need_to_specialize_template_convert_to_string_from_string = 0 }; 00268 00269 static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00270 { 00271 Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); 00272 #ifndef _QX_NO_JSON 00273 return t.toJson(format); 00274 #else // _QX_NO_JSON 00275 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 >' 00276 return QString(); 00277 #endif // _QX_NO_JSON 00278 } 00279 00280 }; 00281 00282 template <typename T> 00283 struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Persistable> 00284 { 00285 00286 enum { qx_need_to_specialize_template_convert_to_string_from_string = 0 }; 00287 00288 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00289 { 00290 Q_UNUSED(s); Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); 00291 #ifndef _QX_NO_JSON 00292 return t.fromJson(s, format); 00293 #else // _QX_NO_JSON 00294 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 >' 00295 return qx_bool(true); 00296 #endif // _QX_NO_JSON 00297 } 00298 00299 }; 00300 00301 template <typename T> 00302 struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Persistable> 00303 { 00304 00305 enum { qx_need_to_specialize_template_convert_to_variant_from_variant = 0 }; 00306 00307 static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00308 { 00309 Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); 00310 #ifndef _QX_NO_JSON 00311 return QVariant(t.toJson(format)); 00312 #else // _QX_NO_JSON 00313 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 >' 00314 return QVariant(); 00315 #endif // _QX_NO_JSON 00316 } 00317 00318 }; 00319 00320 template <typename T> 00321 struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Persistable> 00322 { 00323 00324 enum { qx_need_to_specialize_template_convert_to_variant_from_variant = 0 }; 00325 00326 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00327 { 00328 Q_UNUSED(v); Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); 00329 #ifndef _QX_NO_JSON 00330 return t.fromJson(v.toString(), format); 00331 #else // _QX_NO_JSON 00332 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 >' 00333 return qx_bool(true); 00334 #endif // _QX_NO_JSON 00335 } 00336 00337 }; 00338 00339 #ifndef _QX_NO_JSON 00340 00341 template <typename T> 00342 struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Persistable> 00343 { 00344 static inline QJsonValue toJson(const T & t, const QString & format) 00345 { const qx::IxPersistable * p = static_cast<const qx::IxPersistable *>(& t); return qx::cvt::to_json((* p), format); } 00346 }; 00347 00348 template <typename T> 00349 struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Persistable> 00350 { 00351 static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format) 00352 { qx::IxPersistable * p = static_cast<qx::IxPersistable *>(& t); return qx::cvt::from_json(j, (* p), format); } 00353 }; 00354 00355 #endif // _QX_NO_JSON 00356 00357 template <typename T> 00358 struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Ptr> 00359 { 00360 static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00361 { return (t ? qx::cvt::to_string((* t), format, index, ctx) : ""); } 00362 }; 00363 00364 template <typename T> 00365 struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Ptr> 00366 { 00367 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00368 { if (! t) { qx::trait::construct_ptr<T>::get(t); }; return (t ? qx::cvt::from_string(s, (* t), format, index, ctx) : qx_bool(false)); } 00369 }; 00370 00371 template <typename T> 00372 struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Ptr> 00373 { 00374 static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00375 { return (t ? qx::cvt::to_variant((* t), format, index, ctx) : QVariant()); } 00376 }; 00377 00378 template <typename T> 00379 struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Ptr> 00380 { 00381 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00382 { if (! t && ! v.isNull()) { qx::trait::construct_ptr<T>::get(t); } else if (v.isNull()) { qx::trait::construct_ptr<T>::get(t, true); }; return (t ? qx::cvt::from_variant(v, (* t), format, index, ctx) : qx_bool(false)); } 00383 }; 00384 00385 #ifndef _QX_NO_JSON 00386 00387 template <typename T> 00388 struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Ptr> 00389 { 00390 static inline QJsonValue toJson(const T & t, const QString & format) 00391 { return (t ? qx::cvt::to_json((* t), format) : QJsonValue()); } 00392 }; 00393 00394 template <typename T> 00395 struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Ptr> 00396 { 00397 static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format) 00398 { if (! t && ! j.isNull()) { qx::trait::construct_ptr<T>::get(t); } else if (j.isNull()) { qx::trait::construct_ptr<T>::get(t, true); }; return (t ? qx::cvt::from_json(j, (* t), format) : qx_bool(false)); } 00399 }; 00400 00401 #endif // _QX_NO_JSON 00402 00403 template <typename T> 00404 struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Registered> 00405 { 00406 static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00407 { 00408 if (ctx != qx::cvt::context::e_serialize_registered) 00409 { qx::IxDataMember * pId = qx::QxClass<T>::getSingleton()->getDataMemberX()->getId_WithDaoStrategy(); return (pId ? pId->toVariant((& t), format, index, ctx).toString() : QString()); } 00410 return qx::serialization::qt::to_string<T>(t); 00411 } 00412 }; 00413 00414 template <typename T> 00415 struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Registered> 00416 { 00417 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00418 { 00419 if (ctx != qx::cvt::context::e_serialize_registered) 00420 { 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)); } 00421 return qx::serialization::qt::from_string<T>(t, s); 00422 } 00423 }; 00424 00425 template <typename T> 00426 struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Registered> 00427 { 00428 static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00429 { 00430 if (ctx != qx::cvt::context::e_serialize_registered) 00431 { qx::IxDataMember * pId = qx::QxClass<T>::getSingleton()->getDataMemberX()->getId_WithDaoStrategy(); return (pId ? pId->toVariant((& t), format, index, ctx) : QVariant()); } 00432 return qx::serialization::qt::to_byte_array<T>(t); 00433 } 00434 }; 00435 00436 template <typename T> 00437 struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Registered> 00438 { 00439 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00440 { 00441 if (ctx != qx::cvt::context::e_serialize_registered) 00442 { qx::IxDataMember * pId = qx::QxClass<T>::getSingleton()->getDataMemberX()->getId_WithDaoStrategy(); return (pId ? pId->fromVariant((& t), v, format, index, ctx) : qx_bool(false)); } 00443 return qx::serialization::qt::from_byte_array<T>(t, v.toByteArray()); 00444 } 00445 }; 00446 00447 #ifndef _QX_NO_JSON 00448 00449 template <typename T> 00450 struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Registered> 00451 { 00452 static inline QJsonValue toJson(const T & t, const QString & format) 00453 { return qx::cvt::detail::QxSerializeJsonRegistered<T>::save(t, format); } 00454 }; 00455 00456 template <typename T> 00457 struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Registered> 00458 { 00459 static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format) 00460 { return qx::cvt::detail::QxSerializeJsonRegistered<T>::load(j, t, format); } 00461 }; 00462 00463 #endif // _QX_NO_JSON 00464 00465 template <typename T> 00466 struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Container> 00467 { 00468 static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00469 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::to_string(t); } 00470 }; 00471 00472 template <typename T> 00473 struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Container> 00474 { 00475 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00476 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::from_string(t, s); } 00477 }; 00478 00479 template <typename T> 00480 struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Container> 00481 { 00482 static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00483 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::to_string(t); } 00484 }; 00485 00486 template <typename T> 00487 struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Container> 00488 { 00489 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00490 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::from_string(t, v.toString()); } 00491 }; 00492 00493 #ifndef _QX_NO_JSON 00494 00495 template <typename T> 00496 struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Container> 00497 { 00498 static inline QJsonValue toJson(const T & t, const QString & format) 00499 { 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)); } 00500 }; 00501 00502 template <typename T> 00503 struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Container> 00504 { 00505 static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format) 00506 { 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); } 00507 }; 00508 00509 #endif // _QX_NO_JSON 00510 00511 template <typename T> 00512 struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Enum> 00513 { 00514 static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00515 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QString::number(static_cast<long>(t)); } 00516 }; 00517 00518 template <typename T> 00519 struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Enum> 00520 { 00521 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00522 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); bool bOk = false; t = static_cast<T>(static_cast<long>(s.toLongLong(& bOk))); return bOk; } 00523 }; 00524 00525 template <typename T> 00526 struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Enum> 00527 { 00528 static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00529 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QVariant(static_cast<qlonglong>(t)); } 00530 }; 00531 00532 template <typename T> 00533 struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Enum> 00534 { 00535 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00536 { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); bool bOk = false; t = static_cast<T>(static_cast<long>(v.toLongLong(& bOk))); return bOk; } 00537 }; 00538 00539 #ifndef _QX_NO_JSON 00540 00541 template <typename T> 00542 struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Enum> 00543 { 00544 static inline QJsonValue toJson(const T & t, const QString & format) 00545 { Q_UNUSED(format); return QJsonValue(static_cast<int>(t)); } 00546 }; 00547 00548 template <typename T> 00549 struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Enum> 00550 { 00551 static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format) 00552 { Q_UNUSED(format); t = static_cast<T>(static_cast<long>(qRound(j.toDouble()))); return qx_bool(true); } 00553 }; 00554 00555 #endif // _QX_NO_JSON 00556 00557 template <typename T> 00558 struct QxConvertHelper 00559 { 00560 00561 private: 00562 00563 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; 00564 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; 00565 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; 00566 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; 00567 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; 00568 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; 00569 00570 public: 00571 00572 typedef typename QxConvertHelper<T>::type_str_cvt_helper_5 type; 00573 00574 }; 00575 00576 template <typename T> 00577 struct QxConvert_ToString 00578 { 00579 static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00580 { return qx::cvt::detail::QxConvertHelper_ToString<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::toString(t, format, index, ctx); } 00581 }; 00582 00583 template <typename T> 00584 struct QxConvert_FromString 00585 { 00586 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00587 { return qx::cvt::detail::QxConvertHelper_FromString<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::fromString(s, t, format, index, ctx); } 00588 }; 00589 00590 template <typename T> 00591 struct QxConvert_ToVariant 00592 { 00593 static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00594 { return qx::cvt::detail::QxConvertHelper_ToVariant<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::toVariant(t, format, index, ctx); } 00595 }; 00596 00597 template <typename T> 00598 struct QxConvert_FromVariant 00599 { 00600 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00601 { return qx::cvt::detail::QxConvertHelper_FromVariant<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::fromVariant(v, t, format, index, ctx); } 00602 }; 00603 00604 #ifndef _QX_NO_JSON 00605 00606 template <typename T> 00607 struct QxConvert_ToJson 00608 { 00609 static inline QJsonValue toJson(const T & t, const QString & format) 00610 { return qx::cvt::detail::QxConvertHelper_ToJson<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::toJson(t, format); } 00611 }; 00612 00613 template <typename T> 00614 struct QxConvert_FromJson 00615 { 00616 static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format) 00617 { return qx::cvt::detail::QxConvertHelper_FromJson<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::fromJson(j, t, format); } 00618 }; 00619 00620 #endif // _QX_NO_JSON 00621 00622 } // namespace detail 00623 } // namespace cvt 00624 } // namespace qx 00625 00626 #include "../../inl/QxConvert/QxConvert_WithIndex.inl" 00627 #include "../../inl/QxConvert/QxConvert_ToString.inl" 00628 #include "../../inl/QxConvert/QxConvert_FromString.inl" 00629 #include "../../inl/QxConvert/QxConvert_ToVariant.inl" 00630 #include "../../inl/QxConvert/QxConvert_FromVariant.inl" 00631 #include "../../inl/QxConvert/QxConvert_ToJson.inl" 00632 #include "../../inl/QxConvert/QxConvert_FromJson.inl" 00633 #include "../../inl/QxConvert/QxConvert_Qt.inl" 00634 00635 #endif // _QX_CONVERT_IMPL_H_