- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Directories
- Examples

- File List
- File Members

# qscopedpointer.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 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         // Enforce a complete type.
    00057         // If you get a compile error here, read the secion on forward declared
    00058         // classes in the QScopedPointer documentation.
    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         // Enforce a complete type.
    00072         // If you get a compile error here, read the secion on forward declared
    00073         // classes in the QScopedPointer documentation.
    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