Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
quniquehandle_p.h
Go to the documentation of this file.
1// Copyright (C) 2023 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 QUNIQUEHANDLE_P_H
5#define QUNIQUEHANDLE_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qtconfigmacros.h>
19#include <QtCore/qassert.h>
20
21#include <memory>
22
24
104// clang-format off
105
106template <typename HandleTraits>
108{
109public:
110 using Type = typename HandleTraits::Type;
111
112 QUniqueHandle() = default;
113
114 explicit QUniqueHandle(const Type &handle) noexcept
115 : m_handle{ handle }
116 {}
117
119 : m_handle{ other.release() }
120 {}
121
122 ~QUniqueHandle() noexcept
123 {
124 close();
125 }
126
128 {
129 if (this != std::addressof(rhs))
130 reset(rhs.release());
131
132 return *this;
133 }
134
135 QUniqueHandle(const QUniqueHandle &) = delete;
137
138
139 [[nodiscard]] bool isValid() const noexcept
140 {
141 return m_handle != HandleTraits::invalidValue();
142 }
143
144 [[nodiscard]] explicit operator bool() const noexcept
145 {
146 return isValid();
147 }
148
149 [[nodiscard]] Type get() const noexcept
150 {
151 return m_handle;
152 }
153
154 void reset(const Type& handle) noexcept
155 {
156 if (handle == m_handle)
157 return;
158
159 close();
160 m_handle = handle;
161 }
162
163 [[nodiscard]] Type release() noexcept
164 {
165 Type handle = m_handle;
166 m_handle = HandleTraits::invalidValue();
167 return handle;
168 }
169
170 [[nodiscard]] Type *operator&() noexcept // NOLINT(google-runtime-operator)
171 {
172 Q_ASSERT(!isValid());
173 return &m_handle;
174 }
175
176 void close() noexcept
177 {
178 if (!isValid())
179 return;
180
181 const bool success = HandleTraits::close(m_handle);
182 Q_ASSERT(success);
183
184 m_handle = HandleTraits::invalidValue();
185 }
186
187 [[nodiscard]] friend bool operator==(const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept
188 {
189 return lhs.get() == rhs.get();
190 }
191
192 [[nodiscard]] friend bool operator!=(const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept
193 {
194 return lhs.get() != rhs.get();
195 }
196
197 [[nodiscard]] friend bool operator<(const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept
198 {
199 return lhs.get() < rhs.get();
200 }
201
202 [[nodiscard]] friend bool operator<=(const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept
203 {
204 return lhs.get() <= rhs.get();
205 }
206
207 [[nodiscard]] friend bool operator>(const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept
208 {
209 return lhs.get() > rhs.get();
210 }
211
212 [[nodiscard]] friend bool operator>=(const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept
213 {
214 return lhs.get() >= rhs.get();
215 }
216
217private:
218 Type m_handle{ HandleTraits::invalidValue() };
219};
220
221// clang-format on
222
224
225#endif
friend bool operator!=(const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept
void reset(const Type &handle) noexcept
QUniqueHandle(const QUniqueHandle &)=delete
QUniqueHandle & operator=(const QUniqueHandle &)=delete
Type release() noexcept
friend bool operator>=(const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept
friend bool operator<(const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept
~QUniqueHandle() noexcept
QUniqueHandle & operator=(QUniqueHandle &&rhs) noexcept
friend bool operator>(const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept
void close() noexcept
Type get() const noexcept
friend bool operator<=(const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept
QUniqueHandle(const Type &handle) noexcept
Type * operator&() noexcept
bool isValid() const noexcept
QUniqueHandle(QUniqueHandle &&other) noexcept
QUniqueHandle()=default
friend bool operator==(const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept
Combined button and popup list for selecting options.
GLuint64 GLenum void * handle
GLboolean reset
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QSharedPointer< T > other(t)
[5]
Definition moc.h:23