?? qmetatype.cpp
字號:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtCore module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qmetatype.h"#include "qobjectdefs.h"#include "qdatetime.h"#include "qbytearray.h"#include "qreadwritelock.h"#include "qstring.h"#include "qstringlist.h"#include "qvector.h"#include "qlocale.h"#ifdef QT_BOOTSTRAPPED# define QT_NO_GEOM_VARIANT#else# include "qbitarray.h"# include "qurl.h"# include "qvariant.h"#endif#ifndef QT_NO_GEOM_VARIANT#include "qsize.h"#include "qpoint.h"#include "qrect.h"#include "qline.h"#endif/*! \macro Q_DECLARE_METATYPE(Type) \relates QMetaType This macro makes the type \a Type known to QMetaType. It is needed to use the type \a Type as a custom type in QVariant. Ideally, this macro should be placed below the declaration of the class or struct. If that is not possible, it can be put in a private header file which has to be included every time that type is used in a QVariant. Adding a Q_DECLARE_METATYPE() makes the type known to all template based functions, including QVariant. Note that if you intend to use the type in \e queued signal and slot connections, you also have to call qRegisterMetaType() since such connections are resolved at runtime. This example shows a typical use case of Q_DECLARE_METATYPE(): \code struct MyStruct { int i; ... }; Q_DECLARE_METATYPE(MyStruct) \endcode If \c MyStruct is in a namespace, the Q_DECLARE_METATYPE() macro has to be outside the namespace: \code namespace MyNamespace { ... } Q_DECLARE_METATYPE(MyNamespace::MyStruct) \endcode Since \c{MyStruct} is now known to QMetaType, it can be used in QVariant: \code MyStruct s; QVariant var; var.setValue(s); // copy s into the variant ... // retrieve the value MyStruct s2 = var.value<MyStruct>(); \endcode \sa qRegisterMetaType()*//*! \enum QMetaType::Type These are the built-in types supported by QMetaType: \value Void \c void \value Bool \c bool \value Int \c int \value UInt \c{unsigned int} \value Double \c double \value QChar QChar \value QString QString \value QByteArray QByteArray \value VoidStar \c{void *} \value Long \c{long} \value LongLong LongLong \value Short \c{short} \value Char \c{char} \value ULong \c{unsigned long} \value ULongLong ULongLong \value UShort \c{unsigned short} \value UChar \c{unsigned char} \value Float \c float \value QObjectStar QObject * \value QWidgetStar QWidget * \value QColorGroup QColorGroup \value QCursor QCursor \value QDate QDate \value QSize QSize \value QTime QTime \value QVariantList QVariantList \value QPolygon QPolygon \value QColor QColor \value QSizeF QSizeF \value QRectF QRectF \value QLine QLine \value QTextLength QTextLength \value QStringList QStringList \value QVariantMap QVariantMap \value QIcon QIcon \value QPen QPen \value QLineF QLineF \value QTextFormat QTextFormat \value QRect QRect \value QPoint QPoint \value QUrl QUrl \value QRegExp QRegExp \value QDateTime QDateTime \value QPointF QPointF \value QPalette QPalette \value QFont QFont \value QBrush QBrush \value QRegion QRegion \value QBitArray QBitArray \value QImage QImage \value QKeySequence QKeySequence \value QSizePolicy QSizePolicy \value QPixmap QPixmap \value QLocale QLocale \value QBitmap QBitmap \value QMatrix QMatrix \value User Base value for user types \omitvalue FirstCoreExtType \omitvalue FirstGuiType \omitvalue LastCoreExtType \omitvalue LastCoreType \omitvalue LastGuiType Additional types can be registered using Q_DECLARE_METATYPE(). \sa type(), typeName()*//*! \class QMetaType \brief The QMetaType class manages named types in the meta-object system. \ingroup objectmodel \threadsafe The class is used as a helper to marshall types in QVariant and in queued signals and slots connections. It associates a type name to a type so that it can be created and destructed dynamically at run-time. Declare new types with Q_DECLARE_METATYPE() to make them available to QVariant and other template-based functions. Call qRegisterMetaType() to make type available to non-template based functions, such as the queued signal and slot connections. Any class or struct that has a public default constructor, a public copy constructor, and a public destructor can be registered. The following code allocates and destructs an instance of \c{MyClass}: \code int id = QMetaType::type("MyClass"); if (id != -1) { void *myClassPtr = QMetaType::construct(id); ... QMetaType::destroy(id, myClassPtr); myClassPtr = 0; } \endcode If we want the stream operators \c operator<<() and \c operator>>() to work on QVariant objects that store custom types, the custom type must provide \c operator<<() and \c operator>>() operators. \sa Q_DECLARE_METATYPE(), QVariant::setValue(), QVariant::value(), QVariant::fromValue()*//* Note: these MUST be in the order of the enums */static const struct { const char * typeName; int type; } types[] = { /* All Core types */ {"void", QMetaType::Void}, {"bool", QMetaType::Bool}, {"int", QMetaType::Int}, {"uint", QMetaType::UInt}, {"qlonglong", QMetaType::LongLong}, {"qulonglong", QMetaType::ULongLong}, {"double", QMetaType::Double}, {"QChar", QMetaType::QChar}, {"QVariantMap", QMetaType::QVariantMap}, {"QVariantList", QMetaType::QVariantList}, {"QString", QMetaType::QString}, {"QStringList", QMetaType::QStringList}, {"QByteArray", QMetaType::QByteArray}, {"QBitArray", QMetaType::QBitArray}, {"QDate", QMetaType::QDate}, {"QTime", QMetaType::QTime}, {"QDateTime", QMetaType::QDateTime}, {"QUrl", QMetaType::QUrl}, {"QLocale", QMetaType::QLocale}, {"QRect", QMetaType::QRect}, {"QRectF", QMetaType::QRectF}, {"QSize", QMetaType::QSize}, {"QSizeF", QMetaType::QSizeF}, {"QLine", QMetaType::QLine}, {"QLineF", QMetaType::QLineF}, {"QPoint", QMetaType::QPoint}, {"QPointF", QMetaType::QPointF}, {"QRegExp", QMetaType::QRegExp}, /* All GUI types */ {"QColorGroup", 63}, {"QFont", QMetaType::QFont}, {"QPixmap", QMetaType::QPixmap}, {"QBrush", QMetaType::QBrush}, {"QColor", QMetaType::QColor}, {"QPalette", QMetaType::QPalette}, {"QIcon", QMetaType::QIcon}, {"QImage", QMetaType::QImage}, {"QPolygon", QMetaType::QPolygon}, {"QRegion", QMetaType::QRegion}, {"QBitmap", QMetaType::QBitmap}, {"QCursor", QMetaType::QCursor}, {"QSizePolicy", QMetaType::QSizePolicy}, {"QKeySequence", QMetaType::QKeySequence}, {"QPen", QMetaType::QPen}, {"QTextLength", QMetaType::QTextLength}, {"QTextFormat", QMetaType::QTextFormat}, {"QMatrix", QMetaType::QMatrix}, /* All Metatype builtins */ {"void*", QMetaType::VoidStar}, {"long", QMetaType::Long}, {"short", QMetaType::Short}, {"char", QMetaType::Char}, {"ulong", QMetaType::ULong}, {"ushort", QMetaType::UShort}, {"uchar", QMetaType::UChar}, {"float", QMetaType::Float}, {"QObject*", QMetaType::QObjectStar}, {"QWidget*", QMetaType::QWidgetStar}, /* Type aliases - order doesn't matter */ {"unsigned long", QMetaType::ULong}, {"unsigned int", QMetaType::UInt}, {"unsigned short", QMetaType::UShort}, {"unsigned char", QMetaType::UChar}, {"qint8", QMetaType::Char}, {"quint8", QMetaType::UChar}, {"qint16", QMetaType::Short}, {"quint16", QMetaType::UShort}, {"qint32", QMetaType::Int}, {"quint32", QMetaType::UInt}, {"qint64", QMetaType::LongLong}, {"quint64", QMetaType::ULongLong}, {"QList<QVariant>", QMetaType::QVariantList}, {"QMap<QString,QVariant>", QMetaType::QVariantMap}, // let QMetaTypeId2 figure out the type at compile time {"qreal", QMetaTypeId2<qreal>::MetaType}, {0, QMetaType::Void}};struct QMetaTypeGuiHelper{ QMetaType::Constructor constr; QMetaType::Destructor destr;#ifndef QT_NO_DATASTREAM QMetaType::SaveOperator saveOp; QMetaType::LoadOperator loadOp;#endif};Q_CORE_EXPORT const QMetaTypeGuiHelper *qMetaTypeGuiHelper = 0;class QCustomTypeInfo{public: QCustomTypeInfo() : typeName(), constr(0), destr(0)#ifndef QT_NO_DATASTREAM , saveOp(0), loadOp(0)#endif {} QByteArray typeName; QMetaType::Constructor constr; QMetaType::Destructor destr;#ifndef QT_NO_DATASTREAM QMetaType::SaveOperator saveOp; QMetaType::LoadOperator loadOp;#endif};Q_DECLARE_TYPEINFO(QCustomTypeInfo, Q_MOVABLE_TYPE);Q_GLOBAL_STATIC(QVector<QCustomTypeInfo>, customTypes)Q_GLOBAL_STATIC(QReadWriteLock, customTypesLock)#ifndef QT_NO_DATASTREAM/*! \internal*/void QMetaType::registerStreamOperators(const char *typeName, SaveOperator saveOp, LoadOperator loadOp){ int idx = type(typeName); if (!idx) return; QVector<QCustomTypeInfo> *ct = customTypes(); if (!ct) return; QWriteLocker locker(customTypesLock()); QCustomTypeInfo &inf = (*ct)[idx - User]; inf.saveOp = saveOp; inf.loadOp = loadOp;}#endif/*! Returns the type name associated with the given \a type, or 0 if no matching type was found. The returned pointer must not be deleted. \sa type(), isRegistered(), Type*/const char *QMetaType::typeName(int type){ enum { GuiTypeCount = LastGuiType - FirstGuiType }; if (type >= 0 && type <= LastCoreType) { return types[type].typeName; } else if (type >= FirstGuiType && type <= LastGuiType) { return types[type - FirstGuiType + LastCoreType + 1].typeName; } else if (type >= FirstCoreExtType && type <= LastCoreExtType) { return types[type - FirstCoreExtType + GuiTypeCount + LastCoreType + 2].typeName; } else if (type >= User) { const QVector<QCustomTypeInfo> * const ct = customTypes(); QReadLocker locker(customTypesLock()); return ct && ct->count() > type - User ? ct->at(type - User).typeName.constData() : static_cast<const char *>(0); } return 0;}/*! \internal Same as QMetaType::type(), but doesn't lock the mutex.*/static int qMetaTypeType_unlocked(const QByteArray &typeName){ int i = 0; while (types[i].typeName && strcmp(typeName.constData(), types[i].typeName)) ++i; if (!types[i].type) { const QVector<QCustomTypeInfo> * const ct = customTypes(); if (!ct) return 0; for (int v = 0; v < ct->count(); ++v) { if (ct->at(v).typeName == typeName) return v + QMetaType::User; } } return types[i].type;}/*! \internal Registers a user type for marshalling, with \a typeName, a \a destructor, and a \a constructor. Returns the type's handle, or -1 if the type could not be registered. */int QMetaType::registerType(const char *typeName, Destructor destructor, Constructor constructor){ QVector<QCustomTypeInfo> *ct = customTypes(); if (!ct || !typeName || !destructor || !constructor) return -1;#ifdef QT_NO_QOBJECT ::QByteArray normalizedTypeName = typeName;#else ::QByteArray normalizedTypeName = QMetaObject::normalizedType(typeName);#endif QWriteLocker locker(customTypesLock()); static int currentIdx = User; int idx = qMetaTypeType_unlocked(normalizedTypeName); if (!idx) { idx = currentIdx++; ct->resize(ct->count() + 1); QCustomTypeInfo &inf = (*ct)[idx - User];
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -