Warning, file /include/QtCore/q20utility.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004 #ifndef Q20UTILITY_H
0005 #define Q20UTILITY_H
0006
0007 #include <QtCore/qttypetraits.h>
0008
0009 #include <limits>
0010 #include <utility>
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 QT_BEGIN_NAMESPACE
0029
0030 namespace q20 {
0031 #ifdef __cpp_lib_integer_comparison_functions
0032 using std::cmp_equal;
0033 using std::cmp_not_equal;
0034 using std::cmp_less;
0035 using std::cmp_greater;
0036 using std::cmp_less_equal;
0037 using std::cmp_greater_equal;
0038 using std::in_range;
0039 #else
0040 namespace detail {
0041 template <class T, class U>
0042 constexpr void checkTypeCompatibility() noexcept
0043 {
0044
0045
0046 static_assert(QtPrivate::is_standard_or_extended_integer_type_v<T>,
0047 "Integer types should be used for left T class.");
0048 static_assert(QtPrivate::is_standard_or_extended_integer_type_v<U>,
0049 "Integer types should be used for right U class.");
0050 }
0051 }
0052
0053 template <class T, class U>
0054 constexpr bool cmp_equal(T t, U u) noexcept
0055 {
0056
0057
0058 detail::checkTypeCompatibility<T, U>();
0059
0060 using UT = std::make_unsigned_t<T>;
0061 using UU = std::make_unsigned_t<U>;
0062 if constexpr (std::is_signed_v<T> == std::is_signed_v<U>)
0063 return t == u;
0064 else if constexpr (std::is_signed_v<T>)
0065 return t < 0 ? false : UT(t) == u;
0066 else
0067 return u < 0 ? false : t == UU(u);
0068 }
0069
0070 template <class T, class U>
0071 constexpr bool cmp_not_equal(T t, U u) noexcept
0072 {
0073 return !cmp_equal(t, u);
0074 }
0075
0076 template <class T, class U>
0077 constexpr bool cmp_less(T t, U u) noexcept
0078 {
0079
0080
0081 detail::checkTypeCompatibility<T, U>();
0082
0083 using UT = std::make_unsigned_t<T>;
0084 using UU = std::make_unsigned_t<U>;
0085 if constexpr (std::is_signed_v<T> == std::is_signed_v<U>)
0086 return t < u;
0087 else if constexpr (std::is_signed_v<T>)
0088 return t < 0 ? true : UT(t) < u;
0089 else
0090 return u < 0 ? false : t < UU(u);
0091 }
0092
0093 template <class T, class U>
0094 constexpr bool cmp_greater(T t, U u) noexcept
0095 {
0096 return cmp_less(u, t);
0097 }
0098
0099 template <class T, class U>
0100 constexpr bool cmp_less_equal(T t, U u) noexcept
0101 {
0102 return !cmp_greater(t, u);
0103 }
0104
0105 template <class T, class U>
0106 constexpr bool cmp_greater_equal(T t, U u) noexcept
0107 {
0108 return !cmp_less(t, u);
0109 }
0110
0111 template <class R, class T>
0112 constexpr bool in_range(T t) noexcept
0113 {
0114 return cmp_less_equal(t, (std::numeric_limits<R>::max)())
0115 && cmp_greater_equal(t, (std::numeric_limits<R>::min)());
0116 }
0117
0118 #endif
0119 }
0120
0121
0122 namespace q20 {
0123 #ifdef __cpp_lib_constexpr_algorithms
0124 using std::exchange;
0125 #else
0126 template <typename T, typename U = T>
0127 constexpr T exchange(T& obj, U&& newValue)
0128 {
0129 T old = std::move(obj);
0130 obj = std::forward<U>(newValue);
0131 return old;
0132 }
0133 #endif
0134 }
0135
0136 QT_END_NAMESPACE
0137
0138 #endif