QxOrm 1.1.9
C++ Object Relational Mapping library
QxDataMember.h
Go to the documentation of this file.
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) { m_bAccessDataPointer = true; }
00076    QxDataMember(type_data_member_ptr pData, const QString & sKey, long lVersion, bool bSerialize, bool bDao) : IxDataMember(sKey, lVersion, bSerialize, bDao), m_pData(pData) { m_bAccessDataPointer = true; }
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 qx_bool isValid(const void * pOwner) const          { Q_UNUSED(pOwner); return true; }
00083    virtual qx_bool isValid(void * pOwner)                      { Q_UNUSED(pOwner); return true; }
00084 
00085    virtual QString toString(const void * pOwner, const QString & sFormat, int iIndexName = -1) const              { return qx::cvt::to_string((* getData(pOwner)), sFormat, iIndexName); }
00086    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); }
00087    virtual QVariant toVariant(const void * pOwner, const QString & sFormat, int iIndexName = -1) const            { return qx::cvt::to_variant((* getData(pOwner)), sFormat, iIndexName); }
00088    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); }
00089 
00090    virtual bool isEqual(const void * pOwner1, const void * pOwner2) const
00091    {
00092       if ((pOwner1 == NULL) || (pOwner2 == NULL)) { return false; }
00093       if (pOwner1 == pOwner2) { return true; }
00094       return qxCompareDataMember<qx::trait::has_operator_equal_equal<DataType>::value, 0>::isEqual((* this), pOwner1, pOwner2);
00095    }
00096 
00097 protected:
00098 
00099    virtual boost::any getDataPtr(const void * pOwner) const    { return boost::any(getData(pOwner)); }
00100    virtual boost::any getDataPtr(void * pOwner)                { return boost::any(getData(pOwner)); }
00101    virtual void * getDataVoidPtr(const void * pOwner) const    { return static_cast<void *>(const_cast<DataType *>(getData(pOwner))); }
00102    virtual void * getDataVoidPtr(void * pOwner)                { return static_cast<void *>(getData(pOwner)); }
00103 
00104 public:
00105 
00106 #if _QX_SERIALIZE_POLYMORPHIC
00107    QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::polymorphic_iarchive, boost::archive::polymorphic_oarchive)
00108 #endif // _QX_SERIALIZE_POLYMORPHIC
00109 
00110 #if _QX_SERIALIZE_BINARY
00111    QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::binary_iarchive, boost::archive::binary_oarchive)
00112 #endif // _QX_SERIALIZE_BINARY
00113 
00114 #if _QX_SERIALIZE_TEXT
00115    QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::text_iarchive, boost::archive::text_oarchive)
00116 #endif // _QX_SERIALIZE_TEXT
00117 
00118 #if _QX_SERIALIZE_XML
00119    QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::xml_iarchive, boost::archive::xml_oarchive)
00120 #endif // _QX_SERIALIZE_XML
00121 
00122 #if _QX_SERIALIZE_PORTABLE_BINARY
00123    QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(eos::portable_iarchive, eos::portable_oarchive)
00124 #endif // _QX_SERIALIZE_PORTABLE_BINARY
00125 
00126 #if _QX_SERIALIZE_WIDE_BINARY
00127    QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::binary_wiarchive, boost::archive::binary_woarchive)
00128 #endif // _QX_SERIALIZE_WIDE_BINARY
00129 
00130 #if _QX_SERIALIZE_WIDE_TEXT
00131    QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::text_wiarchive, boost::archive::text_woarchive)
00132 #endif // _QX_SERIALIZE_WIDE_TEXT
00133 
00134 #if _QX_SERIALIZE_WIDE_XML
00135    QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::xml_wiarchive, boost::archive::xml_woarchive)
00136 #endif // _QX_SERIALIZE_WIDE_XML
00137 
00138 private:
00139 
00140    template <class Archive>
00141    static inline void toArchive(Archive & ar, const char * sName, const DataType * pData)
00142    { ar << boost::serialization::make_nvp(sName, (* pData)); }
00143 
00144    template <class Archive>
00145    static inline void fromArchive(Archive & ar, const char * sName, DataType * pData)
00146    { ar >> boost::serialization::make_nvp(sName, (* pData)); }
00147 
00148 private:
00149 
00150    template <bool bCanCompare /* = false */, int dummy>
00151    struct qxCompareDataMember
00152    {
00153       static inline bool isEqual(const QxDataMember<DataType, Owner> & dataMember, const void * pOwner1, const void * pOwner2)
00154       { return (dataMember.toString(pOwner1, "") == dataMember.toString(pOwner2, "")); }
00155    };
00156 
00157    template <int dummy>
00158    struct qxCompareDataMember<true, dummy>
00159    {
00160       static inline bool isEqual(const QxDataMember<DataType, Owner> & dataMember, const void * pOwner1, const void * pOwner2)
00161       { return ((* dataMember.getData(pOwner1)) == (* dataMember.getData(pOwner2))); }
00162    };
00163 
00164 };
00165 
00166 } // namespace qx
00167 
00168 #include "../../inl/QxDataMember/QxDataMember.inl"
00169 
00170 #endif // _QX_DATA_MEMBER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines