| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // Copyright (C) 2016 Intel Corporation. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | |
| 5 | #ifndef QTYPEINFO_H |
| 6 | #define QTYPEINFO_H |
| 7 | |
| 8 | #include <QtCore/qcompilerdetection.h> |
| 9 | #include <QtCore/qcontainerfwd.h> |
| 10 | |
| 11 | #include <type_traits> |
| 12 | |
| 13 | QT_BEGIN_NAMESPACE |
| 14 | |
| 15 | class QDebug; |
| 16 | |
| 17 | /* |
| 18 | QTypeInfo - type trait functionality |
| 19 | */ |
| 20 | |
| 21 | namespace QtPrivate { |
| 22 | |
| 23 | // Helper for QTypeInfo<T>::isComplex, which used to be simply |
| 24 | // !std::is_trivial_v but P3247 deprecated it for C++26. It used to be defined |
| 25 | // (since C++11) by [class]/7 as: "A trivial class is a class that is trivially |
| 26 | // copyable and has one or more default constructors, all of which are either |
| 27 | // trivial or deleted and at least one of which is not deleted. [ Note: In |
| 28 | // particular, a trivially copyable or trivial class does not have virtual |
| 29 | // functions or virtual base classes. — end note ]". |
| 30 | |
| 31 | template <typename T> |
| 32 | inline constexpr bool qIsComplex = |
| 33 | !std::is_trivially_default_constructible_v<T> || !std::is_trivially_copyable_v<T>; |
| 34 | |
| 35 | // A trivially copyable class must also have a trivial, non-deleted |
| 36 | // destructor [class.prop/1.3], CWG1734. Some implementations don't |
| 37 | // check for a trivial destructor, because of backwards compatibility |
| 38 | // with C++98's definition of trivial copyability. |
| 39 | // Since trivial copiability has implications for the ABI, implementations |
| 40 | // can't "just fix" their traits. So, although formally redundant, we |
| 41 | // explicitly check for trivial destruction here. |
| 42 | template <typename T> |
| 43 | inline constexpr bool qIsRelocatable = std::is_trivially_copyable_v<T> && std::is_trivially_destructible_v<T>; |
| 44 | |
| 45 | // Denotes types that are trivially default constructible, and for which |
| 46 | // value-initialization can be achieved by filling their storage with 0 bits. |
| 47 | // There is no type trait we can use for this, so we hardcode a list of |
| 48 | // possibilities that we know are OK on the architectures that we support. |
| 49 | // The most notable exception are pointers to data members, which for instance |
| 50 | // on the Itanium ABI are initialized to -1. |
| 51 | template <typename T> |
| 52 | inline constexpr bool qIsValueInitializationBitwiseZero = |
| 53 | std::is_scalar_v<T> && !std::is_member_object_pointer_v<T>; |
| 54 | |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | The catch-all template. |
| 59 | */ |
| 60 | |
| 61 | template <typename T> |
| 62 | class QTypeInfo |
| 63 | { |
| 64 | public: |
| 65 | enum { |
| 66 | isPointer [[deprecated("Use std::is_pointer instead" )]] = std::is_pointer_v<T>, |
| 67 | isIntegral [[deprecated("Use std::is_integral instead" )]] = std::is_integral_v<T>, |
| 68 | isComplex = QtPrivate::qIsComplex<T>, |
| 69 | isRelocatable = QtPrivate::qIsRelocatable<T>, |
| 70 | isValueInitializationBitwiseZero = QtPrivate::qIsValueInitializationBitwiseZero<T>, |
| 71 | }; |
| 72 | }; |
| 73 | |
| 74 | template<> |
| 75 | class QTypeInfo<void> |
| 76 | { |
| 77 | public: |
| 78 | enum { |
| 79 | isPointer [[deprecated("Use std::is_pointer instead" )]] = false, |
| 80 | isIntegral [[deprecated("Use std::is_integral instead" )]] = false, |
| 81 | isComplex = false, |
| 82 | isRelocatable = false, |
| 83 | isValueInitializationBitwiseZero = false, |
| 84 | }; |
| 85 | }; |
| 86 | |
| 87 | /*! |
| 88 | \class QTypeInfoMerger |
| 89 | \inmodule QtCore |
| 90 | \internal |
| 91 | |
| 92 | \brief QTypeInfoMerger merges the QTypeInfo flags of T1, T2... and presents them |
| 93 | as a QTypeInfo<T> would do. |
| 94 | |
| 95 | Let's assume that we have a simple set of structs: |
| 96 | |
| 97 | \snippet code/src_corelib_global_qglobal.cpp 50 |
| 98 | |
| 99 | To create a proper QTypeInfo specialization for A struct, we have to check |
| 100 | all sub-components; B, C and D, then take the lowest common denominator and call |
| 101 | Q_DECLARE_TYPEINFO with the resulting flags. An easier and less fragile approach is to |
| 102 | use QTypeInfoMerger, which does that automatically. So struct A would have |
| 103 | the following QTypeInfo definition: |
| 104 | |
| 105 | \snippet code/src_corelib_global_qglobal.cpp 51 |
| 106 | */ |
| 107 | template <class T, class...Ts> |
| 108 | class QTypeInfoMerger |
| 109 | { |
| 110 | static_assert(sizeof...(Ts) > 0); |
| 111 | public: |
| 112 | static constexpr bool isComplex = ((QTypeInfo<Ts>::isComplex) || ...); |
| 113 | static constexpr bool isRelocatable = ((QTypeInfo<Ts>::isRelocatable) && ...); |
| 114 | [[deprecated("Use std::is_pointer instead" )]] static constexpr bool isPointer = false; |
| 115 | [[deprecated("Use std::is_integral instead" )]] static constexpr bool isIntegral = false; |
| 116 | static constexpr bool isValueInitializationBitwiseZero = false; |
| 117 | static_assert(!isRelocatable || |
| 118 | std::is_copy_constructible_v<T> || |
| 119 | std::is_move_constructible_v<T>, |
| 120 | "All Ts... are Q_RELOCATABLE_TYPE, but T is neither copy- nor move-constructible, " |
| 121 | "so cannot be Q_RELOCATABLE_TYPE. Please mark T as Q_COMPLEX_TYPE manually." ); |
| 122 | }; |
| 123 | |
| 124 | // QTypeInfo for std::pair: |
| 125 | // std::pair is spec'ed to be struct { T1 first; T2 second; }, so, unlike tuple<>, |
| 126 | // we _can_ specialize QTypeInfo for pair<>: |
| 127 | template <class T1, class T2> |
| 128 | class QTypeInfo<std::pair<T1, T2>> : public QTypeInfoMerger<std::pair<T1, T2>, T1, T2> {}; |
| 129 | |
| 130 | #define Q_DECLARE_MOVABLE_CONTAINER(CONTAINER) \ |
| 131 | template <typename ...T> \ |
| 132 | class QTypeInfo<CONTAINER<T...>> \ |
| 133 | { \ |
| 134 | public: \ |
| 135 | enum { \ |
| 136 | isPointer [[deprecated("Use std::is_pointer instead")]] = false, \ |
| 137 | isIntegral [[deprecated("Use std::is_integral instead")]] = false, \ |
| 138 | isComplex = true, \ |
| 139 | isRelocatable = true, \ |
| 140 | isValueInitializationBitwiseZero = false, \ |
| 141 | }; \ |
| 142 | } |
| 143 | |
| 144 | Q_DECLARE_MOVABLE_CONTAINER(QList); |
| 145 | Q_DECLARE_MOVABLE_CONTAINER(QQueue); |
| 146 | Q_DECLARE_MOVABLE_CONTAINER(QStack); |
| 147 | Q_DECLARE_MOVABLE_CONTAINER(QSet); |
| 148 | Q_DECLARE_MOVABLE_CONTAINER(QMap); |
| 149 | Q_DECLARE_MOVABLE_CONTAINER(QMultiMap); |
| 150 | Q_DECLARE_MOVABLE_CONTAINER(QHash); |
| 151 | Q_DECLARE_MOVABLE_CONTAINER(QMultiHash); |
| 152 | Q_DECLARE_MOVABLE_CONTAINER(QCache); |
| 153 | |
| 154 | #undef Q_DECLARE_MOVABLE_CONTAINER |
| 155 | |
| 156 | /* |
| 157 | Specialize a specific type with: |
| 158 | |
| 159 | Q_DECLARE_TYPEINFO(type, flags); |
| 160 | |
| 161 | where 'type' is the name of the type to specialize and 'flags' is |
| 162 | logically-OR'ed combination of the flags below. |
| 163 | */ |
| 164 | enum { /* TYPEINFO flags */ |
| 165 | Q_COMPLEX_TYPE = 0, |
| 166 | Q_PRIMITIVE_TYPE = 0x1, |
| 167 | Q_RELOCATABLE_TYPE = 0x2, |
| 168 | Q_MOVABLE_TYPE = 0x2, |
| 169 | Q_DUMMY_TYPE = 0x4, |
| 170 | }; |
| 171 | |
| 172 | #define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS) \ |
| 173 | class QTypeInfo<TYPE > \ |
| 174 | { \ |
| 175 | public: \ |
| 176 | enum { \ |
| 177 | isComplex = (((FLAGS) & Q_PRIMITIVE_TYPE) == 0) && QtPrivate::qIsComplex<TYPE>, \ |
| 178 | isRelocatable = !isComplex || ((FLAGS) & Q_RELOCATABLE_TYPE) || QtPrivate::qIsRelocatable<TYPE>, \ |
| 179 | isPointer [[deprecated("Use std::is_pointer instead")]] = std::is_pointer_v< TYPE >, \ |
| 180 | isIntegral [[deprecated("Use std::is_integral instead")]] = std::is_integral< TYPE >::value, \ |
| 181 | isValueInitializationBitwiseZero = QtPrivate::qIsValueInitializationBitwiseZero<TYPE>, \ |
| 182 | }; \ |
| 183 | static_assert(!QTypeInfo<TYPE>::isRelocatable || \ |
| 184 | std::is_copy_constructible_v<TYPE > || \ |
| 185 | std::is_move_constructible_v<TYPE >, \ |
| 186 | #TYPE " is neither copy- nor move-constructible, so cannot be Q_RELOCATABLE_TYPE"); \ |
| 187 | } |
| 188 | |
| 189 | #define Q_DECLARE_TYPEINFO(TYPE, FLAGS) \ |
| 190 | template<> \ |
| 191 | Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS) |
| 192 | |
| 193 | /* Specialize QTypeInfo for QFlags<T> */ |
| 194 | template<typename T> class QFlags; |
| 195 | template<typename T> |
| 196 | Q_DECLARE_TYPEINFO_BODY(QFlags<T>, Q_PRIMITIVE_TYPE); |
| 197 | |
| 198 | QT_END_NAMESPACE |
| 199 | #endif // QTYPEINFO_H |
| 200 | |