Back to home page

EIC code displayed by LXR

 
 

    


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 // Copyright (C) 2024 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
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 //  W A R N I N G
0014 //  -------------
0015 //
0016 // This file is not part of the Qt API. Types and functions defined in this
0017 // file can reliably be replaced by their std counterparts, once available.
0018 // You may use these definitions in your own code, but be aware that we
0019 // will remove them once Qt depends on the C++ version that supports
0020 // them in namespace std. There will be NO deprecation warning, the
0021 // definitions will JUST go away.
0022 //
0023 // If you can't agree to these terms, don't use these definitions!
0024 //
0025 // We mean it.
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     // Both T and U are standard integer types or extended integer types,
0045     // see https://eel.is/c++draft/utility.intcmp#1
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     // Both T and U are standard integer types or extended integer types
0057     // https://eel.is/c++draft/utility.intcmp#1
0058     detail::checkTypeCompatibility<T, U>();
0059     // See https://eel.is/c++draft/utility.intcmp#2
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     // Both T and U are standard integer types or extended integer types
0080     // https://eel.is/c++draft/utility.intcmp#4
0081     detail::checkTypeCompatibility<T, U>();
0082     // See https://eel.is/c++draft/utility.intcmp#5
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 // __cpp_lib_integer_comparison_functions
0119 } // namespace q20
0120 
0121 // like C++20 std::exchange (ie. constexpr, not yet noexcept)
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 /* Q20UTILITY_H */