QxDataMember.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_DATA_MEMBER_H_
  32. #define _QX_DATA_MEMBER_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxDataMember.h
  38. * \author Lionel Marty
  39. * \ingroup QxDataMember
  40. * \brief Concrete class property registered into QxOrm context
  41. */
  42. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  43. #include <boost/serialization/serialization.hpp>
  44. #include <boost/serialization/nvp.hpp>
  45. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  46. #include <QxDataMember/IxDataMember.h>
  47. #include <QxTraits/is_equal.h>
  48. #include <QxTraits/get_class_name.h>
  49. #define QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(ArchiveInput, ArchiveOutput) \
  50. virtual void toArchive(const void * pOwner, ArchiveOutput & ar) const { QxDataMember::toArchive(ar, getNamePtr(), getData(pOwner)); } \
  51. virtual void fromArchive(void * pOwner, ArchiveInput & ar) { QxDataMember::fromArchive(ar, getNamePtr(), getData(pOwner)); }
  52. namespace qx {
  53. /*!
  54. * \ingroup QxDataMember
  55. * \brief qx::QxDataMember<DataType, Owner> : concrete property of type DataType registered into QxOrm context for the class Owner
  56. */
  57. template <typename DataType, class Owner>
  58. class QxDataMember : public IxDataMember
  59. {
  60. protected:
  61. typedef DataType Owner::* type_data_member_ptr;
  62. type_data_member_ptr m_pData; //!< Data member under format "& Owner::DataMember"
  63. public:
  64. QxDataMember(type_data_member_ptr pData, const QString & sKey) : IxDataMember(sKey), m_pData(pData) { this->setAccessDataPointer(true); }
  65. QxDataMember(type_data_member_ptr pData, const QString & sKey, long lVersion, bool bSerialize, bool bDao) : IxDataMember(sKey, lVersion, bSerialize, bDao), m_pData(pData) { this->setAccessDataPointer(true); }
  66. virtual ~QxDataMember() { ; }
  67. inline DataType * getData(void * pOwner) const { return (pOwner ? (& ((static_cast<Owner *>(pOwner))->*m_pData)) : NULL); }
  68. inline const DataType * getData(const void * pOwner) const { return (pOwner ? (& ((static_cast<const Owner *>(pOwner))->*m_pData)) : NULL); }
  69. virtual QVariant toVariant(const void * pOwner, const QString & sFormat, int iIndexName = -1, qx::cvt::context::ctx_type ctx = qx::cvt::context::e_no_context) const { return qx::cvt::to_variant((* getData(pOwner)), sFormat, iIndexName, ctx); }
  70. virtual qx_bool fromVariant(void * pOwner, const QVariant & v, const QString & sFormat, int iIndexName = -1, qx::cvt::context::ctx_type ctx = qx::cvt::context::e_no_context) { return qx::cvt::from_variant(v, (* getData(pOwner)), sFormat, iIndexName, ctx); }
  71. virtual QString getType() const { return QString(qx::trait::get_class_name<DataType>::get()); }
  72. #ifndef _QX_NO_JSON
  73. virtual QJsonValue toJson(const void * pOwner, const QString & sFormat) const { return qx::cvt::to_json((* getData(pOwner)), sFormat); }
  74. virtual qx_bool fromJson(void * pOwner, const QJsonValue & j, const QString & sFormat) { return qx::cvt::from_json(j, (* getData(pOwner)), sFormat); }
  75. #endif // _QX_NO_JSON
  76. virtual bool isEqual(const void * pOwner1, const void * pOwner2) const
  77. {
  78. if ((pOwner1 == NULL) || (pOwner2 == NULL)) { return false; }
  79. if (pOwner1 == pOwner2) { return true; }
  80. return qxCompareDataMember<qx::trait::has_operator_equal_equal<DataType>::value, 0>::isEqual((* this), pOwner1, pOwner2);
  81. }
  82. protected:
  83. virtual qx::any getDataPtr(const void * pOwner) const { return qx::any(getData(pOwner)); }
  84. virtual qx::any getDataPtr(void * pOwner) { return qx::any(getData(pOwner)); }
  85. virtual void * getDataVoidPtr(const void * pOwner) const { return static_cast<void *>(const_cast<DataType *>(getData(pOwner))); }
  86. virtual void * getDataVoidPtr(void * pOwner) { return static_cast<void *>(getData(pOwner)); }
  87. public:
  88. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  89. #if _QX_SERIALIZE_POLYMORPHIC
  90. QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::polymorphic_iarchive, boost::archive::polymorphic_oarchive)
  91. #endif // _QX_SERIALIZE_POLYMORPHIC
  92. #if _QX_SERIALIZE_BINARY
  93. QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::binary_iarchive, boost::archive::binary_oarchive)
  94. #endif // _QX_SERIALIZE_BINARY
  95. #if _QX_SERIALIZE_TEXT
  96. QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::text_iarchive, boost::archive::text_oarchive)
  97. #endif // _QX_SERIALIZE_TEXT
  98. #if _QX_SERIALIZE_XML
  99. QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::xml_iarchive, boost::archive::xml_oarchive)
  100. #endif // _QX_SERIALIZE_XML
  101. #if _QX_SERIALIZE_PORTABLE_BINARY
  102. QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(eos::portable_iarchive, eos::portable_oarchive)
  103. #endif // _QX_SERIALIZE_PORTABLE_BINARY
  104. #if _QX_SERIALIZE_WIDE_BINARY
  105. QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::binary_wiarchive, boost::archive::binary_woarchive)
  106. #endif // _QX_SERIALIZE_WIDE_BINARY
  107. #if _QX_SERIALIZE_WIDE_TEXT
  108. QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::text_wiarchive, boost::archive::text_woarchive)
  109. #endif // _QX_SERIALIZE_WIDE_TEXT
  110. #if _QX_SERIALIZE_WIDE_XML
  111. QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::xml_wiarchive, boost::archive::xml_woarchive)
  112. #endif // _QX_SERIALIZE_WIDE_XML
  113. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  114. private:
  115. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  116. template <class Archive>
  117. static inline void toArchive(Archive & ar, const char * sName, const DataType * pData)
  118. { ar << boost::serialization::make_nvp(sName, (* pData)); }
  119. template <class Archive>
  120. static inline void fromArchive(Archive & ar, const char * sName, DataType * pData)
  121. { ar >> boost::serialization::make_nvp(sName, (* pData)); }
  122. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  123. private:
  124. template <bool bCanCompare /* = false */, int dummy>
  125. struct qxCompareDataMember
  126. {
  127. static inline bool isEqual(const QxDataMember<DataType, Owner> & dataMember, const void * pOwner1, const void * pOwner2)
  128. { return (dataMember.toVariant(pOwner1, "") == dataMember.toVariant(pOwner2, "")); }
  129. };
  130. template <int dummy>
  131. struct qxCompareDataMember<true, dummy>
  132. {
  133. static inline bool isEqual(const QxDataMember<DataType, Owner> & dataMember, const void * pOwner1, const void * pOwner2)
  134. { return ((* dataMember.getData(pOwner1)) == (* dataMember.getData(pOwner2))); }
  135. };
  136. };
  137. } // namespace qx
  138. #include "../../inl/QxDataMember/QxDataMember.inl"
  139. #endif // _QX_DATA_MEMBER_H_