Eneboo - Documentación para desarrolladores
tools/aqzip/qscopedpointer.h
Ir a la documentación de este archivo.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2011 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 ** GNU Lesser General Public License Usage
00011 ** This file may be used under the terms of the GNU Lesser General Public
00012 ** License version 2.1 as published by the Free Software Foundation and
00013 ** appearing in the file LICENSE.LGPL included in the packaging of this
00014 ** file. Please review the following information to ensure the GNU Lesser
00015 ** General Public License version 2.1 requirements will be met:
00016 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00017 **
00018 ** In addition, as a special exception, Nokia gives you certain additional
00019 ** rights. These rights are described in the Nokia Qt LGPL Exception
00020 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
00021 **
00022 ** GNU General Public License Usage
00023 ** Alternatively, this file may be used under the terms of the GNU General
00024 ** Public License version 3.0 as published by the Free Software Foundation
00025 ** and appearing in the file LICENSE.GPL included in the packaging of this
00026 ** file. Please review the following information to ensure the GNU General
00027 ** Public License version 3.0 requirements will be met:
00028 ** http://www.gnu.org/copyleft/gpl.html.
00029 **
00030 ** Other Usage
00031 ** Alternatively, this file may be used in accordance with the terms and
00032 ** conditions contained in a signed written agreement between you and Nokia.
00033 **
00034 **
00035 **
00036 **
00037 **
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QSCOPEDPOINTER_H
00043 #define QSCOPEDPOINTER_H
00044 
00045 #include <stdlib.h>
00046 
00047 #include <qglobal.h>
00048 
00049 template <typename T>
00050 struct QScopedPointerDeleter
00051 {
00052     static inline void cleanup(T *pointer)
00053     {
00054         // Enforce a complete type.
00055         // If you get a compile error here, read the secion on forward declared
00056         // classes in the QScopedPointer documentation.
00057         typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
00058         (void) sizeof(IsIncompleteType);
00059 
00060         delete pointer;
00061     }
00062 };
00063 
00064 template <typename T>
00065 struct QScopedPointerArrayDeleter
00066 {
00067     static inline void cleanup(T *pointer)
00068     {
00069         // Enforce a complete type.
00070         // If you get a compile error here, read the secion on forward declared
00071         // classes in the QScopedPointer documentation.
00072         typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
00073         (void) sizeof(IsIncompleteType);
00074 
00075         delete [] pointer;
00076     }
00077 };
00078 
00079 struct QScopedPointerPodDeleter
00080 {
00081     static inline void cleanup(void *pointer) { if (pointer) ::free(pointer); }
00082 };
00083 
00084 template <typename T, typename Cleanup = QScopedPointerDeleter<T> >
00085 class QScopedPointer
00086 {
00087 #ifndef Q_CC_NOKIAX86
00088     typedef T *QScopedPointer:: *RestrictedBool;
00089 #endif
00090 public:
00091     explicit inline QScopedPointer(T *p = 0) : d(p)
00092     {
00093     }
00094 
00095     inline ~QScopedPointer()
00096     {
00097         T *oldD = this->d;
00098         Cleanup::cleanup(oldD);
00099         this->d = 0;
00100     }
00101 
00102     inline T &operator*() const
00103     {
00104         Q_ASSERT(d);
00105         return *d;
00106     }
00107 
00108     inline T *operator->() const
00109     {
00110         Q_ASSERT(d);
00111         return d;
00112     }
00113 
00114     inline bool operator!() const
00115     {
00116         return !d;
00117     }
00118 
00119 #if defined(Q_CC_NOKIAX86) || defined(Q_QDOC)
00120     inline operator bool() const
00121     {
00122         return isNull() ? 0 : &QScopedPointer::d;
00123     }
00124 #else
00125     inline operator RestrictedBool() const
00126     {
00127         return isNull() ? 0 : &QScopedPointer::d;
00128     }
00129 #endif
00130 
00131     inline T *data() const
00132     {
00133         return d;
00134     }
00135 
00136     inline bool isNull() const
00137     {
00138         return !d;
00139     }
00140 
00141     inline void reset(T *other = 0)
00142     {
00143         if (d == other)
00144             return;
00145         T *oldD = d;
00146         d = other;
00147         Cleanup::cleanup(oldD);
00148     }
00149 
00150     inline T *take()
00151     {
00152         T *oldD = d;
00153         d = 0;
00154         return oldD;
00155     }
00156 
00157     inline void swap(QScopedPointer<T, Cleanup> &other)
00158     {
00159         qSwap(d, other.d);
00160     }
00161 
00162     typedef T *pointer;
00163 
00164 protected:
00165     T *d;
00166 
00167 private:
00168     QScopedPointer(const QScopedPointer &);
00169     QScopedPointer &operator=(const QScopedPointer &);
00170 };
00171 
00172 template <class T, class Cleanup>
00173 inline bool operator==(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
00174 {
00175     return lhs.data() == rhs.data();
00176 }
00177 
00178 template <class T, class Cleanup>
00179 inline bool operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
00180 {
00181     return lhs.data() != rhs.data();
00182 }
00183 
00184 template <class T, class Cleanup>
00185 inline void qSwap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2)
00186 { p1.swap(p2); }
00187 
00188 template <typename T, typename Cleanup = QScopedPointerArrayDeleter<T> >
00189 class QScopedArrayPointer : public QScopedPointer<T, Cleanup>
00190 {
00191 public:
00192     explicit inline QScopedArrayPointer(T *p = 0)
00193         : QScopedPointer<T, Cleanup>(p)
00194     {
00195     }
00196 
00197     inline T &operator[](int i)
00198     {
00199         return this->d[i];
00200     }
00201 
00202     inline const T &operator[](int i) const
00203     {
00204         return this->d[i];
00205     }
00206 
00207 private:
00208     QScopedArrayPointer(const QScopedArrayPointer &);
00209     QScopedArrayPointer &operator=(const QScopedArrayPointer &);
00210 };
00211 
00212 #endif // QSCOPEDPOINTER_H
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Propiedades Amigas 'defines'