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
46QT_BEGIN_NAMESPACE
47
48/*
49 QTypeInfo - type trait functionality
50*/
51
52template <typename T>
53static 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
62template <typename T>
63static 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
76template <typename T>
77class QTypeInfo
78{
79public:
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
93template<>
94class QTypeInfo<void>
95{
96public:
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
110template <typename T>
111class QTypeInfo<T*>
112{
113public:
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
142template <typename T, typename = void>
143struct QTypeInfoQuery : public QTypeInfo<T>
144{
145 enum { isRelocatable = !QTypeInfo<T>::isStatic };
146};
147
148// if QTypeInfo<T>::isRelocatable exists, use it
149template <typename T>
150struct 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*/
173template <class T, class T1, class T2 = T1, class T3 = T1, class T4 = T1>
174class QTypeInfoMerger
175{
176public:
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) \
194template <typename T> class CONTAINER; \
195template <typename T> \
196class QTypeInfo< CONTAINER<T> > \
197{ \
198public: \
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
212Q_DECLARE_MOVABLE_CONTAINER(QList);
213Q_DECLARE_MOVABLE_CONTAINER(QVector);
214Q_DECLARE_MOVABLE_CONTAINER(QQueue);
215Q_DECLARE_MOVABLE_CONTAINER(QStack);
216Q_DECLARE_MOVABLE_CONTAINER(QLinkedList);
217Q_DECLARE_MOVABLE_CONTAINER(QSet);
218
219#undef Q_DECLARE_MOVABLE_CONTAINER
220
221/* These cannot be movable before ### Qt 6, for BC reasons */
222#define Q_DECLARE_MOVABLE_CONTAINER(CONTAINER) \
223template <typename K, typename V> class CONTAINER; \
224template <typename K, typename V> \
225class QTypeInfo< CONTAINER<K, V> > \
226{ \
227public: \
228 enum { \
229 isSpecialized = true, \
230 isPointer = false, \
231 isIntegral = false, \
232 isComplex = true, \
233 isStatic = (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)), \
234 isRelocatable = true, \
235 isLarge = (sizeof(CONTAINER<K, V>) > sizeof(void*)), \
236 isDummy = false, \
237 sizeOf = sizeof(CONTAINER<K, V>) \
238 }; \
239}
240
241Q_DECLARE_MOVABLE_CONTAINER(QMap);
242Q_DECLARE_MOVABLE_CONTAINER(QMultiMap);
243Q_DECLARE_MOVABLE_CONTAINER(QHash);
244Q_DECLARE_MOVABLE_CONTAINER(QMultiHash);
245
246#undef Q_DECLARE_MOVABLE_CONTAINER
247
248/*
249 Specialize a specific type with:
250
251 Q_DECLARE_TYPEINFO(type, flags);
252
253 where 'type' is the name of the type to specialize and 'flags' is
254 logically-OR'ed combination of the flags below.
255*/
256enum { /* TYPEINFO flags */
257 Q_COMPLEX_TYPE = 0,
258 Q_PRIMITIVE_TYPE = 0x1,
259 Q_STATIC_TYPE = 0,
260 Q_MOVABLE_TYPE = 0x2, // ### Qt6: merge movable and relocatable once QList no longer depends on it
261 Q_DUMMY_TYPE = 0x4,
262 Q_RELOCATABLE_TYPE = 0x8
263};
264
265#define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS) \
266class QTypeInfo<TYPE > \
267{ \
268public: \
269 enum { \
270 isSpecialized = true, \
271 isComplex = (((FLAGS) & Q_PRIMITIVE_TYPE) == 0) && !qIsTrivial<TYPE>(), \
272 isStatic = (((FLAGS) & (Q_MOVABLE_TYPE | Q_PRIMITIVE_TYPE)) == 0), \
273 isRelocatable = !isStatic || ((FLAGS) & Q_RELOCATABLE_TYPE) || qIsRelocatable<TYPE>(), \
274 isLarge = (sizeof(TYPE)>sizeof(void*)), \
275 isPointer = false, \
276 isIntegral = std::is_integral< TYPE >::value, \
277 isDummy = (((FLAGS) & Q_DUMMY_TYPE) != 0), \
278 sizeOf = sizeof(TYPE) \
279 }; \
280 static inline const char *name() { return #TYPE; } \
281}
282
283#define Q_DECLARE_TYPEINFO(TYPE, FLAGS) \
284template<> \
285Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS)
286
287/* Specialize QTypeInfo for QFlags<T> */
288template<typename T> class QFlags;
289template<typename T>
290Q_DECLARE_TYPEINFO_BODY(QFlags<T>, Q_PRIMITIVE_TYPE);
291
292/*
293 Specialize a shared type with:
294
295 Q_DECLARE_SHARED(type)
296
297 where 'type' is the name of the type to specialize. NOTE: shared
298 types must define a member-swap, and be defined in the same
299 namespace as Qt for this to work.
300
301 If the type was already released without Q_DECLARE_SHARED applied,
302 _and_ without an explicit Q_DECLARE_TYPEINFO(type, Q_MOVABLE_TYPE),
303 then use Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(type) to mark the
304 type shared (incl. swap()), without marking it movable (which
305 would change the memory layout of QList, a BiC change.
306*/
307
308#define Q_DECLARE_SHARED_IMPL(TYPE, FLAGS) \
309Q_DECLARE_TYPEINFO(TYPE, FLAGS); \
310inline void swap(TYPE &value1, TYPE &value2) \
311 Q_DECL_NOEXCEPT_EXPR(noexcept(value1.swap(value2))) \
312{ value1.swap(value2); }
313#define Q_DECLARE_SHARED(TYPE) Q_DECLARE_SHARED_IMPL(TYPE, Q_MOVABLE_TYPE)
314#define Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(TYPE) \
315 Q_DECLARE_SHARED_IMPL(TYPE, QT_VERSION >= QT_VERSION_CHECK(6,0,0) ? Q_MOVABLE_TYPE : Q_RELOCATABLE_TYPE)
316
317/*
318 QTypeInfo primitive specializations
319*/
320Q_DECLARE_TYPEINFO(bool, Q_PRIMITIVE_TYPE);
321Q_DECLARE_TYPEINFO(char, Q_PRIMITIVE_TYPE);
322Q_DECLARE_TYPEINFO(signed char, Q_PRIMITIVE_TYPE);
323Q_DECLARE_TYPEINFO(uchar, Q_PRIMITIVE_TYPE);
324Q_DECLARE_TYPEINFO(short, Q_PRIMITIVE_TYPE);
325Q_DECLARE_TYPEINFO(ushort, Q_PRIMITIVE_TYPE);
326Q_DECLARE_TYPEINFO(int, Q_PRIMITIVE_TYPE);
327Q_DECLARE_TYPEINFO(uint, Q_PRIMITIVE_TYPE);
328Q_DECLARE_TYPEINFO(long, Q_PRIMITIVE_TYPE);
329Q_DECLARE_TYPEINFO(ulong, Q_PRIMITIVE_TYPE);
330Q_DECLARE_TYPEINFO(qint64, Q_PRIMITIVE_TYPE);
331Q_DECLARE_TYPEINFO(quint64, Q_PRIMITIVE_TYPE);
332Q_DECLARE_TYPEINFO(float, Q_PRIMITIVE_TYPE);
333Q_DECLARE_TYPEINFO(double, Q_PRIMITIVE_TYPE);
334
335#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
336// ### Qt 6: remove the other branch
337// This was required so that QList<T> for these types allocates out of the array storage
338Q_DECLARE_TYPEINFO(long double, Q_PRIMITIVE_TYPE);
339# ifdef Q_COMPILER_UNICODE_STRINGS
340Q_DECLARE_TYPEINFO(char16_t, Q_PRIMITIVE_TYPE);
341Q_DECLARE_TYPEINFO(char32_t, Q_PRIMITIVE_TYPE);
342# endif
343# if !defined(Q_CC_MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
344Q_DECLARE_TYPEINFO(wchar_t, Q_PRIMITIVE_TYPE);
345# endif
346#else
347# ifndef Q_OS_DARWIN
348Q_DECLARE_TYPEINFO(long double, Q_PRIMITIVE_TYPE);
349# else
350Q_DECLARE_TYPEINFO(long double, Q_RELOCATABLE_TYPE);
351# endif
352# ifdef Q_COMPILER_UNICODE_STRINGS
353Q_DECLARE_TYPEINFO(char16_t, Q_RELOCATABLE_TYPE);
354Q_DECLARE_TYPEINFO(char32_t, Q_RELOCATABLE_TYPE);
355# endif
356# if !defined(Q_CC_MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
357Q_DECLARE_TYPEINFO(wchar_t, Q_RELOCATABLE_TYPE);
358# endif
359#endif // Qt 6
360
361QT_END_NAMESPACE
362#endif // QTYPEINFO_H
363