1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QSCOPEDPOINTER_H
43 #define QSCOPEDPOINTER_H
45 #include <QtCore/qglobal.h>
53 struct QScopedPointerDeleter
55 static inline void cleanup(T *pointer)
57 // Enforce a complete type.
58 // If you get a compile error here, read the section on forward declared
59 // classes in the QScopedPointer documentation.
60 typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
61 (void) sizeof(IsIncompleteType);
68 struct QScopedPointerArrayDeleter
70 static inline void cleanup(T *pointer)
72 // Enforce a complete type.
73 // If you get a compile error here, read the section on forward declared
74 // classes in the QScopedPointer documentation.
75 typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
76 (void) sizeof(IsIncompleteType);
82 struct QScopedPointerPodDeleter
84 static inline void cleanup(void *pointer) { if (pointer) free(pointer); }
87 template <typename T, typename Cleanup = QScopedPointerDeleter<T> >
90 typedef T *QScopedPointer:: *RestrictedBool;
92 explicit inline QScopedPointer(T *p = 0) : d(p)
96 inline ~QScopedPointer()
99 Cleanup::cleanup(oldD);
103 inline T &operator*() const
109 inline T *operator->() const
115 inline bool operator!() const
121 inline operator bool() const
123 return isNull() ? 0 : &QScopedPointer::d;
126 inline operator RestrictedBool() const
128 return isNull() ? 0 : &QScopedPointer::d;
132 inline T *data() const
137 inline bool isNull() const
142 inline void reset(T *other = 0)
148 Cleanup::cleanup(oldD);
158 inline void swap(QScopedPointer<T, Cleanup> &other)
169 Q_DISABLE_COPY(QScopedPointer)
172 template <class T, class Cleanup>
173 inline bool operator==(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
175 return lhs.data() == rhs.data();
178 template <class T, class Cleanup>
179 inline bool operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
181 return lhs.data() != rhs.data();
184 template <class T, class Cleanup>
185 Q_INLINE_TEMPLATE void qSwap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2)
190 template <class T, class Cleanup>
191 Q_INLINE_TEMPLATE void swap(QT_PREPEND_NAMESPACE(QScopedPointer)<T, Cleanup> &p1, QT_PREPEND_NAMESPACE(QScopedPointer)<T, Cleanup> &p2)
198 namespace QtPrivate {
199 template <typename X, typename Y> struct QScopedArrayEnsureSameType;
200 template <typename X> struct QScopedArrayEnsureSameType<X,X> { typedef X* Type; };
201 template <typename X> struct QScopedArrayEnsureSameType<const X, X> { typedef X* Type; };
204 template <typename T, typename Cleanup = QScopedPointerArrayDeleter<T> >
205 class QScopedArrayPointer : public QScopedPointer<T, Cleanup>
208 inline QScopedArrayPointer() : QScopedPointer<T, Cleanup>(0) {}
210 template <typename D>
211 explicit inline QScopedArrayPointer(D *p, typename QtPrivate::QScopedArrayEnsureSameType<T,D>::Type = 0)
212 : QScopedPointer<T, Cleanup>(p)
216 inline T &operator[](int i)
221 inline const T &operator[](int i) const
227 explicit inline QScopedArrayPointer(void *) {
228 // Enforce the same type.
230 // If you get a compile error here, make sure you declare
231 // QScopedArrayPointer with the same template type as you pass to the
232 // constructor. See also the QScopedPointer documentation.
234 // Storing a scalar array as a pointer to a different type is not
235 // allowed and results in undefined behavior.
238 Q_DISABLE_COPY(QScopedArrayPointer)
244 #endif // QSCOPEDPOINTER_H