QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxDataMember.h
Go to the documentation of this file.
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
32#ifndef _QX_DATA_MEMBER_H_
33#define _QX_DATA_MEMBER_H_
34
35#ifdef _MSC_VER
36#pragma once
37#endif
38
46#ifdef _QX_ENABLE_BOOST_SERIALIZATION
47#include <boost/serialization/serialization.hpp>
48#include <boost/serialization/nvp.hpp>
49#endif // _QX_ENABLE_BOOST_SERIALIZATION
50
52
53#include <QxTraits/is_equal.h>
55
56#define QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(ArchiveInput, ArchiveOutput) \
57virtual void toArchive(const void * pOwner, ArchiveOutput & ar) const { QxDataMember::toArchive(ar, getNamePtr(), getData(pOwner)); } \
58virtual void fromArchive(void * pOwner, ArchiveInput & ar) { QxDataMember::fromArchive(ar, getNamePtr(), getData(pOwner)); }
59
60namespace qx {
61
66template <typename DataType, class Owner>
68{
69
70protected:
71
72 typedef DataType Owner::* type_data_member_ptr;
73
76
77public:
78
79 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); }
80 virtual ~QxDataMember() { ; }
81
82 inline DataType * getData(void * pOwner) const
83 {
84 void * pOwner_ = (m_pImpl_ ? m_pImpl_->getDataVoidPtr(pOwner) : pOwner);
85 return (pOwner_ ? (& ((static_cast<Owner *>(pOwner_))->*m_pData)) : NULL);
86 }
87 inline const DataType * getData(const void * pOwner) const
88 {
89 const void * pOwner_ = (m_pImpl_ ? m_pImpl_->getDataVoidPtr(pOwner) : pOwner);
90 return (pOwner_ ? (& ((static_cast<const Owner *>(pOwner_))->*m_pData)) : NULL);
91 }
92
93 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); }
94 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); }
95 virtual QString getType() const { return QString(qx::trait::get_class_name<DataType>::get()); }
96
97#ifndef _QX_NO_JSON
98 virtual QJsonValue toJson(const void * pOwner, const QString & sFormat) const { return qx::cvt::to_json((* getData(pOwner)), sFormat); }
99 virtual qx_bool fromJson(void * pOwner, const QJsonValue & j, const QString & sFormat) { return qx::cvt::from_json(j, (* getData(pOwner)), sFormat); }
100#endif // _QX_NO_JSON
101
102 virtual bool isEqual(const void * pOwner1, const void * pOwner2) const
103 {
104 if ((pOwner1 == NULL) || (pOwner2 == NULL)) { return false; }
105 if (pOwner1 == pOwner2) { return true; }
107 }
108
109protected:
110
111 virtual qx::any getDataPtr(const void * pOwner) const { return qx::any(getData(pOwner)); }
112 virtual qx::any getDataPtr(void * pOwner) { return qx::any(getData(pOwner)); }
113 virtual void * getDataVoidPtr(const void * pOwner) const { return static_cast<void *>(const_cast<DataType *>(getData(pOwner))); }
114 virtual void * getDataVoidPtr(void * pOwner) { return static_cast<void *>(getData(pOwner)); }
115
116public:
117
118#ifdef _QX_ENABLE_BOOST_SERIALIZATION
119
120#if _QX_SERIALIZE_POLYMORPHIC
121 QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::polymorphic_iarchive, boost::archive::polymorphic_oarchive)
122#endif // _QX_SERIALIZE_POLYMORPHIC
123
124#if _QX_SERIALIZE_BINARY
125 QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::binary_iarchive, boost::archive::binary_oarchive)
126#endif // _QX_SERIALIZE_BINARY
127
128#if _QX_SERIALIZE_TEXT
129 QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::text_iarchive, boost::archive::text_oarchive)
130#endif // _QX_SERIALIZE_TEXT
131
132#if _QX_SERIALIZE_XML
133 QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::xml_iarchive, boost::archive::xml_oarchive)
134#endif // _QX_SERIALIZE_XML
135
136#if _QX_SERIALIZE_PORTABLE_BINARY
138#endif // _QX_SERIALIZE_PORTABLE_BINARY
139
140#if _QX_SERIALIZE_WIDE_BINARY
141 QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::binary_wiarchive, boost::archive::binary_woarchive)
142#endif // _QX_SERIALIZE_WIDE_BINARY
143
144#if _QX_SERIALIZE_WIDE_TEXT
145 QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::text_wiarchive, boost::archive::text_woarchive)
146#endif // _QX_SERIALIZE_WIDE_TEXT
147
148#if _QX_SERIALIZE_WIDE_XML
149 QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(boost::archive::xml_wiarchive, boost::archive::xml_woarchive)
150#endif // _QX_SERIALIZE_WIDE_XML
151
152#endif // _QX_ENABLE_BOOST_SERIALIZATION
153
154private:
155
156#ifdef _QX_ENABLE_BOOST_SERIALIZATION
157
158 template <class Archive>
159 static inline void toArchive(Archive & ar, const char * sName, const DataType * pData)
160 { ar << boost::serialization::make_nvp(sName, (* pData)); }
161
162 template <class Archive>
163 static inline void fromArchive(Archive & ar, const char * sName, DataType * pData)
164 { ar >> boost::serialization::make_nvp(sName, (* pData)); }
165
166#endif // _QX_ENABLE_BOOST_SERIALIZATION
167
168private:
169
170 template <bool bCanCompare /* = false */, int dummy>
172 {
173 static inline bool isEqual(const QxDataMember<DataType, Owner> & dataMember, const void * pOwner1, const void * pOwner2)
174 { return (dataMember.toVariant(pOwner1, "") == dataMember.toVariant(pOwner2, "")); }
175 };
176
177 template <int dummy>
178 struct qxCompareDataMember<true, dummy>
179 {
180 static inline bool isEqual(const QxDataMember<DataType, Owner> & dataMember, const void * pOwner1, const void * pOwner2)
181 { return ((* dataMember.getData(pOwner1)) == (* dataMember.getData(pOwner2))); }
182 };
183
184};
185
186} // namespace qx
187
188#include "../../inl/QxDataMember/QxDataMember.inl"
189
190#endif // _QX_DATA_MEMBER_H_
Common interface for all class properties registered into QxOrm context.
#define QX_DATA_MEMBER_IMPL_VIRTUAL_ARCHIVE(ArchiveInput, ArchiveOutput)
Portable binary input archive using little endian format.
Portable binary output archive using little endian format.
qx::IxDataMember : common interface for all class properties registered into QxOrm context
virtual void * getDataVoidPtr(const void *pOwner) const =0
void setAccessDataPointer(bool b)
qx_bool : boolean type with code and description message when an error occured
Definition QxBool.h:71
qx::QxDataMember<DataType, Owner> : concrete property of type DataType registered into QxOrm context ...
virtual qx::any getDataPtr(const void *pOwner) const
IxDataMember * m_pImpl_
If not NULL then this data member is owned by a private implementation idiom instance.
virtual QJsonValue toJson(const void *pOwner, const QString &sFormat) const
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)
DataType Owner::* type_data_member_ptr
virtual ~QxDataMember()
virtual void * getDataVoidPtr(void *pOwner)
virtual void * getDataVoidPtr(const void *pOwner) const
QxDataMember(type_data_member_ptr pData, const QString &sKey, long lVersion, bool bSerialize, bool bDao, IxDataMember *pImpl=NULL)
virtual qx::any getDataPtr(void *pOwner)
virtual bool isEqual(const void *pOwner1, const void *pOwner2) const
type_data_member_ptr m_pData
Data member under format "& Owner::DataMember".
static void fromArchive(Archive &ar, const char *sName, DataType *pData)
static void toArchive(Archive &ar, const char *sName, const DataType *pData)
DataType * getData(void *pOwner) const
const DataType * getData(const void *pOwner) const
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
virtual QString getType() const
virtual qx_bool fromJson(void *pOwner, const QJsonValue &j, const QString &sFormat)
qx::trait::get_class_name<T>::get() : return class name of type T under const char * format,...
qx::trait::has_operator_equal_equal<T>::value : return true if T provides operator==() function,...
qx_bool from_variant(const QVariant &v, T &t, const QString &format=QString(), int index=-1, qx::cvt::context::ctx_type ctx=qx::cvt::context::e_no_context)
Definition QxConvert.h:80
QVariant to_variant(const T &t, const QString &format=QString(), int index=-1, qx::cvt::context::ctx_type ctx=qx::cvt::context::e_no_context)
Definition QxConvert.h:79
qx_bool from_json(const QJsonValue &j, T &t, const QString &format=QString())
Definition QxConvert.h:84
QJsonValue to_json(const T &t, const QString &format=QString())
Definition QxConvert.h:83
Root namespace for all QxOrm library features.
static bool isEqual(const QxDataMember< DataType, Owner > &dataMember, const void *pOwner1, const void *pOwner2)
static bool isEqual(const QxDataMember< DataType, Owner > &dataMember, const void *pOwner1, const void *pOwner2)
qx::trait::get_class_name<T>::get() : return class name of type T under const char * format,...