![]() |
QxOrm 1.1.6
C++ Object Relational Mapping library
|
00001 /**************************************************************************** 00002 ** 00003 ** http://www.qxorm.com/ 00004 ** http://sourceforge.net/projects/qxorm/ 00005 ** Original file by Lionel Marty 00006 ** 00007 ** This file is part of the QxOrm library 00008 ** 00009 ** This software is provided 'as-is', without any express or implied 00010 ** warranty. In no event will the authors be held liable for any 00011 ** damages arising from the use of this software. 00012 ** 00013 ** GNU Lesser General Public License Usage 00014 ** This file must be used under the terms of the GNU Lesser 00015 ** General Public License version 2.1 as published by the Free Software 00016 ** Foundation and appearing in the file 'license.lgpl.txt' included in the 00017 ** packaging of this file. Please review the following information to 00018 ** ensure the GNU Lesser General Public License version 2.1 requirements 00019 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 00020 ** 00021 ** If you have questions regarding the use of this file, please contact : 00022 ** contact@qxorm.com 00023 ** 00024 ****************************************************************************/ 00025 00026 #ifndef _QX_DATA_MEMBER_H_ 00027 #define _QX_DATA_MEMBER_H_ 00028 00029 #ifdef _MSC_VER 00030 #pragma once 00031 #endif 00032 00040 #include <boost/serialization/serialization.hpp> 00041 #include <boost/serialization/nvp.hpp> 00042 00043 #include <boost/tuple/tuple.hpp> 00044 #include <boost/tuple/tuple_comparison.hpp> 00045 #include <boost/tuple/tuple_io.hpp> 00046 00047 #include <QxDataMember/IxDataMember.h> 00048 00049 #include <QxCommon/QxStringCvt.h> 00050 00051 #include <QxTraits/is_equal.h> 00052 00053 #define QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(ArchiveInput, ArchiveOutput) \ 00054 virtual void toArchive(const void * pOwner, ArchiveOutput & ar) const { QxDataMember::toArchive(ar, getNamePtr(), getData(pOwner)); } \ 00055 virtual void fromArchive(void * pOwner, ArchiveInput & ar) { QxDataMember::fromArchive(ar, getNamePtr(), getData(pOwner)); } 00056 00057 namespace qx { 00058 00063 template <typename DataType, class Owner> 00064 class QxDataMember : public IxDataMember 00065 { 00066 00067 protected: 00068 00069 typedef DataType Owner::* type_data_member_ptr; 00070 00071 type_data_member_ptr m_pData; 00072 00073 public: 00074 00075 QxDataMember(type_data_member_ptr pData, const QString & sKey) : IxDataMember(sKey), m_pData(pData) { ; } 00076 QxDataMember(type_data_member_ptr pData, const QString & sKey, long lVersion, bool bSerialize, bool bDao) : IxDataMember(sKey, lVersion, bSerialize, bDao), m_pData(pData) { ; } 00077 virtual ~QxDataMember() { ; } 00078 00079 inline DataType * getData(void * pOwner) const { return (pOwner ? (& ((static_cast<Owner *>(pOwner))->*m_pData)) : NULL); } 00080 inline const DataType * getData(const void * pOwner) const { return (pOwner ? (& ((static_cast<const Owner *>(pOwner))->*m_pData)) : NULL); } 00081 00082 virtual boost::any getDataPtr(const void * pOwner) const { return boost::any(getData(pOwner)); } 00083 virtual boost::any getDataPtr(void * pOwner) { return boost::any(getData(pOwner)); } 00084 virtual void * getDataVoidPtr(const void * pOwner) const { return static_cast<void *>(const_cast<DataType *>(getData(pOwner))); } 00085 virtual void * getDataVoidPtr(void * pOwner) { return static_cast<void *>(getData(pOwner)); } 00086 virtual qx_bool isValid(const void * pOwner) const { Q_UNUSED(pOwner); return true; } 00087 virtual qx_bool isValid(void * pOwner) { Q_UNUSED(pOwner); return true; } 00088 00089 virtual QString toString(const void * pOwner, const QString & sFormat, int iIndexName = -1) const { return qx::cvt::to_string((* getData(pOwner)), sFormat, iIndexName); } 00090 virtual qx_bool fromString(void * pOwner, const QString & s, const QString & sFormat, int iIndexName = -1) { return qx::cvt::from_string(s, (* getData(pOwner)), sFormat, iIndexName); } 00091 virtual QVariant toVariant(const void * pOwner, const QString & sFormat, int iIndexName = -1) const { return qx::cvt::to_variant((* getData(pOwner)), sFormat, iIndexName); } 00092 virtual qx_bool fromVariant(void * pOwner, const QVariant & v, const QString & sFormat, int iIndexName = -1) { return qx::cvt::from_variant(v, (* getData(pOwner)), sFormat, iIndexName); } 00093 00094 virtual bool isEqual(const void * pOwner1, const void * pOwner2) const 00095 { 00096 if ((pOwner1 == NULL) || (pOwner2 == NULL)) { return false; } 00097 if (pOwner1 == pOwner2) { return true; } 00098 return qxCompareDataMember<qx::trait::has_operator_equal_equal<DataType>::value, 0>::isEqual((* this), pOwner1, pOwner2); 00099 } 00100 00101 public: 00102 00103 #if _QX_SERIALIZE_POLYMORPHIC 00104 QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::polymorphic_iarchive, boost::archive::polymorphic_oarchive) 00105 #endif // _QX_SERIALIZE_POLYMORPHIC 00106 00107 #if _QX_SERIALIZE_BINARY 00108 QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::binary_iarchive, boost::archive::binary_oarchive) 00109 #endif // _QX_SERIALIZE_BINARY 00110 00111 #if _QX_SERIALIZE_TEXT 00112 QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::text_iarchive, boost::archive::text_oarchive) 00113 #endif // _QX_SERIALIZE_TEXT 00114 00115 #if _QX_SERIALIZE_XML 00116 QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::xml_iarchive, boost::archive::xml_oarchive) 00117 #endif // _QX_SERIALIZE_XML 00118 00119 #if _QX_SERIALIZE_PORTABLE_BINARY 00120 QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(eos::portable_iarchive, eos::portable_oarchive) 00121 #endif // _QX_SERIALIZE_PORTABLE_BINARY 00122 00123 #if _QX_SERIALIZE_WIDE_BINARY 00124 QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::binary_wiarchive, boost::archive::binary_woarchive) 00125 #endif // _QX_SERIALIZE_WIDE_BINARY 00126 00127 #if _QX_SERIALIZE_WIDE_TEXT 00128 QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::text_wiarchive, boost::archive::text_woarchive) 00129 #endif // _QX_SERIALIZE_WIDE_TEXT 00130 00131 #if _QX_SERIALIZE_WIDE_XML 00132 QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::xml_wiarchive, boost::archive::xml_woarchive) 00133 #endif // _QX_SERIALIZE_WIDE_XML 00134 00135 private: 00136 00137 template <class Archive> 00138 static inline void toArchive(Archive & ar, const char * sName, const DataType * pData) 00139 { ar << boost::serialization::make_nvp(sName, (* pData)); } 00140 00141 template <class Archive> 00142 static inline void fromArchive(Archive & ar, const char * sName, DataType * pData) 00143 { ar >> boost::serialization::make_nvp(sName, (* pData)); } 00144 00145 private: 00146 00147 template <bool bCanCompare /* = false */, int dummy> 00148 struct qxCompareDataMember 00149 { 00150 static inline bool isEqual(const QxDataMember<DataType, Owner> & dataMember, const void * pOwner1, const void * pOwner2) 00151 { return (dataMember.toString(pOwner1, "") == dataMember.toString(pOwner2, "")); } 00152 }; 00153 00154 template <int dummy> 00155 struct qxCompareDataMember<true, dummy> 00156 { 00157 static inline bool isEqual(const QxDataMember<DataType, Owner> & dataMember, const void * pOwner1, const void * pOwner2) 00158 { return ((* dataMember.getData(pOwner1)) == (* dataMember.getData(pOwner2))); } 00159 }; 00160 00161 }; 00162 00163 } // namespace qx 00164 00165 #include "../../inl/QxDataMember/QxDataMember.inl" 00166 00167 #endif // _QX_DATA_MEMBER_H_