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