QxBoostOptionalOnly.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_ENABLE_BOOST
  32. #ifndef _QX_BOOST_OPTIONAL_ONLY_H_
  33. #define _QX_BOOST_OPTIONAL_ONLY_H_
  34. #ifdef _MSC_VER
  35. #pragma once
  36. #endif
  37. /*!
  38. * \file QxBoostOptionalOnly.h
  39. * \author Lionel Marty
  40. * \ingroup QxExtras
  41. * \brief Provides support for boost::optional<T> to manage NULL values even if _QX_ENABLE_BOOST compilation option is not defined : so this header file enables just boost::optional feature and should be included just after <QxOrm.h> header file (ideally in a precompiled header)
  42. */
  43. #include <boost/optional.hpp>
  44. #include <boost/none.hpp>
  45. #include <QxOrm.h>
  46. #define _QX_ENABLE_BOOST
  47. #include <QxSerialize/QDataStream/QxSerializeQDataStream_boost_optional.h>
  48. #undef _QX_ENABLE_BOOST
  49. #include <QxTraits/construct_null_qvariant.h>
  50. QX_REGISTER_CLASS_NAME_TEMPLATE_1(boost::optional)
  51. namespace qx {
  52. namespace trait {
  53. template <typename T>
  54. struct get_sql_type< boost::optional<T> >
  55. { static inline const char * get() { return qx::trait::get_sql_type<T>::get(); } };
  56. } // namespace trait
  57. } // namespace qx
  58. namespace qx {
  59. namespace cvt {
  60. namespace detail {
  61. #ifndef _QX_NO_JSON
  62. template <typename T> struct QxConvert_ToJson< boost::optional<T> > {
  63. static inline QJsonValue toJson(const boost::optional<T> & t, const QString & format)
  64. { if (t) { return qx::cvt::to_json((* t), format); }; return QJsonValue(); } };
  65. template <typename T> struct QxConvert_FromJson< boost::optional<T> > {
  66. static inline qx_bool fromJson(const QJsonValue & j, boost::optional<T> & t, const QString & format)
  67. {
  68. if (j.isNull()) { t = boost::none; return qx_bool(true); }
  69. else if (! t) { t = T(); }
  70. return qx::cvt::from_json(j, (* t), format);
  71. } };
  72. #endif // _QX_NO_JSON
  73. template <typename T> struct QxConvert_ToString< boost::optional<T> > {
  74. static inline QString toString(const boost::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  75. { if (t) { return qx::cvt::to_string((* t), format, index, ctx); }; return QString(); } };
  76. template <typename T> struct QxConvert_FromString< boost::optional<T> > {
  77. static inline qx_bool fromString(const QString & s, boost::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  78. { if (! t) { t = T(); }; return qx::cvt::from_string(s, (* t), format, index, ctx); } };
  79. template <typename T> struct QxConvert_ToVariant< boost::optional<T> > {
  80. static inline QVariant toVariant(const boost::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  81. { if (t) { return qx::cvt::to_variant((* t), format, index, ctx); }; return qx::trait::construct_null_qvariant<T>::get(); } };
  82. template <typename T> struct QxConvert_FromVariant< boost::optional<T> > {
  83. static inline qx_bool fromVariant(const QVariant & v, boost::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
  84. {
  85. if (v.isNull()) { t = boost::none; return qx_bool(true); }
  86. else if (! t) { t = T(); }
  87. return qx::cvt::from_variant(v, (* t), format, index, ctx);
  88. } };
  89. } // namespace detail
  90. } // namespace cvt
  91. } // namespace qx
  92. #endif // _QX_BOOST_OPTIONAL_ONLY_H_
  93. #endif // _QX_ENABLE_BOOST