qmetatype.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtCore module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QMETATYPE_H
00043 #define QMETATYPE_H
00044 
00045 #include <QtCore/qglobal.h>
00046 #include <QtCore/qatomic.h>
00047 
00048 #ifndef QT_NO_DATASTREAM
00049 #include <QtCore/qdatastream.h>
00050 #endif
00051 
00052 #ifdef Bool
00053 #error qmetatype.h must be included before any header file that defines Bool
00054 #endif
00055 
00056 QT_BEGIN_HEADER
00057 
00058 QT_BEGIN_NAMESPACE
00059 
00060 QT_MODULE(Core)
00061 
00062 class Q_CORE_EXPORT QMetaType {
00063 public:
00064     enum Type {
00065         // these are merged with QVariant
00066         Void = 0, Bool = 1, Int = 2, UInt = 3, LongLong = 4, ULongLong = 5,
00067         Double = 6, QChar = 7, QVariantMap = 8, QVariantList = 9,
00068         QString = 10, QStringList = 11, QByteArray = 12,
00069         QBitArray = 13, QDate = 14, QTime = 15, QDateTime = 16, QUrl = 17,
00070         QLocale = 18, QRect = 19, QRectF = 20, QSize = 21, QSizeF = 22,
00071         QLine = 23, QLineF = 24, QPoint = 25, QPointF = 26, QRegExp = 27,
00072         QVariantHash = 28, QEasingCurve = 29, LastCoreType = QEasingCurve,
00073 
00074         FirstGuiType = 63 /* QColorGroup */,
00075 #ifdef QT3_SUPPORT
00076         QColorGroup = 63,
00077 #endif
00078         QFont = 64, QPixmap = 65, QBrush = 66, QColor = 67, QPalette = 68,
00079         QIcon = 69, QImage = 70, QPolygon = 71, QRegion = 72, QBitmap = 73,
00080         QCursor = 74, QSizePolicy = 75, QKeySequence = 76, QPen = 77,
00081         QTextLength = 78, QTextFormat = 79, QMatrix = 80, QTransform = 81,
00082         QMatrix4x4 = 82, QVector2D = 83, QVector3D = 84, QVector4D = 85,
00083         QQuaternion = 86,
00084         LastGuiType = QQuaternion,
00085 
00086         FirstCoreExtType = 128 /* VoidStar */,
00087         VoidStar = 128, Long = 129, Short = 130, Char = 131, ULong = 132,
00088         UShort = 133, UChar = 134, Float = 135, QObjectStar = 136, QWidgetStar = 137,
00089         QVariant = 138,
00090         LastCoreExtType = QVariant,
00091 
00092 // This logic must match the one in qglobal.h
00093 #if defined(QT_COORD_TYPE)
00094         QReal = 0,
00095 #elif defined(QT_NO_FPU) || defined(QT_ARCH_ARM) || defined(QT_ARCH_WINDOWSCE) || defined(QT_ARCH_SYMBIAN)
00096         QReal = Float,
00097 #else
00098         QReal = Double,
00099 #endif
00100 
00101         User = 256
00102     };
00103 
00104     typedef void (*Destructor)(void *);
00105     typedef void *(*Constructor)(const void *);
00106 
00107 #ifndef QT_NO_DATASTREAM
00108     typedef void (*SaveOperator)(QDataStream &, const void *);
00109     typedef void (*LoadOperator)(QDataStream &, void *);
00110     static void registerStreamOperators(const char *typeName, SaveOperator saveOp,
00111                                         LoadOperator loadOp);
00112     static void registerStreamOperators(int type, SaveOperator saveOp,
00113                                         LoadOperator loadOp);
00114 #endif
00115     static int registerType(const char *typeName, Destructor destructor,
00116                             Constructor constructor);
00117     static int registerTypedef(const char *typeName, int aliasId);
00118     static int type(const char *typeName);
00119     static const char *typeName(int type);
00120     static bool isRegistered(int type);
00121     static void *construct(int type, const void *copy = 0);
00122     static void destroy(int type, void *data);
00123     static void unregisterType(const char *typeName);
00124 
00125 #ifndef QT_NO_DATASTREAM
00126     static bool save(QDataStream &stream, int type, const void *data);
00127     static bool load(QDataStream &stream, int type, void *data);
00128 #endif
00129 };
00130 
00131 template <typename T>
00132 void qMetaTypeDeleteHelper(T *t)
00133 {
00134     delete t;
00135 }
00136 
00137 template <typename T>
00138 void *qMetaTypeConstructHelper(const T *t)
00139 {
00140     if (!t)
00141         return new T;
00142     return new T(*static_cast<const T*>(t));
00143 }
00144 
00145 #ifndef QT_NO_DATASTREAM
00146 template <typename T>
00147 void qMetaTypeSaveHelper(QDataStream &stream, const T *t)
00148 {
00149     stream << *t;
00150 }
00151 
00152 template <typename T>
00153 void qMetaTypeLoadHelper(QDataStream &stream, T *t)
00154 {
00155     stream >> *t;
00156 }
00157 #endif // QT_NO_DATASTREAM
00158 
00159 template <typename T>
00160 struct QMetaTypeId
00161 {
00162     enum { Defined = 0 };
00163 };
00164 
00165 template <typename T>
00166 struct QMetaTypeId2
00167 {
00168     enum { Defined = QMetaTypeId<T>::Defined };
00169     static inline int qt_metatype_id() { return QMetaTypeId<T>::qt_metatype_id(); }
00170 };
00171 
00172 namespace QtPrivate {
00173     template <typename T, bool Defined = QMetaTypeId2<T>::Defined>
00174     struct QMetaTypeIdHelper {
00175         static inline int qt_metatype_id()
00176         { return QMetaTypeId2<T>::qt_metatype_id(); }
00177     };
00178     template <typename T> struct QMetaTypeIdHelper<T, false> {
00179         static inline int qt_metatype_id()
00180         { return -1; }
00181     };
00182 }
00183 
00184 template <typename T>
00185 int qRegisterMetaType(const char *typeName
00186 #ifndef qdoc
00187     , T * dummy = 0
00188 #endif
00189 )
00190 {
00191     const int typedefOf = dummy ? -1 : QtPrivate::QMetaTypeIdHelper<T>::qt_metatype_id();
00192     if (typedefOf != -1)
00193         return QMetaType::registerTypedef(typeName, typedefOf);
00194 
00195     typedef void*(*ConstructPtr)(const T*);
00196     ConstructPtr cptr = qMetaTypeConstructHelper<T>;
00197     typedef void(*DeletePtr)(T*);
00198     DeletePtr dptr = qMetaTypeDeleteHelper<T>;
00199 
00200     return QMetaType::registerType(typeName, reinterpret_cast<QMetaType::Destructor>(dptr),
00201                                    reinterpret_cast<QMetaType::Constructor>(cptr));
00202 }
00203 
00204 #ifndef QT_NO_DATASTREAM
00205 template <typename T>
00206 void qRegisterMetaTypeStreamOperators(const char *typeName
00207 #ifndef qdoc
00208     , T * /* dummy */ = 0
00209 #endif
00210 )
00211 {
00212     typedef void(*SavePtr)(QDataStream &, const T *);
00213     typedef void(*LoadPtr)(QDataStream &, T *);
00214     SavePtr sptr = qMetaTypeSaveHelper<T>;
00215     LoadPtr lptr = qMetaTypeLoadHelper<T>;
00216 
00217     qRegisterMetaType<T>(typeName);
00218     QMetaType::registerStreamOperators(typeName, reinterpret_cast<QMetaType::SaveOperator>(sptr),
00219                                        reinterpret_cast<QMetaType::LoadOperator>(lptr));
00220 }
00221 #endif // QT_NO_DATASTREAM
00222 
00223 template <typename T>
00224 inline int qMetaTypeId(
00225 #ifndef qdoc
00226     T * /* dummy */ = 0
00227 #endif
00228 )
00229 {
00230     return QMetaTypeId2<T>::qt_metatype_id();
00231 }
00232 
00233 template <typename T>
00234 inline int qRegisterMetaType(
00235 #if !defined(qdoc) && !defined(Q_CC_SUN)
00236     T * dummy = 0
00237 #endif
00238 )
00239 {
00240 #ifdef Q_CC_SUN
00241     return qMetaTypeId(static_cast<T *>(0));
00242 #else
00243     return qMetaTypeId(dummy);
00244 #endif
00245 }
00246 
00247 #ifndef QT_NO_DATASTREAM
00248 template <typename T>
00249 inline int qRegisterMetaTypeStreamOperators()
00250 {
00251     typedef void(*SavePtr)(QDataStream &, const T *);
00252     typedef void(*LoadPtr)(QDataStream &, T *);
00253     SavePtr sptr = qMetaTypeSaveHelper<T>;
00254     LoadPtr lptr = qMetaTypeLoadHelper<T>;
00255 
00256     register int id = qMetaTypeId<T>();
00257     QMetaType::registerStreamOperators(id,
00258                                        reinterpret_cast<QMetaType::SaveOperator>(sptr),
00259                                        reinterpret_cast<QMetaType::LoadOperator>(lptr));
00260 
00261     return id;
00262 }
00263 #endif
00264 
00265 #define Q_DECLARE_METATYPE(TYPE)                                        \
00266     QT_BEGIN_NAMESPACE                                                  \
00267     template <>                                                         \
00268     struct QMetaTypeId< TYPE >                                          \
00269     {                                                                   \
00270         enum { Defined = 1 };                                           \
00271         static int qt_metatype_id()                                     \
00272             {                                                           \
00273                 static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); \
00274                 if (!metatype_id)                                       \
00275                     metatype_id = qRegisterMetaType< TYPE >(#TYPE,      \
00276                                reinterpret_cast< TYPE *>(quintptr(-1))); \
00277                 return metatype_id;                                     \
00278             }                                                           \
00279     };                                                                  \
00280     QT_END_NAMESPACE
00281 
00282 #define Q_DECLARE_BUILTIN_METATYPE(TYPE, NAME) \
00283     QT_BEGIN_NAMESPACE \
00284     template<> struct QMetaTypeId2<TYPE> \
00285     { \
00286         enum { Defined = 1, MetaType = QMetaType::NAME }; \
00287         static inline int qt_metatype_id() { return QMetaType::NAME; } \
00288     }; \
00289     QT_END_NAMESPACE
00290 
00291 class QString;
00292 class QByteArray;
00293 class QChar;
00294 class QStringList;
00295 class QBitArray;
00296 class QDate;
00297 class QTime;
00298 class QDateTime;
00299 class QUrl;
00300 class QLocale;
00301 class QRect;
00302 class QRectF;
00303 class QSize;
00304 class QSizeF;
00305 class QLine;
00306 class QLineF;
00307 class QPoint;
00308 class QPointF;
00309 #ifndef QT_NO_REGEXP
00310 class QRegExp;
00311 #endif
00312 class QEasingCurve;
00313 class QWidget;
00314 class QObject;
00315 
00316 #ifdef QT3_SUPPORT
00317 class QColorGroup;
00318 #endif
00319 class QFont;
00320 class QPixmap;
00321 class QBrush;
00322 class QColor;
00323 class QPalette;
00324 class QIcon;
00325 class QImage;
00326 class QPolygon;
00327 class QRegion;
00328 class QBitmap;
00329 class QCursor;
00330 class QSizePolicy;
00331 class QKeySequence;
00332 class QPen;
00333 class QTextLength;
00334 class QTextFormat;
00335 class QMatrix;
00336 class QTransform;
00337 class QMatrix4x4;
00338 class QVector2D;
00339 class QVector3D;
00340 class QVector4D;
00341 class QQuaternion;
00342 class QVariant;
00343 
00344 QT_END_NAMESPACE
00345 
00346 Q_DECLARE_BUILTIN_METATYPE(QString, QString)
00347 Q_DECLARE_BUILTIN_METATYPE(int, Int)
00348 Q_DECLARE_BUILTIN_METATYPE(uint, UInt)
00349 Q_DECLARE_BUILTIN_METATYPE(bool, Bool)
00350 Q_DECLARE_BUILTIN_METATYPE(double, Double)
00351 Q_DECLARE_BUILTIN_METATYPE(QByteArray, QByteArray)
00352 Q_DECLARE_BUILTIN_METATYPE(QChar, QChar)
00353 Q_DECLARE_BUILTIN_METATYPE(long, Long)
00354 Q_DECLARE_BUILTIN_METATYPE(short, Short)
00355 Q_DECLARE_BUILTIN_METATYPE(char, Char)
00356 Q_DECLARE_BUILTIN_METATYPE(ulong, ULong)
00357 Q_DECLARE_BUILTIN_METATYPE(ushort, UShort)
00358 Q_DECLARE_BUILTIN_METATYPE(uchar, UChar)
00359 Q_DECLARE_BUILTIN_METATYPE(float, Float)
00360 Q_DECLARE_BUILTIN_METATYPE(QObject *, QObjectStar)
00361 Q_DECLARE_BUILTIN_METATYPE(QWidget *, QWidgetStar)
00362 Q_DECLARE_BUILTIN_METATYPE(void *, VoidStar)
00363 Q_DECLARE_BUILTIN_METATYPE(qlonglong, LongLong)
00364 Q_DECLARE_BUILTIN_METATYPE(qulonglong, ULongLong)
00365 Q_DECLARE_BUILTIN_METATYPE(QStringList, QStringList)
00366 Q_DECLARE_BUILTIN_METATYPE(QBitArray, QBitArray)
00367 Q_DECLARE_BUILTIN_METATYPE(QDate, QDate)
00368 Q_DECLARE_BUILTIN_METATYPE(QTime, QTime)
00369 Q_DECLARE_BUILTIN_METATYPE(QDateTime, QDateTime)
00370 Q_DECLARE_BUILTIN_METATYPE(QUrl, QUrl)
00371 Q_DECLARE_BUILTIN_METATYPE(QLocale, QLocale)
00372 Q_DECLARE_BUILTIN_METATYPE(QRect, QRect)
00373 Q_DECLARE_BUILTIN_METATYPE(QRectF, QRectF)
00374 Q_DECLARE_BUILTIN_METATYPE(QSize, QSize)
00375 Q_DECLARE_BUILTIN_METATYPE(QSizeF, QSizeF)
00376 Q_DECLARE_BUILTIN_METATYPE(QLine, QLine)
00377 Q_DECLARE_BUILTIN_METATYPE(QLineF, QLineF)
00378 Q_DECLARE_BUILTIN_METATYPE(QPoint, QPoint)
00379 Q_DECLARE_BUILTIN_METATYPE(QPointF, QPointF)
00380 #ifndef QT_NO_REGEXP
00381 Q_DECLARE_BUILTIN_METATYPE(QRegExp, QRegExp)
00382 #endif
00383 Q_DECLARE_BUILTIN_METATYPE(QEasingCurve, QEasingCurve)
00384 
00385 #ifdef QT3_SUPPORT
00386 Q_DECLARE_BUILTIN_METATYPE(QColorGroup, QColorGroup)
00387 #endif
00388 Q_DECLARE_BUILTIN_METATYPE(QFont, QFont)
00389 Q_DECLARE_BUILTIN_METATYPE(QPixmap, QPixmap)
00390 Q_DECLARE_BUILTIN_METATYPE(QBrush, QBrush)
00391 Q_DECLARE_BUILTIN_METATYPE(QColor, QColor)
00392 Q_DECLARE_BUILTIN_METATYPE(QPalette, QPalette)
00393 Q_DECLARE_BUILTIN_METATYPE(QIcon, QIcon)
00394 Q_DECLARE_BUILTIN_METATYPE(QImage, QImage)
00395 Q_DECLARE_BUILTIN_METATYPE(QPolygon, QPolygon)
00396 Q_DECLARE_BUILTIN_METATYPE(QRegion, QRegion)
00397 Q_DECLARE_BUILTIN_METATYPE(QBitmap, QBitmap)
00398 Q_DECLARE_BUILTIN_METATYPE(QCursor, QCursor)
00399 Q_DECLARE_BUILTIN_METATYPE(QSizePolicy, QSizePolicy)
00400 Q_DECLARE_BUILTIN_METATYPE(QKeySequence, QKeySequence)
00401 Q_DECLARE_BUILTIN_METATYPE(QPen, QPen)
00402 Q_DECLARE_BUILTIN_METATYPE(QTextLength, QTextLength)
00403 Q_DECLARE_BUILTIN_METATYPE(QTextFormat, QTextFormat)
00404 Q_DECLARE_BUILTIN_METATYPE(QMatrix, QMatrix)
00405 Q_DECLARE_BUILTIN_METATYPE(QTransform, QTransform)
00406 Q_DECLARE_BUILTIN_METATYPE(QMatrix4x4, QMatrix4x4)
00407 Q_DECLARE_BUILTIN_METATYPE(QVector2D, QVector2D)
00408 Q_DECLARE_BUILTIN_METATYPE(QVector3D, QVector3D)
00409 Q_DECLARE_BUILTIN_METATYPE(QVector4D, QVector4D)
00410 Q_DECLARE_BUILTIN_METATYPE(QQuaternion, QQuaternion)
00411 Q_DECLARE_BUILTIN_METATYPE(QVariant, QVariant)
00412 
00413 QT_END_HEADER
00414 
00415 #endif // QMETATYPE_H