src/corelib/tools/qscopedpointer.h
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
   111     {
   111     {
   112         Q_ASSERT(d);
   112         Q_ASSERT(d);
   113         return d;
   113         return d;
   114     }
   114     }
   115 
   115 
   116     inline bool operator==(const QScopedPointer<T, Cleanup> &other) const
       
   117     {
       
   118         return d == other.d;
       
   119     }
       
   120 
       
   121     inline bool operator!=(const QScopedPointer<T, Cleanup> &other) const
       
   122     {
       
   123         return d != other.d;
       
   124     }
       
   125 
       
   126     inline bool operator!() const
   116     inline bool operator!() const
   127     {
   117     {
   128         return !d;
   118         return !d;
   129     }
   119     }
   130 
   120 
   179 private:
   169 private:
   180     Q_DISABLE_COPY(QScopedPointer)
   170     Q_DISABLE_COPY(QScopedPointer)
   181 };
   171 };
   182 
   172 
   183 template <class T, class Cleanup>
   173 template <class T, class Cleanup>
       
   174 inline bool operator==(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
       
   175 {
       
   176     return lhs.data() == rhs.data();
       
   177 }
       
   178 
       
   179 template <class T, class Cleanup>
       
   180 inline bool operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
       
   181 {
       
   182     return lhs.data() != rhs.data();
       
   183 }
       
   184 
       
   185 template <class T, class Cleanup>
   184 Q_INLINE_TEMPLATE void qSwap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2)
   186 Q_INLINE_TEMPLATE void qSwap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2)
   185 { p1.swap(p2); }
   187 { p1.swap(p2); }
   186 
   188 
   187 template <typename T, typename Cleanup = QScopedPointerArrayDeleter<T> >
   189 template <typename T, typename Cleanup = QScopedPointerArrayDeleter<T> >
   188 class QScopedArrayPointer : public QScopedPointer<T, Cleanup>
   190 class QScopedArrayPointer : public QScopedPointer<T, Cleanup>
   201     inline const T &operator[](int i) const
   203     inline const T &operator[](int i) const
   202     {
   204     {
   203         return this->d[i];
   205         return this->d[i];
   204     }
   206     }
   205 
   207 
   206     inline bool operator==(const QScopedArrayPointer<T, Cleanup> &other) const
       
   207     {
       
   208         return this->d == other.d;
       
   209     }
       
   210 
       
   211     inline bool operator!=(const QScopedArrayPointer<T, Cleanup> &other) const
       
   212     {
       
   213         return this->d != other.d;
       
   214     }
       
   215 
       
   216 private:
   208 private:
   217     Q_DISABLE_COPY(QScopedArrayPointer)
   209     Q_DISABLE_COPY(QScopedArrayPointer)
   218 };
   210 };
   219 
   211 
   220 /* Internal helper class - exposes the data through data_ptr (legacy from QShared).
       
   221    Required for some internal Qt classes, do not use otherwise. */
       
   222 template <typename T, typename Cleanup = QScopedPointerDeleter<T> >
       
   223 class QCustomScopedPointer : public QScopedPointer<T, Cleanup>
       
   224 {
       
   225 public:
       
   226     explicit inline QCustomScopedPointer(T *p = 0)
       
   227         : QScopedPointer<T, Cleanup>(p)
       
   228     {
       
   229     }
       
   230 
       
   231     inline T *&data_ptr()
       
   232     {
       
   233         return this->d;
       
   234     }
       
   235 
       
   236     inline bool operator==(const QCustomScopedPointer<T, Cleanup> &other) const
       
   237     {
       
   238         return this->d == other.d;
       
   239     }
       
   240 
       
   241     inline bool operator!=(const QCustomScopedPointer<T, Cleanup> &other) const
       
   242     {
       
   243         return this->d != other.d;
       
   244     }
       
   245 
       
   246 private:
       
   247     Q_DISABLE_COPY(QCustomScopedPointer)
       
   248 };
       
   249 
       
   250 /* Internal helper class - a handler for QShared* classes, to be used in QCustomScopedPointer */
       
   251 template <typename T>
       
   252 class QScopedPointerSharedDeleter
       
   253 {
       
   254 public:
       
   255     static inline void cleanup(T *d)
       
   256     {
       
   257         if (d && !d->ref.deref())
       
   258             delete d;
       
   259     }
       
   260 };
       
   261 
       
   262 /* Internal.
       
   263    This class is basically a scoped pointer pointing to a ref-counted object
       
   264  */
       
   265 template <typename T>
       
   266 class QScopedSharedPointer : public QCustomScopedPointer<T, QScopedPointerSharedDeleter<T> >
       
   267 {
       
   268 public:
       
   269     explicit inline QScopedSharedPointer(T *p = 0)
       
   270         : QCustomScopedPointer<T, QScopedPointerSharedDeleter<T> >(p)
       
   271     {
       
   272     }
       
   273 
       
   274     inline void detach()
       
   275     {
       
   276         qAtomicDetach(this->d);
       
   277     }
       
   278 
       
   279     inline void assign(T *other)
       
   280     {
       
   281         if (this->d == other)
       
   282             return;
       
   283         if (other)
       
   284             other->ref.ref();
       
   285         T *oldD = this->d;
       
   286         this->d = other;
       
   287         QScopedPointerSharedDeleter<T>::cleanup(oldD);
       
   288     }
       
   289 
       
   290     inline bool operator==(const QScopedSharedPointer<T> &other) const
       
   291     {
       
   292         return this->d == other.d;
       
   293     }
       
   294 
       
   295     inline bool operator!=(const QScopedSharedPointer<T> &other) const
       
   296     {
       
   297         return this->d != other.d;
       
   298     }
       
   299 
       
   300 private:
       
   301     Q_DISABLE_COPY(QScopedSharedPointer)
       
   302 };
       
   303 
       
   304 QT_END_NAMESPACE
   212 QT_END_NAMESPACE
   305 QT_END_HEADER
   213 QT_END_HEADER
   306 
   214 
   307 #endif // QSCOPEDPOINTER_H
   215 #endif // QSCOPEDPOINTER_H