QProtobufMessage: Extract Methods Private::metaType()

One overload each for string and field-info lookup.

These will be used in a subsequent commit to add rvalue-QVariant
overloads, but commit this separately to avoid introducing
unnecessary differences to our LTS branch, by picking to 6.5.

Task-number: QTBUG-112762
Pick-to: 6.5
Change-Id: I87951851f32a1fa44caa48baa0bdaf0068154781
Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
This commit is contained in:
Marc Mutz 2023-05-02 11:36:51 +02:00
parent aeda214b91
commit dc69102f4f
2 changed files with 38 additions and 14 deletions

View File

@ -61,6 +61,25 @@ void QProtobufMessagePrivate::storeUnknownEntry(QByteArrayView entry)
++unknownEntries[entry.toByteArray()]; ++unknownEntries[entry.toByteArray()];
} }
std::optional<QMetaProperty> QProtobufMessagePrivate::metaProperty(QAnyStringView name) const
{
const int index = getPropertyIndex(name);
const QMetaProperty property = metaObject->property(index);
if (property.isValid())
return property;
return std::nullopt;
}
std::optional<QMetaProperty>
QProtobufMessagePrivate::metaProperty(QtProtobufPrivate::QProtobufPropertyOrderingInfo info) const
{
const int propertyIndex = info.getPropertyIndex() + metaObject->propertyOffset();
const QMetaProperty metaProperty = metaObject->property(propertyIndex);
if (metaProperty.isValid())
return metaProperty;
return std::nullopt;
}
/*! /*!
Set the property \a propertyName to the value stored in \a value. Set the property \a propertyName to the value stored in \a value.
@ -74,11 +93,10 @@ bool QProtobufMessage::setProperty(QAnyStringView propertyName, const QVariant &
{ {
Q_D(QProtobufMessage); Q_D(QProtobufMessage);
int index = d->getPropertyIndex(propertyName); if (auto mp = d->metaProperty(propertyName))
const QMetaProperty &property = d->metaObject->property(index); return mp->writeOnGadget(this, value);
if (!property.isValid())
return false; return false;
return property.writeOnGadget(this, value);
} }
/*! /*!
@ -90,9 +108,9 @@ QVariant QProtobufMessage::property(QAnyStringView propertyName) const
{ {
Q_D(const QProtobufMessage); Q_D(const QProtobufMessage);
int index = d->getPropertyIndex(propertyName); if (const auto mp = d->metaProperty(propertyName))
const QMetaProperty &property = d->metaObject->property(index); return mp->readOnGadget(this);
return property.readOnGadget(this); return false;
} }
/*! /*!
@ -214,13 +232,11 @@ QProtobufMessage::property(const QtProtobufPrivate::QProtobufPropertyOrderingInf
bool QProtobufMessage::setProperty( bool QProtobufMessage::setProperty(
const QtProtobufPrivate::QProtobufPropertyOrderingInfo &fieldInfo, const QVariant &value) const QtProtobufPrivate::QProtobufPropertyOrderingInfo &fieldInfo, const QVariant &value)
{ {
int propertyIndex = fieldInfo.getPropertyIndex() + metaObject()->propertyOffset(); Q_D(QProtobufMessage);
QMetaProperty metaProperty = metaObject()->property(propertyIndex); const auto mp = d->metaProperty(fieldInfo);
if (!mp)
if (!metaProperty.isValid())
return false; return false;
return mp->writeOnGadget(this, value);
return metaProperty.writeOnGadget(this, value);
} }
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -23,8 +23,12 @@
#include <QtCore/qhash.h> #include <QtCore/qhash.h>
#include <QtCore/private/qglobal_p.h> #include <QtCore/private/qglobal_p.h>
#include <optional>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QMetaProperty;
class QProtobufMessagePrivate class QProtobufMessagePrivate
{ {
public: public:
@ -38,6 +42,10 @@ public:
int getPropertyIndex(QAnyStringView propertyName) const; int getPropertyIndex(QAnyStringView propertyName) const;
void storeUnknownEntry(QByteArrayView entry); void storeUnknownEntry(QByteArrayView entry);
std::optional<QMetaProperty> metaProperty(QAnyStringView name) const;
std::optional<QMetaProperty>
metaProperty(QtProtobufPrivate::QProtobufPropertyOrderingInfo ord) const;
static QProtobufMessagePrivate *get(QProtobufMessage *message) { return message->d_func(); } static QProtobufMessagePrivate *get(QProtobufMessage *message) { return message->d_func(); }
static const QProtobufMessagePrivate *get(const QProtobufMessage *message) static const QProtobufMessagePrivate *get(const QProtobufMessage *message)
{ {