File indexing completed on 2025-09-18 09:26:45
0001
0002
0003
0004 #ifndef QSHAREDPOINTER_H
0005 #define QSHAREDPOINTER_H
0006
0007 #include <QtCore/qglobal.h>
0008 #include <QtCore/qatomic.h>
0009 #include <QtCore/qshareddata.h>
0010
0011 #ifndef Q_QDOC
0012 # include <QtCore/qsharedpointer_impl.h>
0013 #else
0014
0015 #include <memory> // for std::shared_ptr
0016
0017 QT_BEGIN_NAMESPACE
0018
0019
0020
0021
0022 template <class T>
0023 class QSharedPointer
0024 {
0025 public:
0026
0027 T *data() const;
0028 T *get() const;
0029 bool isNull() const;
0030 operator bool() const;
0031 bool operator!() const;
0032 T &operator*() const;
0033 T *operator->() const;
0034
0035
0036 QSharedPointer();
0037 template <typename X> explicit QSharedPointer(X *ptr);
0038 template <typename X, typename Deleter> QSharedPointer(X *ptr, Deleter d);
0039 QSharedPointer(std::nullptr_t);
0040 template <typename Deleter> QSharedPointer(std::nullptr_t, Deleter d);
0041 QSharedPointer(const QSharedPointer<T> &other);
0042 QSharedPointer(const QWeakPointer<T> &other);
0043 QSharedPointer(QSharedPointer<T> &&other) noexcept;
0044
0045 ~QSharedPointer() { }
0046
0047 QSharedPointer<T> &operator=(const QSharedPointer<T> &other);
0048 QSharedPointer<T> &operator=(QSharedPointer<T> &&other) noexcept;
0049 QSharedPointer<T> &operator=(const QWeakPointer<T> &other);
0050
0051 template <class X>
0052 QSharedPointer(QSharedPointer<X> && other) noexcept;
0053 template <class X>
0054 QSharedPointer &operator=(QSharedPointer<X> && other) noexcept;
0055
0056 void swap(QSharedPointer<T> &other) noexcept;
0057
0058 QWeakPointer<T> toWeakRef() const;
0059
0060 void clear();
0061
0062 void reset();
0063 void reset(T *t);
0064 template <typename Deleter>
0065 void reset(T *t, Deleter deleter);
0066
0067
0068 template <class X> QSharedPointer<X> staticCast() const &;
0069 template <class X> QSharedPointer<X> staticCast() &&;
0070 template <class X> QSharedPointer<X> dynamicCast() const &;
0071 template <class X> QSharedPointer<X> dynamicCast() &&;
0072 template <class X> QSharedPointer<X> constCast() const &;
0073 template <class X> QSharedPointer<X> constCast() &&;
0074 template <class X> QSharedPointer<X> objectCast() const &;
0075 template <class X> QSharedPointer<X> objectCast() &&;
0076
0077 template <typename... Args>
0078 static inline QSharedPointer<T> create(Args &&... args);
0079
0080
0081 template <typename X>
0082 bool owner_before(const QSharedPointer<X> &other) const noexcept;
0083 template <typename X>
0084 bool owner_before(const QWeakPointer<X> &other) const noexcept;
0085
0086 template <typename X>
0087 bool owner_equal(const QSharedPointer<X> &other) const noexcept;
0088 template <typename X>
0089 bool owner_equal(const QWeakPointer<X> &other) const noexcept;
0090
0091 size_t owner_hash() const noexcept;
0092 };
0093
0094 template <class T>
0095 size_t qHash(const QSharedPointer<T> &key, size_t seed = 0) noexcept;
0096
0097 template <class T>
0098 class QWeakPointer
0099 {
0100 public:
0101
0102 bool isNull() const;
0103 operator bool() const;
0104 bool operator!() const;
0105
0106
0107 QWeakPointer();
0108 QWeakPointer(const QWeakPointer<T> &other) noexcept;
0109 QWeakPointer(QWeakPointer<T> &&other) noexcept;
0110 QWeakPointer(const QSharedPointer<T> &other);
0111
0112 ~QWeakPointer();
0113
0114 QWeakPointer<T> &operator=(const QWeakPointer<T> &other) noexcept;
0115 QWeakPointer<T> &operator=(QWeakPointer<T> &&other) noexcept;
0116 QWeakPointer<T> &operator=(const QSharedPointer<T> &other);
0117
0118 QWeakPointer(const QObject *other);
0119 QWeakPointer<T> &operator=(const QObject *other);
0120
0121 void swap(QWeakPointer<T> &other) noexcept;
0122
0123 T *data() const;
0124 void clear();
0125
0126 QSharedPointer<T> toStrongRef() const;
0127 QSharedPointer<T> lock() const;
0128
0129
0130 template <typename X>
0131 bool owner_before(const QWeakPointer<X> &other) const noexcept;
0132 template <typename X>
0133 bool owner_before(const QSharedPointer<X> &other) const noexcept;
0134
0135 template <typename X>
0136 bool owner_equal(const QWeakPointer<X> &other) const noexcept;
0137 template <typename X>
0138 bool owner_equal(const QSharedPointer<X> &other) const noexcept;
0139
0140 size_t owner_hash() const noexcept;
0141 };
0142
0143 template <class T>
0144 class QEnableSharedFromThis
0145 {
0146 public:
0147 QSharedPointer<T> sharedFromThis();
0148 QSharedPointer<const T> sharedFromThis() const;
0149 };
0150
0151 template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
0152 template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
0153 template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const X *ptr2);
0154 template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const X *ptr2);
0155 template<class T, class X> bool operator==(const T *ptr1, const QSharedPointer<X> &ptr2);
0156 template<class T, class X> bool operator!=(const T *ptr1, const QSharedPointer<X> &ptr2);
0157 template<class T, class X> bool operator==(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
0158 template<class T, class X> bool operator!=(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
0159 template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2);
0160 template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2);
0161 template<class T> bool operator==(const QSharedPointer<T> &lhs, std::nullptr_t);
0162 template<class T> bool operator!=(const QSharedPointer<T> &lhs, std::nullptr_t);
0163 template<class T> bool operator==(std::nullptr_t, const QSharedPointer<T> &rhs);
0164 template<class T> bool operator!=(std::nullptr_t, const QSharedPointer<T> &rhs);
0165 template<class T> bool operator==(const QWeakPointer<T> &lhs, std::nullptr_t);
0166 template<class T> bool operator!=(const QWeakPointer<T> &lhs, std::nullptr_t);
0167 template<class T> bool operator==(std::nullptr_t, const QWeakPointer<T> &rhs);
0168 template<class T> bool operator!=(std::nullptr_t, const QWeakPointer<T> &rhs);
0169
0170 template <class X, class T> QSharedPointer<X> qSharedPointerCast(const QSharedPointer<T> &other);
0171 template <class X, class T> QSharedPointer<X> qSharedPointerCast(QSharedPointer<T> &&other);
0172 template <class X, class T> QSharedPointer<X> qSharedPointerCast(const QWeakPointer<T> &other);
0173 template <class X, class T> QSharedPointer<X> qSharedPointerDynamicCast(const QSharedPointer<T> &src);
0174 template <class X, class T> QSharedPointer<X> qSharedPointerDynamicCast(QSharedPointer<T> &&src);
0175 template <class X, class T> QSharedPointer<X> qSharedPointerDynamicCast(const QWeakPointer<T> &src);
0176 template <class X, class T> QSharedPointer<X> qSharedPointerConstCast(const QSharedPointer<T> &src);
0177 template <class X, class T> QSharedPointer<X> qSharedPointerConstCast(QSharedPointer<T> &&src);
0178 template <class X, class T> QSharedPointer<X> qSharedPointerConstCast(const QWeakPointer<T> &src);
0179 template <class X, class T> QSharedPointer<X> qSharedPointerObjectCast(const QSharedPointer<T> &src);
0180 template <class X, class T> QSharedPointer<X> qSharedPointerObjectCast(QSharedPointer<T> &&src);
0181 template <class X, class T> QSharedPointer<X> qSharedPointerObjectCast(const QWeakPointer<T> &src);
0182 template <typename X, class T> std::shared_ptr<X> qobject_pointer_cast(const std::shared_ptr<T> &src);
0183 template <typename X, class T> std::shared_ptr<X> qobject_pointer_cast(std::shared_ptr<T> &&src);
0184 template <typename X, class T> std::shared_ptr<X> qSharedPointerObjectCast(const std::shared_ptr<T> &src);
0185 template <typename X, class T> std::shared_ptr<X> qSharedPointerObjectCast(std::shared_ptr<T> &&src);
0186
0187 template <class X, class T> QWeakPointer<X> qWeakPointerCast(const QWeakPointer<T> &src);
0188
0189 QT_END_NAMESPACE
0190
0191 #endif
0192
0193 #endif