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
46// Denotes types that are trivially default constructible, and for which
47// value-initialization can be achieved by filling their storage with 0 bits.
48// There is no type trait we can use for this, so we hardcode a list of
49// possibilities that we know are OK on the architectures that we support.
50// The most notable exception are pointers to data members, which for instance
51// on the Itanium ABI are initialized to -1.
52template <typename T>
53inline constexpr bool qIsValueInitializationBitwiseZero =
55
56}
57
58/*
59 The catch-all template.
60*/
61
62template <typename T>
64{
65public:
66 enum {
67 isPointer [[deprecated("Use std::is_pointer instead")]] = std::is_pointer_v<T>,
68 isIntegral [[deprecated("Use std::is_integral instead")]] = std::is_integral_v<T>,
69 isComplex = QtPrivate::qIsComplex<T>,
70 isRelocatable = QtPrivate::qIsRelocatable<T>,
71 isValueInitializationBitwiseZero = QtPrivate::qIsValueInitializationBitwiseZero<T>,
72 };
73};
74
75template<>
76class QTypeInfo<void>
77{
78public:
79 enum {
80 isPointer [[deprecated("Use std::is_pointer instead")]] = false,
81 isIntegral [[deprecated("Use std::is_integral instead")]] = false,
82 isComplex = false,
85 };
86};
87
88/*!
89 \class QTypeInfoMerger
90 \inmodule QtCore
91 \internal
92
93 \brief QTypeInfoMerger merges the QTypeInfo flags of T1, T2... and presents them
94 as a QTypeInfo<T> would do.
95
96 Let's assume that we have a simple set of structs:
97
98 \snippet code/src_corelib_global_qglobal.cpp 50
99
100 To create a proper QTypeInfo specialization for A struct, we have to check
101 all sub-components; B, C and D, then take the lowest common denominator and call
102 Q_DECLARE_TYPEINFO with the resulting flags. An easier and less fragile approach is to
103 use QTypeInfoMerger, which does that automatically. So struct A would have
104 the following QTypeInfo definition:
105
106 \snippet code/src_corelib_global_qglobal.cpp 51
107*/
108template <class T, class...Ts>
110{
111 static_assert(sizeof...(Ts) > 0);
112public:
113 static constexpr bool isComplex = ((QTypeInfo<Ts>::isComplex) || ...);
114 static constexpr bool isRelocatable = ((QTypeInfo<Ts>::isRelocatable) && ...);
115 [[deprecated("Use std::is_pointer instead")]] static constexpr bool isPointer = false;
116 [[deprecated("Use std::is_integral instead")]] static constexpr bool isIntegral = false;
117 static constexpr bool isValueInitializationBitwiseZero = false;
118 static_assert(!isRelocatable ||
119 std::is_copy_constructible_v<T> ||
120 std::is_move_constructible_v<T>,
121 "All Ts... are Q_RELOCATABLE_TYPE, but T is neither copy- nor move-constructible, "
122 "so cannot be Q_RELOCATABLE_TYPE. Please mark T as Q_COMPLEX_TYPE manually.");
123};
124
125// QTypeInfo for std::pair:
126// std::pair is spec'ed to be struct { T1 first; T2 second; }, so, unlike tuple<>,
127// we _can_ specialize QTypeInfo for pair<>:
128template <class T1, class T2>
129class QTypeInfo<std::pair<T1, T2>> : public QTypeInfoMerger<std::pair<T1, T2>, T1, T2> {};
130
131#define Q_DECLARE_MOVABLE_CONTAINER(CONTAINER) template
132 <typename ...T> class
133 QTypeInfo<CONTAINER<T...>> \
134{public
135 :
136 enum {
137 isPointer [[deprecated("Use std::is_pointer instead")]] = false,
138 isIntegral [[deprecated("Use std::is_integral instead")]] = false,
139 isComplex = true,
140 isRelocatable = true,
141 isValueInitializationBitwiseZero = false,
142 }; \
143}
144
154
155#undef Q_DECLARE_MOVABLE_CONTAINER
156
157/*
158 Specialize a specific type with:
159
160 Q_DECLARE_TYPEINFO(type, flags);
161
162 where 'type' is the name of the type to specialize and 'flags' is
163 logically-OR'ed combination of the flags below.
164*/
165enum { /* TYPEINFO flags */
171};
172
173#define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS) class
174 QTypeInfo<TYPE > \
175{public
176 :
177 enum {
178 isComplex = (((FLAGS) & Q_PRIMITIVE_TYPE) == 0) && QtPrivate::qIsComplex<TYPE>,
179 isRelocatable = !isComplex || ((FLAGS) & Q_RELOCATABLE_TYPE) || QtPrivate::qIsRelocatable<TYPE>,
180 isPointer [[deprecated("Use std::is_pointer instead")]] = std::is_pointer_v< TYPE >,
181 isIntegral [[deprecated("Use std::is_integral instead")]] = std::is_integral< TYPE >::value,
182 isValueInitializationBitwiseZero = QtPrivate::qIsValueInitializationBitwiseZero<TYPE>,
183 };
184 static_assert(!QTypeInfo<TYPE>::isRelocatable ||
185 std::is_copy_constructible_v<TYPE > ||
186 std::is_move_constructible_v<TYPE >,
187 #TYPE " is neither copy- nor move-constructible, so cannot be Q_RELOCATABLE_TYPE"); \
188}
189
190#define Q_DECLARE_TYPEINFO(TYPE, FLAGS) template
191 <> Q_DECLARE_TYPEINFO_BODY
192 (TYPE, FLAGS)
193
194/* Specialize QTypeInfo for QFlags<T> */
195template<typename T> class QFlags;
196template<typename T>
198
199QT_END_NAMESPACE
200#endif // QTYPEINFO_H
Definition qlist.h:81
\inmodule QtCore
Definition qtypeinfo.h:110
static constexpr bool isValueInitializationBitwiseZero
Definition qtypeinfo.h:117
static constexpr bool isIntegral
Definition qtypeinfo.h:116
static constexpr bool isPointer
Definition qtypeinfo.h:115
static constexpr bool isComplex
Definition qtypeinfo.h:113
static constexpr bool isRelocatable
Definition qtypeinfo.h:114
@ isValueInitializationBitwiseZero
Definition qtypeinfo.h:84
@ isValueInitializationBitwiseZero
Definition qtypeinfo.h:71
@ isRelocatable
Definition qtypeinfo.h:70
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 qIsValueInitializationBitwiseZero
Definition qtypeinfo.h:53
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:131
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:190
#define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS)
Definition qtypeinfo.h:173
@ Q_PRIMITIVE_TYPE
Definition qtypeinfo.h:167
@ Q_DUMMY_TYPE
Definition qtypeinfo.h:170
@ Q_RELOCATABLE_TYPE
Definition qtypeinfo.h:168
@ Q_COMPLEX_TYPE
Definition qtypeinfo.h:166
@ Q_MOVABLE_TYPE
Definition qtypeinfo.h:169
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