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