[Development] RFC: RAII for property changes
Matthew Woehlke
mw_triad at users.sourceforge.net
Wed Apr 15 17:08:11 CEST 2015
On 2015-04-15 10:43, Marc Mutz wrote:
> On Wednesday 15 April 2015 11:49:56 André Somers wrote:
>> void MyClass::setFoo(QString value)
>> {
>> PropertyGuard guard(this, "foo"); //foo is the name of the Q_PROPERTY
>> Q_UNUSED(guard);
>>
>> m_foo = value;
>> }
>
> This is an interesting idea, though I don't think I have encountered the
> problems with which you motivate PropertyGuard.
>
> For use in a library, though, I fear the string-based mechanism is too
> inefficient. For use within QtWidgets, say, I'd suggest a mechanism that works
> on the member data directly.
FWIW I had the same thought; also, I'm not a fan of needing the
Q_UNUSED, or using a macro to 'hide' it.
What about something like this?
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
It's slightly redundant because declaring the guard and adding a
property are separate, but there is no unused object, and you can use
the same guard for multiple properties.
The implementation is trickier (probably you need a dynamically
allocated templated helper class with a virtual base to store the value
and check for changes), but avoids multiple string-based look-ups.
The "slot" could be a functor, like QObject::connect accepts, that could
be any of the actual slot, std::function, lambda, etc.
--
Matthew
More information about the Development
mailing list