| 1 | // Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | #ifndef Q20TYPE_TRAITS_H |
| 4 | #define Q20TYPE_TRAITS_H |
| 5 | |
| 6 | #include <QtCore/qcompilerdetection.h> |
| 7 | #include <QtCore/qsystemdetection.h> |
| 8 | #include <QtCore/qtconfigmacros.h> |
| 9 | |
| 10 | // |
| 11 | // W A R N I N G |
| 12 | // ------------- |
| 13 | // |
| 14 | // This file is not part of the Qt API. Types and functions defined in this |
| 15 | // file can reliably be replaced by their std counterparts, once available. |
| 16 | // You may use these definitions in your own code, but be aware that we |
| 17 | // will remove them once Qt depends on the C++ version that supports |
| 18 | // them in namespace std. There will be NO deprecation warning, the |
| 19 | // definitions will JUST go away. |
| 20 | // |
| 21 | // If you can't agree to these terms, don't use these definitions! |
| 22 | // |
| 23 | // We mean it. |
| 24 | // |
| 25 | |
| 26 | #include <type_traits> |
| 27 | |
| 28 | QT_BEGIN_NAMESPACE |
| 29 | |
| 30 | namespace q20 { |
| 31 | // like std::is_(un)bounded_array |
| 32 | #ifdef __cpp_lib_bounded_array_traits |
| 33 | using std::is_bounded_array; |
| 34 | using std::is_bounded_array_v; |
| 35 | using std::is_unbounded_array; |
| 36 | using std::is_unbounded_array_v; |
| 37 | #else |
| 38 | template <typename T> struct is_bounded_array : std::false_type {}; |
| 39 | template <typename T, std::size_t N> struct is_bounded_array<T[N]> : std::true_type {}; |
| 40 | template <typename T> struct is_unbounded_array : std::false_type {}; |
| 41 | template <typename T> struct is_unbounded_array<T[]> : std::true_type {}; |
| 42 | template <typename T> constexpr inline bool is_bounded_array_v = q20::is_bounded_array<T>::value; |
| 43 | template <typename T> constexpr inline bool is_unbounded_array_v = q20::is_unbounded_array<T>::value; |
| 44 | #endif |
| 45 | } |
| 46 | |
| 47 | namespace q20 { |
| 48 | // like std::is_constant_evaluated |
| 49 | #ifdef __cpp_lib_is_constant_evaluated |
| 50 | using std::is_constant_evaluated; |
| 51 | #define QT_SUPPORTS_IS_CONSTANT_EVALUATED |
| 52 | #else |
| 53 | constexpr bool is_constant_evaluated() noexcept |
| 54 | { |
| 55 | #ifdef Q_OS_INTEGRITY |
| 56 | // Integrity complains "calling __has_builtin() from a constant expression". |
| 57 | // Avoid the __has_builtin check until we know what's going on. |
| 58 | return false; |
| 59 | #elif __has_builtin(__builtin_is_constant_evaluated) || \ |
| 60 | (defined(Q_CC_MSVC_ONLY) /* >= 1925, but we require 1927 in qglobal.h */) |
| 61 | # define QT_SUPPORTS_IS_CONSTANT_EVALUATED |
| 62 | return __builtin_is_constant_evaluated(); |
| 63 | #else |
| 64 | return false; |
| 65 | #endif |
| 66 | } |
| 67 | #endif // __cpp_lib_is_constant_evaluated |
| 68 | } |
| 69 | |
| 70 | namespace q20 { |
| 71 | // like std::remove_cvref(_t) |
| 72 | #ifdef __cpp_lib_remove_cvref |
| 73 | using std::remove_cvref; |
| 74 | using std::remove_cvref_t; |
| 75 | #else |
| 76 | template <typename T> |
| 77 | using remove_cvref = std::remove_cv<std::remove_reference_t<T>>; |
| 78 | template <typename T> |
| 79 | using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>; |
| 80 | #endif // __cpp_lib_remove_cvref |
| 81 | } |
| 82 | |
| 83 | namespace q20 { |
| 84 | // like std::type_identity(_t) |
| 85 | #ifdef __cpp_lib_type_identity |
| 86 | using std::type_identity; |
| 87 | using std::type_identity_t; |
| 88 | #else |
| 89 | template <typename T> |
| 90 | struct type_identity { using type = T; }; |
| 91 | template <typename T> |
| 92 | using type_identity_t = typename type_identity<T>::type; |
| 93 | #endif // __cpp_lib_type_identity |
| 94 | } |
| 95 | |
| 96 | QT_END_NAMESPACE |
| 97 | |
| 98 | #endif /* Q20TYPE_TRAITS_H */ |
| 99 | |