1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Copyright (C) 2016 Intel Corporation.
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of the QtCore module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU Lesser General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU Lesser
20 ** General Public License version 3 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
22 ** packaging of this file. Please review the following information to
23 ** ensure the GNU Lesser General Public License version 3 requirements
24 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25 **
26 ** GNU General Public License Usage
27 ** Alternatively, this file may be used under the terms of the GNU
28 ** General Public License version 2.0 or (at your option) the GNU General
29 ** Public license version 3 or any later version approved by the KDE Free
30 ** Qt Foundation. The licenses are as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32 ** included in the packaging of this file. Please review the following
33 ** information to ensure the GNU General Public License requirements will
34 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35 ** https://www.gnu.org/licenses/gpl-3.0.html.
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include <QtCore/qglobal.h>
42
43 #ifndef QTYPEINFO_H
44 #define QTYPEINFO_H
45
46 QT_BEGIN_NAMESPACE
47
48 /*
49 QTypeInfo - type trait functionality
50 */
51
52 template <typename T>
qIsRelocatable()53 static constexpr bool qIsRelocatable()
54 {
55 #if defined(Q_CC_CLANG) || !defined(Q_CC_GNU) || Q_CC_GNU >= 501
56 return std::is_trivially_copyable<T>::value && std::is_trivially_destructible<T>::value;
57 #else
58 return std::is_enum<T>::value || std::is_integral<T>::value;
59 #endif
60 }
61
62 template <typename T>
qIsTrivial()63 static constexpr bool qIsTrivial()
64 {
65 #if defined(Q_CC_CLANG) || !defined(Q_CC_GNU) || Q_CC_GNU >= 501
66 return std::is_trivial<T>::value;
67 #else
68 return std::is_enum<T>::value || std::is_integral<T>::value;
69 #endif
70 }
71
72 /*
73 The catch-all template.
74 */
75
76 template <typename T>
77 class QTypeInfo
78 {
79 public:
80 enum {
81 isSpecialized = std::is_enum<T>::value, // don't require every enum to be marked manually
82 isPointer = false,
83 isIntegral = std::is_integral<T>::value,
84 isComplex = !qIsTrivial<T>(),
85 isStatic = true,
86 isRelocatable = qIsRelocatable<T>(),
87 isLarge = (sizeof(T)>sizeof(void*)),
88 isDummy = false, //### Qt6: remove
89 sizeOf = sizeof(T)
90 };
91 };
92
93 template<>
94 class QTypeInfo<void>
95 {
96 public:
97 enum {
98 isSpecialized = true,
99 isPointer = false,
100 isIntegral = false,
101 isComplex = false,
102 isStatic = false,
103 isRelocatable = false,
104 isLarge = false,
105 isDummy = false,
106 sizeOf = 0
107 };
108 };
109
110 template <typename T>
111 class QTypeInfo<T*>
112 {
113 public:
114 enum {
115 isSpecialized = true,
116 isPointer = true,
117 isIntegral = false,
118 isComplex = false,
119 isStatic = false,
120 isRelocatable = true,
121 isLarge = false,
122 isDummy = false,
123 sizeOf = sizeof(T*)
124 };
125 };
126
127 /*!
128 \class QTypeInfoQuery
129 \inmodule QtCore
130 \internal
131 \brief QTypeInfoQuery is used to query the values of a given QTypeInfo<T>
132
133 We use it because there may be some QTypeInfo<T> specializations in user
134 code that don't provide certain flags that we added after Qt 5.0. They are:
135 \list
136 \li isRelocatable: defaults to !isStatic
137 \endlist
138
139 DO NOT specialize this class elsewhere.
140 */
141 // apply defaults for a generic QTypeInfo<T> that didn't provide the new values
142 template <typename T, typename = void>
143 struct QTypeInfoQuery : public QTypeInfo<T>
144 {
145 enum { isRelocatable = !QTypeInfo<T>::isStatic };
146 };
147
148 // if QTypeInfo<T>::isRelocatable exists, use it
149 template <typename T>
150 struct QTypeInfoQuery<T, typename std::enable_if<QTypeInfo<T>::isRelocatable || true>::type> : public QTypeInfo<T>
151 {};
152
153 /*!
154 \class QTypeInfoMerger
155 \inmodule QtCore
156 \internal
157
158 \brief QTypeInfoMerger merges the QTypeInfo flags of T1, T2... and presents them
159 as a QTypeInfo<T> would do.
160
161 Let's assume that we have a simple set of structs:
162
163 \snippet code/src_corelib_global_qglobal.cpp 50
164
165 To create a proper QTypeInfo specialization for A struct, we have to check
166 all sub-components; B, C and D, then take the lowest common denominator and call
167 Q_DECLARE_TYPEINFO with the resulting flags. An easier and less fragile approach is to
168 use QTypeInfoMerger, which does that automatically. So struct A would have
169 the following QTypeInfo definition:
170
171 \snippet code/src_corelib_global_qglobal.cpp 51
172 */
173 template <class T, class T1, class T2 = T1, class T3 = T1, class T4 = T1>
174 class QTypeInfoMerger
175 {
176 public:
177 enum {
178 isSpecialized = true,
179 isComplex = QTypeInfoQuery<T1>::isComplex || QTypeInfoQuery<T2>::isComplex
180 || QTypeInfoQuery<T3>::isComplex || QTypeInfoQuery<T4>::isComplex,
181 isStatic = QTypeInfoQuery<T1>::isStatic || QTypeInfoQuery<T2>::isStatic
182 || QTypeInfoQuery<T3>::isStatic || QTypeInfoQuery<T4>::isStatic,
183 isRelocatable = QTypeInfoQuery<T1>::isRelocatable && QTypeInfoQuery<T2>::isRelocatable
184 && QTypeInfoQuery<T3>::isRelocatable && QTypeInfoQuery<T4>::isRelocatable,
185 isLarge = sizeof(T) > sizeof(void*),
186 isPointer = false,
187 isIntegral = false,
188 isDummy = false,
189 sizeOf = sizeof(T)
190 };
191 };
192
193 #define Q_DECLARE_MOVABLE_CONTAINER(CONTAINER) \
194 template <typename T> class CONTAINER; \
195 template <typename T> \
196 class QTypeInfo< CONTAINER<T> > \
197 { \
198 public: \
199 enum { \
200 isSpecialized = true, \
201 isPointer = false, \
202 isIntegral = false, \
203 isComplex = true, \
204 isRelocatable = true, \
205 isStatic = false, \
206 isLarge = (sizeof(CONTAINER<T>) > sizeof(void*)), \
207 isDummy = false, \
208 sizeOf = sizeof(CONTAINER<T>) \
209 }; \
210 }
211
212 Q_DECLARE_MOVABLE_CONTAINER(QList);
213 Q_DECLARE_MOVABLE_CONTAINER(QVector);
214 Q_DECLARE_MOVABLE_CONTAINER(QQueue);
215 Q_DECLARE_MOVABLE_CONTAINER(QStack);
216 Q_DECLARE_MOVABLE_CONTAINER(QSet);
217
218 #undef Q_DECLARE_MOVABLE_CONTAINER
219
220 /* These cannot be movable before ### Qt 6, for BC reasons */
221 #define Q_DECLARE_MOVABLE_CONTAINER(CONTAINER) \
222 template <typename K, typename V> class CONTAINER; \
223 template <typename K, typename V> \
224 class QTypeInfo< CONTAINER<K, V> > \
225 { \
226 public: \
227 enum { \
228 isSpecialized = true, \
229 isPointer = false, \
230 isIntegral = false, \
231 isComplex = true, \
232 isStatic = (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)), \
233 isRelocatable = true, \
234 isLarge = (sizeof(CONTAINER<K, V>) > sizeof(void*)), \
235 isDummy = false, \
236 sizeOf = sizeof(CONTAINER<K, V>) \
237 }; \
238 }
239
240 Q_DECLARE_MOVABLE_CONTAINER(QMap);
241 Q_DECLARE_MOVABLE_CONTAINER(QMultiMap);
242 Q_DECLARE_MOVABLE_CONTAINER(QHash);
243 Q_DECLARE_MOVABLE_CONTAINER(QMultiHash);
244
245 #undef Q_DECLARE_MOVABLE_CONTAINER
246
247 /*
248 Specialize a specific type with:
249
250 Q_DECLARE_TYPEINFO(type, flags);
251
252 where 'type' is the name of the type to specialize and 'flags' is
253 logically-OR'ed combination of the flags below.
254 */
255 enum { /* TYPEINFO flags */
256 Q_COMPLEX_TYPE = 0,
257 Q_PRIMITIVE_TYPE = 0x1,
258 Q_STATIC_TYPE = 0,
259 Q_MOVABLE_TYPE = 0x2, // ### Qt6: merge movable and relocatable once QList no longer depends on it
260 Q_DUMMY_TYPE = 0x4,
261 Q_RELOCATABLE_TYPE = 0x8
262 };
263
264 #define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS) \
265 class QTypeInfo<TYPE > \
266 { \
267 public: \
268 enum { \
269 isSpecialized = true, \
270 isComplex = (((FLAGS) & Q_PRIMITIVE_TYPE) == 0) && !qIsTrivial<TYPE>(), \
271 isStatic = (((FLAGS) & (Q_MOVABLE_TYPE | Q_PRIMITIVE_TYPE)) == 0), \
272 isRelocatable = !isStatic || ((FLAGS) & Q_RELOCATABLE_TYPE) || qIsRelocatable<TYPE>(), \
273 isLarge = (sizeof(TYPE)>sizeof(void*)), \
274 isPointer = false, \
275 isIntegral = std::is_integral< TYPE >::value, \
276 isDummy = (((FLAGS) & Q_DUMMY_TYPE) != 0), \
277 sizeOf = sizeof(TYPE) \
278 }; \
279 static inline const char *name() { return #TYPE; } \
280 }
281
282 #define Q_DECLARE_TYPEINFO(TYPE, FLAGS) \
283 template<> \
284 Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS)
285
286 /* Specialize QTypeInfo for QFlags<T> */
287 template<typename T> class QFlags;
288 template<typename T>
289 Q_DECLARE_TYPEINFO_BODY(QFlags<T>, Q_PRIMITIVE_TYPE);
290
291 /*
292 Specialize a shared type with:
293
294 Q_DECLARE_SHARED(type)
295
296 where 'type' is the name of the type to specialize. NOTE: shared
297 types must define a member-swap, and be defined in the same
298 namespace as Qt for this to work.
299
300 If the type was already released without Q_DECLARE_SHARED applied,
301 _and_ without an explicit Q_DECLARE_TYPEINFO(type, Q_MOVABLE_TYPE),
302 then use Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(type) to mark the
303 type shared (incl. swap()), without marking it movable (which
304 would change the memory layout of QList, a BiC change.
305 */
306
307 #define Q_DECLARE_SHARED_IMPL(TYPE, FLAGS) \
308 Q_DECLARE_TYPEINFO(TYPE, FLAGS); \
309 inline void swap(TYPE &value1, TYPE &value2) \
310 noexcept(noexcept(value1.swap(value2))) \
311 { value1.swap(value2); }
312 #define Q_DECLARE_SHARED(TYPE) Q_DECLARE_SHARED_IMPL(TYPE, Q_MOVABLE_TYPE)
313 #define Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(TYPE) \
314 Q_DECLARE_SHARED_IMPL(TYPE, QT_VERSION >= QT_VERSION_CHECK(6,0,0) ? Q_MOVABLE_TYPE : Q_RELOCATABLE_TYPE)
315
316 /*
317 QTypeInfo primitive specializations
318 */
319 Q_DECLARE_TYPEINFO(bool, Q_PRIMITIVE_TYPE);
320 Q_DECLARE_TYPEINFO(char, Q_PRIMITIVE_TYPE);
321 Q_DECLARE_TYPEINFO(signed char, Q_PRIMITIVE_TYPE);
322 Q_DECLARE_TYPEINFO(uchar, Q_PRIMITIVE_TYPE);
323 Q_DECLARE_TYPEINFO(short, Q_PRIMITIVE_TYPE);
324 Q_DECLARE_TYPEINFO(ushort, Q_PRIMITIVE_TYPE);
325 Q_DECLARE_TYPEINFO(int, Q_PRIMITIVE_TYPE);
326 Q_DECLARE_TYPEINFO(uint, Q_PRIMITIVE_TYPE);
327 Q_DECLARE_TYPEINFO(long, Q_PRIMITIVE_TYPE);
328 Q_DECLARE_TYPEINFO(ulong, Q_PRIMITIVE_TYPE);
329 Q_DECLARE_TYPEINFO(qint64, Q_PRIMITIVE_TYPE);
330 Q_DECLARE_TYPEINFO(quint64, Q_PRIMITIVE_TYPE);
331 Q_DECLARE_TYPEINFO(float, Q_PRIMITIVE_TYPE);
332 Q_DECLARE_TYPEINFO(double, Q_PRIMITIVE_TYPE);
333
334 #if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
335 // ### Qt 6: remove the other branch
336 // This was required so that QList<T> for these types allocates out of the array storage
337 Q_DECLARE_TYPEINFO(long double, Q_PRIMITIVE_TYPE);
338 # ifdef Q_COMPILER_UNICODE_STRINGS
339 Q_DECLARE_TYPEINFO(char16_t, Q_PRIMITIVE_TYPE);
340 Q_DECLARE_TYPEINFO(char32_t, Q_PRIMITIVE_TYPE);
341 # endif
342 # if !defined(Q_CC_MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
343 Q_DECLARE_TYPEINFO(wchar_t, Q_PRIMITIVE_TYPE);
344 # endif
345 #else
346 # ifndef Q_OS_DARWIN
347 Q_DECLARE_TYPEINFO(long double, Q_PRIMITIVE_TYPE);
348 # else
349 Q_DECLARE_TYPEINFO(long double, Q_RELOCATABLE_TYPE);
350 # endif
351 # ifdef Q_COMPILER_UNICODE_STRINGS
352 Q_DECLARE_TYPEINFO(char16_t, Q_RELOCATABLE_TYPE);
353 Q_DECLARE_TYPEINFO(char32_t, Q_RELOCATABLE_TYPE);
354 # endif
355 # if !defined(Q_CC_MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
356 Q_DECLARE_TYPEINFO(wchar_t, Q_RELOCATABLE_TYPE);
357 # endif
358 #endif // Qt 6
359
360 QT_END_NAMESPACE
361 #endif // QTYPEINFO_H
362