QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxDaoPointer.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_DAO_POINTER_H_
33#define _QX_DAO_POINTER_H_
34
35#ifdef _MSC_VER
36#pragma once
37#endif
38
46#include <QtCore/qsharedpointer.h>
47#include <QtCore/qstringlist.h>
48#include <QtCore/qdatastream.h>
49
51
52namespace qx {
53template <class T> QSharedPointer<T> clone_to_qt_shared_ptr(const T & obj);
54} // namespace qx
55
56namespace qx {
57namespace dao {
58template <typename T> class ptr;
59} // namespace dao
60} // namespace qx
61
62template <typename T> QDataStream & operator<< (QDataStream & stream, const qx::dao::ptr<T> & t);
63template <typename T> QDataStream & operator>> (QDataStream & stream, qx::dao::ptr<T> & t);
64
65namespace qx {
66namespace dao {
67
68namespace detail {
69template <class T> struct QxDao_IsDirty;
70} // namespace detail
71
137template <typename T>
138class ptr
139{
140
141 template <class U> friend QDataStream & ::operator<< (QDataStream & stream, const qx::dao::ptr<U> & t);
142 template <class U> friend QDataStream & ::operator>> (QDataStream & stream, qx::dao::ptr<U> & t);
143
144private:
145
146 QSharedPointer<T> m_pWork;
147 QSharedPointer<T> m_pOriginal;
148
149public:
150
151 ptr() { ; }
152 explicit ptr(T * ptr) : m_pWork(ptr) { ; }
153 explicit ptr(T * ptr, T * original) : m_pWork(ptr), m_pOriginal(original) { ; }
154 ptr(const qx::dao::ptr<T> & other) : m_pWork(other.m_pWork), m_pOriginal(other.m_pOriginal) { ; }
155 ptr(const QSharedPointer<T> & other) : m_pWork(other) { ; }
156 ptr(const QSharedPointer<T> & other, const QSharedPointer<T> & original) : m_pWork(other), m_pOriginal(original) { ; }
157 ptr(const QWeakPointer<T> & other) : m_pWork(other) { ; }
158 ptr(const QWeakPointer<T> & other, const QWeakPointer<T> & original) : m_pWork(other), m_pOriginal(original) { ; }
159 virtual ~ptr() { ; }
160
161 template <typename Deleter> ptr(T * ptr, Deleter deleter) : m_pWork(ptr, deleter) { ; }
162 template <typename Deleter> ptr(T * ptr, T * original, Deleter deleter) : m_pWork(ptr, deleter), m_pOriginal(original) { ; }
163
164 template <class X> ptr(const qx::dao::ptr<X> & other) : m_pWork(qSharedPointerCast<T>(other.m_pWork)), m_pOriginal(qSharedPointerCast<T>(other.m_pOriginal)) { ; }
165 template <class X> ptr(const QSharedPointer<X> & other) : m_pWork(qSharedPointerCast<T>(other)) { ; }
166 template <class X> ptr(const QSharedPointer<X> & other, const QSharedPointer<T> & original) : m_pWork(qSharedPointerCast<T>(other)), m_pOriginal(qSharedPointerCast<T>(original)) { ; }
167 template <class X> ptr(const QWeakPointer<X> & other) : m_pWork(qSharedPointerCast<T>(other.toStrongRef())) { ; }
168 template <class X> ptr(const QWeakPointer<X> & other, const QWeakPointer<X> & original) : m_pWork(qSharedPointerCast<T>(other.toStrongRef())), m_pOriginal(qSharedPointerCast<T>(original.toStrongRef())) { ; }
169
170 qx::dao::ptr<T> & operator=(const qx::dao::ptr<T> & other) { m_pWork = other.m_pWork; m_pOriginal = other.m_pOriginal; return (* this); }
171 qx::dao::ptr<T> & operator=(const QSharedPointer<T> & other) { m_pWork = other; m_pOriginal.clear(); return (* this); }
172 qx::dao::ptr<T> & operator=(const QWeakPointer<T> & other) { m_pWork = other; m_pOriginal.clear(); return (* this); }
173
174 template <class X> qx::dao::ptr<T> & operator=(const qx::dao::ptr<X> & other) { m_pWork = qSharedPointerCast<T>(other.m_pWork); m_pOriginal = qSharedPointerCast<T>(other.m_pOriginal); return (* this); }
175 template <class X> qx::dao::ptr<T> & operator=(const QSharedPointer<X> & other) { m_pWork = qSharedPointerCast<T>(other); m_pOriginal.clear(); return (* this); }
176 template <class X> qx::dao::ptr<T> & operator=(const QWeakPointer<X> & other) { m_pWork = qSharedPointerCast<T>(other.toStrongRef()); m_pOriginal.clear(); return (* this); }
177
178 inline T * get() const { return m_pWork.data(); }
179 inline T * getOriginal() const { return m_pOriginal.data(); }
180 inline T * data() const { return m_pWork.data(); }
181 inline T * dataOriginal() const { return m_pOriginal.data(); }
182 inline bool isNull() const { return m_pWork.isNull(); }
183 inline operator bool() const { return (! m_pWork.isNull()); }
184 inline bool operator!() const { return m_pWork.isNull(); }
185 inline T & operator*() const { return (* m_pWork.data()); }
186 inline T * operator->() const { return m_pWork.data(); }
187 inline void clear() { m_pWork.clear(); m_pOriginal.clear(); }
188 inline void reset() { m_pWork.clear(); m_pOriginal.clear(); }
189 inline void reset(const QSharedPointer<T> & ptr) { m_pWork = ptr; m_pOriginal.clear(); }
190 inline void resetOriginal(const QSharedPointer<T> & ptr) { m_pOriginal = ptr; }
191 inline bool isDirty() const { QStringList lstDiff; return isDirty(lstDiff); }
192 inline QSharedPointer<T> toQtSharedPointer() const { return m_pWork; }
195
196 template <class X> qx::dao::ptr<X> staticCast() const { return qx::dao::ptr<X>(m_pWork.template staticCast<X>(), m_pOriginal.template staticCast<X>()); }
197 template <class X> qx::dao::ptr<X> dynamicCast() const { return qx::dao::ptr<X>(m_pWork.template dynamicCast<X>(), m_pOriginal.template dynamicCast<X>()); }
198 template <class X> qx::dao::ptr<X> constCast() const { return qx::dao::ptr<X>(m_pWork.template constCast<X>(), m_pOriginal.template constCast<X>()); }
199
200 bool isDirty(QStringList & lstDiff) const
201 {
202 lstDiff.clear();
203 if (m_pWork.isNull() || m_pOriginal.isNull()) { lstDiff.append("*"); return true; }
204 if (m_pWork == m_pOriginal) { return false; }
206 return (! lstDiff.isEmpty());
207 }
208
209};
210
211} // namespace dao
212} // namespace qx
213
214template<class T, class X> bool operator==(const qx::dao::ptr<T> & ptr1, const qx::dao::ptr<X> & ptr2) { return (ptr1.toQtSharedPointer() == ptr2.toQtSharedPointer()); }
215template<class T, class X> bool operator!=(const qx::dao::ptr<T> & ptr1, const qx::dao::ptr<X> & ptr2) { return (ptr1.toQtSharedPointer() != ptr2.toQtSharedPointer()); }
216template<class T, class X> bool operator==(const qx::dao::ptr<T> & ptr1, const X * ptr2) { return (ptr1.toQtSharedPointer() == ptr2); }
217template<class T, class X> bool operator!=(const qx::dao::ptr<T> & ptr1, const X * ptr2) { return (ptr1.toQtSharedPointer() != ptr2); }
218template<class T, class X> bool operator==(const T * ptr1, const qx::dao::ptr<X> & ptr2) { return (ptr1 == ptr2.toQtSharedPointer()); }
219template<class T, class X> bool operator!=(const T * ptr1, const qx::dao::ptr<X> & ptr2) { return (ptr1 != ptr2.toQtSharedPointer()); }
220
221template <typename T>
222QDataStream & operator<< (QDataStream & stream, const qx::dao::ptr<T> & t)
223{
224 stream << t.m_pWork;
225 stream << t.m_pOriginal;
226 return stream;
227}
228
229template <typename T>
230QDataStream & operator>> (QDataStream & stream, qx::dao::ptr<T> & t)
231{
232 stream >> t.m_pWork;
233 stream >> t.m_pOriginal;
234 return stream;
235}
236
237#endif // _QX_DAO_POINTER_H_
bool operator==(const qx::dao::ptr< T > &ptr1, const qx::dao::ptr< X > &ptr2)
QDataStream & operator<<(QDataStream &stream, const qx::dao::ptr< T > &t)
bool operator!=(const qx::dao::ptr< T > &ptr1, const qx::dao::ptr< X > &ptr2)
QDataStream & operator>>(QDataStream &stream, qx::dao::ptr< T > &t)
Provide a Qt QDataStream serialization method (save/load) for type QSharedPointer<T>
qx::dao::ptr<T> : provide a classic smart-pointer (like boost::shared_ptr<T> or QSharedPointer<T>) wi...
ptr(const QWeakPointer< T > &other)
bool isDirty(QStringList &lstDiff) const
qx::dao::ptr< X > constCast() const
ptr(T *ptr, T *original, Deleter deleter)
qx::dao::ptr< T > & operator=(const QSharedPointer< T > &other)
T * operator->() const
virtual ~ptr()
ptr(const QSharedPointer< X > &other, const QSharedPointer< T > &original)
T * data() const
qx::dao::ptr< T > & operator=(const qx::dao::ptr< X > &other)
qx::dao::ptr< X > dynamicCast() const
qx::dao::ptr< X > staticCast() const
void saveToOriginal()
ptr(const QSharedPointer< X > &other)
T & operator*() const
ptr(T *ptr, T *original)
T * getOriginal() const
T * get() const
ptr(const QWeakPointer< X > &other)
void resetOriginal(const QSharedPointer< T > &ptr)
ptr(const qx::dao::ptr< T > &other)
bool operator!() const
ptr(T *ptr, Deleter deleter)
bool isNull() const
void restoreFromOriginal()
bool isDirty() const
qx::dao::ptr< T > & operator=(const QWeakPointer< T > &other)
ptr(const QSharedPointer< T > &other)
ptr(const QSharedPointer< T > &other, const QSharedPointer< T > &original)
QSharedPointer< T > m_pOriginal
Keep original pointer containing all values from database.
qx::dao::ptr< T > & operator=(const QSharedPointer< X > &other)
qx::dao::ptr< T > & operator=(const qx::dao::ptr< T > &other)
QSharedPointer< T > m_pWork
Default pointer => user works with this pointer.
qx::dao::ptr< T > & operator=(const QWeakPointer< X > &other)
QSharedPointer< T > toQtSharedPointer() const
ptr(const QWeakPointer< X > &other, const QWeakPointer< X > &original)
void reset(const QSharedPointer< T > &ptr)
T * dataOriginal() const
ptr(const qx::dao::ptr< X > &other)
ptr(const QWeakPointer< T > &other, const QWeakPointer< T > &original)
QSharedPointer< T > clone_to_qt_shared_ptr(const T &obj)
qx::clone_to_qt_shared_ptr(const T & obj) : return a Qt smart-pointer (QSharedPointer<T>) of a new in...
Definition QxClone.h:148
Root namespace for all QxOrm library features.
static void compare(const T &obj1, const T &obj2, QStringList &lstDiff)