[Development] RFC: RAII for property changes

Keith Gardner kreios4004 at gmail.com
Wed Apr 15 21:05:39 CEST 2015


>
> >   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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20150415/3cd72bcd/attachment.html>


More information about the Development mailing list