QxStdOptional.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_STD_OPTIONAL_H_
  32. #define _QX_STD_OPTIONAL_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxStdOptional.h
  38. * \author Lionel Marty
  39. * \ingroup QxExtras
  40. * \brief Support std::optional<T> class (requires a C++17 compiler) to manage NULL database value, this header should be included just after <QxOrm.h> header file (ideally in a precompiled header)
  41. */
  42. #include <optional>
  43. #include <QtCore/qdatastream.h>
  44. #include <QxOrm.h>
  45. #include <QxTraits/construct_null_qvariant.h>
  46. template <typename T>
  47. QDataStream & operator<< (QDataStream & stream, const std::optional<T> & t)
  48. {
  49. qint8 iHasData = (t ? 1 : 0);
  50. stream << iHasData;
  51. if (t) { stream << (* t); }
  52. return stream;
  53. }
  54. template <typename T>
  55. QDataStream & operator>> (QDataStream & stream, std::optional<T> & t)
  56. {
  57. qint8 iHasData = 0;
  58. stream >> iHasData;
  59. if (iHasData) { t = T(); stream >> (* t); }
  60. else { t = std::optional<T>(); }
  61. return stream;
  62. }
  63. QX_REGISTER_CLASS_NAME_TEMPLATE_1(std::optional)
  64. namespace qx {
  65. namespace trait {
  66. template <typename T>
  67. struct get_sql_type< std::optional<T> >
  68. { static inline const char * get() { return qx::trait::get_sql_type<T>::get(); } };
  69. } // namespace trait
  70. } // namespace qx
  71. namespace qx {
  72. namespace cvt {
  73. namespace detail {
  74. #ifndef _QX_NO_JSON
  75. template <typename T> struct QxConvert_ToJson< std::optional<T> > {
  76. static inline QJsonValue toJson(const std::optional<T> & t, const QString & format)
  77. { if (t) { return qx::cvt::to_json((* t), format); }; return QJsonValue(); } };
  78. template <typename T> struct QxConvert_FromJson< std::optional<T> > {
  79. static inline qx_bool fromJson(const QJsonValue & j, std::optional<T> & t, const QString & format)
  80. {
  81. if (j.isNull()) { t = std::optional<T>(); return qx_bool(true); }
  82. else if (! t) { t = T(); }
  83. return qx::cvt::from_json(j, (* t), format);
  84. } };
  85. #endif // _QX_NO_JSON
  86. template <typename T> struct QxConvert_ToString< std::optional<T> > {
  87. static inline QString toString(const std::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  88. { if (t) { return qx::cvt::to_string((* t), format, index, ctx); }; return QString(); } };
  89. template <typename T> struct QxConvert_FromString< std::optional<T> > {
  90. static inline qx_bool fromString(const QString & s, std::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  91. { if (! t) { t = T(); }; return qx::cvt::from_string(s, (* t), format, index, ctx); } };
  92. template <typename T> struct QxConvert_ToVariant< std::optional<T> > {
  93. static inline QVariant toVariant(const std::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  94. { if (t) { return qx::cvt::to_variant((* t), format, index, ctx); }; return qx::trait::construct_null_qvariant<T>::get(); } };
  95. template <typename T> struct QxConvert_FromVariant< std::optional<T> > {
  96. static inline qx_bool fromVariant(const QVariant & v, std::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  97. {
  98. if (v.isNull()) { t = std::optional<T>(); return qx_bool(true); }
  99. else if (! t) { t = T(); }
  100. return qx::cvt::from_variant(v, (* t), format, index, ctx);
  101. } };
  102. } // namespace detail
  103. } // namespace cvt
  104. } // namespace qx
  105. #endif // _QX_STD_OPTIONAL_H_