QxOrm
1.5.0
C++ Object Relational Mapping library
|
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_DAO_POINTER_H_ 00033 #define _QX_DAO_POINTER_H_ 00034 00035 #ifdef _MSC_VER 00036 #pragma once 00037 #endif 00038 00046 #include <QtCore/qsharedpointer.h> 00047 #include <QtCore/qstringlist.h> 00048 #include <QtCore/qdatastream.h> 00049 00050 #include <QxSerialize/QDataStream/QxSerializeQDataStream_QSharedPointer.h> 00051 00052 namespace qx { 00053 template <class T> QSharedPointer<T> clone_to_qt_shared_ptr(const T & obj); 00054 } // namespace qx 00055 00056 namespace qx { 00057 namespace dao { 00058 template <typename T> class ptr; 00059 } // namespace dao 00060 } // namespace qx 00061 00062 template <typename T> QDataStream & operator<< (QDataStream & stream, const qx::dao::ptr<T> & t); 00063 template <typename T> QDataStream & operator>> (QDataStream & stream, qx::dao::ptr<T> & t); 00064 00065 namespace qx { 00066 namespace dao { 00067 00068 namespace detail { 00069 template <class T> struct QxDao_IsDirty; 00070 } // namespace detail 00071 00137 template <typename T> 00138 class ptr 00139 { 00140 00141 template <class U> friend QDataStream & ::operator<< (QDataStream & stream, const qx::dao::ptr<U> & t); 00142 template <class U> friend QDataStream & ::operator>> (QDataStream & stream, qx::dao::ptr<U> & t); 00143 00144 private: 00145 00146 QSharedPointer<T> m_pWork; 00147 QSharedPointer<T> m_pOriginal; 00148 00149 public: 00150 00151 ptr() { ; } 00152 explicit ptr(T * ptr) : m_pWork(ptr) { ; } 00153 explicit ptr(T * ptr, T * original) : m_pWork(ptr), m_pOriginal(original) { ; } 00154 ptr(const qx::dao::ptr<T> & other) : m_pWork(other.m_pWork), m_pOriginal(other.m_pOriginal) { ; } 00155 ptr(const QSharedPointer<T> & other) : m_pWork(other) { ; } 00156 ptr(const QSharedPointer<T> & other, const QSharedPointer<T> & original) : m_pWork(other), m_pOriginal(original) { ; } 00157 ptr(const QWeakPointer<T> & other) : m_pWork(other) { ; } 00158 ptr(const QWeakPointer<T> & other, const QWeakPointer<T> & original) : m_pWork(other), m_pOriginal(original) { ; } 00159 virtual ~ptr() { ; } 00160 00161 template <typename Deleter> ptr(T * ptr, Deleter deleter) : m_pWork(ptr, deleter) { ; } 00162 template <typename Deleter> ptr(T * ptr, T * original, Deleter deleter) : m_pWork(ptr, deleter), m_pOriginal(original) { ; } 00163 00164 template <class X> ptr(const qx::dao::ptr<X> & other) : m_pWork(qSharedPointerCast<T>(other.m_pWork)), m_pOriginal(qSharedPointerCast<T>(other.m_pOriginal)) { ; } 00165 template <class X> ptr(const QSharedPointer<X> & other) : m_pWork(qSharedPointerCast<T>(other)) { ; } 00166 template <class X> ptr(const QSharedPointer<X> & other, const QSharedPointer<T> & original) : m_pWork(qSharedPointerCast<T>(other)), m_pOriginal(qSharedPointerCast<T>(original)) { ; } 00167 template <class X> ptr(const QWeakPointer<X> & other) : m_pWork(qSharedPointerCast<T>(other.toStrongRef())) { ; } 00168 template <class X> ptr(const QWeakPointer<X> & other, const QWeakPointer<X> & original) : m_pWork(qSharedPointerCast<T>(other.toStrongRef())), m_pOriginal(qSharedPointerCast<T>(original.toStrongRef())) { ; } 00169 00170 qx::dao::ptr<T> & operator=(const qx::dao::ptr<T> & other) { m_pWork = other.m_pWork; m_pOriginal = other.m_pOriginal; return (* this); } 00171 qx::dao::ptr<T> & operator=(const QSharedPointer<T> & other) { m_pWork = other; m_pOriginal.clear(); return (* this); } 00172 qx::dao::ptr<T> & operator=(const QWeakPointer<T> & other) { m_pWork = other; m_pOriginal.clear(); return (* this); } 00173 00174 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); } 00175 template <class X> qx::dao::ptr<T> & operator=(const QSharedPointer<X> & other) { m_pWork = qSharedPointerCast<T>(other); m_pOriginal.clear(); return (* this); } 00176 template <class X> qx::dao::ptr<T> & operator=(const QWeakPointer<X> & other) { m_pWork = qSharedPointerCast<T>(other.toStrongRef()); m_pOriginal.clear(); return (* this); } 00177 00178 inline T * get() const { return m_pWork.data(); } 00179 inline T * getOriginal() const { return m_pOriginal.data(); } 00180 inline T * data() const { return m_pWork.data(); } 00181 inline T * dataOriginal() const { return m_pOriginal.data(); } 00182 inline bool isNull() const { return m_pWork.isNull(); } 00183 inline operator bool() const { return (! m_pWork.isNull()); } 00184 inline bool operator!() const { return m_pWork.isNull(); } 00185 inline T & operator*() const { return (* m_pWork.data()); } 00186 inline T * operator->() const { return m_pWork.data(); } 00187 inline void clear() { m_pWork.clear(); m_pOriginal.clear(); } 00188 inline void reset() { m_pWork.clear(); m_pOriginal.clear(); } 00189 inline void reset(const QSharedPointer<T> & ptr) { m_pWork = ptr; m_pOriginal.clear(); } 00190 inline void resetOriginal(const QSharedPointer<T> & ptr) { m_pOriginal = ptr; } 00191 inline bool isDirty() const { QStringList lstDiff; return isDirty(lstDiff); } 00192 inline QSharedPointer<T> toQtSharedPointer() const { return m_pWork; } 00193 inline void saveToOriginal() { m_pOriginal.clear(); if (m_pWork) { m_pOriginal = qx::clone_to_qt_shared_ptr(* m_pWork); } } 00194 inline void restoreFromOriginal() { m_pWork.clear(); if (m_pOriginal) { m_pWork = qx::clone_to_qt_shared_ptr(* m_pOriginal); } } 00195 00196 template <class X> qx::dao::ptr<X> staticCast() const { return qx::dao::ptr<X>(m_pWork.template staticCast<X>(), m_pOriginal.template staticCast<X>()); } 00197 template <class X> qx::dao::ptr<X> dynamicCast() const { return qx::dao::ptr<X>(m_pWork.template dynamicCast<X>(), m_pOriginal.template dynamicCast<X>()); } 00198 template <class X> qx::dao::ptr<X> constCast() const { return qx::dao::ptr<X>(m_pWork.template constCast<X>(), m_pOriginal.template constCast<X>()); } 00199 00200 bool isDirty(QStringList & lstDiff) const 00201 { 00202 lstDiff.clear(); 00203 if (m_pWork.isNull() || m_pOriginal.isNull()) { lstDiff.append("*"); return true; } 00204 if (m_pWork == m_pOriginal) { return false; } 00205 qx::dao::detail::QxDao_IsDirty<T>::compare((* m_pWork), (* m_pOriginal), lstDiff); 00206 return (! lstDiff.isEmpty()); 00207 } 00208 00209 }; 00210 00211 } // namespace dao 00212 } // namespace qx 00213 00214 template<class T, class X> bool operator==(const qx::dao::ptr<T> & ptr1, const qx::dao::ptr<X> & ptr2) { return (ptr1.toQtSharedPointer() == ptr2.toQtSharedPointer()); } 00215 template<class T, class X> bool operator!=(const qx::dao::ptr<T> & ptr1, const qx::dao::ptr<X> & ptr2) { return (ptr1.toQtSharedPointer() != ptr2.toQtSharedPointer()); } 00216 template<class T, class X> bool operator==(const qx::dao::ptr<T> & ptr1, const X * ptr2) { return (ptr1.toQtSharedPointer() == ptr2); } 00217 template<class T, class X> bool operator!=(const qx::dao::ptr<T> & ptr1, const X * ptr2) { return (ptr1.toQtSharedPointer() != ptr2); } 00218 template<class T, class X> bool operator==(const T * ptr1, const qx::dao::ptr<X> & ptr2) { return (ptr1 == ptr2.toQtSharedPointer()); } 00219 template<class T, class X> bool operator!=(const T * ptr1, const qx::dao::ptr<X> & ptr2) { return (ptr1 != ptr2.toQtSharedPointer()); } 00220 00221 template <typename T> 00222 QDataStream & operator<< (QDataStream & stream, const qx::dao::ptr<T> & t) 00223 { 00224 stream << t.m_pWork; 00225 stream << t.m_pOriginal; 00226 return stream; 00227 } 00228 00229 template <typename T> 00230 QDataStream & operator>> (QDataStream & stream, qx::dao::ptr<T> & t) 00231 { 00232 stream >> t.m_pWork; 00233 stream >> t.m_pOriginal; 00234 return stream; 00235 } 00236 00237 #endif // _QX_DAO_POINTER_H_