| 1 | // Copyright (C) 2024 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 Q20UTILITY_H |
| 5 | #define Q20UTILITY_H |
| 6 | |
| 7 | #include <QtCore/qttypetraits.h> |
| 8 | |
| 9 | #include <limits> |
| 10 | #include <utility> |
| 11 | |
| 12 | // |
| 13 | // W A R N I N G |
| 14 | // ------------- |
| 15 | // |
| 16 | // This file is not part of the Qt API. Types and functions defined in this |
| 17 | // file can reliably be replaced by their std counterparts, once available. |
| 18 | // You may use these definitions in your own code, but be aware that we |
| 19 | // will remove them once Qt depends on the C++ version that supports |
| 20 | // them in namespace std. There will be NO deprecation warning, the |
| 21 | // definitions will JUST go away. |
| 22 | // |
| 23 | // If you can't agree to these terms, don't use these definitions! |
| 24 | // |
| 25 | // We mean it. |
| 26 | // |
| 27 | |
| 28 | QT_BEGIN_NAMESPACE |
| 29 | |
| 30 | namespace q20 { |
| 31 | #ifdef __cpp_lib_integer_comparison_functions |
| 32 | using std::cmp_equal; |
| 33 | using std::cmp_not_equal; |
| 34 | using std::cmp_less; |
| 35 | using std::cmp_greater; |
| 36 | using std::cmp_less_equal; |
| 37 | using std::cmp_greater_equal; |
| 38 | using std::in_range; |
| 39 | #else |
| 40 | namespace detail { |
| 41 | template <class T, class U> |
| 42 | constexpr void checkTypeCompatibility() noexcept |
| 43 | { |
| 44 | // Both T and U are standard integer types or extended integer types, |
| 45 | // see https://eel.is/c++draft/utility.intcmp#1 |
| 46 | static_assert(QtPrivate::is_standard_or_extended_integer_type_v<T>, |
| 47 | "Integer types should be used for left T class." ); |
| 48 | static_assert(QtPrivate::is_standard_or_extended_integer_type_v<U>, |
| 49 | "Integer types should be used for right U class." ); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | template <class T, class U> |
| 54 | constexpr bool cmp_equal(T t, U u) noexcept |
| 55 | { |
| 56 | // Both T and U are standard integer types or extended integer types |
| 57 | // https://eel.is/c++draft/utility.intcmp#1 |
| 58 | detail::checkTypeCompatibility<T, U>(); |
| 59 | // See https://eel.is/c++draft/utility.intcmp#2 |
| 60 | using UT = std::make_unsigned_t<T>; |
| 61 | using UU = std::make_unsigned_t<U>; |
| 62 | if constexpr (std::is_signed_v<T> == std::is_signed_v<U>) |
| 63 | return t == u; |
| 64 | else if constexpr (std::is_signed_v<T>) |
| 65 | return t < 0 ? false : UT(t) == u; |
| 66 | else |
| 67 | return u < 0 ? false : t == UU(u); |
| 68 | } |
| 69 | |
| 70 | template <class T, class U> |
| 71 | constexpr bool cmp_not_equal(T t, U u) noexcept |
| 72 | { |
| 73 | return !cmp_equal(t, u); |
| 74 | } |
| 75 | |
| 76 | template <class T, class U> |
| 77 | constexpr bool cmp_less(T t, U u) noexcept |
| 78 | { |
| 79 | // Both T and U are standard integer types or extended integer types |
| 80 | // https://eel.is/c++draft/utility.intcmp#4 |
| 81 | detail::checkTypeCompatibility<T, U>(); |
| 82 | // See https://eel.is/c++draft/utility.intcmp#5 |
| 83 | using UT = std::make_unsigned_t<T>; |
| 84 | using UU = std::make_unsigned_t<U>; |
| 85 | if constexpr (std::is_signed_v<T> == std::is_signed_v<U>) |
| 86 | return t < u; |
| 87 | else if constexpr (std::is_signed_v<T>) |
| 88 | return t < 0 ? true : UT(t) < u; |
| 89 | else |
| 90 | return u < 0 ? false : t < UU(u); |
| 91 | } |
| 92 | |
| 93 | template <class T, class U> |
| 94 | constexpr bool cmp_greater(T t, U u) noexcept |
| 95 | { |
| 96 | return cmp_less(u, t); |
| 97 | } |
| 98 | |
| 99 | template <class T, class U> |
| 100 | constexpr bool cmp_less_equal(T t, U u) noexcept |
| 101 | { |
| 102 | return !cmp_greater(t, u); |
| 103 | } |
| 104 | |
| 105 | template <class T, class U> |
| 106 | constexpr bool cmp_greater_equal(T t, U u) noexcept |
| 107 | { |
| 108 | return !cmp_less(t, u); |
| 109 | } |
| 110 | |
| 111 | template <class R, class T> |
| 112 | constexpr bool in_range(T t) noexcept |
| 113 | { |
| 114 | return cmp_less_equal(t, (std::numeric_limits<R>::max)()) |
| 115 | && cmp_greater_equal(t, (std::numeric_limits<R>::min)()); |
| 116 | } |
| 117 | |
| 118 | #endif // __cpp_lib_integer_comparison_functions |
| 119 | } // namespace q20 |
| 120 | |
| 121 | // like C++20 std::exchange (ie. constexpr, not yet noexcept) |
| 122 | namespace q20 { |
| 123 | #ifdef __cpp_lib_constexpr_algorithms |
| 124 | using std::exchange; |
| 125 | #else |
| 126 | template <typename T, typename U = T> |
| 127 | constexpr T exchange(T& obj, U&& newValue) |
| 128 | { |
| 129 | T old = std::move(obj); |
| 130 | obj = std::forward<U>(newValue); |
| 131 | return old; |
| 132 | } |
| 133 | #endif |
| 134 | } |
| 135 | |
| 136 | QT_END_NAMESPACE |
| 137 | |
| 138 | #endif /* Q20UTILITY_H */ |
| 139 | |