Global Classes
#include <qi/property.hpp>
A property is basically a simple variable that you can set and get.
qi::Property<int> p;
p.set(18);
std::cout << p.get() << std::endl;
It also allows you to have callbacks called when the property is changed.
void callback(const int& value) {
std::cout << "New value: " << value << std::endl;
}
// ...
qi::Property<int> p;
p.connect(callback);
p.set(42);
p.set(24);
p.set(12);
This will print:
New value: 42
New value: 24
New value: 12
Warning
This output is not guaranteed. callback
is called asynchronously, thus the
multiple calls may be running in parallel and print a different
implementation-dependent output.
You can set a custom getter and setter on a property.
int getter(const int& value) {
std::cout << "User is requesting value" << std::endl;
return value;
}
bool setter(int& storage, const int& value) {
std::cout << "User is changing value from " << storage << " to " << value
<< std::endl;
if (value < 0)
{
std::cout << "Cannot set to negative values" << std::endl;
return false;
}
else
{
storage = value;
return true;
}
}
// ...
qi::Property<int> p(getter, setter);
p.set(42);
std::cout << "Property is " << p.get() << std::endl;
p.set(-12);
std::cout << "Property is " << p.get() << std::endl;
This will print:
User is changing value from 0 to 42
Property is 42
User is changing value from 42 to -12
Cannot set to negative values
Property is 42
Note
If a callback is connected on the property, it is not triggered when the setter failed to set the new value.
You can of course use boost::bind
and qi::bind
for custom getters and
setters to bind this
or extra arguments. You can also specify a custom
setter without specifying a getter. The next example shows these two points.
class MyClass {
public:
MyClass() : prop(qi::Property<int>::Getter(),
qi::bind(&MyClass::propSet, this, _1, _2)) {
}
qi::Property<int> prop;
private:
bool propSet(int& storage, int value) {
++_setCounter; // count the number of times the property has been set
storage = value;
return true;
}
qi::Atomic<int> _setCounter;
};
Property
(Getter getter)Property
(AutoAnyReference defaultValue, Getter getter)operator=
(const T& v)get
() constset
(const T& v)signal
()setValue
(AutoAnyReference value)value
() constgetLockedReadWrite
()getLockedReadOnly
() constPovide thread-safe access to a stored value and signal to connected callbacks when the value changed. qi::Signal which implement a similar pattern but without storing the value. For more performance in a single-threaded context, consider using UnsafeProperty instead. qi/property.hpp
qi::Property<T>::
Property
(Getter getter = Getter()¶qi::Property<T>::
Property
(AutoAnyReference defaultValue, Getter getter = Getter()¶qi::Property<T>::
operator=
(const T& v)¶qi::Property<T>::
get
() const
¶qi::Property<T>::
set
(const T& v)¶qi::Property<T>::
signal
()¶qi::Property<T>::
setValue
(AutoAnyReference value)¶qi::Property<T>::
value
() const
¶qi::Property<T>::
getLockedReadWrite
()¶Brief:
Returns: | Acquire write-enabled scoped thread-safe access to the value stored in this object. |
---|
qi::Property<T>::
getLockedReadOnly
() const
¶Brief:
Returns: | Acquire read-only scoped thread-safe access to the value stored in this object. |
---|