On 15-4-2015 21:05, Keith Gardner wrote:

    >  QPropertyGuard g{this};
    >   g.addProperty("a"); // use QObject::property
    >   g.addProperty("b", m_b); // take value member by reference
    >   g.addProperty(m_c, &cChanged); // ...and also slot address

    There's type erasure going on, so it will allocate memory.
    Allocating memory
    in a setter that might just change an int is what I call inefficient.


Would something like this be better?

// constructor
// Provide an initial value and a std::function to notify about the change
Property<bool> m_example = Property<bool>(false, [&](bool value){
    emit exampleChanged(value);
});

// getter
bool *::example() const {
    return m_example;
}

// setter
void *::setExample(bool value) {
    m_example = value;
}

The working code for this example can be found https://github.com/kreios4004/Property_Class.

Thank you for posting that.

I took a look at it, but I am not sure it actually solves much. It moves the check if something changed to the wrapped variable, but it makes using that value then more complicated. Instead of using the variable directly, one now has to go through m_example.getValue() everywhere. Not pretty, I think. The case of modifying more than one property in one go is also not solved. On the one hand, dependent properties (such a isValid property) are not updated unless you manually write that code again, and on the other hand if you use this to set more than one property in a method, you still are sending signals at the point the class is in an inconsistent state.

One nice side effect of my implementation is that the notification is actually only send once. Even if the property value is changed multiple times in the meantime, you still get only one notification. That can be very convenient, even if you don't use it much.

André

_______________________________________________
Development mailing list
Development@qt-project.org
http://lists.qt-project.org/mailman/listinfo/development

Reply via email to