1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QMETATYPE_H
43#define QMETATYPE_H
44
45#include <QtCore/qglobal.h>
46#include <QtCore/qatomic.h>
47
48#ifndef QT_NO_DATASTREAM
49#include <QtCore/qdatastream.h>
50#endif
51
52#ifdef Bool
53#error qmetatype.h must be included before any header file that defines Bool
54#endif
55
56QT_BEGIN_HEADER
57
58QT_BEGIN_NAMESPACE
59
60QT_MODULE(Core)
61
62class Q_CORE_EXPORT QMetaType {
63public:
64 enum Type {
65 // these are merged with QVariant
66 Void = 0, Bool = 1, Int = 2, UInt = 3, LongLong = 4, ULongLong = 5,
67 Double = 6, QChar = 7, QVariantMap = 8, QVariantList = 9,
68 QString = 10, QStringList = 11, QByteArray = 12,
69 QBitArray = 13, QDate = 14, QTime = 15, QDateTime = 16, QUrl = 17,
70 QLocale = 18, QRect = 19, QRectF = 20, QSize = 21, QSizeF = 22,
71 QLine = 23, QLineF = 24, QPoint = 25, QPointF = 26, QRegExp = 27,
72 QVariantHash = 28, QEasingCurve = 29, LastCoreType = QEasingCurve,
73
74 FirstGuiType = 63 /* QColorGroup */,
75#ifdef QT3_SUPPORT
76 QColorGroup = 63,
77#endif
78 QFont = 64, QPixmap = 65, QBrush = 66, QColor = 67, QPalette = 68,
79 QIcon = 69, QImage = 70, QPolygon = 71, QRegion = 72, QBitmap = 73,
80 QCursor = 74, QSizePolicy = 75, QKeySequence = 76, QPen = 77,
81 QTextLength = 78, QTextFormat = 79, QMatrix = 80, QTransform = 81,
82 QMatrix4x4 = 82, QVector2D = 83, QVector3D = 84, QVector4D = 85,
83 QQuaternion = 86,
84 LastGuiType = QQuaternion,
85
86 FirstCoreExtType = 128 /* VoidStar */,
87 VoidStar = 128, Long = 129, Short = 130, Char = 131, ULong = 132,
88 UShort = 133, UChar = 134, Float = 135, QObjectStar = 136, QWidgetStar = 137,
89 QVariant = 138,
90 LastCoreExtType = QVariant,
91
92// This logic must match the one in qglobal.h
93#if defined(QT_COORD_TYPE)
94 QReal = 0,
95#elif defined(QT_NO_FPU) || defined(QT_ARCH_ARM) || defined(QT_ARCH_WINDOWSCE) || defined(QT_ARCH_SYMBIAN)
96 QReal = Float,
97#else
98 QReal = Double,
99#endif
100
101 User = 256
102 };
103
104 typedef void (*Destructor)(void *);
105 typedef void *(*Constructor)(const void *);
106
107#ifndef QT_NO_DATASTREAM
108 typedef void (*SaveOperator)(QDataStream &, const void *);
109 typedef void (*LoadOperator)(QDataStream &, void *);
110 static void registerStreamOperators(const char *typeName, SaveOperator saveOp,
111 LoadOperator loadOp);
112 static void registerStreamOperators(int type, SaveOperator saveOp,
113 LoadOperator loadOp);
114#endif
115 static int registerType(const char *typeName, Destructor destructor,
116 Constructor constructor);
117 static int registerTypedef(const char *typeName, int aliasId);
118 static int type(const char *typeName);
119 static const char *typeName(int type);
120 static bool isRegistered(int type);
121 static void *construct(int type, const void *copy = 0);
122 static void destroy(int type, void *data);
123 static void unregisterType(const char *typeName);
124
125#ifndef QT_NO_DATASTREAM
126 static bool save(QDataStream &stream, int type, const void *data);
127 static bool load(QDataStream &stream, int type, void *data);
128#endif
129};
130
131template <typename T>
132void qMetaTypeDeleteHelper(T *t)
133{
134 delete t;
135}
136
137template <typename T>
138void *qMetaTypeConstructHelper(const T *t)
139{
140 if (!t)
141 return new T();
142 return new T(*static_cast<const T*>(t));
143}
144
145#ifndef QT_NO_DATASTREAM
146template <typename T>
147void qMetaTypeSaveHelper(QDataStream &stream, const T *t)
148{
149 stream << *t;
150}
151
152template <typename T>
153void qMetaTypeLoadHelper(QDataStream &stream, T *t)
154{
155 stream >> *t;
156}
157#endif // QT_NO_DATASTREAM
158
159template <typename T>
160struct QMetaTypeId
161{
162 enum { Defined = 0 };
163};
164
165template <typename T>
166struct QMetaTypeId2
167{
168 enum { Defined = QMetaTypeId<T>::Defined };
169 static inline int qt_metatype_id() { return QMetaTypeId<T>::qt_metatype_id(); }
170};
171
172namespace QtPrivate {
173 template <typename T, bool Defined = QMetaTypeId2<T>::Defined>
174 struct QMetaTypeIdHelper {
175 static inline int qt_metatype_id()
176 { return QMetaTypeId2<T>::qt_metatype_id(); }
177 };
178 template <typename T> struct QMetaTypeIdHelper<T, false> {
179 static inline int qt_metatype_id()
180 { return -1; }
181 };
182}
183
184template <typename T>
185int qRegisterMetaType(const char *typeName
186#ifndef qdoc
187 , T * dummy = 0
188#endif
189)
190{
191 const int typedefOf = dummy ? -1 : QtPrivate::QMetaTypeIdHelper<T>::qt_metatype_id();
192 if (typedefOf != -1)
193 return QMetaType::registerTypedef(typeName, typedefOf);
194
195 typedef void*(*ConstructPtr)(const T*);
196 ConstructPtr cptr = qMetaTypeConstructHelper<T>;
197 typedef void(*DeletePtr)(T*);
198 DeletePtr dptr = qMetaTypeDeleteHelper<T>;
199
200 return QMetaType::registerType(typeName, reinterpret_cast<QMetaType::Destructor>(dptr),
201 reinterpret_cast<QMetaType::Constructor>(cptr));
202}
203
204#ifndef QT_NO_DATASTREAM
205template <typename T>
206void qRegisterMetaTypeStreamOperators(const char *typeName
207#ifndef qdoc
208 , T * /* dummy */ = 0
209#endif
210)
211{
212 typedef void(*SavePtr)(QDataStream &, const T *);
213 typedef void(*LoadPtr)(QDataStream &, T *);
214 SavePtr sptr = qMetaTypeSaveHelper<T>;
215 LoadPtr lptr = qMetaTypeLoadHelper<T>;
216
217 qRegisterMetaType<T>(typeName);
218 QMetaType::registerStreamOperators(typeName, reinterpret_cast<QMetaType::SaveOperator>(sptr),
219 reinterpret_cast<QMetaType::LoadOperator>(lptr));
220}
221#endif // QT_NO_DATASTREAM
222
223template <typename T>
224inline int qMetaTypeId(
225#ifndef qdoc
226 T * /* dummy */ = 0
227#endif
228)
229{
230 return QMetaTypeId2<T>::qt_metatype_id();
231}
232
233template <typename T>
234inline int qRegisterMetaType(
235#if !defined(qdoc) && !defined(Q_CC_SUN)
236 T * dummy = 0
237#endif
238)
239{
240#ifdef Q_CC_SUN
241 return qMetaTypeId(static_cast<T *>(0));
242#else
243 return qMetaTypeId(dummy);
244#endif
245}
246
247#ifndef QT_NO_DATASTREAM
248template <typename T>
249inline int qRegisterMetaTypeStreamOperators()
250{
251 typedef void(*SavePtr)(QDataStream &, const T *);
252 typedef void(*LoadPtr)(QDataStream &, T *);
253 SavePtr sptr = qMetaTypeSaveHelper<T>;
254 LoadPtr lptr = qMetaTypeLoadHelper<T>;
255
256 register int id = qMetaTypeId<T>();
257 QMetaType::registerStreamOperators(id,
258 reinterpret_cast<QMetaType::SaveOperator>(sptr),
259 reinterpret_cast<QMetaType::LoadOperator>(lptr));
260
261 return id;
262}
263#endif
264
265#define Q_DECLARE_METATYPE(TYPE) \
266 QT_BEGIN_NAMESPACE \
267 template <> \
268 struct QMetaTypeId< TYPE > \
269 { \
270 enum { Defined = 1 }; \
271 static int qt_metatype_id() \
272 { \
273 static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); \
274 if (!metatype_id) \
275 metatype_id = qRegisterMetaType< TYPE >(#TYPE, \
276 reinterpret_cast< TYPE *>(quintptr(-1))); \
277 return metatype_id; \
278 } \
279 }; \
280 QT_END_NAMESPACE
281
282#define Q_DECLARE_BUILTIN_METATYPE(TYPE, NAME) \
283 QT_BEGIN_NAMESPACE \
284 template<> struct QMetaTypeId2<TYPE> \
285 { \
286 enum { Defined = 1, MetaType = QMetaType::NAME }; \
287 static inline int qt_metatype_id() { return QMetaType::NAME; } \
288 }; \
289 QT_END_NAMESPACE
290
291class QString;
292class QByteArray;
293class QChar;
294class QStringList;
295class QBitArray;
296class QDate;
297class QTime;
298class QDateTime;
299class QUrl;
300class QLocale;
301class QRect;
302class QRectF;
303class QSize;
304class QSizeF;
305class QLine;
306class QLineF;
307class QPoint;
308class QPointF;
309#ifndef QT_NO_REGEXP
310class QRegExp;
311#endif
312class QEasingCurve;
313class QWidget;
314class QObject;
315
316#ifdef QT3_SUPPORT
317class QColorGroup;
318#endif
319class QFont;
320class QPixmap;
321class QBrush;
322class QColor;
323class QPalette;
324class QIcon;
325class QImage;
326class QPolygon;
327class QRegion;
328class QBitmap;
329class QCursor;
330class QSizePolicy;
331class QKeySequence;
332class QPen;
333class QTextLength;
334class QTextFormat;
335class QMatrix;
336class QTransform;
337class QMatrix4x4;
338class QVector2D;
339class QVector3D;
340class QVector4D;
341class QQuaternion;
342class QVariant;
343
344QT_END_NAMESPACE
345
346Q_DECLARE_BUILTIN_METATYPE(QString, QString)
347Q_DECLARE_BUILTIN_METATYPE(int, Int)
348Q_DECLARE_BUILTIN_METATYPE(uint, UInt)
349Q_DECLARE_BUILTIN_METATYPE(bool, Bool)
350Q_DECLARE_BUILTIN_METATYPE(double, Double)
351Q_DECLARE_BUILTIN_METATYPE(QByteArray, QByteArray)
352Q_DECLARE_BUILTIN_METATYPE(QChar, QChar)
353Q_DECLARE_BUILTIN_METATYPE(long, Long)
354Q_DECLARE_BUILTIN_METATYPE(short, Short)
355Q_DECLARE_BUILTIN_METATYPE(char, Char)
356Q_DECLARE_BUILTIN_METATYPE(signed char, Char)
357Q_DECLARE_BUILTIN_METATYPE(ulong, ULong)
358Q_DECLARE_BUILTIN_METATYPE(ushort, UShort)
359Q_DECLARE_BUILTIN_METATYPE(uchar, UChar)
360Q_DECLARE_BUILTIN_METATYPE(float, Float)
361Q_DECLARE_BUILTIN_METATYPE(QObject *, QObjectStar)
362Q_DECLARE_BUILTIN_METATYPE(QWidget *, QWidgetStar)
363Q_DECLARE_BUILTIN_METATYPE(void *, VoidStar)
364Q_DECLARE_BUILTIN_METATYPE(qlonglong, LongLong)
365Q_DECLARE_BUILTIN_METATYPE(qulonglong, ULongLong)
366Q_DECLARE_BUILTIN_METATYPE(QStringList, QStringList)
367Q_DECLARE_BUILTIN_METATYPE(QBitArray, QBitArray)
368Q_DECLARE_BUILTIN_METATYPE(QDate, QDate)
369Q_DECLARE_BUILTIN_METATYPE(QTime, QTime)
370Q_DECLARE_BUILTIN_METATYPE(QDateTime, QDateTime)
371Q_DECLARE_BUILTIN_METATYPE(QUrl, QUrl)
372Q_DECLARE_BUILTIN_METATYPE(QLocale, QLocale)
373Q_DECLARE_BUILTIN_METATYPE(QRect, QRect)
374Q_DECLARE_BUILTIN_METATYPE(QRectF, QRectF)
375Q_DECLARE_BUILTIN_METATYPE(QSize, QSize)
376Q_DECLARE_BUILTIN_METATYPE(QSizeF, QSizeF)
377Q_DECLARE_BUILTIN_METATYPE(QLine, QLine)
378Q_DECLARE_BUILTIN_METATYPE(QLineF, QLineF)
379Q_DECLARE_BUILTIN_METATYPE(QPoint, QPoint)
380Q_DECLARE_BUILTIN_METATYPE(QPointF, QPointF)
381#ifndef QT_NO_REGEXP
382Q_DECLARE_BUILTIN_METATYPE(QRegExp, QRegExp)
383#endif
384Q_DECLARE_BUILTIN_METATYPE(QEasingCurve, QEasingCurve)
385
386#ifdef QT3_SUPPORT
387Q_DECLARE_BUILTIN_METATYPE(QColorGroup, QColorGroup)
388#endif
389Q_DECLARE_BUILTIN_METATYPE(QFont, QFont)
390Q_DECLARE_BUILTIN_METATYPE(QPixmap, QPixmap)
391Q_DECLARE_BUILTIN_METATYPE(QBrush, QBrush)
392Q_DECLARE_BUILTIN_METATYPE(QColor, QColor)
393Q_DECLARE_BUILTIN_METATYPE(QPalette, QPalette)
394Q_DECLARE_BUILTIN_METATYPE(QIcon, QIcon)
395Q_DECLARE_BUILTIN_METATYPE(QImage, QImage)
396Q_DECLARE_BUILTIN_METATYPE(QPolygon, QPolygon)
397Q_DECLARE_BUILTIN_METATYPE(QRegion, QRegion)
398Q_DECLARE_BUILTIN_METATYPE(QBitmap, QBitmap)
399Q_DECLARE_BUILTIN_METATYPE(QCursor, QCursor)
400Q_DECLARE_BUILTIN_METATYPE(QSizePolicy, QSizePolicy)
401Q_DECLARE_BUILTIN_METATYPE(QKeySequence, QKeySequence)
402Q_DECLARE_BUILTIN_METATYPE(QPen, QPen)
403Q_DECLARE_BUILTIN_METATYPE(QTextLength, QTextLength)
404Q_DECLARE_BUILTIN_METATYPE(QTextFormat, QTextFormat)
405Q_DECLARE_BUILTIN_METATYPE(QMatrix, QMatrix)
406Q_DECLARE_BUILTIN_METATYPE(QTransform, QTransform)
407Q_DECLARE_BUILTIN_METATYPE(QMatrix4x4, QMatrix4x4)
408Q_DECLARE_BUILTIN_METATYPE(QVector2D, QVector2D)
409Q_DECLARE_BUILTIN_METATYPE(QVector3D, QVector3D)
410Q_DECLARE_BUILTIN_METATYPE(QVector4D, QVector4D)
411Q_DECLARE_BUILTIN_METATYPE(QQuaternion, QQuaternion)
412Q_DECLARE_BUILTIN_METATYPE(QVariant, QVariant)
413
414QT_END_HEADER
415
416#endif // QMETATYPE_H
417