QxOrm  1.5.0
C++ Object Relational Mapping library
QxDataMember.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** https://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 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00047 #include <boost/serialization/serialization.hpp>
00048 #include <boost/serialization/nvp.hpp>
00049 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00050 
00051 #include <QxDataMember/IxDataMember.h>
00052 
00053 #include <QxTraits/is_equal.h>
00054 #include <QxTraits/get_class_name.h>
00055 
00056 #define QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(ArchiveInput, ArchiveOutput) \
00057 virtual void toArchive(const void * pOwner, ArchiveOutput & ar) const   { QxDataMember::toArchive(ar, getNamePtr(), getData(pOwner)); } \
00058 virtual void fromArchive(void * pOwner, ArchiveInput & ar)              { QxDataMember::fromArchive(ar, getNamePtr(), getData(pOwner)); }
00059 
00060 namespace qx {
00061 
00066 template <typename DataType, class Owner>
00067 class QxDataMember : public IxDataMember
00068 {
00069 
00070 protected:
00071 
00072    typedef DataType Owner::* type_data_member_ptr;
00073 
00074    type_data_member_ptr m_pData;    
00075    IxDataMember * m_pImpl_;         
00076 
00077 public:
00078 
00079    QxDataMember(type_data_member_ptr pData, const QString & sKey, long lVersion, bool bSerialize, bool bDao, IxDataMember * pImpl = NULL) : IxDataMember(sKey, lVersion, bSerialize, bDao, pImpl), m_pData(pData), m_pImpl_(pImpl) { this->setAccessDataPointer(true); }
00080    virtual ~QxDataMember() { ; }
00081 
00082    inline DataType * getData(void * pOwner) const
00083    {
00084       void * pOwner_ = (m_pImpl_ ? m_pImpl_->getDataVoidPtr(pOwner) : pOwner);
00085       return (pOwner_ ? (& ((static_cast<Owner *>(pOwner_))->*m_pData)) : NULL);
00086    }
00087    inline const DataType * getData(const void * pOwner) const
00088    {
00089       const void * pOwner_ = (m_pImpl_ ? m_pImpl_->getDataVoidPtr(pOwner) : pOwner);
00090       return (pOwner_ ? (& ((static_cast<const Owner *>(pOwner_))->*m_pData)) : NULL);
00091    }
00092 
00093    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); }
00094    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); }
00095    virtual QString getType() const { QMutexLocker locker(& IxDataMember::m_mutex); return QString(qx::trait::get_class_name<DataType>::get()); }
00096 
00097 #ifndef _QX_NO_JSON
00098    virtual QJsonValue toJson(const void * pOwner, const QString & sFormat) const             { return qx::cvt::to_json((* getData(pOwner)), sFormat); }
00099    virtual qx_bool fromJson(void * pOwner, const QJsonValue & j, const QString & sFormat)    { return qx::cvt::from_json(j, (* getData(pOwner)), sFormat); }
00100 #endif // _QX_NO_JSON
00101 
00102    virtual bool isEqual(const void * pOwner1, const void * pOwner2) const
00103    {
00104       if ((pOwner1 == NULL) || (pOwner2 == NULL)) { return false; }
00105       if (pOwner1 == pOwner2) { return true; }
00106       return qxCompareDataMember<qx::trait::has_operator_equal_equal<DataType>::value, 0>::isEqual((* this), pOwner1, pOwner2);
00107    }
00108 
00109 protected:
00110 
00111    virtual qx::any getDataPtr(const void * pOwner) const       { return qx::any(getData(pOwner)); }
00112    virtual qx::any getDataPtr(void * pOwner)                   { return qx::any(getData(pOwner)); }
00113    virtual void * getDataVoidPtr(const void * pOwner) const    { return static_cast<void *>(const_cast<DataType *>(getData(pOwner))); }
00114    virtual void * getDataVoidPtr(void * pOwner)                { return static_cast<void *>(getData(pOwner)); }
00115 
00116 public:
00117 
00118 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00119 
00120 #if _QX_SERIALIZE_POLYMORPHIC
00121    QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::polymorphic_iarchive, boost::archive::polymorphic_oarchive)
00122 #endif // _QX_SERIALIZE_POLYMORPHIC
00123 
00124 #if _QX_SERIALIZE_BINARY
00125    QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::binary_iarchive, boost::archive::binary_oarchive)
00126 #endif // _QX_SERIALIZE_BINARY
00127 
00128 #if _QX_SERIALIZE_TEXT
00129    QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::text_iarchive, boost::archive::text_oarchive)
00130 #endif // _QX_SERIALIZE_TEXT
00131 
00132 #if _QX_SERIALIZE_XML
00133    QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::xml_iarchive, boost::archive::xml_oarchive)
00134 #endif // _QX_SERIALIZE_XML
00135 
00136 #if _QX_SERIALIZE_PORTABLE_BINARY
00137    QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(eos::portable_iarchive, eos::portable_oarchive)
00138 #endif // _QX_SERIALIZE_PORTABLE_BINARY
00139 
00140 #if _QX_SERIALIZE_WIDE_BINARY
00141    QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::binary_wiarchive, boost::archive::binary_woarchive)
00142 #endif // _QX_SERIALIZE_WIDE_BINARY
00143 
00144 #if _QX_SERIALIZE_WIDE_TEXT
00145    QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::text_wiarchive, boost::archive::text_woarchive)
00146 #endif // _QX_SERIALIZE_WIDE_TEXT
00147 
00148 #if _QX_SERIALIZE_WIDE_XML
00149    QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::xml_wiarchive, boost::archive::xml_woarchive)
00150 #endif // _QX_SERIALIZE_WIDE_XML
00151 
00152 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00153 
00154 private:
00155 
00156 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00157 
00158    template <class Archive>
00159    static inline void toArchive(Archive & ar, const char * sName, const DataType * pData)
00160    { ar << boost::serialization::make_nvp(sName, (* pData)); }
00161 
00162    template <class Archive>
00163    static inline void fromArchive(Archive & ar, const char * sName, DataType * pData)
00164    { ar >> boost::serialization::make_nvp(sName, (* pData)); }
00165 
00166 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00167 
00168 private:
00169 
00170    template <bool bCanCompare /* = false */, int dummy>
00171    struct qxCompareDataMember
00172    {
00173       static inline bool isEqual(const QxDataMember<DataType, Owner> & dataMember, const void * pOwner1, const void * pOwner2)
00174       { return (dataMember.toVariant(pOwner1, "") == dataMember.toVariant(pOwner2, "")); }
00175    };
00176 
00177    template <int dummy>
00178    struct qxCompareDataMember<true, dummy>
00179    {
00180       static inline bool isEqual(const QxDataMember<DataType, Owner> & dataMember, const void * pOwner1, const void * pOwner2)
00181       { return ((* dataMember.getData(pOwner1)) == (* dataMember.getData(pOwner2))); }
00182    };
00183 
00184 };
00185 
00186 } // namespace qx
00187 
00188 #include "../../inl/QxDataMember/QxDataMember.inl"
00189 
00190 #endif // _QX_DATA_MEMBER_H_