Skip to content

Commit

Permalink
QMetaPropertyBuilder: carry metatype
Browse files Browse the repository at this point in the history
Properties of non-dynamic classes always have the metatype stored, so we
can make use of it. Moreover, when the builder is converted into a
metaobject, we need to resolve the metatype anyway.
As a driveby, add a dedicated metatype test to tst_qmetaobjectbuilder.

Change-Id: I7eea0cd8fc2da5d92c7fc803f05cd81e3a9d4cf4
Reviewed-by: Lars Knoll <[email protected]>
  • Loading branch information
Inkane committed Nov 30, 2020
1 parent 130b209 commit 465701b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/corelib/kernel/qmetaobjectbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ class QMetaPropertyBuilderPrivate
{
public:
QMetaPropertyBuilderPrivate
(const QByteArray& _name, const QByteArray& _type, int notifierIdx=-1,
(const QByteArray& _name, const QByteArray& _type, QMetaType _metaType, int notifierIdx=-1,
int _revision = 0)
: name(_name),
type(QMetaObject::normalizedType(_type.constData())),
metaType(_metaType),
flags(Readable | Writable | Scriptable), notifySignal(notifierIdx),
revision(_revision)
{
Expand All @@ -164,6 +165,7 @@ class QMetaPropertyBuilderPrivate

QByteArray name;
QByteArray type;
QMetaType metaType;
int flags;
int notifySignal;
int revision;
Expand Down Expand Up @@ -555,9 +557,17 @@ QMetaMethodBuilder QMetaObjectBuilder::addConstructor(const QMetaMethod &prototy
*/
QMetaPropertyBuilder QMetaObjectBuilder::addProperty(const QByteArray &name, const QByteArray &type,
int notifierId)
{
return addProperty(name, type, QMetaType::fromName(name), notifierId);
}

/*!
\overload
*/
QMetaPropertyBuilder QMetaObjectBuilder::addProperty(const QByteArray &name, const QByteArray &type, QMetaType metaType, int notifierId)
{
int index = int(d->properties.size());
d->properties.push_back(QMetaPropertyBuilderPrivate(name, type, notifierId));
d->properties.push_back(QMetaPropertyBuilderPrivate(name, type, metaType, notifierId));
return QMetaPropertyBuilder(this, index);
}

Expand All @@ -571,7 +581,7 @@ QMetaPropertyBuilder QMetaObjectBuilder::addProperty(const QByteArray &name, con
*/
QMetaPropertyBuilder QMetaObjectBuilder::addProperty(const QMetaProperty &prototype)
{
QMetaPropertyBuilder property = addProperty(prototype.name(), prototype.typeName());
QMetaPropertyBuilder property = addProperty(prototype.name(), prototype.typeName(), prototype.metaType());
property.setReadable(prototype.isReadable());
property.setWritable(prototype.isWritable());
property.setResettable(prototype.isResettable());
Expand Down
1 change: 1 addition & 0 deletions src/corelib/kernel/qmetaobjectbuilder_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class Q_CORE_EXPORT QMetaObjectBuilder
QMetaMethodBuilder addConstructor(const QMetaMethod& prototype);

QMetaPropertyBuilder addProperty(const QByteArray& name, const QByteArray& type, int notifierId=-1);
QMetaPropertyBuilder addProperty(const QByteArray& name, const QByteArray& type, QMetaType metaType, int notifierId=-1);
QMetaPropertyBuilder addProperty(const QMetaProperty& prototype);

QMetaEnumBuilder addEnumerator(const QByteArray& name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ private slots:

void classNameFirstInStringData();

void propertyMetaType();

private:
static bool checkForSideEffects
(const QMetaObjectBuilder& builder,
Expand Down Expand Up @@ -1688,6 +1690,24 @@ void tst_QMetaObjectBuilder::classNameFirstInStringData()
free(mo);
}

struct MyFoo {};

void tst_QMetaObjectBuilder::propertyMetaType()
{
QMetaType meta = QMetaType::fromType<MyFoo>();
auto metaId = meta.id();
QMetaObjectBuilder builder;
builder.setClassName("Test");
builder.addProperty("test", "MyFoo");
auto mo = builder.toMetaObject();

QMetaProperty metaProp = mo->property(mo->indexOfProperty("test"));
QCOMPARE(metaProp.typeName(), meta.name());
QCOMPARE(metaProp.typeId(), metaId);
QCOMPARE(metaProp.metaType(), meta);
free(mo);
}

QTEST_MAIN(tst_QMetaObjectBuilder)

#include "tst_qmetaobjectbuilder.moc"

0 comments on commit 465701b

Please sign in to comment.