115 using Type =
typename HandleTraits::Type;
116 static_assert(std::is_nothrow_default_constructible_v<Type>);
117 static_assert(std::is_nothrow_constructible_v<Type>);
118 static_assert(std::is_nothrow_copy_constructible_v<Type>);
119 static_assert(std::is_nothrow_move_constructible_v<Type>);
120 static_assert(std::is_nothrow_copy_assignable_v<Type>);
121 static_assert(std::is_nothrow_move_assignable_v<Type>);
122 static_assert(std::is_nothrow_destructible_v<Type>);
123 static_assert(
noexcept(std::declval<Type>() == std::declval<Type>()));
124 static_assert(
noexcept(std::declval<Type>() != std::declval<Type>()));
125 static_assert(
noexcept(std::declval<Type>() < std::declval<Type>()));
126 static_assert(
noexcept(std::declval<Type>() <= std::declval<Type>()));
127 static_assert(
noexcept(std::declval<Type>() > std::declval<Type>()));
128 static_assert(
noexcept(std::declval<Type>() >= std::declval<Type>()));
130 QUniqueHandle() =
default;
132 explicit QUniqueHandle(
const Type &handle)
noexcept
136 QUniqueHandle(QUniqueHandle &&other)
noexcept
137 : m_handle{ other.release() }
140 ~QUniqueHandle()
noexcept
145 void swap(QUniqueHandle &other)
noexcept
147 qSwap(m_handle, other.m_handle);
150 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QUniqueHandle)
152 QUniqueHandle(
const QUniqueHandle &) =
delete;
153 QUniqueHandle &operator=(
const QUniqueHandle &) =
delete;
156 [[nodiscard]]
bool isValid()
const noexcept
158 return m_handle != HandleTraits::invalidValue();
161 [[nodiscard]]
explicit operator
bool()
const noexcept
166 [[nodiscard]] Type get()
const noexcept
171 void reset(
const Type& handle = HandleTraits::invalidValue())
noexcept
173 if (handle == m_handle)
180 [[nodiscard]] Type release()
noexcept
182 return std::exchange(m_handle, HandleTraits::invalidValue());
185 [[nodiscard]] Type *operator&()
noexcept
187 Q_ASSERT(!isValid());
191 void close()
noexcept
196 const bool success = HandleTraits::close(m_handle);
199 m_handle = HandleTraits::invalidValue();
203 friend bool comparesEqual(
const QUniqueHandle& lhs,
const QUniqueHandle& rhs)
noexcept
205 return lhs.get() == rhs.get();
208 friend Qt::strong_ordering compareThreeWay(
const QUniqueHandle& lhs,
209 const QUniqueHandle& rhs)
noexcept
211 if constexpr (std::is_pointer_v<Type>)
212 return qCompareThreeWay(Qt::totally_ordered_wrapper{ lhs.get() },
213 Qt::totally_ordered_wrapper{ rhs.get() });
215 return qCompareThreeWay(lhs.get(), rhs.get());
218 Q_DECLARE_STRONGLY_ORDERED(QUniqueHandle)
220 Type m_handle{ HandleTraits::invalidValue() };
225template <
typename Trait>
226void swap(QUniqueHandle<Trait> &lhs, QUniqueHandle<Trait> &rhs)
noexcept