Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
29
QT_BEGIN_NAMESPACE
30
31
namespace
q20
{
32
#
ifdef
__cpp_lib_integer_comparison_functions
33
using
std
::
cmp_equal
;
34
using
std
::
cmp_not_equal
;
35
using
std
::
cmp_less
;
36
using
std
::
cmp_greater
;
37
using
std
::
cmp_less_equal
;
38
using
std
::
cmp_greater_equal
;
39
using
std
::
in_range
;
40
#
else
41
namespace
detail
{
42
template
<
class
T,
class
U>
43
constexpr
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
54
template
<
class
T,
class
U>
55
constexpr
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
71
template
<
class
T,
class
U>
72
constexpr
bool
cmp_not_equal
(T t, U u)
noexcept
73
{
74
return
!cmp_equal(t, u);
75
}
76
77
template
<
class
T,
class
U>
78
constexpr
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
94
template
<
class
T,
class
U>
95
constexpr
bool
cmp_greater
(T t, U u)
noexcept
96
{
97
return
cmp_less(u, t);
98
}
99
100
template
<
class
T,
class
U>
101
constexpr
bool
cmp_less_equal
(T t, U u)
noexcept
102
{
103
return
!cmp_greater(t, u);
104
}
105
106
template
<
class
T,
class
U>
107
constexpr
bool
cmp_greater_equal
(T t, U u)
noexcept
108
{
109
return
!cmp_less(t, u);
110
}
111
112
template
<
class
R,
class
T>
113
constexpr
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)
123
namespace
q20
{
124
#
ifdef
__cpp_lib_constexpr_algorithms
125
using
std
::
exchange
;
126
#
else
127
template
<
typename
T,
typename
U = T>
128
constexpr
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
137
QT_END_NAMESPACE
138
139
#
endif
/* Q20UTILITY_H */
q20::detail
Definition
q20bit.h:51
q20::detail::checkTypeCompatibility
constexpr void checkTypeCompatibility() noexcept
Definition
q20utility.h:43
q20
Definition
qnumeric.cpp:467
q20::cmp_less_equal
constexpr bool cmp_less_equal(T t, U u) noexcept
Definition
q20utility.h:101
q20::exchange
constexpr T exchange(T &obj, U &&newValue)
Definition
q20utility.h:128
q20::cmp_not_equal
constexpr bool cmp_not_equal(T t, U u) noexcept
Definition
q20utility.h:72
q20::cmp_greater
constexpr bool cmp_greater(T t, U u) noexcept
Definition
q20utility.h:95
q20::cmp_equal
constexpr bool cmp_equal(T t, U u) noexcept
Definition
q20utility.h:55
q20::cmp_greater_equal
constexpr bool cmp_greater_equal(T t, U u) noexcept
Definition
q20utility.h:107
q20::in_range
constexpr bool in_range(T t) noexcept
Definition
q20utility.h:113
q20::cmp_less
constexpr bool cmp_less(T t, U u) noexcept
Definition
q20utility.h:78
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qtbase
src
corelib
global
q20utility.h
Generated on
for Qt by
1.18.0