1 | // Copyright (C) 2022 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 QTTYPETRAITS_H |
5 | #define QTTYPETRAITS_H |
6 | |
7 | #include <QtCore/qtconfigmacros.h> |
8 | #include <QtCore/qtdeprecationmarkers.h> |
9 | |
10 | #include <type_traits> |
11 | #include <utility> |
12 | |
13 | #if 0 |
14 | #pragma qt_class(QtTypeTraits) |
15 | #pragma qt_sync_stop_processing |
16 | #endif |
17 | |
18 | QT_BEGIN_NAMESPACE |
19 | |
20 | // like std::to_underlying |
21 | template <typename Enum> |
22 | constexpr std::underlying_type_t<Enum> qToUnderlying(Enum e) noexcept |
23 | { |
24 | return static_cast<std::underlying_type_t<Enum>>(e); |
25 | } |
26 | |
27 | #ifndef QT_NO_AS_CONST |
28 | #if QT_DEPRECATED_SINCE(6, 6) |
29 | |
30 | // this adds const to non-const objects (like std::as_const) |
31 | template <typename T> |
32 | QT_DEPRECATED_VERSION_X_6_6("Use std::as_const() instead." ) |
33 | constexpr typename std::add_const<T>::type &qAsConst(T &t) noexcept { return t; } |
34 | // prevent rvalue arguments: |
35 | template <typename T> |
36 | void qAsConst(const T &&) = delete; |
37 | |
38 | #endif // QT_DEPRECATED_SINCE(6, 6) |
39 | #endif // QT_NO_AS_CONST |
40 | |
41 | #ifndef QT_NO_QEXCHANGE |
42 | |
43 | // like std::exchange |
44 | template <typename T, typename U = T> |
45 | constexpr T qExchange(T &t, U &&newValue) |
46 | noexcept(std::conjunction_v<std::is_nothrow_move_constructible<T>, |
47 | std::is_nothrow_assignable<T &, U>>) |
48 | { |
49 | T old = std::move(t); |
50 | t = std::forward<U>(newValue); |
51 | return old; |
52 | } |
53 | |
54 | #endif // QT_NO_QEXCHANGE |
55 | |
56 | namespace QtPrivate { |
57 | // helper to be used to trigger a "dependent static_assert(false)" |
58 | // (for instance, in a final `else` branch of a `if constexpr`.) |
59 | template <typename T> struct type_dependent_false : std::false_type {}; |
60 | template <auto T> struct value_dependent_false : std::false_type {}; |
61 | } |
62 | |
63 | QT_END_NAMESPACE |
64 | |
65 | #endif // QTTYPETRAITS_H |
66 | |