Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/QtCore/qscopedpointer.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright (C) 2016 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QSCOPEDPOINTER_H
0005 #define QSCOPEDPOINTER_H
0006 
0007 #include <QtCore/qglobal.h>
0008 
0009 #include <stdlib.h>
0010 
0011 QT_BEGIN_NAMESPACE
0012 
0013 template <typename T>
0014 struct QScopedPointerDeleter
0015 {
0016     static inline void cleanup(T *pointer) noexcept
0017     {
0018         // Enforce a complete type.
0019         // If you get a compile error here, read the section on forward declared
0020         // classes in the QScopedPointer documentation.
0021         typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
0022         (void) sizeof(IsIncompleteType);
0023 
0024         delete pointer;
0025     }
0026     void operator()(T *pointer) const noexcept
0027     {
0028         cleanup(pointer);
0029     }
0030 };
0031 
0032 template <typename T>
0033 struct QScopedPointerArrayDeleter
0034 {
0035     static inline void cleanup(T *pointer) noexcept
0036     {
0037         // Enforce a complete type.
0038         // If you get a compile error here, read the section on forward declared
0039         // classes in the QScopedPointer documentation.
0040         typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
0041         (void) sizeof(IsIncompleteType);
0042 
0043         delete[] pointer;
0044     }
0045     void operator()(T *pointer) const noexcept
0046     {
0047         cleanup(pointer);
0048     }
0049 };
0050 
0051 struct QScopedPointerPodDeleter
0052 {
0053     static inline void cleanup(void *pointer) noexcept { free(pointer); }
0054     void operator()(void *pointer) const noexcept { cleanup(pointer); }
0055 };
0056 
0057 #ifndef QT_NO_QOBJECT
0058 template <typename T>
0059 struct QScopedPointerObjectDeleteLater
0060 {
0061     static inline void cleanup(T *pointer) { if (pointer) pointer->deleteLater(); }
0062     void operator()(T *pointer) const { cleanup(pointer); }
0063 };
0064 
0065 class QObject;
0066 typedef QScopedPointerObjectDeleteLater<QObject> QScopedPointerDeleteLater;
0067 #endif
0068 
0069 template <typename T, typename Cleanup = QScopedPointerDeleter<T> >
0070 class QScopedPointer
0071 {
0072 public:
0073     Q_NODISCARD_CTOR
0074     explicit QScopedPointer(T *p = nullptr) noexcept : d(p)
0075     {
0076     }
0077 
0078     inline ~QScopedPointer()
0079     {
0080         T *oldD = this->d;
0081         Cleanup::cleanup(oldD);
0082     }
0083 
0084     inline T &operator*() const
0085     {
0086         Q_ASSERT(d);
0087         return *d;
0088     }
0089 
0090     T *operator->() const noexcept
0091     {
0092         return d;
0093     }
0094 
0095     bool operator!() const noexcept
0096     {
0097         return !d;
0098     }
0099 
0100     explicit operator bool() const
0101     {
0102         return !isNull();
0103     }
0104 
0105     T *data() const noexcept
0106     {
0107         return d;
0108     }
0109 
0110     T *get() const noexcept
0111     {
0112         return d;
0113     }
0114 
0115     bool isNull() const noexcept
0116     {
0117         return !d;
0118     }
0119 
0120     void reset(T *other = nullptr) noexcept(noexcept(Cleanup::cleanup(std::declval<T *>())))
0121     {
0122         if (d == other)
0123             return;
0124         T *oldD = std::exchange(d, other);
0125         Cleanup::cleanup(oldD);
0126     }
0127 
0128 #if QT_DEPRECATED_SINCE(6, 1)
0129     QT_DEPRECATED_VERSION_X_6_1("Use std::unique_ptr instead, and call release().")
0130     T *take() noexcept
0131     {
0132         T *oldD = std::exchange(d, nullptr);
0133         return oldD;
0134     }
0135 #endif
0136 
0137 #if QT_DEPRECATED_SINCE(6, 2)
0138     QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedPointer.")
0139     void swap(QScopedPointer<T, Cleanup> &other) noexcept
0140     {
0141         qt_ptr_swap(d, other.d);
0142     }
0143 #endif
0144 
0145     typedef T *pointer;
0146 
0147     friend bool operator==(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs) noexcept
0148     {
0149         return lhs.data() == rhs.data();
0150     }
0151 
0152     friend bool operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs) noexcept
0153     {
0154         return lhs.data() != rhs.data();
0155     }
0156 
0157     friend bool operator==(const QScopedPointer<T, Cleanup> &lhs, std::nullptr_t) noexcept
0158     {
0159         return lhs.isNull();
0160     }
0161 
0162     friend bool operator==(std::nullptr_t, const QScopedPointer<T, Cleanup> &rhs) noexcept
0163     {
0164         return rhs.isNull();
0165     }
0166 
0167     friend bool operator!=(const QScopedPointer<T, Cleanup> &lhs, std::nullptr_t) noexcept
0168     {
0169         return !lhs.isNull();
0170     }
0171 
0172     friend bool operator!=(std::nullptr_t, const QScopedPointer<T, Cleanup> &rhs) noexcept
0173     {
0174         return !rhs.isNull();
0175     }
0176 
0177 #if QT_DEPRECATED_SINCE(6, 2)
0178     QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedPointer.")
0179     friend void swap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2) noexcept
0180     { p1.swap(p2); }
0181 #endif
0182 
0183 protected:
0184     T *d;
0185 
0186 private:
0187     Q_DISABLE_COPY_MOVE(QScopedPointer)
0188 };
0189 
0190 template <typename T, typename Cleanup = QScopedPointerArrayDeleter<T> >
0191 class QScopedArrayPointer : public QScopedPointer<T, Cleanup>
0192 {
0193     template <typename Ptr>
0194     using if_same_type = typename std::enable_if<std::is_same<typename std::remove_cv<T>::type, Ptr>::value, bool>::type;
0195 public:
0196     Q_NODISCARD_CTOR
0197     inline QScopedArrayPointer() : QScopedPointer<T, Cleanup>(nullptr) {}
0198     inline ~QScopedArrayPointer() = default;
0199 
0200     template <typename D, if_same_type<D> = true>
0201     Q_NODISCARD_CTOR
0202     explicit QScopedArrayPointer(D *p)
0203         : QScopedPointer<T, Cleanup>(p)
0204     {
0205     }
0206 
0207     T &operator[](qsizetype i)
0208     {
0209         return this->d[i];
0210     }
0211 
0212     const T &operator[](qsizetype i) const
0213     {
0214         return this->d[i];
0215     }
0216 
0217 #if QT_DEPRECATED_SINCE(6, 2)
0218     QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedArrayPointer.")
0219     void swap(QScopedArrayPointer &other) noexcept // prevent QScopedPointer <->QScopedArrayPointer swaps
0220     { QScopedPointer<T, Cleanup>::swap(other); }
0221 #endif
0222 
0223 private:
0224     explicit inline QScopedArrayPointer(void *)
0225     {
0226         // Enforce the same type.
0227 
0228         // If you get a compile error here, make sure you declare
0229         // QScopedArrayPointer with the same template type as you pass to the
0230         // constructor. See also the QScopedPointer documentation.
0231 
0232         // Storing a scalar array as a pointer to a different type is not
0233         // allowed and results in undefined behavior.
0234     }
0235 
0236     Q_DISABLE_COPY_MOVE(QScopedArrayPointer)
0237 };
0238 
0239 #if QT_DEPRECATED_SINCE(6, 2)
0240 template <typename T, typename Cleanup>
0241 QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedArrayPointer.")
0242 inline void swap(QScopedArrayPointer<T, Cleanup> &lhs, QScopedArrayPointer<T, Cleanup> &rhs) noexcept
0243 { lhs.swap(rhs); }
0244 #endif
0245 
0246 QT_END_NAMESPACE
0247 
0248 #endif // QSCOPEDPOINTER_H