- 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.hxx
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 _QITYPE_DETAIL_PROPERTY_HXX_
8 #define _QITYPE_DETAIL_PROPERTY_HXX_
9
10 #include <boost/thread/locks.hpp>
11 #include <qi/future.hpp>
12 #include <qi/property.hpp>
13
14 namespace qi
15 {
16 inline FutureSync<void> GenericProperty::set(const AnyValue& v)
17 {
18 auto conv = v.convert(_type);
19 if (!conv->type())
20 throw std::runtime_error(std::string("Failed converting ") + v.type()->infoString() + " to " + _type->infoString());
21
22 Property<AnyValue>::set(AnyValue(*conv, false, conv.ownsReference()));
23 conv.release();
24
25 return FutureSync<void>(0);
26 }
27
28 template<typename T>
29 PropertyImpl<T>::PropertyImpl(Getter getter, Setter setter,
30 SignalBase::OnSubscribers onsubscribe)
31 : SignalF<void(const T&)>(std::move(onsubscribe))
32 , _getter(std::move(getter))
33 , _setter(std::move(setter))
34 {
35 }
36
37 template <typename T>
38 PropertyImpl<T>::PropertyImpl(ExecutionContext* execContext,
39 Getter getter,
40 Setter setter,
41 SignalBase::OnSubscribers onsubscribe)
42 : SignalF<void(const T&)>(execContext, std::move(onsubscribe))
43 , _getter(std::move(getter))
44 , _setter(std::move(setter))
45 {
46 }
47
48 template<typename T>
49 PropertyImpl<T>::PropertyImpl(AutoAnyReference defaultValue,
50 Getter getter, Setter setter,
51 SignalBase::OnSubscribers onsubscribe)
52 : SignalF<void(const T&)>(std::move(onsubscribe))
53 , _getter(std::move(getter))
54 , _setter(std::move(setter))
55 , _value(defaultValue.to<T>())
56 {
57 }
58
59 template <typename T>
60 PropertyImpl<T>::PropertyImpl(AutoAnyReference defaultValue,
61 ExecutionContext* execContext,
62 Getter getter,
63 Setter setter,
64 SignalBase::OnSubscribers onsubscribe)
65 : SignalF<void(const T&)>(execContext, std::move(onsubscribe))
66 , _getter(std::move(getter))
67 , _setter(std::move(setter))
68 , _value(defaultValue.to<T>())
69 {
70 }
71
72 template<typename T>
73 T PropertyImpl<T>::getImpl() const
74 {
75 if (_getter)
76 return _getter(boost::ref(_value));
77 else
78 return _value;
79 }
80 template<typename T>
81 void PropertyImpl<T>::setImpl(const T& v)
82 {
83 qiLogDebug("qitype.property") << "set " << this << " " << (!!_setter);
84 if (_setter)
85 {
86 const bool ok = _setter(boost::ref(_value), v);
87 if (ok)
88 (*this)(_value);
89 }
90 else
91 {
92 _value = v;
93 (*this)(_value);
94 }
95 }
96
97
98 template<typename T>
99 FutureSync<T> UnsafeProperty<T>::get() const
100 {
101 return FutureSync<T>(this->getImpl());
102 }
103
104 template<typename T>
105 FutureSync<void> UnsafeProperty<T>::set(const T& v)
106 {
107 this->setImpl(v);
108 return FutureSync<void>(0);
109 }
110
111 template<typename T>
112 FutureSync<AnyValue> UnsafeProperty<T>::value() const
113 {
114 return FutureSync<AnyValue>(AnyValue::from(this->getImpl()));
115 }
116
117 template<typename T>
118 FutureSync<void> UnsafeProperty<T>::setValue(AutoAnyReference value)
119 {
120 this->setImpl(value.to<T>());
121 return FutureSync<void>(0);
122 }
123
124 template<typename T>
125 Property<T>::~Property()
126 {
127 _tracked.destroy();
128 joinStrand();
129 SignalBase::clearExecutionContext();
130 }
131
132 template<typename T>
133 FutureSync<T> Property<T>::get() const
134 {
135 return strand().async(track([this]{ return this->getImpl(); }, &_tracked));
136 }
137
138 template<typename T>
139 FutureSync<void> Property<T>::set(const T& v)
140 {
141 return strand().async(track([this, v]{ this->setImpl(v); }, &_tracked));
142 }
143
144 template<typename T>
145 FutureSync<AnyValue> Property<T>::value() const
146 {
147 return strand().async(track([this]{ return AnyValue::from(this->getImpl()); }, &_tracked));
148 }
149
150 template<typename T>
151 FutureSync<void> Property<T>::setValue(AutoAnyReference value)
152 {
153 const auto v = value.to<T>();
154 return strand().async(track([this, v]{ this->setImpl(v); }, &_tracked));
155 }
156
157 template<typename T>
158 Strand& Property<T>::strand() const
159 {
160 struct Src : boost::static_visitor<Strand&>, ka::src_t {};
161 return boost::apply_visitor(Src{}, _strand);
162 }
163
164 template<typename T>
165 void Property<T>::joinStrand() QI_NOEXCEPT(true)
166 {
167 struct JoinStrand : boost::static_visitor<void>
168 {
169 void operator()(Strand*) const
170 {
171 // Do nothing, we do not own the strand, we have no right to join it.
172 }
173
174 void operator()(Strand& strand) const
175 {
176 strand.join();
177 }
178 };
179 boost::apply_visitor(JoinStrand{}, _strand);
180 }
181 }
182
183 #endif // _QITYPE_DETAIL_PROPERTY_HXX_
qi::Property::value
FutureSync< AnyValue > value() const override
Definition: property.hxx:145
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::track
auto track(F &&f, T &&toTrack) -> decltype(trackWithFallback(detail::throwPointerLockException, std::forward< F >(f), std::forward< T >(toTrack)))
Definition: trackable.hxx:420
qi::detail::AnyReferenceBase::convert
UniqueAnyReference convert(TypeInterface *targetType) const
qiLogDebug
#define qiLogDebug(...)
Definition: log.hpp:76
qi::AnyValue::from
static AnyValue from(const T &r)
Definition: anyvalue.hpp:94
qi::UnsafeProperty::get
FutureSync< T > get() const override
Definition: property.hxx:99
qi::PropertyImpl::Getter
boost::function< T(boost::reference_wrapper< const T >)> Getter
Definition: property.hpp:72
qi::ExecutionContext
Definition: executioncontext.hpp:63
qi::Property::~Property
~Property() override
Definition: property.hxx:125
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::UnsafeProperty::setValue
FutureSync< void > setValue(AutoAnyReference value) override
Definition: property.hxx:118
qi::PropertyImpl::Setter
boost::function< bool(boost::reference_wrapper< T >, const T &)> Setter
Definition: property.hpp:71
qi::Strand
Definition: strand.hpp:125
qi::detail::AnyReferenceBase::to
T to() const
Convert to anything or throw trying.
Definition: anyreference.hxx:138
property.hpp
qi::SignalF
Definition: signal.hpp:171
qi::SignalBase::OnSubscribers
boost::function< Future< void >(bool)> OnSubscribers
Definition: signal.hpp:45
qi::detail::AnyReferenceBase::type
TypeInterface * type() const
Definition: anyreference.hpp:348
qi::Property::set
FutureSync< void > set(const T &v) override
Definition: property.hxx:139
future.hpp
qi::PropertyImpl::PropertyImpl
PropertyImpl(Getter getter=Getter(), Setter setter=Setter(), SignalBase::OnSubscribers onsubscribe=SignalBase::OnSubscribers())
Definition: property.hxx:29
qi::TypeInterface::infoString
const char * infoString()
Definition: typeinterface.hpp:178
qi::UnsafeProperty::set
FutureSync< void > set(const T &v) override
Definition: property.hxx:105
qi::PropertyImpl::setImpl
void setImpl(const T &v)
Definition: property.hxx:81
qi::FutureSync
Definition: future_fwd.hpp:70
qi::AutoAnyReference
Definition: anyreference.hpp:409
qi::GenericProperty::set
FutureSync< void > set(const AnyValue &v) override
Definition: property.hxx:16
qi::Property::get
FutureSync< T > get() const override
Definition: property.hxx:133
qi::UnsafeProperty::value
FutureSync< AnyValue > value() const override
Definition: property.hxx:112
qi::SignalBase::clearExecutionContext
void clearExecutionContext()