QxOrm  1.2.8
C++ Object Relational Mapping library
QxDaoPointer.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://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 
00049 namespace qx {
00050 template <class T> QSharedPointer<T> clone_to_qt_shared_ptr(const T & obj);
00051 } // namespace qx
00052 
00053 namespace qx {
00054 namespace dao {
00055 
00056 namespace detail {
00057 template <class T> struct QxDao_IsDirty;
00058 } // namespace detail
00059 
00125 template <typename T>
00126 class ptr
00127 {
00128 
00129 private:
00130 
00131    QSharedPointer<T> m_pWork;       
00132    QSharedPointer<T> m_pOriginal;   
00133 
00134 public:
00135 
00136    ptr()                                                                                                             { ; }
00137    explicit ptr(T * ptr) : m_pWork(ptr)                                                                              { ; }
00138    explicit ptr(T * ptr, T * original) : m_pWork(ptr), m_pOriginal(original)                                         { ; }
00139    ptr(const qx::dao::ptr<T> & other) : m_pWork(other.m_pWork), m_pOriginal(other.m_pOriginal)                       { ; }
00140    ptr(const QSharedPointer<T> & other) : m_pWork(other)                                                             { ; }
00141    ptr(const QSharedPointer<T> & other, const QSharedPointer<T> & original) : m_pWork(other), m_pOriginal(original)  { ; }
00142    ptr(const QWeakPointer<T> & other) : m_pWork(other)                                                               { ; }
00143    ptr(const QWeakPointer<T> & other, const QWeakPointer<T> & original) : m_pWork(other), m_pOriginal(original)      { ; }
00144    virtual ~ptr()                                                                                                    { ; }
00145 
00146    template <typename Deleter> ptr(T * ptr, Deleter deleter) : m_pWork(ptr, deleter)                                       { ; }
00147    template <typename Deleter> ptr(T * ptr, T * original, Deleter deleter) : m_pWork(ptr, deleter), m_pOriginal(original)  { ; }
00148 
00149    template <class X> ptr(const qx::dao::ptr<X> & other) : m_pWork(qSharedPointerCast<T>(other.m_pWork)), m_pOriginal(qSharedPointerCast<T>(other.m_pOriginal))                                                { ; }
00150    template <class X> ptr(const QSharedPointer<X> & other) : m_pWork(qSharedPointerCast<T>(other))                                                                                                             { ; }
00151    template <class X> ptr(const QSharedPointer<X> & other, const QSharedPointer<T> & original) : m_pWork(qSharedPointerCast<T>(other)), m_pOriginal(qSharedPointerCast<T>(original))                           { ; }
00152    template <class X> ptr(const QWeakPointer<X> & other) : m_pWork(qSharedPointerCast<T>(other.toStrongRef()))                                                                                                 { ; }
00153    template <class X> ptr(const QWeakPointer<X> & other, const QWeakPointer<X> & original) : m_pWork(qSharedPointerCast<T>(other.toStrongRef())), m_pOriginal(qSharedPointerCast<T>(original.toStrongRef()))   { ; }
00154 
00155    qx::dao::ptr<T> & operator=(const qx::dao::ptr<T> & other)     { m_pWork = other.m_pWork; m_pOriginal = other.m_pOriginal; return (* this); }
00156    qx::dao::ptr<T> & operator=(const QSharedPointer<T> & other)   { m_pWork = other; m_pOriginal.clear(); return (* this); }
00157    qx::dao::ptr<T> & operator=(const QWeakPointer<T> & other)     { m_pWork = other; m_pOriginal.clear(); return (* this); }
00158 
00159    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); }
00160    template <class X> qx::dao::ptr<T> & operator=(const QSharedPointer<X> & other)  { m_pWork = qSharedPointerCast<T>(other); m_pOriginal.clear(); return (* this); }
00161    template <class X> qx::dao::ptr<T> & operator=(const QWeakPointer<X> & other)    { m_pWork = qSharedPointerCast<T>(other.toStrongRef()); m_pOriginal.clear(); return (* this); }
00162 
00163    inline T * get() const                                      { return m_pWork.data(); }
00164    inline T * getOriginal() const                              { return m_pOriginal.data(); }
00165    inline T * data() const                                     { return m_pWork.data(); }
00166    inline T * dataOriginal() const                             { return m_pOriginal.data(); }
00167    inline bool isNull() const                                  { return m_pWork.isNull(); }
00168    inline operator bool() const                                { return (! m_pWork.isNull()); }
00169    inline bool operator!() const                               { return m_pWork.isNull(); }
00170    inline T & operator*() const                                { return (* m_pWork.data()); }
00171    inline T * operator->() const                               { return m_pWork.data(); }
00172    inline void clear()                                         { m_pWork.clear(); m_pOriginal.clear(); }
00173    inline void reset()                                         { m_pWork.clear(); m_pOriginal.clear(); }
00174    inline void reset(const QSharedPointer<T> & ptr)            { m_pWork = ptr; m_pOriginal.clear(); }
00175    inline void resetOriginal(const QSharedPointer<T> & ptr)    { m_pOriginal = ptr; }
00176    inline void saveToOriginal()                                { m_pOriginal.clear(); if (m_pWork) { m_pOriginal = qx::clone_to_qt_shared_ptr(* m_pWork); } }
00177    inline void restoreFromOriginal()                           { m_pWork.clear(); if (m_pOriginal) { m_pWork = qx::clone_to_qt_shared_ptr(* m_pOriginal); } }
00178    inline bool isDirty() const                                 { QStringList lstDiff; return isDirty(lstDiff); }
00179    inline QSharedPointer<T> toQtSharedPointer() const          { return m_pWork; }
00180 
00181    template <class X> qx::dao::ptr<X> staticCast() const       { return qx::dao::ptr<X>(m_pWork.template staticCast<X>(), m_pOriginal.template staticCast<X>()); }
00182    template <class X> qx::dao::ptr<X> dynamicCast() const      { return qx::dao::ptr<X>(m_pWork.template dynamicCast<X>(), m_pOriginal.template dynamicCast<X>()); }
00183    template <class X> qx::dao::ptr<X> constCast() const        { return qx::dao::ptr<X>(m_pWork.template constCast<X>(), m_pOriginal.template constCast<X>()); }
00184 
00185    bool isDirty(QStringList & lstDiff) const
00186    {
00187       lstDiff.clear();
00188       if (m_pWork.isNull() || m_pOriginal.isNull()) { lstDiff.append("*"); return true; }
00189       if (m_pWork == m_pOriginal) { return false; }
00190       qx::dao::detail::QxDao_IsDirty<T>::compare((* m_pWork), (* m_pOriginal), lstDiff);
00191       return (! lstDiff.isEmpty());
00192    }
00193 
00194 };
00195 
00196 } // namespace dao
00197 } // namespace qx
00198 
00199 template<class T, class X> bool operator==(const qx::dao::ptr<T> & ptr1, const qx::dao::ptr<X> & ptr2)   { return (ptr1.toQtSharedPointer() == ptr2.toQtSharedPointer()); }
00200 template<class T, class X> bool operator!=(const qx::dao::ptr<T> & ptr1, const qx::dao::ptr<X> & ptr2)   { return (ptr1.toQtSharedPointer() != ptr2.toQtSharedPointer()); }
00201 template<class T, class X> bool operator==(const qx::dao::ptr<T> & ptr1, const X * ptr2)                 { return (ptr1.toQtSharedPointer() == ptr2); }
00202 template<class T, class X> bool operator!=(const qx::dao::ptr<T> & ptr1, const X * ptr2)                 { return (ptr1.toQtSharedPointer() != ptr2); }
00203 template<class T, class X> bool operator==(const T * ptr1, const qx::dao::ptr<X> & ptr2)                 { return (ptr1 == ptr2.toQtSharedPointer()); }
00204 template<class T, class X> bool operator!=(const T * ptr1, const qx::dao::ptr<X> & ptr2)                 { return (ptr1 != ptr2.toQtSharedPointer()); }
00205 
00206 #endif // _QX_DAO_POINTER_H_