libqi-api  2.8.7.4
 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 {
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>
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>
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>
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>
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>
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>
100  {
101  return FutureSync<T>(this->getImpl());
102  }
103 
104  template<typename T>
106  {
107  this->setImpl(v);
108  return FutureSync<void>(0);
109  }
110 
111  template<typename T>
113  {
114  return FutureSync<AnyValue>(AnyValue::from(this->getImpl()));
115  }
116 
117  template<typename T>
119  {
120  this->setImpl(value.to<T>());
121  return FutureSync<void>(0);
122  }
123 
124  template<typename T>
126  {
127  _tracked.destroy();
128  joinStrand();
130  }
131 
132  template<typename T>
134  {
135  return strand().async(track([this]{ return this->getImpl(); }, &_tracked));
136  }
137 
138  template<typename T>
140  {
141  return strand().async(track([this, v]{ this->setImpl(v); }, &_tracked));
142  }
143 
144  template<typename T>
146  {
147  return strand().async(track([this]{ return AnyValue::from(this->getImpl()); }, &_tracked));
148  }
149 
150  template<typename T>
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_
FutureSync< AnyValue > value() const override
Definition: property.hxx:145
T getImpl() const
Definition: property.hxx:73
FutureSync< void > setValue(AutoAnyReference value) override
Definition: property.hxx:151
auto track(F &&f, T &&toTrack) -> decltype(trackWithFallback(detail::throwPointerLockException, std::forward< F >(f), std::forward< T >(toTrack)))
Definition: trackable.hxx:420
UniqueAnyReference convert(TypeInterface *targetType) const
#define qiLogDebug(...)
Definition: log.hpp:76
static AnyValue from(const T &r)
Definition: anyvalue.hpp:94
FutureSync< T > get() const override
Definition: property.hxx:99
boost::function< T(boost::reference_wrapper< const T >)> Getter
Definition: property.hpp:72
~Property() override
Definition: property.hxx:125
#define QI_NOEXCEPT(cond)
Specify that a function may throw or not. Do nothing if noexcept is not available.
Definition: macro.hpp:318
FutureSync< void > setValue(AutoAnyReference value) override
Definition: property.hxx:118
boost::function< bool(boost::reference_wrapper< T >, const T &)> Setter
Definition: property.hpp:71
T to() const
Convert to anything or throw trying.
boost::function< Future< void >(bool)> OnSubscribers
Definition: signal.hpp:45
TypeInterface * type() const
FutureSync< void > set(const T &v) override
Definition: property.hxx:139
PropertyImpl(Getter getter=Getter(), Setter setter=Setter(), SignalBase::OnSubscribers onsubscribe=SignalBase::OnSubscribers())
Definition: property.hxx:29
const char * infoString()
FutureSync< void > set(const T &v) override
Definition: property.hxx:105
void setImpl(const T &v)
Definition: property.hxx:81
FutureSync< void > set(const AnyValue &v) override
Definition: property.hxx:16
FutureSync< T > get() const override
Definition: property.hxx:133
FutureSync< AnyValue > value() const override
Definition: property.hxx:112
void clearExecutionContext()