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
52/*
53 The catch-all template.
54*/
55
56template <typename T>
57class QTypeInfo
58{
59public:
60 enum {
61 isSpecialized = std::is_enum<T>::value, // don't require every enum to be marked manually
62 isPointer = false,
63 isIntegral = std::is_integral<T>::value,
64 isComplex = !isIntegral && !std::is_enum<T>::value,
65 isStatic = true,
66 isRelocatable = std::is_enum<T>::value,
67 isLarge = (sizeof(T)>sizeof(void*)),
68 isDummy = false, //### Qt6: remove
69 sizeOf = sizeof(T)
70 };
71};
72
73template<>
74class QTypeInfo<void>
75{
76public:
77 enum {
78 isSpecialized = true,
79 isPointer = false,
80 isIntegral = false,
81 isComplex = false,
82 isStatic = false,
83 isRelocatable = false,
84 isLarge = false,
85 isDummy = false,
86 sizeOf = 0
87 };
88};
89
90template <typename T>
91class QTypeInfo<T*>
92{
93public:
94 enum {
95 isSpecialized = true,
96 isPointer = true,
97 isIntegral = false,
98 isComplex = false,
99 isStatic = false,
100 isRelocatable = true,
101 isLarge = false,
102 isDummy = false,
103 sizeOf = sizeof(T*)
104 };
105};
106
107/*!
108 \class QTypeInfoQuery
109 \inmodule QtCore
110 \internal
111 \brief QTypeInfoQuery is used to query the values of a given QTypeInfo<T>
112
113 We use it because there may be some QTypeInfo<T> specializations in user
114 code that don't provide certain flags that we added after Qt 5.0. They are:
115 \list
116 \li isRelocatable: defaults to !isStatic
117 \endlist
118
119 DO NOT specialize this class elsewhere.
120*/
121// apply defaults for a generic QTypeInfo<T> that didn't provide the new values
122template <typename T, typename = void>
123struct QTypeInfoQuery : public QTypeInfo<T>
124{
125 enum { isRelocatable = !QTypeInfo<T>::isStatic };
126};
127
128// if QTypeInfo<T>::isRelocatable exists, use it
129template <typename T>
130struct QTypeInfoQuery<T, typename std::enable_if<QTypeInfo<T>::isRelocatable || true>::type> : public QTypeInfo<T>
131{};
132
133/*!
134 \class QTypeInfoMerger
135 \inmodule QtCore
136 \internal
137
138 \brief QTypeInfoMerger merges the QTypeInfo flags of T1, T2... and presents them
139 as a QTypeInfo<T> would do.
140
141 Let's assume that we have a simple set of structs:
142
143 \snippet code/src_corelib_global_qglobal.cpp 50
144
145 To create a proper QTypeInfo specialization for A struct, we have to check
146 all sub-components; B, C and D, then take the lowest common denominator and call
147 Q_DECLARE_TYPEINFO with the resulting flags. An easier and less fragile approach is to
148 use QTypeInfoMerger, which does that automatically. So struct A would have
149 the following QTypeInfo definition:
150
151 \snippet code/src_corelib_global_qglobal.cpp 51
152*/
153template <class T, class T1, class T2 = T1, class T3 = T1, class T4 = T1>
154class QTypeInfoMerger
155{
156public:
157 enum {
158 isSpecialized = true,
159 isComplex = QTypeInfoQuery<T1>::isComplex || QTypeInfoQuery<T2>::isComplex
160 || QTypeInfoQuery<T3>::isComplex || QTypeInfoQuery<T4>::isComplex,
161 isStatic = QTypeInfoQuery<T1>::isStatic || QTypeInfoQuery<T2>::isStatic
162 || QTypeInfoQuery<T3>::isStatic || QTypeInfoQuery<T4>::isStatic,
163 isRelocatable = QTypeInfoQuery<T1>::isRelocatable && QTypeInfoQuery<T2>::isRelocatable
164 && QTypeInfoQuery<T3>::isRelocatable && QTypeInfoQuery<T4>::isRelocatable,
165 isLarge = sizeof(T) > sizeof(void*),
166 isPointer = false,
167 isIntegral = false,
168 isDummy = false,
169 sizeOf = sizeof(T)
170 };
171};
172
173#define Q_DECLARE_MOVABLE_CONTAINER(CONTAINER) \
174template <typename T> class CONTAINER; \
175template <typename T> \
176class QTypeInfo< CONTAINER<T> > \
177{ \
178public: \
179 enum { \
180 isSpecialized = true, \
181 isPointer = false, \
182 isIntegral = false, \
183 isComplex = true, \
184 isRelocatable = true, \
185 isStatic = false, \
186 isLarge = (sizeof(CONTAINER<T>) > sizeof(void*)), \
187 isDummy = false, \
188 sizeOf = sizeof(CONTAINER<T>) \
189 }; \
190}
191
192Q_DECLARE_MOVABLE_CONTAINER(QList);
193Q_DECLARE_MOVABLE_CONTAINER(QVector);
194Q_DECLARE_MOVABLE_CONTAINER(QQueue);
195Q_DECLARE_MOVABLE_CONTAINER(QStack);
196Q_DECLARE_MOVABLE_CONTAINER(QLinkedList);
197Q_DECLARE_MOVABLE_CONTAINER(QSet);
198
199#undef Q_DECLARE_MOVABLE_CONTAINER
200
201/* These cannot be movable before ### Qt 6, for BC reasons */
202#define Q_DECLARE_MOVABLE_CONTAINER(CONTAINER) \
203template <typename K, typename V> class CONTAINER; \
204template <typename K, typename V> \
205class QTypeInfo< CONTAINER<K, V> > \
206{ \
207public: \
208 enum { \
209 isSpecialized = true, \
210 isPointer = false, \
211 isIntegral = false, \
212 isComplex = true, \
213 isStatic = (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)), \
214 isRelocatable = true, \
215 isLarge = (sizeof(CONTAINER<K, V>) > sizeof(void*)), \
216 isDummy = false, \
217 sizeOf = sizeof(CONTAINER<K, V>) \
218 }; \
219}
220
221Q_DECLARE_MOVABLE_CONTAINER(QMap);
222Q_DECLARE_MOVABLE_CONTAINER(QMultiMap);
223Q_DECLARE_MOVABLE_CONTAINER(QHash);
224Q_DECLARE_MOVABLE_CONTAINER(QMultiHash);
225
226#undef Q_DECLARE_MOVABLE_CONTAINER
227
228/*
229 Specialize a specific type with:
230
231 Q_DECLARE_TYPEINFO(type, flags);
232
233 where 'type' is the name of the type to specialize and 'flags' is
234 logically-OR'ed combination of the flags below.
235*/
236enum { /* TYPEINFO flags */
237 Q_COMPLEX_TYPE = 0,
238 Q_PRIMITIVE_TYPE = 0x1,
239 Q_STATIC_TYPE = 0,
240 Q_MOVABLE_TYPE = 0x2, // ### Qt6: merge movable and relocatable once QList no longer depends on it
241 Q_DUMMY_TYPE = 0x4,
242 Q_RELOCATABLE_TYPE = 0x8
243};
244
245#define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS) \
246class QTypeInfo<TYPE > \
247{ \
248public: \
249 enum { \
250 isSpecialized = true, \
251 isComplex = (((FLAGS) & Q_PRIMITIVE_TYPE) == 0), \
252 isStatic = (((FLAGS) & (Q_MOVABLE_TYPE | Q_PRIMITIVE_TYPE)) == 0), \
253 isRelocatable = !isStatic || ((FLAGS) & Q_RELOCATABLE_TYPE), \
254 isLarge = (sizeof(TYPE)>sizeof(void*)), \
255 isPointer = false, \
256 isIntegral = std::is_integral< TYPE >::value, \
257 isDummy = (((FLAGS) & Q_DUMMY_TYPE) != 0), \
258 sizeOf = sizeof(TYPE) \
259 }; \
260 static inline const char *name() { return #TYPE; } \
261}
262
263#define Q_DECLARE_TYPEINFO(TYPE, FLAGS) \
264template<> \
265Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS)
266
267/* Specialize QTypeInfo for QFlags<T> */
268template<typename T> class QFlags;
269template<typename T>
270Q_DECLARE_TYPEINFO_BODY(QFlags<T>, Q_PRIMITIVE_TYPE);
271
272/*
273 Specialize a shared type with:
274
275 Q_DECLARE_SHARED(type)
276
277 where 'type' is the name of the type to specialize. NOTE: shared
278 types must define a member-swap, and be defined in the same
279 namespace as Qt for this to work.
280
281 If the type was already released without Q_DECLARE_SHARED applied,
282 _and_ without an explicit Q_DECLARE_TYPEINFO(type, Q_MOVABLE_TYPE),
283 then use Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(type) to mark the
284 type shared (incl. swap()), without marking it movable (which
285 would change the memory layout of QList, a BiC change.
286*/
287
288#define Q_DECLARE_SHARED_IMPL(TYPE, FLAGS) \
289Q_DECLARE_TYPEINFO(TYPE, FLAGS); \
290inline void swap(TYPE &value1, TYPE &value2) \
291 Q_DECL_NOEXCEPT_EXPR(noexcept(value1.swap(value2))) \
292{ value1.swap(value2); }
293#define Q_DECLARE_SHARED(TYPE) Q_DECLARE_SHARED_IMPL(TYPE, Q_MOVABLE_TYPE)
294#define Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(TYPE) \
295 Q_DECLARE_SHARED_IMPL(TYPE, QT_VERSION >= QT_VERSION_CHECK(6,0,0) ? Q_MOVABLE_TYPE : Q_RELOCATABLE_TYPE)
296
297/*
298 QTypeInfo primitive specializations
299*/
300Q_DECLARE_TYPEINFO(bool, Q_PRIMITIVE_TYPE);
301Q_DECLARE_TYPEINFO(char, Q_PRIMITIVE_TYPE);
302Q_DECLARE_TYPEINFO(signed char, Q_PRIMITIVE_TYPE);
303Q_DECLARE_TYPEINFO(uchar, Q_PRIMITIVE_TYPE);
304Q_DECLARE_TYPEINFO(short, Q_PRIMITIVE_TYPE);
305Q_DECLARE_TYPEINFO(ushort, Q_PRIMITIVE_TYPE);
306Q_DECLARE_TYPEINFO(int, Q_PRIMITIVE_TYPE);
307Q_DECLARE_TYPEINFO(uint, Q_PRIMITIVE_TYPE);
308Q_DECLARE_TYPEINFO(long, Q_PRIMITIVE_TYPE);
309Q_DECLARE_TYPEINFO(ulong, Q_PRIMITIVE_TYPE);
310Q_DECLARE_TYPEINFO(qint64, Q_PRIMITIVE_TYPE);
311Q_DECLARE_TYPEINFO(quint64, Q_PRIMITIVE_TYPE);
312Q_DECLARE_TYPEINFO(float, Q_PRIMITIVE_TYPE);
313Q_DECLARE_TYPEINFO(double, Q_PRIMITIVE_TYPE);
314
315#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
316// ### Qt 6: remove the other branch
317// This was required so that QList<T> for these types allocates out of the array storage
318Q_DECLARE_TYPEINFO(long double, Q_PRIMITIVE_TYPE);
319# ifdef Q_COMPILER_UNICODE_STRINGS
320Q_DECLARE_TYPEINFO(char16_t, Q_PRIMITIVE_TYPE);
321Q_DECLARE_TYPEINFO(char32_t, Q_PRIMITIVE_TYPE);
322# endif
323# if !defined(Q_CC_MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
324Q_DECLARE_TYPEINFO(wchar_t, Q_PRIMITIVE_TYPE);
325# endif
326#else
327# ifndef Q_OS_DARWIN
328Q_DECLARE_TYPEINFO(long double, Q_PRIMITIVE_TYPE);
329# else
330Q_DECLARE_TYPEINFO(long double, Q_RELOCATABLE_TYPE);
331# endif
332# ifdef Q_COMPILER_UNICODE_STRINGS
333Q_DECLARE_TYPEINFO(char16_t, Q_RELOCATABLE_TYPE);
334Q_DECLARE_TYPEINFO(char32_t, Q_RELOCATABLE_TYPE);
335# endif
336# if !defined(Q_CC_MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
337Q_DECLARE_TYPEINFO(wchar_t, Q_RELOCATABLE_TYPE);
338# endif
339#endif // Qt 6
340
341QT_END_NAMESPACE
342#endif // QTYPEINFO_H
343