QxConvert_Impl.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifndef _QX_CONVERT_IMPL_H_
  32. #define _QX_CONVERT_IMPL_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. #include <QxConvert/QxConvert.h>
  37. #include <QxCommon/QxBool.h>
  38. #include <QxDao/QxDateNeutral.h>
  39. #include <QxDao/QxTimeNeutral.h>
  40. #include <QxDao/QxDateTimeNeutral.h>
  41. #include <QxDao/QxSqlQuery.h>
  42. #include <QxDao/IxPersistable.h>
  43. #include <QxCollection/QxCollection.h>
  44. #include <QxRegister/QxClass.h>
  45. #include <QxSerialize/QxArchive.h>
  46. #include <QxSerialize/QxSerializeQDataStream.h>
  47. #include <QxSerialize/QDataStream/QxSerializeQDataStream_all_include.h>
  48. #include <QxSerialize/QJson/QxSerializeQJson_qx_registered_class.h>
  49. #include <QxValidator/QxInvalidValue.h>
  50. #include <QxValidator/QxInvalidValueX.h>
  51. #include <QxTraits/is_smart_ptr.h>
  52. #include <QxTraits/is_container.h>
  53. #include <QxTraits/is_qx_registered.h>
  54. #include <QxTraits/is_qt_variant_compatible.h>
  55. #include <QxTraits/get_class_name_primitive.h>
  56. #include <QxTraits/construct_ptr.h>
  57. #include <QxTraits/generic_container.h>
  58. #define QX_STR_CVT_QDATE_FORMAT "yyyyMMdd"
  59. #define QX_STR_CVT_QTIME_FORMAT "hhmmsszzz"
  60. #define QX_STR_CVT_QDATETIME_FORMAT "yyyyMMddhhmmsszzz"
  61. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  62. #if _QX_SERIALIZE_POLYMORPHIC
  63. #define QX_CVT_DEFAULT_ARCHIVE qx::serialization::polymorphic_xml
  64. #elif _QX_SERIALIZE_XML
  65. #define QX_CVT_DEFAULT_ARCHIVE qx::serialization::xml
  66. #elif _QX_SERIALIZE_TEXT
  67. #define QX_CVT_DEFAULT_ARCHIVE qx::serialization::text
  68. #elif _QX_SERIALIZE_BINARY
  69. #define QX_CVT_DEFAULT_ARCHIVE qx::serialization::binary
  70. #endif // _QX_SERIALIZE_XML
  71. #else // _QX_ENABLE_BOOST_SERIALIZATION
  72. #define QX_CVT_DEFAULT_ARCHIVE qx::serialization::qt
  73. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  74. #define QX_CVT_USING_ARCHIVE_IMPL(className) \
  75. namespace qx { namespace cvt { namespace detail { \
  76. template <> struct QxConvert_ToString< className > { \
  77. static inline QString toString(const className & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) \
  78. { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::to_string(t); } }; \
  79. template <> struct QxConvert_FromString< className > { \
  80. static inline qx_bool fromString(const QString & s, className & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) \
  81. { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::from_string(t, s); } }; \
  82. template <> struct QxConvert_ToVariant< className > { \
  83. static inline QVariant toVariant(const className & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) \
  84. { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::to_string(t); } }; \
  85. template <> struct QxConvert_FromVariant< className > { \
  86. static inline qx_bool fromVariant(const QVariant & v, className & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) \
  87. { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); QString s = v.toString(); return QX_CVT_DEFAULT_ARCHIVE::from_string(t, s); } }; \
  88. } } } // namespace qx::cvt::detail
  89. #if (QT_VERSION >= 0x050800)
  90. #define QX_JSON_DATE_TIME_FORMAT Qt::ISODateWithMs
  91. #define QX_JSON_DATE_TIME_FORMAT_SIZE 23 // yyyy-MM-ddTHH:mm:ss.zzz
  92. #else // (QT_VERSION >= 0x050800)
  93. #define QX_JSON_DATE_TIME_FORMAT Qt::ISODate
  94. #define QX_JSON_DATE_TIME_FORMAT_SIZE 19 // yyyy-MM-ddTHH:mm:ss
  95. #endif // (QT_VERSION >= 0x050800)
  96. namespace qx {
  97. namespace cvt {
  98. namespace detail {
  99. namespace helper {
  100. struct QxConvertHelper_Generic { };
  101. struct QxConvertHelper_Ptr { };
  102. struct QxConvertHelper_Registered { };
  103. struct QxConvertHelper_Persistable { };
  104. struct QxConvertHelper_Container { };
  105. struct QxConvertHelper_Enum { };
  106. inline bool checkConvertQVariantToString(const QVariant & v) { return ((v.type() == QVariant::List) || (v.type() == QVariant::Map) || (v.type() == QVariant::Hash) || (v.type() == QVariant::StringList)); }
  107. } // namespace helper
  108. template <typename T, typename H>
  109. struct QxConvertHelper_ToString
  110. {
  111. static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  112. { 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(); }
  113. };
  114. template <typename T, typename H>
  115. struct QxConvertHelper_FromString
  116. {
  117. static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  118. { 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(); }
  119. };
  120. template <typename T, typename H>
  121. struct QxConvertHelper_ToVariant
  122. {
  123. static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  124. { 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(); }
  125. };
  126. template <typename T, typename H>
  127. struct QxConvertHelper_FromVariant
  128. {
  129. static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  130. { 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(); }
  131. };
  132. #ifndef _QX_NO_JSON
  133. template <typename T, typename H>
  134. struct QxConvertHelper_ToJson
  135. {
  136. static inline QJsonValue toJson(const T & t, const QString & format)
  137. { qAssertMsg(false, "qx::cvt::detail::QxConvertHelper_ToJson", "template must be specialized"); Q_UNUSED(t); Q_UNUSED(format); return QJsonValue(); }
  138. };
  139. template <typename T, typename H>
  140. struct QxConvertHelper_FromJson
  141. {
  142. static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format)
  143. { qAssertMsg(false, "qx::cvt::detail::QxConvertHelper_FromJson", "template must be specialized"); Q_UNUSED(j); Q_UNUSED(t); Q_UNUSED(format); return qx_bool(); }
  144. };
  145. #endif // _QX_NO_JSON
  146. template <typename T>
  147. struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Generic>
  148. {
  149. enum { qx_need_to_specialize_template_convert_to_string_from_string = 0 };
  150. static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  151. {
  152. Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx);
  153. 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 >'
  154. return QString();
  155. }
  156. };
  157. template <typename T>
  158. struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Generic>
  159. {
  160. enum { qx_need_to_specialize_template_convert_to_string_from_string = 0 };
  161. static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  162. {
  163. Q_UNUSED(s); Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx);
  164. 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 >'
  165. return qx_bool(true);
  166. }
  167. };
  168. template <typename T>
  169. struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Generic>
  170. {
  171. static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  172. { return cvtQVariant<qx::trait::is_qt_variant_compatible<T>::value, 0>::toVariant(t, format, index, ctx); }
  173. private:
  174. template <bool isQVariantCompatible /* = false */, int dummy>
  175. struct cvtQVariant
  176. { 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); }; };
  177. template <int dummy>
  178. struct cvtQVariant<true, dummy>
  179. { 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); }; };
  180. };
  181. template <typename T>
  182. struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Generic>
  183. {
  184. static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  185. { return cvtQVariant<qx::trait::is_qt_variant_compatible<T>::value, 0>::fromVariant(v, t, format, index, ctx); }
  186. private:
  187. template <bool isQVariantCompatible /* = false */, int dummy>
  188. struct cvtQVariant
  189. { 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); }; };
  190. template <int dummy>
  191. struct cvtQVariant<true, dummy>
  192. { 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); }; };
  193. };
  194. #ifndef _QX_NO_JSON
  195. template <typename T>
  196. struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Generic>
  197. {
  198. static inline QJsonValue toJson(const T & t, const QString & format)
  199. { 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)); }
  200. };
  201. template <typename T>
  202. struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Generic>
  203. {
  204. static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format)
  205. { 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); }
  206. };
  207. #endif // _QX_NO_JSON
  208. template <typename T>
  209. struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Persistable>
  210. {
  211. enum { qx_need_to_specialize_template_convert_to_string_from_string = 0 };
  212. static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  213. {
  214. Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx);
  215. #ifndef _QX_NO_JSON
  216. return t.toJson(format);
  217. #else // _QX_NO_JSON
  218. 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 >'
  219. return QString();
  220. #endif // _QX_NO_JSON
  221. }
  222. };
  223. template <typename T>
  224. struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Persistable>
  225. {
  226. enum { qx_need_to_specialize_template_convert_to_string_from_string = 0 };
  227. static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  228. {
  229. Q_UNUSED(s); Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx);
  230. #ifndef _QX_NO_JSON
  231. return t.fromJson(s, format);
  232. #else // _QX_NO_JSON
  233. 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 >'
  234. return qx_bool(true);
  235. #endif // _QX_NO_JSON
  236. }
  237. };
  238. template <typename T>
  239. struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Persistable>
  240. {
  241. enum { qx_need_to_specialize_template_convert_to_variant_from_variant = 0 };
  242. static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  243. {
  244. Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx);
  245. #ifndef _QX_NO_JSON
  246. return QVariant(t.toJson(format));
  247. #else // _QX_NO_JSON
  248. 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 >'
  249. return QVariant();
  250. #endif // _QX_NO_JSON
  251. }
  252. };
  253. template <typename T>
  254. struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Persistable>
  255. {
  256. enum { qx_need_to_specialize_template_convert_to_variant_from_variant = 0 };
  257. static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  258. {
  259. Q_UNUSED(v); Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx);
  260. #ifndef _QX_NO_JSON
  261. return t.fromJson(v.toString(), format);
  262. #else // _QX_NO_JSON
  263. 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 >'
  264. return qx_bool(true);
  265. #endif // _QX_NO_JSON
  266. }
  267. };
  268. #ifndef _QX_NO_JSON
  269. template <typename T>
  270. struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Persistable>
  271. {
  272. static inline QJsonValue toJson(const T & t, const QString & format)
  273. { const qx::IxPersistable * p = static_cast<const qx::IxPersistable *>(& t); return qx::cvt::to_json((* p), format); }
  274. };
  275. template <typename T>
  276. struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Persistable>
  277. {
  278. static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format)
  279. { qx::IxPersistable * p = static_cast<qx::IxPersistable *>(& t); return qx::cvt::from_json(j, (* p), format); }
  280. };
  281. #endif // _QX_NO_JSON
  282. template <typename T>
  283. struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Ptr>
  284. {
  285. static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  286. { return (t ? qx::cvt::to_string((* t), format, index, ctx) : ""); }
  287. };
  288. template <typename T>
  289. struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Ptr>
  290. {
  291. static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  292. { if (! t) { qx::trait::construct_ptr<T>::get(t); }; return (t ? qx::cvt::from_string(s, (* t), format, index, ctx) : qx_bool(false)); }
  293. };
  294. template <typename T>
  295. struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Ptr>
  296. {
  297. static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  298. { return (t ? qx::cvt::to_variant((* t), format, index, ctx) : QVariant()); }
  299. };
  300. template <typename T>
  301. struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Ptr>
  302. {
  303. static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  304. { 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)); }
  305. };
  306. #ifndef _QX_NO_JSON
  307. template <typename T>
  308. struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Ptr>
  309. {
  310. static inline QJsonValue toJson(const T & t, const QString & format)
  311. { return (t ? qx::cvt::to_json((* t), format) : QJsonValue()); }
  312. };
  313. template <typename T>
  314. struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Ptr>
  315. {
  316. static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format)
  317. { 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)); }
  318. };
  319. #endif // _QX_NO_JSON
  320. template <typename T>
  321. struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Registered>
  322. {
  323. static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  324. {
  325. if (ctx != qx::cvt::context::e_serialize_registered)
  326. { qx::IxDataMember * pId = qx::QxClass<T>::getSingleton()->getDataMemberX()->getId_WithDaoStrategy(); return (pId ? pId->toVariant((& t), format, index, ctx).toString() : QString()); }
  327. return qx::serialization::qt::to_string<T>(t);
  328. }
  329. };
  330. template <typename T>
  331. struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Registered>
  332. {
  333. static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  334. {
  335. if (ctx != qx::cvt::context::e_serialize_registered)
  336. { 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)); }
  337. return qx::serialization::qt::from_string<T>(t, s);
  338. }
  339. };
  340. template <typename T>
  341. struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Registered>
  342. {
  343. static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  344. {
  345. if (ctx != qx::cvt::context::e_serialize_registered)
  346. { qx::IxDataMember * pId = qx::QxClass<T>::getSingleton()->getDataMemberX()->getId_WithDaoStrategy(); return (pId ? pId->toVariant((& t), format, index, ctx) : QVariant()); }
  347. return qx::serialization::qt::to_byte_array<T>(t);
  348. }
  349. };
  350. template <typename T>
  351. struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Registered>
  352. {
  353. static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  354. {
  355. if (ctx != qx::cvt::context::e_serialize_registered)
  356. { qx::IxDataMember * pId = qx::QxClass<T>::getSingleton()->getDataMemberX()->getId_WithDaoStrategy(); return (pId ? pId->fromVariant((& t), v, format, index, ctx) : qx_bool(false)); }
  357. return qx::serialization::qt::from_byte_array<T>(t, v.toByteArray());
  358. }
  359. };
  360. #ifndef _QX_NO_JSON
  361. template <typename T>
  362. struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Registered>
  363. {
  364. static inline QJsonValue toJson(const T & t, const QString & format)
  365. { return qx::cvt::detail::QxSerializeJsonRegistered<T>::save(t, format); }
  366. };
  367. template <typename T>
  368. struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Registered>
  369. {
  370. static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format)
  371. { return qx::cvt::detail::QxSerializeJsonRegistered<T>::load(j, t, format); }
  372. };
  373. #endif // _QX_NO_JSON
  374. template <typename T>
  375. struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Container>
  376. {
  377. static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  378. { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::to_string(t); }
  379. };
  380. template <typename T>
  381. struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Container>
  382. {
  383. static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  384. { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::from_string(t, s); }
  385. };
  386. template <typename T>
  387. struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Container>
  388. {
  389. static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  390. { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::to_string(t); }
  391. };
  392. template <typename T>
  393. struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Container>
  394. {
  395. static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  396. { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QX_CVT_DEFAULT_ARCHIVE::from_string(t, v.toString()); }
  397. };
  398. #ifndef _QX_NO_JSON
  399. template <typename T>
  400. struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Container>
  401. {
  402. static inline QJsonValue toJson(const T & t, const QString & format)
  403. { 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)); }
  404. };
  405. template <typename T>
  406. struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Container>
  407. {
  408. static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format)
  409. { 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); }
  410. };
  411. #endif // _QX_NO_JSON
  412. template <typename T>
  413. struct QxConvertHelper_ToString<T, qx::cvt::detail::helper::QxConvertHelper_Enum>
  414. {
  415. static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  416. { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QString::number(static_cast<long>(t)); }
  417. };
  418. template <typename T>
  419. struct QxConvertHelper_FromString<T, qx::cvt::detail::helper::QxConvertHelper_Enum>
  420. {
  421. static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  422. { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); bool bOk = false; t = static_cast<T>(static_cast<long>(s.toLongLong(& bOk))); return bOk; }
  423. };
  424. template <typename T>
  425. struct QxConvertHelper_ToVariant<T, qx::cvt::detail::helper::QxConvertHelper_Enum>
  426. {
  427. static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  428. { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); return QVariant(static_cast<qlonglong>(t)); }
  429. };
  430. template <typename T>
  431. struct QxConvertHelper_FromVariant<T, qx::cvt::detail::helper::QxConvertHelper_Enum>
  432. {
  433. static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  434. { Q_UNUSED(format); Q_UNUSED(index); Q_UNUSED(ctx); bool bOk = false; t = static_cast<T>(static_cast<long>(v.toLongLong(& bOk))); return bOk; }
  435. };
  436. #ifndef _QX_NO_JSON
  437. template <typename T>
  438. struct QxConvertHelper_ToJson<T, qx::cvt::detail::helper::QxConvertHelper_Enum>
  439. {
  440. static inline QJsonValue toJson(const T & t, const QString & format)
  441. { Q_UNUSED(format); return QJsonValue(static_cast<int>(t)); }
  442. };
  443. template <typename T>
  444. struct QxConvertHelper_FromJson<T, qx::cvt::detail::helper::QxConvertHelper_Enum>
  445. {
  446. static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format)
  447. { Q_UNUSED(format); t = static_cast<T>(static_cast<long>(qRound(j.toDouble()))); return qx_bool(true); }
  448. };
  449. #endif // _QX_NO_JSON
  450. template <typename T>
  451. struct QxConvertHelper
  452. {
  453. private:
  454. 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;
  455. 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;
  456. 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;
  457. 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;
  458. 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;
  459. 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;
  460. public:
  461. typedef typename QxConvertHelper<T>::type_str_cvt_helper_5 type;
  462. };
  463. template <typename T>
  464. struct QxConvert_ToString
  465. {
  466. static inline QString toString(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  467. { return qx::cvt::detail::QxConvertHelper_ToString<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::toString(t, format, index, ctx); }
  468. };
  469. template <typename T>
  470. struct QxConvert_FromString
  471. {
  472. static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  473. { return qx::cvt::detail::QxConvertHelper_FromString<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::fromString(s, t, format, index, ctx); }
  474. };
  475. template <typename T>
  476. struct QxConvert_ToVariant
  477. {
  478. static inline QVariant toVariant(const T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  479. { return qx::cvt::detail::QxConvertHelper_ToVariant<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::toVariant(t, format, index, ctx); }
  480. };
  481. template <typename T>
  482. struct QxConvert_FromVariant
  483. {
  484. static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  485. { return qx::cvt::detail::QxConvertHelper_FromVariant<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::fromVariant(v, t, format, index, ctx); }
  486. };
  487. #ifndef _QX_NO_JSON
  488. template <typename T>
  489. struct QxConvert_ToJson
  490. {
  491. static inline QJsonValue toJson(const T & t, const QString & format)
  492. { return qx::cvt::detail::QxConvertHelper_ToJson<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::toJson(t, format); }
  493. };
  494. template <typename T>
  495. struct QxConvert_FromJson
  496. {
  497. static inline qx_bool fromJson(const QJsonValue & j, T & t, const QString & format)
  498. { return qx::cvt::detail::QxConvertHelper_FromJson<T, typename qx::cvt::detail::QxConvertHelper<T>::type>::fromJson(j, t, format); }
  499. };
  500. #endif // _QX_NO_JSON
  501. } // namespace detail
  502. } // namespace cvt
  503. } // namespace qx
  504. #include "../../inl/QxConvert/QxConvert_WithIndex.inl"
  505. #include "../../inl/QxConvert/QxConvert_ToString.inl"
  506. #include "../../inl/QxConvert/QxConvert_FromString.inl"
  507. #include "../../inl/QxConvert/QxConvert_ToVariant.inl"
  508. #include "../../inl/QxConvert/QxConvert_FromVariant.inl"
  509. #include "../../inl/QxConvert/QxConvert_ToJson.inl"
  510. #include "../../inl/QxConvert/QxConvert_FromJson.inl"
  511. #include "../../inl/QxConvert/QxConvert_Qt.inl"
  512. #endif // _QX_CONVERT_IMPL_H_