qpointer.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtCore module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QPOINTER_H
00043 #define QPOINTER_H
00044 
00045 #include <QtCore/qobject.h>
00046 
00047 QT_BEGIN_HEADER
00048 
00049 QT_BEGIN_NAMESPACE
00050 
00051 QT_MODULE(Core)
00052 
00053 template <class T>
00054 class QPointer
00055 {
00056     QObject *o;
00057 public:
00058     inline QPointer() : o(0) {}
00059     inline QPointer(T *p) : o(p)
00060         { QMetaObject::addGuard(&o); }
00061     inline QPointer(const QPointer<T> &p) : o(p.o)
00062         { QMetaObject::addGuard(&o); }
00063     inline ~QPointer()
00064         { QMetaObject::removeGuard(&o); }
00065     inline QPointer<T> &operator=(const QPointer<T> &p)
00066         { if (this != &p) QMetaObject::changeGuard(&o, p.o); return *this; }
00067     inline QPointer<T> &operator=(T* p)
00068         { if (o != p) QMetaObject::changeGuard(&o, p); return *this; }
00069 
00070     inline bool isNull() const
00071         { return !o; }
00072 
00073     inline T* operator->() const
00074         { return static_cast<T*>(const_cast<QObject*>(o)); }
00075     inline T& operator*() const
00076         { return *static_cast<T*>(const_cast<QObject*>(o)); }
00077     inline operator T*() const
00078         { return static_cast<T*>(const_cast<QObject*>(o)); }
00079     inline T* data() const
00080         { return static_cast<T*>(const_cast<QObject*>(o)); }
00081 };
00082 
00083 
00084 #if (!defined(Q_CC_SUN) || (__SUNPRO_CC >= 0x580)) // ambiguity between const T * and T *
00085 
00086 template <class T>
00087 inline bool operator==(const T *o, const QPointer<T> &p)
00088 { return o == p.operator->(); }
00089 
00090 template<class T>
00091 inline bool operator==(const QPointer<T> &p, const T *o)
00092 { return p.operator->() == o; }
00093 
00094 #else
00095 
00096 template<class T>
00097 inline bool operator==(const void *o, const QPointer<T> &p)
00098 { return o == p.operator->(); }
00099 
00100 template<class T>
00101 inline bool operator==(const QPointer<T> &p, const void *o)
00102 { return p.operator->() == o; }
00103 
00104 #endif
00105 
00106 template <class T>
00107 inline bool operator==(T *o, const QPointer<T> &p)
00108 { return o == p.operator->(); }
00109 
00110 template<class T>
00111 inline bool operator==(const QPointer<T> &p, T *o)
00112 { return p.operator->() == o; }
00113 
00114 template<class T>
00115 inline bool operator==(const QPointer<T> &p1, const QPointer<T> &p2)
00116 { return p1.operator->() == p2.operator->(); }
00117 
00118 
00119 #if (!defined(Q_CC_SUN) || (__SUNPRO_CC >= 0x580)) // ambiguity between const T * and T *
00120 
00121 template <class T>
00122 inline bool operator!=(const T *o, const QPointer<T> &p)
00123 { return o != p.operator->(); }
00124 
00125 template<class T>
00126 inline bool operator!= (const QPointer<T> &p, const T *o)
00127 { return p.operator->() != o; }
00128 
00129 #else
00130 
00131 template<class T>
00132 inline bool operator!= (const void *o, const QPointer<T> &p)
00133 { return o != p.operator->(); }
00134 
00135 template<class T>
00136 inline bool operator!= (const QPointer<T> &p, const void *o)
00137 { return p.operator->() != o; }
00138 
00139 #endif
00140 
00141 template <class T>
00142 inline bool operator!=(T *o, const QPointer<T> &p)
00143 { return o != p.operator->(); }
00144 
00145 template<class T>
00146 inline bool operator!= (const QPointer<T> &p, T *o)
00147 { return p.operator->() != o; }
00148 
00149 template<class T>
00150 inline bool operator!= (const QPointer<T> &p1, const QPointer<T> &p2)
00151 { return p1.operator->() != p2.operator->() ; }
00152 
00153 // Make MSVC < 1400 (2005) handle "if (NULL == p)" syntax
00154 #if defined(Q_CC_MSVC) && (_MSC_VER < 1400)
00155 template<class T>
00156 inline bool operator== (int i, const QPointer<T> &p)
00157 { Q_ASSERT(i == 0); return !i && p.isNull(); }
00158 
00159 template<class T>
00160 inline bool operator!= (int i, const QPointer<T> &p)
00161 { Q_ASSERT(i == 0); return !i && !p.isNull(); }
00162 #endif
00163 
00164 QT_END_NAMESPACE
00165 
00166 QT_END_HEADER
00167 
00168 #endif // QPOINTER_H