Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions thirdparty/next/inc/core/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,14 @@ class NEXT_LIBRARY_EXPORT Object {
virtual const LinkList &getReceivers() const;

virtual void setParent(Object *parent, int32_t position = -1, bool force = false);

virtual string typeName() const;

virtual Variant property(const char *name) const;
virtual void setProperty(const char *name, const Variant &value);

const list<string> dynamicPropertyNames() const;

virtual bool event(Event *event);

virtual bool isSerializable() const;
Expand Down Expand Up @@ -216,9 +220,11 @@ class NEXT_LIBRARY_EXPORT Object {
Object::LinkList m_recievers;
Object::LinkList m_senders;

Object *m_currentSender;

queue<Event *> m_eventQueue;
list<string> m_dynamicPropertyNames;
list<Variant> m_dynamicPropertyValues;

Object *m_currentSender;

ObjectSystem *m_system;

Expand Down
43 changes: 38 additions & 5 deletions thirdparty/next/src/core/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -855,10 +855,17 @@ Variant Object::property(const char *name) const {
PROFILE_FUNCTION();
const MetaObject *meta = metaObject();
int index = meta->indexOfProperty(name);
if(index > -1) {
return meta->property(index).read(this);
if(index < 0) { // Check dynamic property
auto it = std::find(m_dynamicPropertyNames.begin(), m_dynamicPropertyNames.end(), name);
if(it == m_dynamicPropertyNames.end()) {
return Variant();
}

size_t index = std::distance(m_dynamicPropertyNames.begin(), it);
return *std::next(m_dynamicPropertyValues.begin(), index);
}
return Variant();

return meta->property(index).read(this);
}
/*!
Sets the property with \a name to \a value.
Expand All @@ -873,9 +880,35 @@ void Object::setProperty(const char *name, const Variant &value) {
PROFILE_FUNCTION();
const MetaObject *meta = metaObject();
int index = meta->indexOfProperty(name);
if(index > -1) {
meta->property(index).write(this, value);
if(index < 0) {
auto nameIterator = std::find(m_dynamicPropertyNames.begin(), m_dynamicPropertyNames.end(), name);
if(nameIterator != m_dynamicPropertyNames.end()) {
index = std::distance(m_dynamicPropertyNames.begin(), nameIterator);
}

if(!value.isValid() && index > -1) { // Remove dynamic property if exists
auto valueIterator = std::next(m_dynamicPropertyValues.begin(), index);
m_dynamicPropertyNames.erase(nameIterator);
m_dynamicPropertyValues.erase(valueIterator);
} else { // Set a new value
if(index < 0) {
m_dynamicPropertyNames.push_back(name);
m_dynamicPropertyValues.push_back(value);
} else {
*std::next(m_dynamicPropertyValues.begin(), index) = value;
}
}

return;
}

meta->property(index).write(this, value);
}
/*!
Returns the names of all properties that were dynamically added to the object using setProperty()
*/
const list<string> Object::dynamicPropertyNames() const {
return m_dynamicPropertyNames;
}
/*!
Returns object which sent signal.
Expand Down
20 changes: 18 additions & 2 deletions thirdparty/next/tests/tst_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ void Clone_object() {
obj2->setName("TestComponent2");
obj2->setParent(obj1);
obj1->setVector(Vector2(10.0, 20.0));
obj1->setProperty("dynamic1", 100);
obj2->setProperty("dynamic2", true);

Object::connect(obj1, _SIGNAL(signal(int)), obj2, _SLOT(setSlot(int)));
Object::connect(obj2, _SIGNAL(signal(int)), obj1, _SLOT(setSlot(int)));
Expand All @@ -199,6 +197,24 @@ void Clone_object() {
delete obj1;
}

void Dynamic_properties() {
ObjectSystem objectSystem;
TestObject::registerClassFactory(&objectSystem);

TestObject *obj1 = ObjectSystem::objectCreate<TestObject>();

obj1->setProperty("dynamic1", 100); // Set a new dynamic value
obj1->setProperty("dynamic1", 200); // Override dynamic value

int value = obj1->property("dynamic1").toInt();
QCOMPARE(value, 200);

obj1->setProperty("dynamic1", Variant()); // Delete dynamic property
QCOMPARE(obj1->property("dynamic1").isValid(), false);

delete obj1;
}

} REGISTER(ObjectTest)

#include "tst_object.moc"