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
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
14
15class QDebug;
16
17/*
18 QTypeInfo - type trait functionality
19*/
20
21namespace 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
31template <typename T>
32inline constexpr bool qIsComplex =
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.
42template <typename 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.
51template <typename T>
52inline constexpr bool qIsValueInitializationBitwiseZero =
54
55}
56
57/*
58 The catch-all template.
59*/
60
61template <typename T>
63{
64public:
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
74template<>
75class QTypeInfo<void>
76{
77public:
78 enum {
79 isPointer [[deprecated("Use std::is_pointer instead")]] = false,
80 isIntegral [[deprecated("Use std::is_integral instead")]] = false,
81 isComplex = 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*/
107template <class T, class...Ts>
109{
110 static_assert(sizeof...(Ts) > 0);
111public:
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<>:
127template <class T1, class T2>
128class QTypeInfo<std::pair<T1, T2>> : public QTypeInfoMerger<std::pair<T1, T2>, T1, T2> {};
129
130#define Q_DECLARE_MOVABLE_CONTAINER(CONTAINER) template
131 <typename ...T> class
132 QTypeInfo<CONTAINER<T...>> \
133{public
134 :
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
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*/
164enum { /* TYPEINFO flags */
170};
171
172#define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS) class
173 QTypeInfo<TYPE > \
174{public
175 :
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) template
190 <> Q_DECLARE_TYPEINFO_BODY
191 (TYPE, FLAGS)
192
193/* Specialize QTypeInfo for QFlags<T> */
194template<typename T> class QFlags;
195template<typename T>
197
198QT_END_NAMESPACE
199#endif // QTYPEINFO_H
Definition qlist.h:80
\inmodule QtCore
Definition qtypeinfo.h:109
static constexpr bool isValueInitializationBitwiseZero
Definition qtypeinfo.h:116
static constexpr bool isIntegral
Definition qtypeinfo.h:115
static constexpr bool isPointer
Definition qtypeinfo.h:114
static constexpr bool isComplex
Definition qtypeinfo.h:112
static constexpr bool isRelocatable
Definition qtypeinfo.h:113
@ isValueInitializationBitwiseZero
Definition qtypeinfo.h:83
@ isValueInitializationBitwiseZero
Definition qtypeinfo.h:70
@ isRelocatable
Definition qtypeinfo.h:69
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)
constexpr bool qIsRelocatable
Definition qtypeinfo.h:43
constexpr bool qIsValueInitializationBitwiseZero
Definition qtypeinfo.h:52
constexpr bool qIsComplex
Definition qtypeinfo.h:32
Provider parseProvider(const QString &filename)
Definition provider.cpp:292
#define qPrintable(string)
Definition qstring.h:1685
#define QStringLiteral(str)
Definition qstring.h:1826
#define Q_DECLARE_MOVABLE_CONTAINER(CONTAINER)
Definition qtypeinfo.h:130
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:189
#define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS)
Definition qtypeinfo.h:172
@ Q_PRIMITIVE_TYPE
Definition qtypeinfo.h:166
@ Q_DUMMY_TYPE
Definition qtypeinfo.h:169
@ Q_RELOCATABLE_TYPE
Definition qtypeinfo.h:167
@ Q_COMPLEX_TYPE
Definition qtypeinfo.h:165
@ Q_MOVABLE_TYPE
Definition qtypeinfo.h:168
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