(1) 首先给出其定义:

(2)

++

++

(3) class Q_CORE_EXPORT QMetaType 的完整的整合了注释的源代码:
class Q_CORE_EXPORT QMetaType //重新整理这个重要的基础类
{
public:
#ifndef Q_CLANG_QDOC //未定义此宏,这部分是重要的。
// The code that actually gets compiled.
enum Type { //定义了一个类内部的枚举类型
// these are merged with QVariant
QT_FOR_EACH_STATIC_TYPE(QT_DEFINE_METATYPE_ID)
FirstCoreType = Bool, //= 1
LastCoreType = QVariantPair, //= 58
FirstGuiType = QFont, //=0x1000
LastGuiType = QColorSpace, //=0x1017
FirstWidgetsType = QSizePolicy, //=0x2000
LastWidgetsType = QSizePolicy, //=0x2000
HighestInternalId = LastWidgetsType, //=0x2000
QReal = sizeof(qreal) == sizeof(double) ? Double : Float,
//这里是在依托机器平台定义浮点数的默认类型
UnknownType = 0,
User = 65536
};
#else //此 else 分支是无用的,仅仅为了生成 doc 文档
// If we are using QDoc it fakes the Type enum looks like this.
enum Type {
UnknownType = 0, Bool = 1, Int = 2, UInt = 3, LongLong = 4, ULongLong = 5,
Double = 6, Long = 32, Short = 33, Char = 34, ULong = 35, UShort = 36,
UChar = 37, Float = 38,
VoidStar = 31,
QChar = 7, QString = 10, QStringList = 11, QByteArray = 12,
QBitArray = 13, QDate = 14, QTime = 15, QDateTime = 16, QUrl = 17,
QLocale = 18, QRect = 19, QRectF = 20, QSize = 21, QSizeF = 22,
QLine = 23, QLineF = 24, QPoint = 25, QPointF = 26,
QEasingCurve = 29, QUuid = 30, QVariant = 41, QModelIndex = 42,
QPersistentModelIndex = 50, QRegularExpression = 44,
QJsonValue = 45, QJsonObject = 46, QJsonArray = 47, QJsonDocument = 48,
QByteArrayList = 49, QObjectStar = 39, SChar = 40,
Void = 43,
Nullptr = 51,
QVariantMap = 8, QVariantList = 9, QVariantHash = 28,
QCborSimpleType = 52, QCborValue = 53, QCborArray = 54, QCborMap = 55,
Char16 = 56, Char32 = 57,
// Gui types
QFont = 0x1000, QPixmap = 0x1001, QBrush = 0x1002, QColor = 0x1003, QPalette = 0x1004,
QIcon = 0x1005, QImage = 0x1006, QPolygon = 0x1007, QRegion = 0x1008, QBitmap = 0x1009,
QCursor = 0x100a, QKeySequence = 0x100b, QPen = 0x100c, QTextLength = 0x100d, QTextFormat = 0x100e,
QTransform = 0x1010, QMatrix4x4 = 0x1011, QVector2D = 0x1012,
QVector3D = 0x1013, QVector4D = 0x1014, QQuaternion = 0x1015, QPolygonF = 0x1016, QColorSpace = 0x1017,
// Widget types
QSizePolicy = 0x2000,
LastCoreType = Char32,
LastGuiType = QColorSpace,
User = 65536
};
#endif //#ifndef Q_CLANG_QDOC
enum TypeFlag //又定义了一个类中类,描述与区分类的各种属性
{
NeedsConstruction = 0x1, //需要构造
NeedsDestruction = 0x2, //需要析构
RelocatableType = 0x4, //可重定位
#if QT_DEPRECATED_SINCE(6, 0) //经测试是定义了这个宏的
MovableType Q_DECL_ENUMERATOR_DEPRECATED_X(
"Use RelocatableType instead.") = RelocatableType,
#endif
PointerToQObject = 0x8 , //对象指针
IsEnumeration = 0x10, //是枚举量
SharedPointerToQObject = 0x20, //对象的共享指针
WeakPointerToQObject = 0x40, //对象的弱指针
TrackingPointerToQObject = 0x80 ,//对象的跟踪指针
IsUnsignedEnumeration = 0x100, //无符号枚举类型?
IsGadget = 0x200, //Gadget 是单词:小工具
//Q_Gadget宏 Q_Gadget宏是Q_OBJECT宏的轻量级版本,
//适用于不继承 QObject但仍希望使用 QMetaObject提供的一些反射功能的类。
//Q_Gadgets可以有Q_ENUM、Q_PROPERTY和Q_INVOKABLE,但它们不能使用信号和槽。
PointerToGadget = 0x400,
IsPointer = 0x800 , //本对象是指针
IsQmlList = 0x1000, // QML list
// used in the QML engine to recognize
// QQmlListProperty<T> and list<T>
IsConst = 0x2000, //是常量
}; //enum TypeFlag
//这些都是为了描述数据的类型,而全面定义的数据类型枚举类
Q_DECLARE_FLAGS(TypeFlags, TypeFlag) //以后可以使用 TypeFlags类
//即完成了类型定义 QFlags<TypeFlag> = TypeFlags 加强了
//Q_DECLARE_FLAGS(Flags, Enum) = typedef QFlags<Enum> Flags;
static void registerNormalizedTypedef( //无官方注释
const QT_PREPEND_NAMESPACE(QByteArray) &normalizedTypeName,
QMetaType type);
#if QT_DEPRECATED_SINCE(6, 0) //本宏定义是有的
//# define QT_DEPRECATED_VERSION_6_0 QT_DEPRECATED
QT_DEPRECATED_VERSION_6_0 //type
static int type(const char *typeName)
{ return QMetaType::fromName(typeName).id(); }
QT_DEPRECATED_VERSION_6_0 //总之这些函数都要被废弃,不仔细看了
static int type( //type
const QT_PREPEND_NAMESPACE(QByteArray) &typeName)
{ return QMetaType::fromName(typeName).id(); }
QT_DEPRECATED_VERSION_6_0 //typeName
static const char * typeName(int type)
{ return QMetaType(type).name(); }
QT_DEPRECATED_VERSION_6_0
static int sizeOf(int type) //sizeOf
{ return QMetaType(type).sizeOf(); }
QT_DEPRECATED_VERSION_6_0 //typeFlags
static TypeFlags typeFlags(int type)
{ return QMetaType(type).flags(); }
QT_DEPRECATED_VERSION_6_0 //metaObjectForType
static const QMetaObject *metaObjectForType(int type)
{ return QMetaType(type).metaObject(); }
QT_DEPRECATED_VERSION_6_0 //create
static void *create(int type, const void *copy = nullptr)
{ return QMetaType(type).create(copy); }
QT_DEPRECATED_VERSION_6_0 //destroy
static void destroy(int type, void *data)
{ return QMetaType(type).destroy(data); }
QT_DEPRECATED_VERSION_6_0
static void *construct( //construct
int type, void *where, const void *copy)
{ return QMetaType(type).construct(where, copy); }
QT_DEPRECATED_VERSION_6_0 //destruct
static void destruct(int type, void *where)
{ return QMetaType(type).destruct(where); }
#endif //#if QT_DEPRECATED_SINCE(6, 0)
private:
int idHelper() const ; //定义了私有的成员函数
friend class QVariant; //友元类
//本类的数据成员是一个对象指针
const QtPrivate::QMetaTypeInterface * d_ptr = nullptr;
public :
//如果形参里的数据类型已被注册,则返回 true;否则返回 false。
static bool isRegistered(int type);
constexpr QMetaType() = default;//默认构造函数
//Constructs a QMetaType object
//that contains all information about 形参的 type类型.
explicit QMetaType(int type);
explicit constexpr QMetaType //有参构造函数
(const QtPrivate::QMetaTypeInterface *d)
: d_ptr(d) {}
//如果此 QMetaType对象包含有关类型的有效信息,则返回true,
bool isValid() const; //否则返回false。
//如果此QMetaType对象包含有关类型的有效信息,则返回true,
bool isRegistered() const; //否则返回false。
//可见,上面的俩函数是一样的
#if defined(QT_QMETATYPE_BC_COMPAT) || defined(Q_QDOC)
int id() const;
#else
//Returns id type hold by this QMetatype instance.
// Ot7:移除行外版本未使用的int参数痕迹,以避免 ODR 违反
// ### Qt 7: Remove traces of out of line version
// unused int parameter is used to avoid ODR violation
int id(int = 0) const //总之就是 id() 函数
{
if (d_ptr) {
if (int id = d_ptr->typeId.loadRelaxed())
return id;
return idHelper();
}
return 0;
};
#endif
//Returns the size of the given type
//返回本对象封装的类型的大小
constexpr qsizetype sizeOf() const;
///Returns the alignment of the type in bytes
//(即alignof(T)其中T是构造此QMetaType实例的实际类型)
//这个函数通常与构造函数一起使用,用于对类型使用的内存进行低级管理。
//(i.e. alignof(T), where T is the actual type for which
//this QMetaType instance was constructed for).
//This function is typically used together with construct()
//to perform low-level management of the memory used by a type.
constexpr qsizetype alignOf() const;
//Returns flags of the type for which
//this QMetaType instance was constructed.
constexpr TypeFlags flags() const;
//return a QMetaObject relative to this type.
constexpr const QMetaObject * metaObject() const;
//Returns the type name associated with this QMetaType,
//or a null pointer if no matching type was found.
//The returned pointer must not be deleted.
constexpr const char * name() const;
//Returns a copy of copy
void *create(const void * copy = nullptr) const;
//好像是要析构形参指针处的数据。目前无官方文档
void destroy(void *data) const; //本函似乎要回收内存
void destruct(void *data) const; //本函仅仅析构数据
//在地址 where 处构造变量的感觉
void *construct(void *where, const void *copy = nullptr) const;
//Returns
//QPartialOrdering :: Unordered
//if comparison is not supported or the values are unordered.
//Otherwise, returns
//QPartialOrdering :: Less,
//QPartialOrdering :: Equivalent or
//QPartialOrdering :: Greater
QPartialOrdering compare(const void *lhs, const void *rhs) const;
//Comparison is only supported if the type's less than operator
//was visible to the metatype declaration.
bool equals(const void *lhs, const void *rhs) const;
//Returns true if a less than or equality operator
//for the type described by this metatype
//was visible to the metatype declaration, otherwise false.
bool isEqualityComparable() const;
//Returns true if a less than operator for the type described by
//this metatype was visible to the metatype declaration,
//otherwise false.
bool isOrdered() const; //这些成员函数,关注的是本类型是否支持排序
#ifndef QT_NO_DATASTREAM
//Writes the object pointed to by data to the given stream.
//Returns true if the object is saved successfully;
//otherwise returns false.
//The type must have been registered with
// Q_DECLARE_METATYPE() beforehand.
//Normally, you should not need to call this function directly.
//Instead, use QVariant's operator<<(), 比如封装了字符串链表类型
//which relies on save() to stream custom types.
bool save(QDataStream &stream, const void *data) const;
//Reads the object of this type from the given stream into data.
bool load(QDataStream &stream, void *data) const;
//Returns true, if the meta type system has registered
//data stream operators for this meta type. 即 <<、>> 运算符
bool hasRegisteredDataStreamOperators() const;
#if QT_DEPRECATED_SINCE(6, 0) //下面的函数无用了
QT_DEPRECATED_VERSION_6_0
static bool save(QDataStream &stream,
int type, const void *data)
{ return QMetaType(type).save(stream, data); }
QT_DEPRECATED_VERSION_6_0
static bool load(QDataStream &stream, int type, void *data)
{ return QMetaType(type).load(stream, data); }
#endif //#if QT_DEPRECATED_SINCE(6, 0)
#endif //#ifndef QT_NO_DATASTREAM
//Returns the QMetaType corresponding to the type
//in the template parameter.
//似乎是要生成一个封装了 模板参数 T 类型的本 QMetaType对象
template<typename T> //静态的模板成员函数
constexpr static QMetaType fromType();
//Returns a QMetaType matching typeName.
//The returned object is not valid
//if the typeName is not known to QMetaType
static QMetaType fromName(QByteArrayView name);
//基于类型的文本名得到一个 QMetaType 对象
//定义友元类,说明本类支持 == 与 != 运算符
friend bool operator==(QMetaType a, QMetaType b)
{ //只要内部封装的数据内容相同,就是等价
if (a.d_ptr == b.d_ptr) return true;
// one type is undefined, the other is defined
if (!a.d_ptr || !b.d_ptr) return false;
// avoid id call if we already have the id
const int aId = a.id();
const int bId = b.id();
return aId == bId; //比较成员函数 id() 的返回值
} //由此可见 id() 函数非常重要
friend bool operator!=(QMetaType a, QMetaType b)
{ return !(a == b); }
public:
#ifndef QT_NO_DEBUG_STREAM
//Streams the object at rhs to the debug stream dbg.
//Returns true on success, otherwise false.
//把形参2 中的内容输出到控制台的意思
bool debugStream(QDebug& dbg, const void *rhs);
//Returns true, if the meta type system has a
//registered debug stream operator for this meta type.
bool hasRegisteredDebugStreamOperator() const;
#if QT_DEPRECATED_SINCE(6, 0) //被删,不重要不看了
QT_DEPRECATED_VERSION_6_0
static bool debugStream(QDebug& dbg, const void *rhs, int typeId)
{ return QMetaType(typeId).debugStream(dbg, rhs); }
template<typename T>
QT_DEPRECATED_VERSION_6_0
static bool hasRegisteredDebugStreamOperator()
{ return QMetaType::fromType<T>().hasRegisteredDebugStreamOperator(); }
QT_DEPRECATED_VERSION_6_0
static bool hasRegisteredDebugStreamOperator(int typeId)
{ return QMetaType(typeId).hasRegisteredDebugStreamOperator(); }
#endif //#if QT_DEPRECATED_SINCE(6, 0)
#endif //#ifndef QT_NO_DEBUG_STREAM
// type erased converter function
using ConverterFunction = //定义了 std 里的 function对象
std::function<bool(const void *src, void *target)>;
// type erased mutable view, primarily for containers
using MutableViewFunction = //类型擦除可变视图,主要用于容器
std::function<bool(void *src, void *target)>;
//Registers the possibility of an implicit conversion
//from type From to type To in the meta type system.
//Returns true if the registration succeeded, otherwise false.
// implicit conversion supported like double -> float
template<typename From, typename To>
static bool registerConverter()
{
return registerConverter<From, To>(
QtPrivate::convertImplicit<From, To>
);
}
// member function as in "QString QFont::toString() const"
template<typename From, typename To>
static bool registerConverter(To(From::*function)() const)
{
static_assert((!QMetaTypeId2<To> ::IsBuiltIn ||
!QMetaTypeId2<From>::IsBuiltIn),
"QMetaType::registerConverter: "
"At least one of the types must be a custom type.");
const QMetaType fromType = QMetaType::fromType<From>();
const QMetaType toType = QMetaType::fromType<To>();
auto converter = [function](const void *from, void *to) -> bool
{ const From *f = static_cast<const From *>(from);
To *t = static_cast<To *>(to);
*t = (f->*function)();
return true;
};
return registerConverterImpl<From, To>(
converter, fromType, toType);
}
// member function as in "double QString::toDouble(bool *ok = nullptr) const"
template<typename From, typename To>
static bool registerConverter(To(From::*function)(bool*) const)
{
static_assert((!QMetaTypeId2<To>::IsBuiltIn || !QMetaTypeId2<From>::IsBuiltIn),
"QMetaType::registerConverter: At least one of the types must be a custom type.");
const QMetaType fromType = QMetaType::fromType<From>();
const QMetaType toType = QMetaType::fromType<To>();
auto converter = [function](const void *from, void *to) -> bool {
const From *f = static_cast<const From *>(from);
To *t = static_cast<To *>(to);
bool result = true;
*t = (f->*function)(&result);
if (!result)
*t = To();
return result;
};
return registerConverterImpl<From, To>(converter, fromType, toType);
}
// functor or function pointer 这又是几个函数重载,不看了
template<typename From, typename To, typename UnaryFunction>
static bool registerConverter(UnaryFunction function)
{
static_assert((!QMetaTypeId2<To>::IsBuiltIn ||
!QMetaTypeId2<From>::IsBuiltIn),
"QMetaType::registerConverter: "
"At least one of the types must be a custom type.");
const QMetaType fromType = QMetaType::fromType<From>();
const QMetaType toType = QMetaType::fromType<To>();
auto converter = [function = std::move(function)]
(const void *from, void *to) -> bool {
const From *f = static_cast<const From *>(from);
To *t = static_cast<To *>(to);
*t = function(*f);
return true;
};
return registerConverterImpl<From, To>(
std::move(converter), fromType, toType);
}
// member function
template<typename From, typename To>
static bool registerMutableView(To(From::*function)())
{
static_assert((!QMetaTypeId2<To>::IsBuiltIn ||
!QMetaTypeId2<From>::IsBuiltIn),
"QMetaType::registerMutableView: "
"At least one of the types must be a custom type.");
const QMetaType fromType = QMetaType::fromType<From>();
const QMetaType toType = QMetaType::fromType<To> ();
auto view = [function](void *from, void *to) -> bool {
From *f = static_cast<From *>(from);
To *t = static_cast<To *>(to);
*t = (f->*function)();
return true;
};
return registerMutableViewImpl<From, To>(
view, fromType, toType);
}
// functor or function pointer 无官方注释,不懂啥意思
template<typename From, typename To, typename UnaryFunction>
static bool registerMutableView(UnaryFunction function)
{
static_assert((!QMetaTypeId2<To>::IsBuiltIn ||
!QMetaTypeId2<From>::IsBuiltIn),
"QMetaType::registerMutableView: "
"At least one of the types must be a custom type.");
const QMetaType fromType = QMetaType::fromType<From>();
const QMetaType toType = QMetaType::fromType<To> ();
auto view = [function = std::move(function)]
(void *from, void *to) -> bool
{
From *f = static_cast<From *>(from);
To *t = static_cast<To *>(to);
*t = function(*f);
return true;
};
return registerMutableViewImpl<From, To>(
std::move(view), fromType, toType);
}
private:
template<typename From, typename To> //私有函数是不会有注释的
static bool registerConverterImpl(ConverterFunction converter,
QMetaType fromType, QMetaType toType)
{
if (registerConverterFunction(std::move(converter),
fromType, toType))
{
static const auto unregister = qScopeGuard([=] {
unregisterConverterFunction(fromType, toType);
});
return true;
}
else { return false; }
}
template<typename From, typename To>
static bool registerMutableViewImpl(MutableViewFunction view,
QMetaType fromType, QMetaType toType)
{
if (registerMutableViewFunction(std::move(view),
fromType, toType))
{
static const auto unregister = qScopeGuard([=] {
unregisterMutableViewFunction(fromType, toType);
});
return true;
}
else { return false; }
}
public:
//Converts the object at from from fromType
//to the preallocated space at to typed toType.
//Returns true, if the conversion succeeded, otherwise false.
Both from and to have to be valid pointers.
static bool convert
(QMetaType fromType, const void *from, QMetaType toType, void *to);
//Returns true if QMetaType::convert
//can convert from fromType to toType.
static bool canConvert(QMetaType fromType, QMetaType toType);
//Creates a mutable view on the object at from of fromType
//in the preallocated space at to typed toType.
//Returns true if the conversion succeeded, otherwise false.
static bool view(QMetaType fromType, void *from,
QMetaType toType, void *to);
//Returns true if QMetaType::view can create a mutable view of type toType
//on type fromType.
//Converting between pointers of types derived from QObject
//will return true for this function
//if a qobject_cast from the type described by fromType to the type
//described by toType would succeed.
//You can create a mutable view of type QSequentialIterable
//on any container registered with Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE().
//Similarly you can create a mutable view of type QAssociativeIterable
//on any container registered with Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE().
static bool canView(QMetaType fromType, QMetaType toType);
#if QT_DEPRECATED_SINCE(6, 0) //是有此宏定义的
QT_DEPRECATED_VERSION_6_0 //废弃 convert
static bool convert(const void *from, int fromTypeId,
void *to, int toTypeId)
{ return convert(QMetaType(fromTypeId), from,
QMetaType(toTypeId), to);
}
QT_DEPRECATED_VERSION_6_0 // compare
static bool compare(const void *lhs, const void *rhs,
int typeId, int *result)
{
QMetaType t(typeId);
auto c = t.compare(lhs, rhs);
if (c == QPartialOrdering::Unordered) {
*result = 0;
return false;
} else if (c == QPartialOrdering::Less) {
*result = -1;
return true;
} else if (c == QPartialOrdering::Equivalent) {
*result = 0;
return true;
} else {
*result = 1;
return true;
}
}
QT_DEPRECATED_VERSION_6_0 // equals
static bool equals(const void *lhs, const void *rhs,
int typeId, int *result)
{
QMetaType t(typeId);
if (!t.isEqualityComparable())
return false;
*result = t.equals(lhs, rhs) ? 0 : -1;
return true;
}
#endif //#if QT_DEPRECATED_SINCE(6, 0)
//Returns true, if the meta type system
//has a registered conversion from meta type id fromType to toType
template<typename From, typename To> //本函无形参
static bool hasRegisteredConverterFunction()
{ return hasRegisteredConverterFunction(
QMetaType::fromType<From>(),
QMetaType::fromType<To> ()
);
}
static bool hasRegisteredConverterFunction( //重载函数
QMetaType fromType, QMetaType toType );
//Returns true, if the meta type system has a registered
//mutable view on meta type id fromType of meta type id toType.
template<typename From, typename To>
static bool hasRegisteredMutableViewFunction()
{
return hasRegisteredMutableViewFunction(
QMetaType::fromType<From>(), QMetaType::fromType<To>());
}
static bool hasRegisteredMutableViewFunction( //函数重载,略
QMetaType fromType, QMetaType toType);
#ifndef Q_CLANG_QDOC //这个宏确实没有定义
template<typename, bool> //定义了一些友元函数
friend struct QtPrivate::SequentialValueTypeIsMetaType;
//namespace QtPrivate { 这个 QtPrivate 是个命名空间,不是类名称
template<typename, bool>
friend struct QtPrivate::AssociativeValueTypeIsMetaType;
template<typename, bool>
friend struct QtPrivate::IsMetaTypePair;
template<typename, typename>
friend struct QtPrivate::MetaTypeSmartPointerHelper;
#endif //#ifndef Q_CLANG_QDOC
static bool registerConverterFunction( //无官方注释
const ConverterFunction &f, QMetaType from, QMetaType to);
static void unregisterConverterFunction( //无官方注释
QMetaType from, QMetaType to);
static bool registerMutableViewFunction( //无官方注释
const MutableViewFunction &f,
QMetaType from, QMetaType to);
static void unregisterMutableViewFunction( //无官方注释
QMetaType from, QMetaType to);
static void unregisterMetaType(QMetaType type); //无官方注释
const QtPrivate::QMetaTypeInterface * iface() //无官方注释
{ return d_ptr; }
}; //class Q_CORE_EXPORT QMetaType 结束
(4)给出成员函数 canConvert ( fromType,toType) 支持的类型转换方向:



(5)
谢谢
5693

被折叠的 条评论
为什么被折叠?



