1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29// Used by both tst_qmetatype and tst_qsettings
30
31#ifndef TST_QMETATYPE_H
32#define TST_QMETATYPE_H
33
34#include <qmetatype.h>
35#include <float.h>
36
37#define FOR_EACH_PRIMITIVE_METATYPE(F) \
38 QT_FOR_EACH_STATIC_PRIMITIVE_TYPE(F) \
39 QT_FOR_EACH_STATIC_CORE_POINTER(F) \
40
41#define FOR_EACH_COMPLEX_CORE_METATYPE(F) \
42 QT_FOR_EACH_STATIC_CORE_CLASS(F) \
43 QT_FOR_EACH_STATIC_CORE_TEMPLATE(F)
44
45#define FOR_EACH_CORE_METATYPE(F) \
46 FOR_EACH_PRIMITIVE_METATYPE(F) \
47 FOR_EACH_COMPLEX_CORE_METATYPE(F) \
48
49template <int ID>
50struct MetaEnumToType {};
51
52#define DEFINE_META_ENUM_TO_TYPE(MetaTypeName, MetaTypeId, RealType) \
53template<> \
54struct MetaEnumToType<QMetaType::MetaTypeName> { \
55 typedef RealType Type; \
56};
57FOR_EACH_CORE_METATYPE(DEFINE_META_ENUM_TO_TYPE)
58#undef DEFINE_META_ENUM_TO_TYPE
59
60template <int ID>
61struct DefaultValueFactory
62{
63 typedef typename MetaEnumToType<ID>::Type Type;
64 static Type *create() { return new Type; }
65};
66
67template <>
68struct DefaultValueFactory<QMetaType::Void>
69{
70 typedef MetaEnumToType<QMetaType::Void>::Type Type;
71 static Type *create() { return 0; }
72};
73
74template <int ID>
75struct DefaultValueTraits
76{
77 // By default we assume that a default-constructed value (new T) is
78 // initialized; e.g. QCOMPARE(*(new T), *(new T)) should succeed
79 enum { IsInitialized = true };
80};
81
82#define DEFINE_NON_INITIALIZED_DEFAULT_VALUE_TRAITS(MetaTypeName, MetaTypeId, RealType) \
83template<> struct DefaultValueTraits<QMetaType::MetaTypeName> { \
84 enum { IsInitialized = false }; \
85};
86// Primitive types (int et al) aren't initialized
87FOR_EACH_PRIMITIVE_METATYPE(DEFINE_NON_INITIALIZED_DEFAULT_VALUE_TRAITS)
88#undef DEFINE_NON_INITIALIZED_DEFAULT_VALUE_TRAITS
89
90template <int ID>
91struct TestValueFactory {};
92
93template<> struct TestValueFactory<QMetaType::Void> {
94 static void *create() { return 0; }
95};
96
97template<> struct TestValueFactory<QMetaType::QString> {
98 static QString *create() { return new QString(QString::fromLatin1(str: "QString")); }
99};
100template<> struct TestValueFactory<QMetaType::Int> {
101 static int *create() { return new int(INT_MIN); }
102};
103template<> struct TestValueFactory<QMetaType::UInt> {
104 static uint *create() { return new uint(UINT_MAX); }
105};
106template<> struct TestValueFactory<QMetaType::Bool> {
107 static bool *create() { return new bool(true); }
108};
109template<> struct TestValueFactory<QMetaType::Double> {
110 static double *create() { return new double(DBL_MIN); }
111};
112template<> struct TestValueFactory<QMetaType::QByteArray> {
113 static QByteArray *create() { return new QByteArray(QByteArray("QByteArray")); }
114};
115template<> struct TestValueFactory<QMetaType::QByteArrayList> {
116 static QByteArrayList *create() { return new QByteArrayList(QByteArrayList() << "Q" << "Byte" << "Array" << "List"); }
117};
118template<> struct TestValueFactory<QMetaType::QVariantMap> {
119 static QVariantMap *create() { return new QVariantMap(); }
120};
121template<> struct TestValueFactory<QMetaType::QVariantHash> {
122 static QVariantHash *create() { return new QVariantHash(); }
123};
124template<> struct TestValueFactory<QMetaType::QVariantList> {
125 static QVariantList *create() { return new QVariantList(QVariantList() << 123 << "Q" << "Variant" << "List"); }
126};
127template<> struct TestValueFactory<QMetaType::QChar> {
128 static QChar *create() { return new QChar(QChar('q')); }
129};
130template<> struct TestValueFactory<QMetaType::Long> {
131 static long *create() { return new long(LONG_MIN); }
132};
133template<> struct TestValueFactory<QMetaType::Short> {
134 static short *create() { return new short(SHRT_MIN); }
135};
136template<> struct TestValueFactory<QMetaType::Char> {
137 static char *create() { return new char('c'); }
138};
139template<> struct TestValueFactory<QMetaType::ULong> {
140 static ulong *create() { return new ulong(ULONG_MAX); }
141};
142template<> struct TestValueFactory<QMetaType::UShort> {
143 static ushort *create() { return new ushort(USHRT_MAX); }
144};
145template<> struct TestValueFactory<QMetaType::SChar> {
146 static signed char *create() { return new signed char(CHAR_MIN); }
147};
148template<> struct TestValueFactory<QMetaType::UChar> {
149 static uchar *create() { return new uchar(UCHAR_MAX); }
150};
151template<> struct TestValueFactory<QMetaType::Float> {
152 static float *create() { return new float(FLT_MIN); }
153};
154template<> struct TestValueFactory<QMetaType::QObjectStar> {
155 static QObject * *create() { return new QObject *(0); }
156};
157template<> struct TestValueFactory<QMetaType::VoidStar> {
158 static void * *create() { return new void *(0); }
159};
160template<> struct TestValueFactory<QMetaType::LongLong> {
161 static qlonglong *create() { return new qlonglong(LLONG_MIN); }
162};
163template<> struct TestValueFactory<QMetaType::ULongLong> {
164 static qulonglong *create() { return new qulonglong(ULLONG_MAX); }
165};
166template<> struct TestValueFactory<QMetaType::QStringList> {
167 static QStringList *create() { return new QStringList(QStringList() << "Q" << "t"); }
168};
169template<> struct TestValueFactory<QMetaType::QBitArray> {
170 static QBitArray *create() { return new QBitArray(QBitArray(256, true)); }
171};
172template<> struct TestValueFactory<QMetaType::QDate> {
173 static QDate *create() { return new QDate(QDate::currentDate()); }
174};
175template<> struct TestValueFactory<QMetaType::QTime> {
176 static QTime *create() { return new QTime(QTime::currentTime()); }
177};
178template<> struct TestValueFactory<QMetaType::QDateTime> {
179 static QDateTime *create() { return new QDateTime(QDateTime::currentDateTime()); }
180};
181template<> struct TestValueFactory<QMetaType::QUrl> {
182 static QUrl *create() { return new QUrl("http://www.example.org"); }
183};
184template<> struct TestValueFactory<QMetaType::QLocale> {
185 static QLocale *create() { return new QLocale(QLocale::c()); }
186};
187template<> struct TestValueFactory<QMetaType::QRect> {
188 static QRect *create() { return new QRect(10, 20, 30, 40); }
189};
190template<> struct TestValueFactory<QMetaType::QRectF> {
191 static QRectF *create() { return new QRectF(10, 20, 30, 40); }
192};
193template<> struct TestValueFactory<QMetaType::QSize> {
194 static QSize *create() { return new QSize(10, 20); }
195};
196template<> struct TestValueFactory<QMetaType::QSizeF> {
197 static QSizeF *create() { return new QSizeF(10, 20); }
198};
199template<> struct TestValueFactory<QMetaType::QLine> {
200 static QLine *create() { return new QLine(10, 20, 30, 40); }
201};
202template<> struct TestValueFactory<QMetaType::QLineF> {
203 static QLineF *create() { return new QLineF(10, 20, 30, 40); }
204};
205template<> struct TestValueFactory<QMetaType::QPoint> {
206 static QPoint *create() { return new QPoint(10, 20); }
207};
208template<> struct TestValueFactory<QMetaType::QPointF> {
209 static QPointF *create() { return new QPointF(10, 20); }
210};
211template<> struct TestValueFactory<QMetaType::QEasingCurve> {
212 static QEasingCurve *create() { return new QEasingCurve(QEasingCurve::InOutElastic); }
213};
214template<> struct TestValueFactory<QMetaType::QUuid> {
215 static QUuid *create() { return new QUuid(); }
216};
217template<> struct TestValueFactory<QMetaType::QModelIndex> {
218 static QModelIndex *create() { return new QModelIndex(); }
219};
220template<> struct TestValueFactory<QMetaType::QPersistentModelIndex> {
221 static QPersistentModelIndex *create() { return new QPersistentModelIndex(); }
222};
223template<> struct TestValueFactory<QMetaType::Nullptr> {
224 static std::nullptr_t *create() { return new std::nullptr_t; }
225};
226template<> struct TestValueFactory<QMetaType::QRegExp> {
227 static QRegExp *create()
228 {
229#ifndef QT_NO_REGEXP
230 return new QRegExp("A*");
231#else
232 return 0;
233#endif
234 }
235};
236template<> struct TestValueFactory<QMetaType::QRegularExpression> {
237 static QRegularExpression *create()
238 {
239#if QT_CONFIG(regularexpression)
240 return new QRegularExpression("abc.*def");
241#else
242 return 0;
243#endif
244 }
245};
246template<> struct TestValueFactory<QMetaType::QJsonValue> {
247 static QJsonValue *create() { return new QJsonValue(123.); }
248};
249template<> struct TestValueFactory<QMetaType::QJsonObject> {
250 static QJsonObject *create() {
251 QJsonObject *o = new QJsonObject();
252 o->insert(key: "a", value: 123.);
253 o->insert(key: "b", value: true);
254 o->insert(key: "c", value: QJsonValue::Null);
255 o->insert(key: "d", value: QLatin1String("ciao"));
256 return o;
257 }
258};
259template<> struct TestValueFactory<QMetaType::QJsonArray> {
260 static QJsonArray *create() {
261 QJsonArray *a = new QJsonArray();
262 a->append(value: 123.);
263 a->append(value: true);
264 a->append(value: QJsonValue::Null);
265 a->append(value: QLatin1String("ciao"));
266 return a;
267 }
268};
269template<> struct TestValueFactory<QMetaType::QJsonDocument> {
270 static QJsonDocument *create() {
271 return new QJsonDocument(
272 QJsonDocument::fromJson(json: "{ 'foo': 123, 'bar': [true, null, 'ciao'] }")
273 );
274 }
275};
276
277template<> struct TestValueFactory<QMetaType::QCborSimpleType> {
278 static QCborSimpleType *create() { return new QCborSimpleType(QCborSimpleType::True); }
279};
280template<> struct TestValueFactory<QMetaType::QCborValue> {
281 static QCborValue *create() { return new QCborValue(123.); }
282};
283template<> struct TestValueFactory<QMetaType::QCborMap> {
284 static QCborMap *create() {
285 return new QCborMap{{0, 0}, {"Hello", 1}, {1, nullptr}};
286 }
287};
288template<> struct TestValueFactory<QMetaType::QCborArray> {
289 static QCborArray *create() {
290 return new QCborArray{0, 1, -2, 2.5, false, nullptr, "Hello", QByteArray("World") };
291 }
292};
293
294template<> struct TestValueFactory<QMetaType::QVariant> {
295 static QVariant *create() { return new QVariant(QStringList(QStringList() << "Q" << "t")); }
296};
297
298#endif // TST_QMETATYPE_H
299

source code of qtbase/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h