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
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
28QT_BEGIN_NAMESPACE
29
30namespace q20 {
31#ifdef __cpp_lib_integer_comparison_functions
32using std::cmp_equal;
33using std::cmp_not_equal;
34using std::cmp_less;
35using std::cmp_greater;
36using std::cmp_less_equal;
38using std::in_range;
39#else
40namespace detail {
41template <class T, class U>
42constexpr 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
53template <class T, class U>
54constexpr 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
70template <class T, class U>
71constexpr bool cmp_not_equal(T t, U u) noexcept
72{
73 return !cmp_equal(t, u);
74}
75
76template <class T, class U>
77constexpr 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
93template <class T, class U>
94constexpr bool cmp_greater(T t, U u) noexcept
95{
96 return cmp_less(u, t);
97}
98
99template <class T, class U>
100constexpr bool cmp_less_equal(T t, U u) noexcept
101{
102 return !cmp_greater(t, u);
103}
104
105template <class T, class U>
106constexpr bool cmp_greater_equal(T t, U u) noexcept
107{
108 return !cmp_less(t, u);
109}
110
111template <class R, class T>
112constexpr 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)
122namespace q20 {
123#ifdef __cpp_lib_constexpr_algorithms
124using std::exchange;
125#else
126template <typename T, typename U = T>
127constexpr 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
136QT_END_NAMESPACE
137
138#endif /* Q20UTILITY_H */
constexpr void checkTypeCompatibility() noexcept
Definition q20utility.h:42
constexpr bool cmp_less_equal(T t, U u) noexcept
Definition q20utility.h:100
constexpr T exchange(T &obj, U &&newValue)
Definition q20utility.h:127
constexpr bool cmp_not_equal(T t, U u) noexcept
Definition q20utility.h:71
constexpr bool cmp_greater(T t, U u) noexcept
Definition q20utility.h:94
constexpr bool cmp_equal(T t, U u) noexcept
Definition q20utility.h:54
constexpr bool cmp_greater_equal(T t, U u) noexcept
Definition q20utility.h:106
constexpr bool in_range(T t) noexcept
Definition q20utility.h:112
constexpr bool cmp_less(T t, U u) noexcept
Definition q20utility.h:77