Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtypeinfo.h
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
5
6#ifndef QTYPEINFO_H
7#define QTYPEINFO_H
8
9#include <QtCore/qcompilerdetection.h>
10#include <QtCore/qcontainerfwd.h>
11
12#include <type_traits>
13
15
16class QDebug;
17
18/*
19 QTypeInfo - type trait functionality
20*/
21
22namespace QtPrivate {
23
24// Helper for QTypeInfo<T>::isComplex, which used to be simply
25// !std::is_trivial_v but P3247 deprecated it for C++26. It used to be defined
26// (since C++11) by [class]/7 as: "A trivial class is a class that is trivially
27// copyable and has one or more default constructors, all of which are either
28// trivial or deleted and at least one of which is not deleted. [ Note: In
29// particular, a trivially copyable or trivial class does not have virtual
30// functions or virtual base classes. — end note ]".
31
32template <typename T>
33inline constexpr bool qIsComplex =
35
36// A trivially copyable class must also have a trivial, non-deleted
37// destructor [class.prop/1.3], CWG1734. Some implementations don't
38// check for a trivial destructor, because of backwards compatibility
39// with C++98's definition of trivial copyability.
40// Since trivial copiability has implications for the ABI, implementations
41// can't "just fix" their traits. So, although formally redundant, we
42// explicitly check for trivial destruction here.
43template <typename T>
45
46template <typename T> inline constexpr bool qIsQtRelocatableContainer = false;
47
48// Denotes types that are trivially default constructible, and for which
49// value-initialization can be achieved by filling their storage with 0 bits.
50// There is no type trait we can use for this, so we hardcode a list of
51// possibilities that we know are OK on the architectures that we support.
52// The most notable exception are pointers to data members, which for instance
53// on the Itanium ABI are initialized to -1.
54template <typename T>
55inline constexpr bool qIsValueInitializationBitwiseZero =
57
58}
59
60/*
61 The catch-all template.
62*/
63
64template <typename T>
66{
67public:
68 enum {
69 isPointer [[deprecated("Use std::is_pointer instead")]] = std::is_pointer_v<T>,
70 isIntegral [[deprecated("Use std::is_integral instead")]] = std::is_integral_v<T>,
71 isComplex = QtPrivate::qIsComplex<T>,
72 isRelocatable = QtPrivate::qIsRelocatable<T>,
73 isValueInitializationBitwiseZero = QtPrivate::qIsValueInitializationBitwiseZero<T>,
74 };
75};
76
77template<>
78class QTypeInfo<void>
79{
80public:
81 enum {
82 isPointer [[deprecated("Use std::is_pointer instead")]] = false,
83 isIntegral [[deprecated("Use std::is_integral instead")]] = false,
84 isComplex = false,
87 };
88};
89
90/*!
91 \class QTypeInfoMerger
92 \inmodule QtCore
93 \internal
94
95 \brief QTypeInfoMerger merges the QTypeInfo flags of T1, T2... and presents them
96 as a QTypeInfo<T> would do.
97
98 Let's assume that we have a simple set of structs:
99
100 \snippet code/src_corelib_global_qglobal.cpp 50
101
102 To create a proper QTypeInfo specialization for A struct, we have to check
103 all sub-components; B, C and D, then take the lowest common denominator and call
104 Q_DECLARE_TYPEINFO with the resulting flags. An easier and less fragile approach is to
105 use QTypeInfoMerger, which does that automatically. So struct A would have
106 the following QTypeInfo definition:
107
108 \snippet code/src_corelib_global_qglobal.cpp 51
109*/
110template <class T, class...Ts>
112{
113 static_assert(sizeof...(Ts) > 0);
114public:
115 static constexpr bool isComplex = ((QTypeInfo<Ts>::isComplex) || ...);
116 static constexpr bool isRelocatable = ((QTypeInfo<Ts>::isRelocatable) && ...);
117 [[deprecated("Use std::is_pointer instead")]] static constexpr bool isPointer = false;
118 [[deprecated("Use std::is_integral instead")]] static constexpr bool isIntegral = false;
119 static constexpr bool isValueInitializationBitwiseZero = false;
120 static_assert(!isRelocatable ||
121 std::is_copy_constructible_v<T> ||
122 std::is_move_constructible_v<T>,
123 "All Ts... are Q_RELOCATABLE_TYPE, but T is neither copy- nor move-constructible, "
124 "so cannot be Q_RELOCATABLE_TYPE. Please mark T as Q_COMPLEX_TYPE manually.");
125};
126
127// QTypeInfo for std::pair:
128// std::pair is spec'ed to be struct { T1 first; T2 second; }, so, unlike tuple<>,
129// we _can_ specialize QTypeInfo for pair<>:
130template <class T1, class T2>
131class QTypeInfo<std::pair<T1, T2>> : public QTypeInfoMerger<std::pair<T1, T2>, T1, T2> {};
132
133#define Q_DECLARE_MOVABLE_CONTAINER(CONTAINER) namespace
134 QtPrivate { template
135 <typename ...T> constexpr bool qIsQtRelocatableContainer<CONTAINER<T...>> = true; \
136}template
137 <typename ...T> class
138 QTypeInfo<CONTAINER<T...>> \
139{public
140 :
141 enum {
142 isPointer [[deprecated("Use std::is_pointer instead")]] = false,
143 isIntegral [[deprecated("Use std::is_integral instead")]] = false,
144 isComplex = true,
145 isRelocatable = true,
146 isValueInitializationBitwiseZero = false,
147 }; \
148}
149
159// if you add a new container here after a .0 release, see qvariant.h for
160// QVariant::Private::hasAlwaysBeenAbleToUseInternalSpace()
161
162#undef Q_DECLARE_MOVABLE_CONTAINER
163
164/*
165 Specialize a specific type with:
166
167 Q_DECLARE_TYPEINFO(type, flags);
168
169 where 'type' is the name of the type to specialize and 'flags' is
170 logically-OR'ed combination of the flags below.
171*/
172enum { /* TYPEINFO flags */
178};
179
180#define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS) class
181 QTypeInfo<TYPE > \
182{public
183 :
184 enum {
185 isComplex = (((FLAGS) & Q_PRIMITIVE_TYPE) == 0) && QtPrivate::qIsComplex<TYPE>,
186 isRelocatable = !isComplex || ((FLAGS) & Q_RELOCATABLE_TYPE) || QtPrivate::qIsRelocatable<TYPE>,
187 isPointer [[deprecated("Use std::is_pointer instead")]] = std::is_pointer_v< TYPE >,
188 isIntegral [[deprecated("Use std::is_integral instead")]] = std::is_integral< TYPE >::value,
189 isValueInitializationBitwiseZero = QtPrivate::qIsValueInitializationBitwiseZero<TYPE>,
190 };
191 static_assert(!QTypeInfo<TYPE>::isRelocatable ||
192 std::is_copy_constructible_v<TYPE > ||
193 std::is_move_constructible_v<TYPE >,
194 #TYPE " is neither copy- nor move-constructible, so cannot be Q_RELOCATABLE_TYPE"); \
195}
196
197#define Q_DECLARE_TYPEINFO(TYPE, FLAGS) template
198 <> Q_DECLARE_TYPEINFO_BODY
199 (TYPE, FLAGS)
200
201/* Specialize QTypeInfo for QFlags<T> */
202template<typename T> class QFlags;
203template<typename T>
205
206QT_END_NAMESPACE
207#endif // QTYPEINFO_H
Definition qlist.h:81
\inmodule QtCore
Definition qtypeinfo.h:112
static constexpr bool isValueInitializationBitwiseZero
Definition qtypeinfo.h:119
static constexpr bool isIntegral
Definition qtypeinfo.h:118
static constexpr bool isPointer
Definition qtypeinfo.h:117
static constexpr bool isComplex
Definition qtypeinfo.h:115
static constexpr bool isRelocatable
Definition qtypeinfo.h:116
@ isValueInitializationBitwiseZero
Definition qtypeinfo.h:86
@ isValueInitializationBitwiseZero
Definition qtypeinfo.h:73
@ isRelocatable
Definition qtypeinfo.h:72
static void writeMetadataGenerators(QTextStream &stream)
Definition ctf.cpp:93
static void writeEpilogue(QTextStream &stream, const QString &fileName)
Definition ctf.cpp:49
static void writeWrapper(QTextStream &stream, const Tracepoint &tracepoint, const Provider &provider)
Definition ctf.cpp:56
static void writePrologue(QTextStream &stream, const QString &fileName, const Provider &provider)
Definition ctf.cpp:16
void writeCtf(QFile &device, const Provider &p)
Definition ctf.cpp:292
QT_FORWARD_DECLARE_CLASS(QTextStream)
Combined button and popup list for selecting options.
constexpr bool qIsRelocatable
Definition qtypeinfo.h:44
constexpr bool qIsQtRelocatableContainer
Definition qtypeinfo.h:46
constexpr bool qIsValueInitializationBitwiseZero
Definition qtypeinfo.h:55
constexpr bool qIsComplex
Definition qtypeinfo.h:33
Provider parseProvider(const QString &filename)
Definition provider.cpp:292
#define qPrintable(string)
Definition qstring.h:1683
#define QStringLiteral(str)
Definition qstring.h:1825
#define Q_DECLARE_MOVABLE_CONTAINER(CONTAINER)
Definition qtypeinfo.h:133
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:197
#define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS)
Definition qtypeinfo.h:180
@ Q_PRIMITIVE_TYPE
Definition qtypeinfo.h:174
@ Q_DUMMY_TYPE
Definition qtypeinfo.h:177
@ Q_RELOCATABLE_TYPE
Definition qtypeinfo.h:175
@ Q_COMPLEX_TYPE
Definition qtypeinfo.h:173
@ Q_MOVABLE_TYPE
Definition qtypeinfo.h:176
QStringList prefixText
Definition provider.h:85
QList< TraceEnum > enumerations
Definition provider.h:86
QList< TraceFlags > flags
Definition provider.h:87
QString name
Definition provider.h:83
QList< Tracepoint > tracepoints
Definition provider.h:84
int valueSize
Definition provider.h:63
QString name
Definition provider.h:56
QList< EnumValue > values
Definition provider.h:62
QString name
Definition provider.h:67
QList< FlagValue > values
Definition provider.h:72