QxOrm  1.2.3
C++ Object Relational Mapping library
QxDaoPointer.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://www.qxorm.com/
00004 ** http://sourceforge.net/projects/qxorm/
00005 ** Original file by Lionel Marty
00006 **
00007 ** This file is part of the QxOrm library
00008 **
00009 ** This software is provided 'as-is', without any express or implied
00010 ** warranty. In no event will the authors be held liable for any
00011 ** damages arising from the use of this software.
00012 **
00013 ** GNU Lesser General Public License Usage
00014 ** This file must be used under the terms of the GNU Lesser
00015 ** General Public License version 2.1 as published by the Free Software
00016 ** Foundation and appearing in the file 'license.lgpl.txt' included in the
00017 ** packaging of this file.  Please review the following information to
00018 ** ensure the GNU Lesser General Public License version 2.1 requirements
00019 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00020 **
00021 ** If you have questions regarding the use of this file, please contact :
00022 ** contact@qxorm.com
00023 **
00024 ****************************************************************************/
00025 
00026 #ifndef _QX_DAO_POINTER_H_
00027 #define _QX_DAO_POINTER_H_
00028 
00029 #ifdef _MSC_VER
00030 #pragma once
00031 #endif
00032 
00040 #include <QtCore/qsharedpointer.h>
00041 #include <QtCore/qstringlist.h>
00042 
00043 namespace qx {
00044 template <class T> QSharedPointer<T> clone_to_qt_shared_ptr(const T & obj);
00045 } // namespace qx
00046 
00047 namespace qx {
00048 namespace dao {
00049 
00050 namespace detail {
00051 template <class T> struct QxDao_IsDirty;
00052 } // namespace detail
00053 
00119 template <typename T>
00120 class ptr
00121 {
00122 
00123 private:
00124 
00125    QSharedPointer<T> m_pWork;       
00126    QSharedPointer<T> m_pOriginal;   
00127 
00128 public:
00129 
00130    ptr()                                                                                                             { ; }
00131    explicit ptr(T * ptr) : m_pWork(ptr)                                                                              { ; }
00132    explicit ptr(T * ptr, T * original) : m_pWork(ptr), m_pOriginal(original)                                         { ; }
00133    ptr(const qx::dao::ptr<T> & other) : m_pWork(other.m_pWork), m_pOriginal(other.m_pOriginal)                       { ; }
00134    ptr(const QSharedPointer<T> & other) : m_pWork(other)                                                             { ; }
00135    ptr(const QSharedPointer<T> & other, const QSharedPointer<T> & original) : m_pWork(other), m_pOriginal(original)  { ; }
00136    ptr(const QWeakPointer<T> & other) : m_pWork(other)                                                               { ; }
00137    ptr(const QWeakPointer<T> & other, const QWeakPointer<T> & original) : m_pWork(other), m_pOriginal(original)      { ; }
00138    virtual ~ptr()                                                                                                    { ; }
00139 
00140    template <typename Deleter> ptr(T * ptr, Deleter deleter) : m_pWork(ptr, deleter)                                       { ; }
00141    template <typename Deleter> ptr(T * ptr, T * original, Deleter deleter) : m_pWork(ptr, deleter), m_pOriginal(original)  { ; }
00142 
00143    template <class X> ptr(const qx::dao::ptr<X> & other) : m_pWork(qSharedPointerCast<T>(other.m_pWork)), m_pOriginal(qSharedPointerCast<T>(other.m_pOriginal))                                                { ; }
00144    template <class X> ptr(const QSharedPointer<X> & other) : m_pWork(qSharedPointerCast<T>(other))                                                                                                             { ; }
00145    template <class X> ptr(const QSharedPointer<X> & other, const QSharedPointer<T> & original) : m_pWork(qSharedPointerCast<T>(other)), m_pOriginal(qSharedPointerCast<T>(original))                           { ; }
00146    template <class X> ptr(const QWeakPointer<X> & other) : m_pWork(qSharedPointerCast<T>(other.toStrongRef()))                                                                                                 { ; }
00147    template <class X> ptr(const QWeakPointer<X> & other, const QWeakPointer<X> & original) : m_pWork(qSharedPointerCast<T>(other.toStrongRef())), m_pOriginal(qSharedPointerCast<T>(original.toStrongRef()))   { ; }
00148 
00149    qx::dao::ptr<T> & operator=(const qx::dao::ptr<T> & other)     { m_pWork = other.m_pWork; m_pOriginal = other.m_pOriginal; return (* this); }
00150    qx::dao::ptr<T> & operator=(const QSharedPointer<T> & other)   { m_pWork = other; m_pOriginal.clear(); return (* this); }
00151    qx::dao::ptr<T> & operator=(const QWeakPointer<T> & other)     { m_pWork = other; m_pOriginal.clear(); return (* this); }
00152 
00153    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); }
00154    template <class X> qx::dao::ptr<T> & operator=(const QSharedPointer<X> & other)  { m_pWork = qSharedPointerCast<T>(other); m_pOriginal.clear(); return (* this); }
00155    template <class X> qx::dao::ptr<T> & operator=(const QWeakPointer<X> & other)    { m_pWork = qSharedPointerCast<T>(other.toStrongRef()); m_pOriginal.clear(); return (* this); }
00156 
00157    inline T * get() const                                      { return m_pWork.data(); }
00158    inline T * getOriginal() const                              { return m_pOriginal.data(); }
00159    inline T * data() const                                     { return m_pWork.data(); }
00160    inline T * dataOriginal() const                             { return m_pOriginal.data(); }
00161    inline bool isNull() const                                  { return m_pWork.isNull(); }
00162    inline operator bool() const                                { return (! m_pWork.isNull()); }
00163    inline bool operator!() const                               { return m_pWork.isNull(); }
00164    inline T & operator*() const                                { return (* m_pWork.data()); }
00165    inline T * operator->() const                               { return m_pWork.data(); }
00166    inline void clear()                                         { m_pWork.clear(); m_pOriginal.clear(); }
00167    inline void reset()                                         { m_pWork.clear(); m_pOriginal.clear(); }
00168    inline void reset(const QSharedPointer<T> & ptr)            { m_pWork = ptr; m_pOriginal.clear(); }
00169    inline void resetOriginal(const QSharedPointer<T> & ptr)    { m_pOriginal = ptr; }
00170    inline void saveToOriginal()                                { m_pOriginal.clear(); if (m_pWork) { m_pOriginal = qx::clone_to_qt_shared_ptr(* m_pWork); } }
00171    inline void restoreFromOriginal()                           { m_pWork.clear(); if (m_pOriginal) { m_pWork = qx::clone_to_qt_shared_ptr(* m_pOriginal); } }
00172    inline bool isDirty() const                                 { QStringList lstDiff; return isDirty(lstDiff); }
00173    inline QSharedPointer<T> toQtSharedPointer() const          { return m_pWork; }
00174 
00175    template <class X> qx::dao::ptr<X> staticCast() const       { return qx::dao::ptr<X>(m_pWork.staticCast<X>(), m_pOriginal.staticCast<X>()); }
00176    template <class X> qx::dao::ptr<X> dynamicCast() const      { return qx::dao::ptr<X>(m_pWork.dynamicCast<X>(), m_pOriginal.dynamicCast<X>()); }
00177    template <class X> qx::dao::ptr<X> constCast() const        { return qx::dao::ptr<X>(m_pWork.constCast<X>(), m_pOriginal.constCast<X>()); }
00178 
00179    bool isDirty(QStringList & lstDiff) const
00180    {
00181       lstDiff.clear();
00182       if (m_pWork.isNull() || m_pOriginal.isNull()) { lstDiff.append("*"); return true; }
00183       if (m_pWork == m_pOriginal) { return false; }
00184       qx::dao::detail::QxDao_IsDirty<T>::compare((* m_pWork), (* m_pOriginal), lstDiff);
00185       return (! lstDiff.isEmpty());
00186    }
00187 
00188 };
00189 
00190 } // namespace dao
00191 } // namespace qx
00192 
00193 template<class T, class X> bool operator==(const qx::dao::ptr<T> & ptr1, const qx::dao::ptr<X> & ptr2)   { return (ptr1.toQtSharedPointer() == ptr2.toQtSharedPointer()); }
00194 template<class T, class X> bool operator!=(const qx::dao::ptr<T> & ptr1, const qx::dao::ptr<X> & ptr2)   { return (ptr1.toQtSharedPointer() != ptr2.toQtSharedPointer()); }
00195 template<class T, class X> bool operator==(const qx::dao::ptr<T> & ptr1, const X * ptr2)                 { return (ptr1.toQtSharedPointer() == ptr2); }
00196 template<class T, class X> bool operator!=(const qx::dao::ptr<T> & ptr1, const X * ptr2)                 { return (ptr1.toQtSharedPointer() != ptr2); }
00197 template<class T, class X> bool operator==(const T * ptr1, const qx::dao::ptr<X> & ptr2)                 { return (ptr1 == ptr2.toQtSharedPointer()); }
00198 template<class T, class X> bool operator!=(const T * ptr1, const qx::dao::ptr<X> & ptr2)                 { return (ptr1 != ptr2.toQtSharedPointer()); }
00199 
00200 #endif // _QX_DAO_POINTER_H_