diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index b908fc3d25..5e2a83cf51 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -654,17 +654,16 @@ namespace QtMetaTypePrivate { struct VariantData { - VariantData(const int metaTypeId_, + VariantData(QMetaType metaType_, const void *data_, const uint flags_) - : metaTypeId(metaTypeId_) + : metaType(std::move(metaType_)) , data(data_) , flags(flags_) { } - VariantData(const VariantData &other) - : metaTypeId(other.metaTypeId), data(other.data), flags(other.flags){} - const int metaTypeId; + VariantData(const VariantData &other) = delete; + const QMetaType metaType; const void *data; const uint flags; private: @@ -868,7 +867,7 @@ class QSequentialIterableImpl public: const void * _iterable; void *_iterator; - int _metaType_id; + QMetaType _metaType; uint _metaType_flags; uint _iteratorCapabilities; // Iterator capabilities looks actually like @@ -881,7 +880,7 @@ public: enum Position { ToBegin, ToEnd }; typedef void (*moveIteratorFunc)(const void *p, void **, Position position); typedef void (*advanceFunc)(void **p, int); - typedef VariantData (*getFunc)( void * const *p, int metaTypeId, uint flags); + typedef VariantData (*getFunc)( void * const *p, const QMetaType &metaType, uint flags); typedef void (*destroyIterFunc)(void **p); typedef bool (*equalIterFunc)(void * const *p, void * const *other); typedef void (*copyIterFunc)(void **, void * const *); @@ -931,14 +930,14 @@ public: } template - static VariantData getImpl(void * const *iterator, int metaTypeId, uint flags) - { return VariantData(metaTypeId, IteratorOwner::getData(iterator), flags); } + static VariantData getImpl(void * const *iterator, const QMetaType &metaType, uint flags) + { return VariantData(metaType, IteratorOwner::getData(iterator), flags); } public: template QSequentialIterableImpl(const T*p) : _iterable(p) , _iterator(nullptr) - , _metaType_id(qMetaTypeId()) + , _metaType(QMetaType::fromType()) , _metaType_flags(QTypeInfo::isPointer) , _iteratorCapabilities(ContainerAPI::IteratorCapabilities | (0 << 4) | (ContainerCapabilitiesImpl::ContainerCapabilities << (4+3))) , _size(sizeImpl) @@ -956,7 +955,6 @@ public: QSequentialIterableImpl() : _iterable(nullptr) , _iterator(nullptr) - , _metaType_id(QMetaType::UnknownType) , _metaType_flags(0) , _iteratorCapabilities(0 | (0 << 4) ) // no iterator capabilities, revision 0 , _size(nullptr) @@ -989,10 +987,10 @@ public: _append(_iterable, newElement); } - inline VariantData getCurrent() const { return _get(&_iterator, _metaType_id, _metaType_flags); } + inline VariantData getCurrent() const { return _get(&_iterator, _metaType, _metaType_flags); } VariantData at(int idx) const - { return VariantData(_metaType_id, _at(_iterable, idx), _metaType_flags); } + { return VariantData(_metaType, _at(_iterable, idx), _metaType_flags); } int size() const { Q_ASSERT(_iterable); return _size(_iterable); } @@ -1058,15 +1056,15 @@ class QAssociativeIterableImpl public: const void *_iterable; void *_iterator; - int _metaType_id_key; + QMetaType _metaType_key; + QMetaType _metaType_value; uint _metaType_flags_key; - int _metaType_id_value; uint _metaType_flags_value; typedef int(*sizeFunc)(const void *p); typedef void (*findFunc)(const void *container, const void *p, void **iterator); typedef void (*beginFunc)(const void *p, void **); typedef void (*advanceFunc)(void **p, int); - typedef VariantData (*getFunc)(void * const *p, int metaTypeId, uint flags); + typedef VariantData (*getFunc)(void * const *p, const QMetaType &metaTypeId, uint flags); typedef void (*destroyIterFunc)(void **p); typedef bool (*equalIterFunc)(void * const *p, void * const *other); typedef void (*copyIterFunc)(void **, void * const *); @@ -1105,20 +1103,20 @@ public: { IteratorOwner::assign(iterator, static_cast(container)->end()); } template - static VariantData getKeyImpl(void * const *iterator, int metaTypeId, uint flags) - { return VariantData(metaTypeId, &AssociativeContainerAccessor::getKey(*static_cast(*iterator)), flags); } + static VariantData getKeyImpl(void * const *iterator, const QMetaType &metaType, uint flags) + { return VariantData(metaType, &AssociativeContainerAccessor::getKey(*static_cast(*iterator)), flags); } template - static VariantData getValueImpl(void * const *iterator, int metaTypeId, uint flags) - { return VariantData(metaTypeId, &AssociativeContainerAccessor::getValue(*static_cast(*iterator)), flags); } + static VariantData getValueImpl(void * const *iterator, const QMetaType &metaType, uint flags) + { return VariantData(metaType, &AssociativeContainerAccessor::getValue(*static_cast(*iterator)), flags); } public: template QAssociativeIterableImpl(const T*p) : _iterable(p) , _iterator(nullptr) - , _metaType_id_key(qMetaTypeId()) + , _metaType_key(QMetaType::fromType()) + , _metaType_value(QMetaType::fromType()) , _metaType_flags_key(QTypeInfo::isPointer) - , _metaType_id_value(qMetaTypeId()) , _metaType_flags_value(QTypeInfo::isPointer) , _size(sizeImpl) , _find(findImpl) @@ -1136,9 +1134,7 @@ public: QAssociativeIterableImpl() : _iterable(nullptr) , _iterator(nullptr) - , _metaType_id_key(QMetaType::UnknownType) , _metaType_flags_key(0) - , _metaType_id_value(QMetaType::UnknownType) , _metaType_flags_value(0) , _size(nullptr) , _find(nullptr) @@ -1160,8 +1156,8 @@ public: inline void destroyIter() { _destroyIter(&_iterator); } - inline VariantData getCurrentKey() const { return _getKey(&_iterator, _metaType_id_key, _metaType_flags_key); } - inline VariantData getCurrentValue() const { return _getValue(&_iterator, _metaType_id_value, _metaType_flags_value); } + inline VariantData getCurrentKey() const { return _getKey(&_iterator, _metaType_key, _metaType_flags_key); } + inline VariantData getCurrentValue() const { return _getValue(&_iterator, _metaType_value, _metaType_flags_value); } inline void find(const VariantData &key) { _find(_iterable, key.data, &_iterator); } @@ -1188,29 +1184,29 @@ struct QAssociativeIterableConvertFunctor class QPairVariantInterfaceImpl { const void *_pair; - int _metaType_id_first; + QMetaType _metaType_first; + QMetaType _metaType_second; uint _metaType_flags_first; - int _metaType_id_second; uint _metaType_flags_second; - typedef VariantData (*getFunc)(const void * const *p, int metaTypeId, uint flags); + typedef VariantData (*getFunc)(const void * const *p, const QMetaType &metaType, uint flags); getFunc _getFirst; getFunc _getSecond; template - static VariantData getFirstImpl(const void * const *pair, int metaTypeId, uint flags) - { return VariantData(metaTypeId, &static_cast(*pair)->first, flags); } + static VariantData getFirstImpl(const void * const *pair, const QMetaType &metaType, uint flags) + { return VariantData(metaType, &static_cast(*pair)->first, flags); } template - static VariantData getSecondImpl(const void * const *pair, int metaTypeId, uint flags) - { return VariantData(metaTypeId, &static_cast(*pair)->second, flags); } + static VariantData getSecondImpl(const void * const *pair, const QMetaType &metaType, uint flags) + { return VariantData(metaType, &static_cast(*pair)->second, flags); } public: template QPairVariantInterfaceImpl(const T*p) : _pair(p) - , _metaType_id_first(qMetaTypeId()) + , _metaType_first(QMetaType::fromType()) + , _metaType_second(QMetaType::fromType()) , _metaType_flags_first(QTypeInfo::isPointer) - , _metaType_id_second(qMetaTypeId()) , _metaType_flags_second(QTypeInfo::isPointer) , _getFirst(getFirstImpl) , _getSecond(getSecondImpl) @@ -1219,17 +1215,15 @@ public: QPairVariantInterfaceImpl() : _pair(nullptr) - , _metaType_id_first(QMetaType::UnknownType) , _metaType_flags_first(0) - , _metaType_id_second(QMetaType::UnknownType) , _metaType_flags_second(0) , _getFirst(nullptr) , _getSecond(nullptr) { } - inline VariantData first() const { return _getFirst(&_pair, _metaType_id_first, _metaType_flags_first); } - inline VariantData second() const { return _getSecond(&_pair, _metaType_id_second, _metaType_flags_second); } + inline VariantData first() const { return _getFirst(&_pair, _metaType_first, _metaType_flags_first); } + inline VariantData second() const { return _getSecond(&_pair, _metaType_second, _metaType_flags_second); } }; QT_METATYPE_PRIVATE_DECLARE_TYPEINFO(QPairVariantInterfaceImpl, Q_MOVABLE_TYPE) diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index bc95a20e39..741a477860 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -4245,12 +4245,12 @@ QSequentialIterable::const_iterator QSequentialIterable::end() const static const QVariant variantFromVariantDataHelper(const QtMetaTypePrivate::VariantData &d) { QVariant v; - if (d.metaTypeId == qMetaTypeId()) + if (d.metaType == QMetaType::fromType()) v = *reinterpret_cast(d.data); else - v = QVariant(QMetaType(d.metaTypeId), d.data); + v = QVariant(d.metaType, d.data); if (d.flags & QVariantConstructionFlags::ShouldDeleteVariantData) - QMetaType::destroy(d.metaTypeId, const_cast(d.data)); + d.metaType.destroy(const_cast(d.data)); return v; } @@ -4533,8 +4533,8 @@ void QAssociativeIterable::const_iterator::end() void QAssociativeIterable::const_iterator::find(const QVariant &key) { - Q_ASSERT(key.userType() == m_impl._metaType_id_key); - const QtMetaTypePrivate::VariantData dkey(key.userType(), key.constData(), 0 /*key.flags()*/); + Q_ASSERT(key.metaType() == m_impl._metaType_key); + const QtMetaTypePrivate::VariantData dkey(key.metaType(), key.constData(), 0 /*key.flags()*/); m_impl.find(dkey); } @@ -4580,7 +4580,7 @@ QAssociativeIterable::const_iterator QAssociativeIterable::find(const QVariant & { const_iterator it(*this, new QAtomicInt(0)); QVariant key_ = key; - if (key_.canConvert(m_impl._metaType_id_key) && key_.convert(m_impl._metaType_id_key)) + if (key_.canConvert(m_impl._metaType_key.id()) && key_.convert(m_impl._metaType_key.id())) it.find(key_); else it.end(); diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index ed54eaee2b..281080d332 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -817,13 +817,13 @@ namespace QtPrivate { if (QMetaType::hasRegisteredConverterFunction(typeId, qMetaTypeId()) && !(typeId == qMetaTypeId >())) { QtMetaTypePrivate::QPairVariantInterfaceImpl pi = v.value(); const QtMetaTypePrivate::VariantData d1 = pi.first(); - QVariant v1(QMetaType(d1.metaTypeId), d1.data); - if (d1.metaTypeId == qMetaTypeId()) + QVariant v1(d1.metaType, d1.data); + if (d1.metaType == QMetaType::fromType()) v1 = *reinterpret_cast(d1.data); const QtMetaTypePrivate::VariantData d2 = pi.second(); - QVariant v2(QMetaType(d2.metaTypeId), d2.data); - if (d2.metaTypeId == qMetaTypeId()) + QVariant v2(d2.metaType, d2.data); + if (d2.metaType == QMetaType::fromType()) v2 = *reinterpret_cast(d2.data); return QPair(v1, v2); diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index af0d1e0cf1..b1cf955e93 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -4515,7 +4515,7 @@ void tst_QVariant::shouldDeleteVariantDataWorksForSequential() iterator._metaType_flags = QVariantConstructionFlags::ShouldDeleteVariantData; iterator._size = [](const void *) {return 1;}; - iterator._metaType_id = qMetaTypeId(); + iterator._metaType = QMetaType::fromType(); iterator._moveTo = [](const void *, void **, QtMetaTypePrivate::QSequentialIterableImpl::Position) {}; iterator._append = [](const void *, const void *) {}; iterator._advance = [](void **, int) {}; @@ -4526,9 +4526,9 @@ void tst_QVariant::shouldDeleteVariantDataWorksForSequential() MyType mytype {1, "eins"}; return QMetaType::create(qMetaTypeId(), &mytype); }; - iterator._get = [](void * const *, int, uint) -> QtMetaTypePrivate::VariantData { + iterator._get = [](void * const *, const QMetaType &, uint) -> QtMetaTypePrivate::VariantData { MyType mytype {2, "zwei"}; - return {qMetaTypeId(), QMetaType::create(qMetaTypeId(), &mytype), QVariantConstructionFlags::ShouldDeleteVariantData}; + return {QMetaType::fromType(), QMetaType::create(qMetaTypeId(), &mytype), QVariantConstructionFlags::ShouldDeleteVariantData}; }; QSequentialIterable iterable {iterator}; QVariant value1 = iterable.at(0); @@ -4550,8 +4550,8 @@ void tst_QVariant::shouldDeleteVariantDataWorksForAssociative() iterator._metaType_flags_value = QVariantConstructionFlags::ShouldDeleteVariantData; iterator._size = [](const void *) {return 1;}; - iterator._metaType_id_value = qMetaTypeId(); - iterator._metaType_id_key = qMetaTypeId(); + iterator._metaType_value = QMetaType::fromType(); + iterator._metaType_key = QMetaType::fromType(); iterator._begin = [](const void *, void **) {}; iterator._end = [](const void *, void **) {}; iterator._advance = [](void **, int) {}; @@ -4561,21 +4561,21 @@ void tst_QVariant::shouldDeleteVariantDataWorksForAssociative() iterator._find = [](const void *, const void *, void **iterator ) -> void { (*iterator) = reinterpret_cast(quintptr(42)); }; - iterator._getKey = [](void * const *iterator, int, uint) -> QtMetaTypePrivate::VariantData { + iterator._getKey = [](void * const *iterator, const QMetaType &, uint) -> QtMetaTypePrivate::VariantData { MyType mytype {1, "key"}; if (reinterpret_cast(*iterator) == 42) { mytype.number = 42; mytype.text = "find_key"; } - return {qMetaTypeId(), QMetaType::create(qMetaTypeId(), &mytype), QVariantConstructionFlags::ShouldDeleteVariantData}; + return {QMetaType::fromType(), QMetaType::create(qMetaTypeId(), &mytype), QVariantConstructionFlags::ShouldDeleteVariantData}; }; - iterator._getValue = [](void * const *iterator, int, uint) -> QtMetaTypePrivate::VariantData { + iterator._getValue = [](void * const *iterator, const QMetaType &, uint) -> QtMetaTypePrivate::VariantData { MyType mytype {2, "value"}; if (reinterpret_cast(*iterator) == 42) { mytype.number = 42; mytype.text = "find_value"; } - return {qMetaTypeId(), QMetaType::create(qMetaTypeId(), &mytype), QVariantConstructionFlags::ShouldDeleteVariantData}; + return {QMetaType::fromType(), QMetaType::create(qMetaTypeId(), &mytype), QVariantConstructionFlags::ShouldDeleteVariantData}; }; QAssociativeIterable iterable {iterator}; auto it = iterable.begin();