1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QSHAREDPOINTER_H
41#define QSHAREDPOINTER_H
42
43#include <QtCore/qglobal.h>
44#include <QtCore/qatomic.h>
45#include <QtCore/qshareddata.h>
46
47#ifndef Q_QDOC
48# include <QtCore/qsharedpointer_impl.h>
49#else
50
51#include <memory> // for std::shared_ptr
52
53QT_BEGIN_NAMESPACE
54
55
56// These classes are here to fool qdoc into generating a better documentation
57
58template <class T>
59class QSharedPointer
60{
61public:
62 // basic accessor functions
63 T *data() const;
64 T *get() const;
65 bool isNull() const;
66 operator bool() const;
67 bool operator!() const;
68 T &operator*() const;
69 T *operator ->() const;
70
71 // constructors
72 QSharedPointer();
73 template <typename X> explicit QSharedPointer(X *ptr);
74 template <typename X, typename Deleter> QSharedPointer(X *ptr, Deleter d);
75 QSharedPointer(std::nullptr_t);
76 template <typename Deleter> QSharedPointer(std::nullptr_t, Deleter d);
77 QSharedPointer(const QSharedPointer<T> &other);
78 QSharedPointer(const QWeakPointer<T> &other);
79
80 ~QSharedPointer() { }
81
82 QSharedPointer<T> &operator=(const QSharedPointer<T> &other);
83 QSharedPointer<T> &operator=(const QWeakPointer<T> &other);
84
85 void swap(QSharedPointer<T> &other);
86
87 QWeakPointer<T> toWeakRef() const;
88
89 void clear();
90
91 void reset();
92 void reset(T *t);
93 template <typename Deleter>
94 void reset(T *t, Deleter deleter);
95
96 // casts:
97 template <class X> QSharedPointer<X> staticCast() const;
98 template <class X> QSharedPointer<X> dynamicCast() const;
99 template <class X> QSharedPointer<X> constCast() const;
100 template <class X> QSharedPointer<X> objectCast() const;
101
102 template <typename... Args>
103 static inline QSharedPointer<T> create(Args &&... args);
104};
105
106template <class T>
107class QWeakPointer
108{
109public:
110 // basic accessor functions
111 bool isNull() const;
112 operator bool() const;
113 bool operator!() const;
114
115 // constructors:
116 QWeakPointer();
117 QWeakPointer(const QWeakPointer<T> &other);
118 QWeakPointer(const QSharedPointer<T> &other);
119
120 ~QWeakPointer();
121
122 QWeakPointer<T> &operator=(const QWeakPointer<T> &other);
123 QWeakPointer<T> &operator=(const QSharedPointer<T> &other);
124
125 QWeakPointer(const QObject *other);
126 QWeakPointer<T> &operator=(const QObject *other);
127
128 void swap(QWeakPointer<T> &other);
129
130 T *data() const;
131 void clear();
132
133 QSharedPointer<T> toStrongRef() const;
134 QSharedPointer<T> lock() const;
135};
136
137template <class T>
138class QEnableSharedFromThis
139{
140public:
141 QSharedPointer<T> sharedFromThis();
142 QSharedPointer<const T> sharedFromThis() const;
143};
144
145template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
146template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
147template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const X *ptr2);
148template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const X *ptr2);
149template<class T, class X> bool operator==(const T *ptr1, const QSharedPointer<X> &ptr2);
150template<class T, class X> bool operator!=(const T *ptr1, const QSharedPointer<X> &ptr2);
151template<class T, class X> bool operator==(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
152template<class T, class X> bool operator!=(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
153template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2);
154template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2);
155template<class T> bool operator==(const QSharedPointer<T> &lhs, std::nullptr_t);
156template<class T> bool operator!=(const QSharedPointer<T> &lhs, std::nullptr_t);
157template<class T> bool operator==(std::nullptr_t, const QSharedPointer<T> &rhs);
158template<class T> bool operator!=(std::nullptr_t, const QSharedPointer<T> &rhs);
159template<class T> bool operator==(const QWeakPointer<T> &lhs, std::nullptr_t);
160template<class T> bool operator!=(const QWeakPointer<T> &lhs, std::nullptr_t);
161template<class T> bool operator==(std::nullptr_t, const QWeakPointer<T> &rhs);
162template<class T> bool operator!=(std::nullptr_t, const QWeakPointer<T> &rhs);
163
164template <class X, class T> QSharedPointer<X> qSharedPointerCast(const QSharedPointer<T> &other);
165template <class X, class T> QSharedPointer<X> qSharedPointerCast(const QWeakPointer<T> &other);
166template <class X, class T> QSharedPointer<X> qSharedPointerDynamicCast(const QSharedPointer<T> &src);
167template <class X, class T> QSharedPointer<X> qSharedPointerDynamicCast(const QWeakPointer<T> &src);
168template <class X, class T> QSharedPointer<X> qSharedPointerConstCast(const QSharedPointer<T> &src);
169template <class X, class T> QSharedPointer<X> qSharedPointerConstCast(const QWeakPointer<T> &src);
170template <class X, class T> QSharedPointer<X> qSharedPointerObjectCast(const QSharedPointer<T> &src);
171template <class X, class T> QSharedPointer<X> qSharedPointerObjectCast(const QWeakPointer<T> &src);
172template <typename X, class T> std::shared_ptr<X> qobject_pointer_cast(const std::shared_ptr<T> &src);
173template <typename X, class T> std::shared_ptr<X> qobject_pointer_cast(std::shared_ptr<T> &&src);
174template <typename X, class T> std::shared_ptr<X> qSharedPointerObjectCast(const std::shared_ptr<T> &src);
175template <typename X, class T> std::shared_ptr<X> qSharedPointerObjectCast(std::shared_ptr<T> &&src);
176
177template <class X, class T> QWeakPointer<X> qWeakPointerCast(const QWeakPointer<T> &src);
178
179QT_END_NAMESPACE
180
181#endif // Q_QDOC
182
183#endif // QSHAREDPOINTER_H
184

source code of qtbase/src/corelib/tools/qsharedpointer.h