Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/QtCore/qpointer.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 QPOINTER_H
0005 #define QPOINTER_H
0006 
0007 #include <QtCore/qsharedpointer.h>
0008 #include <QtCore/qtypeinfo.h>
0009 
0010 #ifndef QT_NO_QOBJECT
0011 
0012 QT_BEGIN_NAMESPACE
0013 
0014 class QVariant;
0015 
0016 template <class T>
0017 class QPointer
0018 {
0019     static_assert(!std::is_pointer<T>::value, "QPointer's template type must not be a pointer type");
0020 
0021     template <typename X>
0022     using if_convertible = std::enable_if_t<std::is_convertible_v<X*, T*>, bool>;
0023     template <typename X>
0024     friend class QPointer;
0025 
0026     using QObjectType =
0027         typename std::conditional<std::is_const<T>::value, const QObject, QObject>::type;
0028     QWeakPointer<QObjectType> wp;
0029 public:
0030     Q_NODISCARD_CTOR
0031     QPointer() noexcept = default;
0032     Q_NODISCARD_CTOR
0033     constexpr QPointer(std::nullptr_t) noexcept : QPointer{} {}
0034     Q_WEAK_OVERLOAD
0035     Q_NODISCARD_CTOR
0036     inline QPointer(T *p) : wp(p, true) { }
0037     // compiler-generated copy/move ctor/assignment operators are fine!
0038     // compiler-generated dtor is fine!
0039 
0040     template <typename X, if_convertible<X> = true>
0041     Q_NODISCARD_CTOR
0042     QPointer(QPointer<X> &&other) noexcept
0043         : wp(std::exchange(other.wp, nullptr).internalData(), true) {}
0044     template <typename X, if_convertible<X> = true>
0045     Q_NODISCARD_CTOR
0046     QPointer(const QPointer<X> &other) noexcept
0047         : wp(other.wp.internalData(), true) {}
0048 
0049     template <typename X, if_convertible<X> = true>
0050     QPointer &operator=(const QPointer<X> &other) noexcept
0051     {
0052         QPointer(other).swap(*this);
0053         return *this;
0054     }
0055 
0056     template <typename X, if_convertible<X> = true>
0057     QPointer &operator=(QPointer<X> &&other) noexcept
0058     {
0059         QPointer(std::move(other)).swap(*this);
0060         return *this;
0061     }
0062 
0063 #ifdef Q_QDOC
0064     // Stop qdoc from complaining about missing function
0065     ~QPointer();
0066 #endif
0067 
0068     inline void swap(QPointer &other) noexcept { wp.swap(other.wp); }
0069 
0070     inline QPointer<T> &operator=(T* p)
0071     { wp.assign(static_cast<QObjectType*>(p)); return *this; }
0072 
0073     T* data() const noexcept
0074     { return static_cast<T*>(wp.internalData()); }
0075     T* get() const noexcept
0076     { return data(); }
0077     T* operator->() const noexcept
0078     { return data(); }
0079     T& operator*() const noexcept
0080     { return *data(); }
0081     operator T*() const noexcept
0082     { return data(); }
0083 
0084     bool isNull() const noexcept
0085     { return wp.isNull(); }
0086 
0087     void clear() noexcept
0088     { wp.clear(); }
0089 
0090     friend void swap(QPointer &lhs, QPointer &rhs) noexcept
0091     { lhs.swap(rhs); }
0092 
0093 #define DECLARE_COMPARE_SET(T1, A1, T2, A2) \
0094     friend bool operator==(T1, T2) noexcept \
0095     { return A1 == A2; } \
0096     friend bool operator!=(T1, T2) noexcept \
0097     { return A1 != A2; }
0098 
0099 #define DECLARE_TEMPLATE_COMPARE_SET(T1, A1, T2, A2) \
0100     template <typename X> \
0101     friend bool operator==(T1, T2) noexcept \
0102     { return A1 == A2; } \
0103     template <typename X> \
0104     friend bool operator!=(T1, T2) noexcept \
0105     { return A1 != A2; }
0106 
0107     DECLARE_TEMPLATE_COMPARE_SET(const QPointer &p1, p1.data(), const QPointer<X> &p2, p2.data())
0108     DECLARE_TEMPLATE_COMPARE_SET(const QPointer &p1, p1.data(), X *ptr, ptr)
0109     DECLARE_TEMPLATE_COMPARE_SET(X *ptr, ptr, const QPointer &p2, p2.data())
0110     DECLARE_COMPARE_SET(const QPointer &p1, p1.data(), std::nullptr_t, nullptr)
0111     DECLARE_COMPARE_SET(std::nullptr_t, nullptr, const QPointer &p2, p2.data())
0112 #undef DECLARE_COMPARE_SET
0113 #undef DECLARE_TEMPLATE_COMPARE_SET
0114 };
0115 template <class T> Q_DECLARE_TYPEINFO_BODY(QPointer<T>, Q_RELOCATABLE_TYPE);
0116 
0117 template<typename T>
0118 QPointer<T>
0119 qPointerFromVariant(const QVariant &variant)
0120 {
0121     const auto wp = QtSharedPointer::weakPointerFromVariant_internal(variant);
0122     return QPointer<T>{qobject_cast<T*>(QtPrivate::EnableInternalData::internalData(wp))};
0123 }
0124 
0125 QT_END_NAMESPACE
0126 
0127 #endif // QT_NO_QOBJECT
0128 
0129 #endif // QPOINTER_H