- Aldebaran documentation |
- C++ Libraries |
- index

libqi-api 2.8.7.4

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
property.hpp
Go to the documentation of this file.
1 #pragma once
2 /*
3 ** Copyright (C) 2013 Aldebaran Robotics
4 ** See COPYING for the license
5 */
6
7 #ifndef _QI_PROPERTY_HPP_
8 #define _QI_PROPERTY_HPP_
9
10 #include <boost/function.hpp>
11 #include <boost/thread/mutex.hpp>
12 #include <boost/variant.hpp>
13 #include <ka/mutablestore.hpp>
14 #include <qi/signal.hpp>
15 #include <qi/future.hpp>
16 #include <qi/strand.hpp>
17
18 #ifdef _MSC_VER
19 # pragma warning( push )
20 // needs to have dll-interface to be used by clients
21 # pragma warning( disable: 4251 )
22 // non dll-interface class * used as base for dll-interface class
23 # pragma warning( disable: 4275 )
24 #endif
25
26 namespace qi
27 {
30 class QI_API PropertyBase
31 {
32 public:
33 PropertyBase() = default;
34
35 // NonCopyable
36 PropertyBase(const PropertyBase&) = delete;
37 PropertyBase& operator=(const PropertyBase&) = delete;
38
39 virtual ~PropertyBase() = default;
40 virtual SignalBase* signal() = 0;
41 virtual FutureSync<void> setValue(AutoAnyReference value) = 0;
42 virtual FutureSync<AnyValue> value() const = 0;
43 };
44
45 namespace util
46 {
47 // Used as a qi::Property::Setter to only store and dispatch a value if it was changed.
48 struct SetAndNotifyIfChanged
49 {
50 template<class T>
51 bool operator()(boost::reference_wrapper<T> ref_to_current_value, const T& newValue) const {
52 T& currentValue = ref_to_current_value;
53 if (currentValue != newValue)
54 {
55 currentValue = newValue;
56 return true;
57 }
58 return false;
59 }
60 };
61
62 }
63
64 template<typename T>
65 class PropertyImpl: public SignalF<void(const T&)>, public PropertyBase
66 {
67 public:
71 using Setter = boost::function<bool (boost::reference_wrapper<T>, const T&)>;
72 using Getter = boost::function<T(boost::reference_wrapper<const T>)>;
73 using SignalType = SignalF<void(const T&)>;
74 using PropertyType = T;
75
84 PropertyImpl(Getter getter = Getter(), Setter setter = Setter(),
85 SignalBase::OnSubscribers onsubscribe = SignalBase::OnSubscribers());
86
89 PropertyImpl(ExecutionContext* execContext, Getter getter = Getter(), Setter setter = Setter(),
90 SignalBase::OnSubscribers onsubscribe = SignalBase::OnSubscribers());
91
92 PropertyImpl(AutoAnyReference defaultValue, Getter getter = Getter(), Setter setter = Setter(),
93 SignalBase::OnSubscribers onsubscribe = SignalBase::OnSubscribers());
94
97 PropertyImpl(AutoAnyReference defaultValue, ExecutionContext* execContext = nullptr,
98 Getter getter = Getter(), Setter setter = Setter(),
99 SignalBase::OnSubscribers onsubscribe = SignalBase::OnSubscribers());
100
101 virtual FutureSync<T> get() const = 0;
102 virtual FutureSync<void> set(const T& v) = 0;
103
104 PropertyImpl<T>& operator=(const T& v) { this->set(v); return *this; }
105
106 SignalBase* signal() override { return this; }
107
108 protected:
109 Getter _getter;
110 Setter _setter;
111 T _value;
112
113 T getImpl() const;
114 void setImpl(const T& v);
115 };
116
122 template<typename T>
123 class UnsafeProperty: public PropertyImpl<T>
124 {
125 public:
126 using ImplType = PropertyImpl<T>;
127 using Getter = typename ImplType::Getter;
128 using Setter = typename ImplType::Setter;
129
130 UnsafeProperty(Getter getter = Getter(), Setter setter = Setter(),
131 SignalBase::OnSubscribers onsubscribe = SignalBase::OnSubscribers())
132 : PropertyImpl<T>(std::move(getter), std::move(setter), std::move(onsubscribe))
133 {}
134
135 UnsafeProperty(AutoAnyReference defaultValue, Getter getter = Getter(), Setter setter = Setter(),
136 SignalBase::OnSubscribers onsubscribe = SignalBase::OnSubscribers())
137 : PropertyImpl<T>(std::move(defaultValue), std::move(getter), std::move(setter), std::move(onsubscribe))
138 {}
139
140 UnsafeProperty<T>& operator=(const T& v) { this->set(v); return *this; }
141
142 FutureSync<T> get() const override;
143 FutureSync<void> set(const T& v) override;
144
145 SignalBase* signal() override { return this; }
146 FutureSync<void> setValue(AutoAnyReference value) override;
147 FutureSync<AnyValue> value() const override;
148
149 };
150
156 template<typename T>
157 class Property: public PropertyImpl<T>
158 {
159 public:
160 using ImplType = PropertyImpl<T>;
161 using Getter = typename ImplType::Getter;
162 using Setter = typename ImplType::Setter;
163
166 Property(Getter getter = Getter(), Setter setter = Setter(),
167 SignalBase::OnSubscribers onsubscribe = SignalBase::OnSubscribers())
168 : PropertyImpl<T>(std::move(getter), std::move(setter), std::move(onsubscribe))
169 {}
170
173 Property(Strand& strand,
174 Getter getter = Getter(),
175 Setter setter = Setter(),
176 SignalBase::OnSubscribers onsubscribe = SignalBase::OnSubscribers())
177 : PropertyImpl<T>(&strand, std::move(getter), std::move(setter), std::move(onsubscribe))
178 , _strand{ &strand }
179 {
180 }
181
184 Property(AutoAnyReference defaultValue, Getter getter = Getter(), Setter setter = Setter(),
185 SignalBase::OnSubscribers onsubscribe = SignalBase::OnSubscribers())
186 : PropertyImpl<T>(std::move(defaultValue), std::move(getter), std::move(setter), std::move(onsubscribe))
187 {}
188
191 Property(AutoAnyReference defaultValue,
192 Strand& strand,
193 Getter getter = Getter(),
194 Setter setter = Setter(),
195 SignalBase::OnSubscribers onsubscribe = SignalBase::OnSubscribers())
196 : PropertyImpl<T>(std::move(defaultValue),
197 &strand,
198 std::move(getter),
199 std::move(setter),
200 std::move(onsubscribe))
201 , _strand{ &strand }
202 {
203 }
204
205 ~Property() override;
206
207 Property<T>& operator=(const T& v) { this->set(v); return *this; }
208
209 FutureSync<T> get() const override;
210 FutureSync<void> set(const T& v) override;
211
212 SignalBase* signal() override { return this; }
213 FutureSync<void> setValue(AutoAnyReference value) override;
214 FutureSync<AnyValue> value() const override;
215
216 private:
217 // This class has to have a Trackable member even though it holds a strand because it does not
218 // always own that strand, and thus it can not always join it before being destroyed. Tracking
219 // a member ensures that no stranded callback might be executed after it has been destroyed.
220 struct Tracked : public Trackable<Tracked> { using Trackable<Tracked>::destroy; };
221 mutable Tracked _tracked;
222
223 Strand& strand() const;
224 void joinStrand() QI_NOEXCEPT(true);
225
226 // TODO:
227 // Right now, mutable_store_t stores the Mutable first (the pointer in this case) which
228 // is the one that will be instanciated by the default constructor. But since Strand is
229 // neither copyable nor movable and mutable_store_t relies on boost::variant and boost::variant
230 // does not support in-place construction of its value, the Strand value can never be set to the
231 // mutable_store_t. So use ka::mutable_store_t once we are able to construct values in-place in
232 // it.
233 mutable boost::variant<Strand, Strand*> _strand;
234 };
235
237 class QI_API GenericProperty : public Property<AnyValue>
238 {
239 public:
250 template<typename... Args>
251 explicit GenericProperty(const AutoAnyReference& defaultValue, Args&&... args)
252 : Property<AnyValue>(defaultValue, ka::fwd<Args>(args)...)
253 , _type{ defaultValue.type() }
254 {
255 _setSignature(makeTupleSignature({ _type }));
256 }
257
268 template<typename... Args>
269 explicit GenericProperty(TypeInterface* type, Args&&... args)
270 : GenericProperty(AnyValue{ type }, ka::fwd<Args>(args)...)
271 {
272 }
273
274 GenericProperty& operator=(const AnyValue& v)
275 {
276 this->set(v);
277 return *this;
278 }
279
280 FutureSync<void> setValue(AutoAnyReference value) override
281 {
282 return set(AnyValue(value, false, false));
283 }
284
285 FutureSync<void> set(const AnyValue& v) override;
286
287 qi::Signature signature() const override
288 {
289 return makeTupleSignature({ _type });
290 }
291
292 private:
293 TypeInterface* _type;
294 };
295
296 }
297
298 #include <qi/type/detail/property.hxx>
299
300 #ifdef _MSC_VER
301 # pragma warning( pop )
302 #endif
303
304 #endif // _QITYPE_PROPERTY_HPP_
qi::Property::value
FutureSync< AnyValue > value() const override
Definition: property.hxx:145
qi::detail::setValue
void setValue(qi::Promise< R > &p, const boost::function< R()> &f)
Definition: executioncontext.hpp:148
qi::GenericProperty::operator=
GenericProperty & operator=(const AnyValue &v)
Definition: property.hpp:274
qi::PropertyImpl::getImpl
T getImpl() const
Definition: property.hxx:73
qi::AnyValue
Definition: anyvalue.hpp:19
qi::Property::setValue
FutureSync< void > setValue(AutoAnyReference value) override
Definition: property.hxx:151
qi::util::SetAndNotifyIfChanged
Definition: property.hpp:48
QI_API
#define QI_API
Definition: api.hpp:33
qi::UnsafeProperty
Definition: property.hpp:123
qi::PropertyBase
Definition: property.hpp:30
qi::UnsafeProperty::signal
SignalBase * signal() override
Definition: property.hpp:145
qi::SignalBase::_setSignature
void _setSignature(const Signature &s)
qi::Property::Getter
typename ImplType::Getter Getter
Definition: property.hpp:161
qi::Signature
Definition: signature.hpp:66
qi::Property::Property
Property(AutoAnyReference defaultValue, Getter getter=Getter(), Setter setter=Setter(), SignalBase::OnSubscribers onsubscribe=SignalBase::OnSubscribers())
Definition: property.hpp:184
qi::Property::Property
Property(AutoAnyReference defaultValue, Strand &strand, Getter getter=Getter(), Setter setter=Setter(), SignalBase::OnSubscribers onsubscribe=SignalBase::OnSubscribers())
Definition: property.hpp:191
qi::UnsafeProperty::Getter
typename ImplType::Getter Getter
Definition: property.hpp:127
qi::PropertyImpl< qi::ServiceDirectoryProxy::Status >::Getter
boost::function< qi::ServiceDirectoryProxy::Status(boost::reference_wrapper< const qi::ServiceDirectoryProxy::Status >)> Getter
Definition: property.hpp:72
qi::ExecutionContext
Definition: executioncontext.hpp:63
qi::Property::~Property
~Property() override
Definition: property.hxx:125
qi::UnsafeProperty::UnsafeProperty
UnsafeProperty(AutoAnyReference defaultValue, Getter getter=Getter(), Setter setter=Setter(), SignalBase::OnSubscribers onsubscribe=SignalBase::OnSubscribers())
Definition: property.hpp:135
qi::Property
Definition: property.hpp:157
QI_NOEXCEPT
#define QI_NOEXCEPT(cond)
Specify that a function may throw or not. Do nothing if noexcept is not available.
Definition: macro.hpp:318
qi::Property::Setter
typename ImplType::Setter Setter
Definition: property.hpp:162
qi::UnsafeProperty::setValue
FutureSync< void > setValue(AutoAnyReference value) override
Definition: property.hxx:118
qi::PropertyImpl::_getter
Getter _getter
Definition: property.hpp:109
qi::PropertyImpl< qi::ServiceDirectoryProxy::Status >::Setter
boost::function< bool(boost::reference_wrapper< qi::ServiceDirectoryProxy::Status >, const qi::ServiceDirectoryProxy::Status &)> Setter
Definition: property.hpp:71
qi::TypeInterface
Definition: typeinterface.hpp:129
qi::GenericProperty::signature
qi::Signature signature() const override
Definition: property.hpp:287
qi::Strand
Definition: strand.hpp:125
qi::UnsafeProperty::UnsafeProperty
UnsafeProperty(Getter getter=Getter(), Setter setter=Setter(), SignalBase::OnSubscribers onsubscribe=SignalBase::OnSubscribers())
Definition: property.hpp:130
qi::SignalF
Definition: signal.hpp:171
qi::SignalBase::OnSubscribers
boost::function< Future< void >(bool)> OnSubscribers
Definition: signal.hpp:45
strand.hpp
qi::detail::AnyReferenceBase::type
TypeInterface * type() const
Definition: anyreference.hpp:348
qi::ServiceDirectoryProxy::Status
Definition: servicedirectoryproxy.hpp:79
qi::SignalBase
Definition: signal.hpp:42
qi::GenericProperty::GenericProperty
GenericProperty(const AutoAnyReference &defaultValue, Args &&...args)
Definition: property.hpp:251
qi::PropertyImpl::set
virtual FutureSync< void > set(const T &v)=0
qi::Property::set
FutureSync< void > set(const T &v) override
Definition: property.hxx:139
future.hpp
qi::GenericProperty
Type-erased property, simulating a typed property but using AnyValue.
Definition: property.hpp:237
qi::UnsafeProperty::operator=
UnsafeProperty< T > & operator=(const T &v)
Definition: property.hpp:140
signal.hpp
qi::PropertyImpl::PropertyImpl
PropertyImpl(Getter getter=Getter(), Setter setter=Setter(), SignalBase::OnSubscribers onsubscribe=SignalBase::OnSubscribers())
Definition: property.hxx:29
qi::Property::Property
Property(Getter getter=Getter(), Setter setter=Setter(), SignalBase::OnSubscribers onsubscribe=SignalBase::OnSubscribers())
Definition: property.hpp:166
qi::makeTupleSignature
qi::Signature makeTupleSignature(const std::vector< qi::AnyReference > &vgv, bool resolveDynamic=false, const std::string &name=std::string(), const std::vector< std::string > &names=std::vector< std::string >())
qi::PropertyImpl::signal
SignalBase * signal() override
Definition: property.hpp:106
qi::util::SetAndNotifyIfChanged::operator()
bool operator()(boost::reference_wrapper< T > ref_to_current_value, const T &newValue) const
Definition: property.hpp:51
qi::UnsafeProperty::set
FutureSync< void > set(const T &v) override
Definition: property.hxx:105
qi::PropertyImpl::operator=
PropertyImpl< T > & operator=(const T &v)
Definition: property.hpp:104
qi::Trackable
Object tracking by blocking destruction while shared pointers are present.
Definition: trackable.hpp:45
qi::PropertyImpl::setImpl
void setImpl(const T &v)
Definition: property.hxx:81
qi::FutureSync
Definition: future_fwd.hpp:70
qi::GenericProperty::GenericProperty
GenericProperty(TypeInterface *type, Args &&...args)
Definition: property.hpp:269
qi::AutoAnyReference
Definition: anyreference.hpp:409
property.hxx
qi::UnsafeProperty::Setter
typename ImplType::Setter Setter
Definition: property.hpp:128
qi::Property::Property
Property(Strand &strand, Getter getter=Getter(), Setter setter=Setter(), SignalBase::OnSubscribers onsubscribe=SignalBase::OnSubscribers())
Definition: property.hpp:173
qi::Property::operator=
Property< T > & operator=(const T &v)
Definition: property.hpp:207
qi::PropertyImpl::_value
T _value
Definition: property.hpp:111
qi::UnsafeProperty::value
FutureSync< AnyValue > value() const override
Definition: property.hxx:112
qi::PropertyImpl
Definition: property.hpp:65
qi::GenericProperty::setValue
FutureSync< void > setValue(AutoAnyReference value) override
Definition: property.hpp:280
qi::PropertyImpl::_setter
Setter _setter
Definition: property.hpp:110
qi::Property::signal
SignalBase * signal() override
Definition: property.hpp:212