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 QSCOPEDPOINTER_H
00043 #define QSCOPEDPOINTER_H
00044
00045 #include <QtCore/qglobal.h>
00046
00047 QT_BEGIN_HEADER
00048 QT_BEGIN_NAMESPACE
00049 QT_MODULE(Core)
00050
00051 template <typename T>
00052 struct QScopedPointerDeleter
00053 {
00054 static inline void cleanup(T *pointer)
00055 {
00056
00057
00058
00059 typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
00060 (void) sizeof(IsIncompleteType);
00061
00062 delete pointer;
00063 }
00064 };
00065
00066 template <typename T>
00067 struct QScopedPointerArrayDeleter
00068 {
00069 static inline void cleanup(T *pointer)
00070 {
00071
00072
00073
00074 typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
00075 (void) sizeof(IsIncompleteType);
00076
00077 delete [] pointer;
00078 }
00079 };
00080
00081 struct QScopedPointerPodDeleter
00082 {
00083 static inline void cleanup(void *pointer) { if (pointer) qFree(pointer); }
00084 };
00085
00086 template <typename T, typename Cleanup = QScopedPointerDeleter<T> >
00087 class QScopedPointer
00088 {
00089 #ifndef Q_CC_NOKIAX86
00090 typedef T *QScopedPointer:: *RestrictedBool;
00091 #endif
00092 public:
00093 explicit inline QScopedPointer(T *p = 0) : d(p)
00094 {
00095 }
00096
00097 inline ~QScopedPointer()
00098 {
00099 T *oldD = this->d;
00100 Cleanup::cleanup(oldD);
00101 this->d = 0;
00102 }
00103
00104 inline T &operator*() const
00105 {
00106 Q_ASSERT(d);
00107 return *d;
00108 }
00109
00110 inline T *operator->() const
00111 {
00112 Q_ASSERT(d);
00113 return d;
00114 }
00115
00116 inline bool operator!() const
00117 {
00118 return !d;
00119 }
00120
00121 #if defined(Q_CC_NOKIAX86) || defined(Q_QDOC)
00122 inline operator bool() const
00123 {
00124 return isNull() ? 0 : &QScopedPointer::d;
00125 }
00126 #else
00127 inline operator RestrictedBool() const
00128 {
00129 return isNull() ? 0 : &QScopedPointer::d;
00130 }
00131 #endif
00132
00133 inline T *data() const
00134 {
00135 return d;
00136 }
00137
00138 inline bool isNull() const
00139 {
00140 return !d;
00141 }
00142
00143 inline void reset(T *other = 0)
00144 {
00145 if (d == other)
00146 return;
00147 T *oldD = d;
00148 d = other;
00149 Cleanup::cleanup(oldD);
00150 }
00151
00152 inline T *take()
00153 {
00154 T *oldD = d;
00155 d = 0;
00156 return oldD;
00157 }
00158
00159 inline void swap(QScopedPointer<T, Cleanup> &other)
00160 {
00161 qSwap(d, other.d);
00162 }
00163
00164 typedef T *pointer;
00165
00166 protected:
00167 T *d;
00168
00169 private:
00170 Q_DISABLE_COPY(QScopedPointer)
00171 };
00172
00173 template <class T, class Cleanup>
00174 inline bool operator==(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
00175 {
00176 return lhs.data() == rhs.data();
00177 }
00178
00179 template <class T, class Cleanup>
00180 inline bool operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
00181 {
00182 return lhs.data() != rhs.data();
00183 }
00184
00185 template <class T, class Cleanup>
00186 Q_INLINE_TEMPLATE void qSwap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2)
00187 { p1.swap(p2); }
00188
00189 template <typename T, typename Cleanup = QScopedPointerArrayDeleter<T> >
00190 class QScopedArrayPointer : public QScopedPointer<T, Cleanup>
00191 {
00192 public:
00193 explicit inline QScopedArrayPointer(T *p = 0)
00194 : QScopedPointer<T, Cleanup>(p)
00195 {
00196 }
00197
00198 inline T &operator[](int i)
00199 {
00200 return this->d[i];
00201 }
00202
00203 inline const T &operator[](int i) const
00204 {
00205 return this->d[i];
00206 }
00207
00208 private:
00209 Q_DISABLE_COPY(QScopedArrayPointer)
00210 };
00211
00212 QT_END_NAMESPACE
00213 QT_END_HEADER
00214
00215 #endif // QSCOPEDPOINTER_H