Go to the
documentation of this file.
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
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