| 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 | |
| 49 | template <int ID> |
| 50 | struct MetaEnumToType {}; |
| 51 | |
| 52 | #define DEFINE_META_ENUM_TO_TYPE(MetaTypeName, MetaTypeId, RealType) \ |
| 53 | template<> \ |
| 54 | struct MetaEnumToType<QMetaType::MetaTypeName> { \ |
| 55 | typedef RealType Type; \ |
| 56 | }; |
| 57 | FOR_EACH_CORE_METATYPE(DEFINE_META_ENUM_TO_TYPE) |
| 58 | #undef DEFINE_META_ENUM_TO_TYPE |
| 59 | |
| 60 | template <int ID> |
| 61 | struct DefaultValueFactory |
| 62 | { |
| 63 | typedef typename MetaEnumToType<ID>::Type Type; |
| 64 | static Type *create() { return new Type; } |
| 65 | }; |
| 66 | |
| 67 | template <> |
| 68 | struct DefaultValueFactory<QMetaType::Void> |
| 69 | { |
| 70 | typedef MetaEnumToType<QMetaType::Void>::Type Type; |
| 71 | static Type *create() { return 0; } |
| 72 | }; |
| 73 | |
| 74 | template <int ID> |
| 75 | struct 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) \ |
| 83 | template<> struct DefaultValueTraits<QMetaType::MetaTypeName> { \ |
| 84 | enum { IsInitialized = false }; \ |
| 85 | }; |
| 86 | // Primitive types (int et al) aren't initialized |
| 87 | FOR_EACH_PRIMITIVE_METATYPE(DEFINE_NON_INITIALIZED_DEFAULT_VALUE_TRAITS) |
| 88 | #undef DEFINE_NON_INITIALIZED_DEFAULT_VALUE_TRAITS |
| 89 | |
| 90 | template <int ID> |
| 91 | struct TestValueFactory {}; |
| 92 | |
| 93 | template<> struct TestValueFactory<QMetaType::Void> { |
| 94 | static void *create() { return 0; } |
| 95 | }; |
| 96 | |
| 97 | template<> struct TestValueFactory<QMetaType::QString> { |
| 98 | static QString *create() { return new QString(QString::fromLatin1(str: "QString" )); } |
| 99 | }; |
| 100 | template<> struct TestValueFactory<QMetaType::Int> { |
| 101 | static int *create() { return new int(INT_MIN); } |
| 102 | }; |
| 103 | template<> struct TestValueFactory<QMetaType::UInt> { |
| 104 | static uint *create() { return new uint(UINT_MAX); } |
| 105 | }; |
| 106 | template<> struct TestValueFactory<QMetaType::Bool> { |
| 107 | static bool *create() { return new bool(true); } |
| 108 | }; |
| 109 | template<> struct TestValueFactory<QMetaType::Double> { |
| 110 | static double *create() { return new double(DBL_MIN); } |
| 111 | }; |
| 112 | template<> struct TestValueFactory<QMetaType::QByteArray> { |
| 113 | static QByteArray *create() { return new QByteArray(QByteArray("QByteArray" )); } |
| 114 | }; |
| 115 | template<> struct TestValueFactory<QMetaType::QByteArrayList> { |
| 116 | static QByteArrayList *create() { return new QByteArrayList(QByteArrayList() << "Q" << "Byte" << "Array" << "List" ); } |
| 117 | }; |
| 118 | template<> struct TestValueFactory<QMetaType::QVariantMap> { |
| 119 | static QVariantMap *create() { return new QVariantMap(); } |
| 120 | }; |
| 121 | template<> struct TestValueFactory<QMetaType::QVariantHash> { |
| 122 | static QVariantHash *create() { return new QVariantHash(); } |
| 123 | }; |
| 124 | template<> struct TestValueFactory<QMetaType::QVariantList> { |
| 125 | static QVariantList *create() { return new QVariantList(QVariantList() << 123 << "Q" << "Variant" << "List" ); } |
| 126 | }; |
| 127 | template<> struct TestValueFactory<QMetaType::QChar> { |
| 128 | static QChar *create() { return new QChar(QChar('q')); } |
| 129 | }; |
| 130 | template<> struct TestValueFactory<QMetaType::Long> { |
| 131 | static long *create() { return new long(LONG_MIN); } |
| 132 | }; |
| 133 | template<> struct TestValueFactory<QMetaType::Short> { |
| 134 | static short *create() { return new short(SHRT_MIN); } |
| 135 | }; |
| 136 | template<> struct TestValueFactory<QMetaType::Char> { |
| 137 | static char *create() { return new char('c'); } |
| 138 | }; |
| 139 | template<> struct TestValueFactory<QMetaType::ULong> { |
| 140 | static ulong *create() { return new ulong(ULONG_MAX); } |
| 141 | }; |
| 142 | template<> struct TestValueFactory<QMetaType::UShort> { |
| 143 | static ushort *create() { return new ushort(USHRT_MAX); } |
| 144 | }; |
| 145 | template<> struct TestValueFactory<QMetaType::SChar> { |
| 146 | static signed char *create() { return new signed char(CHAR_MIN); } |
| 147 | }; |
| 148 | template<> struct TestValueFactory<QMetaType::UChar> { |
| 149 | static uchar *create() { return new uchar(UCHAR_MAX); } |
| 150 | }; |
| 151 | template<> struct TestValueFactory<QMetaType::Float> { |
| 152 | static float *create() { return new float(FLT_MIN); } |
| 153 | }; |
| 154 | template<> struct TestValueFactory<QMetaType::QObjectStar> { |
| 155 | static QObject * *create() { return new QObject *(0); } |
| 156 | }; |
| 157 | template<> struct TestValueFactory<QMetaType::VoidStar> { |
| 158 | static void * *create() { return new void *(0); } |
| 159 | }; |
| 160 | template<> struct TestValueFactory<QMetaType::LongLong> { |
| 161 | static qlonglong *create() { return new qlonglong(LLONG_MIN); } |
| 162 | }; |
| 163 | template<> struct TestValueFactory<QMetaType::ULongLong> { |
| 164 | static qulonglong *create() { return new qulonglong(ULLONG_MAX); } |
| 165 | }; |
| 166 | template<> struct TestValueFactory<QMetaType::QStringList> { |
| 167 | static QStringList *create() { return new QStringList(QStringList() << "Q" << "t" ); } |
| 168 | }; |
| 169 | template<> struct TestValueFactory<QMetaType::QBitArray> { |
| 170 | static QBitArray *create() { return new QBitArray(QBitArray(256, true)); } |
| 171 | }; |
| 172 | template<> struct TestValueFactory<QMetaType::QDate> { |
| 173 | static QDate *create() { return new QDate(QDate::currentDate()); } |
| 174 | }; |
| 175 | template<> struct TestValueFactory<QMetaType::QTime> { |
| 176 | static QTime *create() { return new QTime(QTime::currentTime()); } |
| 177 | }; |
| 178 | template<> struct TestValueFactory<QMetaType::QDateTime> { |
| 179 | static QDateTime *create() { return new QDateTime(QDateTime::currentDateTime()); } |
| 180 | }; |
| 181 | template<> struct TestValueFactory<QMetaType::QUrl> { |
| 182 | static QUrl *create() { return new QUrl("http://www.example.org" ); } |
| 183 | }; |
| 184 | template<> struct TestValueFactory<QMetaType::QLocale> { |
| 185 | static QLocale *create() { return new QLocale(QLocale::c()); } |
| 186 | }; |
| 187 | template<> struct TestValueFactory<QMetaType::QRect> { |
| 188 | static QRect *create() { return new QRect(10, 20, 30, 40); } |
| 189 | }; |
| 190 | template<> struct TestValueFactory<QMetaType::QRectF> { |
| 191 | static QRectF *create() { return new QRectF(10, 20, 30, 40); } |
| 192 | }; |
| 193 | template<> struct TestValueFactory<QMetaType::QSize> { |
| 194 | static QSize *create() { return new QSize(10, 20); } |
| 195 | }; |
| 196 | template<> struct TestValueFactory<QMetaType::QSizeF> { |
| 197 | static QSizeF *create() { return new QSizeF(10, 20); } |
| 198 | }; |
| 199 | template<> struct TestValueFactory<QMetaType::QLine> { |
| 200 | static QLine *create() { return new QLine(10, 20, 30, 40); } |
| 201 | }; |
| 202 | template<> struct TestValueFactory<QMetaType::QLineF> { |
| 203 | static QLineF *create() { return new QLineF(10, 20, 30, 40); } |
| 204 | }; |
| 205 | template<> struct TestValueFactory<QMetaType::QPoint> { |
| 206 | static QPoint *create() { return new QPoint(10, 20); } |
| 207 | }; |
| 208 | template<> struct TestValueFactory<QMetaType::QPointF> { |
| 209 | static QPointF *create() { return new QPointF(10, 20); } |
| 210 | }; |
| 211 | template<> struct TestValueFactory<QMetaType::QEasingCurve> { |
| 212 | static QEasingCurve *create() { return new QEasingCurve(QEasingCurve::InOutElastic); } |
| 213 | }; |
| 214 | template<> struct TestValueFactory<QMetaType::QUuid> { |
| 215 | static QUuid *create() { return new QUuid(); } |
| 216 | }; |
| 217 | template<> struct TestValueFactory<QMetaType::QModelIndex> { |
| 218 | static QModelIndex *create() { return new QModelIndex(); } |
| 219 | }; |
| 220 | template<> struct TestValueFactory<QMetaType::QPersistentModelIndex> { |
| 221 | static QPersistentModelIndex *create() { return new QPersistentModelIndex(); } |
| 222 | }; |
| 223 | template<> struct TestValueFactory<QMetaType::Nullptr> { |
| 224 | static std::nullptr_t *create() { return new std::nullptr_t; } |
| 225 | }; |
| 226 | template<> 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 | }; |
| 236 | template<> 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 | }; |
| 246 | template<> struct TestValueFactory<QMetaType::QJsonValue> { |
| 247 | static QJsonValue *create() { return new QJsonValue(123.); } |
| 248 | }; |
| 249 | template<> 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 | }; |
| 259 | template<> 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 | }; |
| 269 | template<> 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 | |
| 277 | template<> struct TestValueFactory<QMetaType::QCborSimpleType> { |
| 278 | static QCborSimpleType *create() { return new QCborSimpleType(QCborSimpleType::True); } |
| 279 | }; |
| 280 | template<> struct TestValueFactory<QMetaType::QCborValue> { |
| 281 | static QCborValue *create() { return new QCborValue(123.); } |
| 282 | }; |
| 283 | template<> struct TestValueFactory<QMetaType::QCborMap> { |
| 284 | static QCborMap *create() { |
| 285 | return new QCborMap{{0, 0}, {"Hello" , 1}, {1, nullptr}}; |
| 286 | } |
| 287 | }; |
| 288 | template<> 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 | |
| 294 | template<> struct TestValueFactory<QMetaType::QVariant> { |
| 295 | static QVariant *create() { return new QVariant(QStringList(QStringList() << "Q" << "t" )); } |
| 296 | }; |
| 297 | |
| 298 | #endif // TST_QMETATYPE_H |
| 299 | |