📄 qmetatype.cpp
字号:
{ QReadLocker locker(customTypesLock()); return qMetaTypeType_unlocked(typeName);}#ifndef QT_NO_DATASTREAM/*! Writes the object pointed to by \a data with the ID \a type to the given \a stream. The type must have been registered with qRegisterMetaType() and qRegisterMetaTypeStreamOperators() beforehand. Normally, you should not need to call this function directly. Instead, use QVariant's \c operator<<(), which relies on save() to stream custom types. \sa load(), qRegisterMetaTypeStreamOperators()*/bool QMetaType::save(QDataStream &stream, int type, const void *data){ // FIXME - also stream simple types? if (!data || !isRegistered(type)) return false; const QVector<QCustomTypeInfo> * const ct = customTypes(); if (!ct) return false; SaveOperator saveOp = 0; { QReadLocker locker(customTypesLock()); saveOp = ct->at(type - User).saveOp; } if (!saveOp) return false; saveOp(stream, data); return true;}/*! Reads the object of the specified \a type from the given \a stream into \a data. The type must have been registered with qRegisterMetaType() and qRegisterMetaTypeStreamOperators() beforehand. Normally, you should not need to call this function directly. Instead, use QVariant's \c operator>>(), which relies on load() to stream custom types. \sa save(), qRegisterMetaTypeStreamOperators()*/bool QMetaType::load(QDataStream &stream, int type, void *data){ // FIXME - also stream simple types? if (!data || !isRegistered(type)) return false; const QVector<QCustomTypeInfo> * const ct = customTypes(); if (!ct) return false; LoadOperator loadOp = 0; { QReadLocker locker(customTypesLock()); loadOp = ct->at(type - User).loadOp; } if (!loadOp) return false; loadOp(stream, data); return true;}#endif/*! Returns a copy of \a copy, assuming it is of type \a type. If \a copy is zero, creates a default type. \sa destroy(), isRegistered(), Type*/void *QMetaType::construct(int type, const void *copy){ if (copy) { switch(type) { case QMetaType::VoidStar: case QMetaType::QObjectStar: case QMetaType::QWidgetStar: return new void *(*static_cast<void* const *>(copy)); case QMetaType::Long: return new long(*static_cast<const long*>(copy)); case QMetaType::Int: return new int(*static_cast<const int*>(copy)); case QMetaType::Short: return new short(*static_cast<const short*>(copy)); case QMetaType::Char: return new char(*static_cast<const char*>(copy)); case QMetaType::ULong: return new ulong(*static_cast<const ulong*>(copy)); case QMetaType::UInt: return new uint(*static_cast<const uint*>(copy)); case QMetaType::UShort: return new ushort(*static_cast<const ushort*>(copy)); case QMetaType::UChar: return new uchar(*static_cast<const uchar*>(copy)); case QMetaType::Bool: return new bool(*static_cast<const bool*>(copy)); case QMetaType::Float: return new float(*static_cast<const float*>(copy)); case QMetaType::Double: return new double(*static_cast<const double*>(copy)); case QMetaType::QChar: return new ::QChar(*static_cast<const ::QChar*>(copy)); case QMetaType::QByteArray: return new ::QByteArray(*static_cast<const ::QByteArray*>(copy)); case QMetaType::QString: return new ::QString(*static_cast<const ::QString*>(copy)); case QMetaType::Void: return 0; default: ; } } else { switch(type) { case QMetaType::VoidStar: case QMetaType::QObjectStar: case QMetaType::QWidgetStar: return new void *; case QMetaType::Long: return new long; case QMetaType::Int: return new int; case QMetaType::Short: return new short; case QMetaType::Char: return new char; case QMetaType::ULong: return new ulong; case QMetaType::UInt: return new uint; case QMetaType::UShort: return new ushort; case QMetaType::UChar: return new uchar; case QMetaType::Bool: return new bool; case QMetaType::Float: return new float; case QMetaType::Double: return new double; case QMetaType::QChar: return new ::QChar; case QMetaType::QByteArray: return new ::QByteArray; case QMetaType::QString: return new ::QString; case QMetaType::Void: return 0; default: ; } } Constructor constr = 0; { const QVector<QCustomTypeInfo> * const ct = customTypes(); QReadLocker locker(customTypesLock()); if (type < User || !ct || ct->count() <= type - User) return 0; constr = ct->at(type - User).constr; } // unlock to prevent reentrancy return constr(copy);}/*! Destroys the \a data, assuming it is of the \a type given. \sa construct(), isRegistered(), Type*/void QMetaType::destroy(int type, void *data){ if (!data) return; switch(type) { case QMetaType::VoidStar: case QMetaType::QObjectStar: case QMetaType::QWidgetStar: delete static_cast<void**>(data); break; case QMetaType::Long: delete static_cast<long*>(data); break; case QMetaType::Int: delete static_cast<int*>(data); break; case QMetaType::Short: delete static_cast<short*>(data); break; case QMetaType::Char: delete static_cast<char*>(data); break; case QMetaType::ULong: delete static_cast<ulong*>(data); break; case QMetaType::UInt: delete static_cast<uint*>(data); break; case QMetaType::UShort: delete static_cast<ushort*>(data); break; case QMetaType::UChar: delete static_cast<uchar*>(data); break; case QMetaType::Bool: delete static_cast<bool*>(data); break; case QMetaType::Float: delete static_cast<float*>(data); break; case QMetaType::Double: delete static_cast<double*>(data); break; case QMetaType::QChar: delete static_cast< ::QChar*>(data); break; case QMetaType::QByteArray: delete static_cast< ::QByteArray*>(data); break; case QMetaType::QString: delete static_cast< ::QString*>(data); break; case QMetaType::Void: break; default: { const QVector<QCustomTypeInfo> * const ct = customTypes(); Destructor destr = 0; { QReadLocker locker(customTypesLock()); if (type < User || !ct || ct->count() <= type - User) break; destr = ct->at(type - User).destr; } // unlock to prevent reentrancy destr(data); break; } }}/*! \fn int qRegisterMetaType(const char *typeName) \relates QMetaType \threadsafe Registers the type name \a typeName to the type \c{T}. Returns the internal ID used by QMetaType. Any class or struct that has a public constructor, a public copy constructor, and a public destructor can be registered. After a type has been registered, you can create and destroy objects of that type dynamically at run-time. This example registers the class \c{MyClass}: \code qRegisterMetaType<MyClass>("MyClass"); \endcode \sa qRegisterMetaTypeStreamOperators(), QMetaType::isRegistered(), Q_DECLARE_METATYPE()*//*! \fn int qRegisterMetaTypeStreamOperators(const char *typeName) \relates QMetaType \threadsafe Registers the stream operators for the type \c{T} called \a typeName. Afterward, the type can be streamed using QMetaType::load() and QMetaType::save(). These functions are used when streaming a QVariant. \code qRegisterMetaTypeStreamOperator<MyClass>("MyClass"); \endcode The stream operators should have the following signatures: \code QDataStream &operator<<(QDataStream &out, const MyClass &myObj); QDataStream &operator>>(QDataStream &in, MyClass &myObj); \endcode \sa qRegisterMetaType(), QMetaType::isRegistered(), Q_DECLARE_METATYPE()*//*! \typedef QMetaType::Destructor \internal*//*! \typedef QMetaType::Constructor \internal*//*! \typedef QMetaType::SaveOperator \internal*//*! \typedef QMetaType::LoadOperator \internal*//*! \fn int qMetaTypeId() \relates QMetaType \threadsafe \since 4.1 Returns the meta type id of type \c T at compile time. If the type was not declared with Q_DECLARE_METATYPE(), compilation will fail. Typical usage: \code int id = qMetaTypeId<QString>(); // id is now QMetaType::QString id = qMetaTypeId<MyStruct>(); // compile error if MyStruct not declared \endcode QMetaType::type() returns the same ID as qMetaTypeId(), but does a lookup at runtime based on the name of the type. QMetaType::type() is a bit slower, but compilation succeeds if a type is not registered. \sa Q_DECLARE_METATYPE(), QMetaType::type()*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -