Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsharedhandle_p.h
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSHAREDHANDLE_P_H
5#define QSHAREDHANDLE_P_H
6
7#include <QtCore/private/quniquehandle_p.h>
8#include <QtCore/qtconfigmacros.h>
9#include <QtCore/qcompare.h>
10
11#if __cpp_lib_concepts
12# include <concepts>
13#endif
14
15//
16// W A R N I N G
17// -------------
18//
19// This file is not part of the Qt API. It exists purely as an
20// implementation detail. This header file may change from version to
21// version without notice, or even be removed.
22//
23// We mean it.
24//
25
26QT_BEGIN_NAMESPACE
27
28namespace QtPrivate {
29
30#if __cpp_lib_concepts
31
32// clang-format off
33
34// Define a concept for the traits
35template <typename T>
37{
38 typename T::Type;
39
40 { T::invalidValue() } noexcept -> std::same_as<typename T::Type>;
41 { T::ref(std::declval<typename T::Type>()) } -> std::same_as<typename T::Type>;
42 { T::unref(std::declval<typename T::Type>()) } -> std::same_as<bool>;
43};
44
45// clang-format on
46
47#endif
48
49#if __cpp_lib_concepts
51#else
52template <typename SharedHandleTraits>
53#endif
55{
56 using Type = typename SharedHandleTraits::Type;
57
58 [[nodiscard]] static Type invalidValue() noexcept(noexcept(SharedHandleTraits::invalidValue()))
59 {
60 return SharedHandleTraits::invalidValue();
61 }
62
63 [[nodiscard]] static bool
64 close(Type handle) noexcept(noexcept(SharedHandleTraits::unref(handle)))
65 {
66 return SharedHandleTraits::unref(handle);
67 }
68};
69
70#if __cpp_lib_concepts
72#else
73template <typename HandleTraits>
74#endif
76{
77private:
79
80 static constexpr bool BaseResetIsNoexcept =
81 noexcept(std::declval<BaseClass>().reset(std::declval<typename HandleTraits::Type>()));
82
83 static constexpr bool RefIsNoexcept =
84 noexcept(HandleTraits::ref(std::declval<typename HandleTraits::Type>()));
85
86 static constexpr bool BaseMoveIsNoexcept =
88
89public:
90 using typename BaseClass::Type;
91
93 {
96
97 // syntactic sugar
100 };
101
102 QSharedHandle() = default;
103
104 explicit QSharedHandle(typename HandleTraits::Type object, RefMode mode)
106 {
107 }
108
110 : BaseClass{
111 HandleTraits::ref(o.get()),
112 }
113 {
114 }
115
116 QSharedHandle(QSharedHandle &&) noexcept = default;
117
118 // NOLINTNEXTLINE: bugprone-unhandled-self-assign
120 {
121 if (BaseClass::get() != o.get())
122 BaseClass::reset(HandleTraits::ref(o.get()));
123 return *this;
124 };
125
127 {
128 BaseClass::operator=(std::forward<QSharedHandle>(o));
129 return *this;
130 }
131
132 void reset(typename HandleTraits::Type o,
133 RefMode mode) noexcept(RefIsNoexcept && BaseResetIsNoexcept)
134 {
135 if (mode == NeedsRef)
136 BaseClass::reset(HandleTraits::ref(o));
137 else
138 BaseClass::reset(o);
139 }
140
141 void reset() noexcept(BaseResetIsNoexcept) { BaseClass::reset(); }
142
144
145 using BaseClass::get;
146 using BaseClass::isValid;
147 using BaseClass::operator bool;
148 using BaseClass::release;
149 using BaseClass::operator&;
150
151 void swap(QSharedHandle &other) noexcept(noexcept(std::declval<BaseClass>().swap(other)))
152 {
153 BaseClass::swap(other);
154 }
155};
156
157template <typename Trait>
158void swap(QSharedHandle<Trait> &lhs, QSharedHandle<Trait> &rhs) noexcept(noexcept(lhs.swap(rhs)))
159{
160 lhs.swap(rhs);
161}
162
163} // namespace QtPrivate
164
165QT_END_NAMESPACE
166
167#endif // QSHAREDHANDLE_P_H
void swap(QSharedHandle< Trait > &lhs, QSharedHandle< Trait > &rhs) noexcept(noexcept(lhs.swap(rhs)))
void reset(typename HandleTraits::Type o, RefMode mode) noexcept(RefIsNoexcept &&BaseResetIsNoexcept)
void reset() noexcept(BaseResetIsNoexcept)
QSharedHandle & operator=(const QSharedHandle &o) noexcept(RefIsNoexcept &&BaseResetIsNoexcept)
QSharedHandle & operator=(QSharedHandle &&o) noexcept(BaseMoveIsNoexcept)
void swap(QSharedHandle &other) noexcept(noexcept(std::declval< BaseClass >().swap(other)))
QSharedHandle(const QSharedHandle &o)
QSharedHandle(typename HandleTraits::Type object, RefMode mode)
QSharedHandle(QSharedHandle &&) noexcept=default
static Type invalidValue() noexcept(noexcept(SharedHandleTraits::invalidValue()))
static bool close(Type handle) noexcept(noexcept(SharedHandleTraits::unref(handle)))