libqi-api  release-2.5.3-2016-11-18
 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 <qi/signal.hpp>
13 #include <qi/future.hpp>
14 
15 #ifdef _MSC_VER
16 # pragma warning( push )
17  // needs to have dll-interface to be used by clients
18 # pragma warning( disable: 4251 )
19  // non dll-interface class * used as base for dll-interface class
20 # pragma warning( disable: 4275 )
21 #endif
22 
23 namespace qi
24 {
28  {
29  public:
30  virtual ~PropertyBase() = default;
31  virtual SignalBase* signal() = 0;
32  virtual FutureSync<void> setValue(AutoAnyReference value) = 0;
33  virtual FutureSync<AnyValue> value() const = 0;
34  };
35 
36  template<typename T>
37  class PropertyImpl: public SignalF<void(const T&)>, public PropertyBase
38  {
39  public:
43  using Setter = boost::function<bool (T&, const T&)>;
44  using Getter = boost::function<T(const T&)>;
46  using PropertyType = T;
47 
56  PropertyImpl(Getter getter = Getter(), Setter setter = Setter(),
58 
59  PropertyImpl(AutoAnyReference defaultValue, Getter getter = Getter(), Setter setter = Setter(),
61 
62  virtual FutureSync<T> get() const = 0;
63  virtual FutureSync<void> set(const T& v) = 0;
64 
65  PropertyImpl<T>& operator=(const T& v) { this->set(v); return *this; }
66 
67  SignalBase* signal() override { return this; }
68 
69  protected:
72  T _value;
73 
74  T getImpl() const;
75  void setImpl(const T& v);
76  };
77 
83  template<typename T>
84  class UnsafeProperty: public PropertyImpl<T>
85  {
86  public:
88  using Getter = typename ImplType::Getter;
89  using Setter = typename ImplType::Setter;
90 
91  UnsafeProperty(Getter getter = Getter(), Setter setter = Setter(),
93  : PropertyImpl<T>(std::move(getter), std::move(setter), std::move(onsubscribe))
94  {}
95 
96  UnsafeProperty(AutoAnyReference defaultValue, Getter getter = Getter(), Setter setter = Setter(),
98  : PropertyImpl<T>(std::move(defaultValue), std::move(getter), std::move(setter), std::move(onsubscribe))
99  {}
100 
101  UnsafeProperty<T>& operator=(const T& v) { this->set(v); return *this; }
102 
103  FutureSync<T> get() const override;
104  FutureSync<void> set(const T& v) override;
105 
106  SignalBase* signal() override { return this; }
108  FutureSync<AnyValue> value() const override;
109 
110  };
111 
112 
118  template<typename T>
119  class Property: public PropertyImpl<T>
120  {
121  public:
123  using Getter = typename ImplType::Getter;
124  using Setter = typename ImplType::Setter;
125  class ScopedLockReadWrite;
126  class ScopedLockReadOnly;
127 
128  Property(Getter getter = Getter(), Setter setter = Setter(),
130  : PropertyImpl<T>(std::move(getter), std::move(setter), std::move(onsubscribe))
131  {}
132 
133  Property(AutoAnyReference defaultValue, Getter getter = Getter(), Setter setter = Setter(),
135  : PropertyImpl<T>(std::move(defaultValue), std::move(getter), std::move(setter), std::move(onsubscribe))
136  {}
137 
138  Property<T>& operator=(const T& v) { this->set(v); return *this; }
139 
140  FutureSync<T> get() const override;
141  FutureSync<void> set(const T& v) override;
142 
143  SignalBase* signal() override { return this; }
145  FutureSync<AnyValue> value() const override;
146 
148  ScopedLockReadWrite getLockedReadWrite();
150  ScopedLockReadOnly getLockedReadOnly() const;
151  private:
152  mutable boost::mutex _mutex;
153  };
154 
159  template<class T>
161  : boost::noncopyable
162  {
163  public:
165  : _property(property)
166  , _lock(property._mutex)
167  {
168  }
169 
171  {
172  _property(_property._value); // Trigger update of the observer callbacks with the new value.
173  }
174 
175  T& operator*() { return _property._value; }
176  T* operator->() { return &_property._value; }
177 
178  const T& operator*() const { return _property._value; }
179  const T* operator->() const { return &_property._value; }
180 
181  private:
182  Property<T>& _property;
183  boost::mutex::scoped_lock _lock;
184  };
185 
190  template<class T>
192  : boost::noncopyable
193  {
194  public:
196  : _property(property)
197  , _lock(property._mutex)
198  {
199  }
200 
201  const T& get() const { return _property._value; }
202  const T& operator*() const { return get(); }
203  const T* operator->() const { return &get(); }
204 
205  private:
206  const Property<T>& _property;
207  boost::mutex::scoped_lock _lock;
208  };
209 
210 
212  class QI_API GenericProperty : public Property<AnyValue>
213  {
214  public:
215  GenericProperty(TypeInterface* type, Getter getter = Getter(), Setter setter = Setter())
216  :Property<AnyValue>(getter, setter)
217  , _type(type)
218  { // Initialize with default value for given type
219  set(AnyValue(_type));
220  std::vector<TypeInterface*> types(&_type, &_type + 1);
221  _setSignature(makeTupleSignature(types));
222  }
223 
224  GenericProperty& operator=(const AnyValue& v) { this->set(v); return *this; }
225 
226  FutureSync<void> setValue(AutoAnyReference value) override { return set(AnyValue(value, false, false));}
227  FutureSync<void> set(const AnyValue& v) override;
228  qi::Signature signature() const override {
229  return makeTupleSignature(std::vector<TypeInterface*>(&_type, &_type + 1));
230  }
231  private:
232  TypeInterface* _type;
233  };
234 }
235 
237 
238 #ifdef _MSC_VER
239 # pragma warning( pop )
240 #endif
241 
242 #endif // _QITYPE_PROPERTY_HPP_
FutureSync< AnyValue > value() const override
Definition: property.hxx:115
void setValue(qi::Promise< R > &p, const boost::function< R()> &f)
GenericProperty & operator=(const AnyValue &v)
Definition: property.hpp:224
T getImpl() const
Definition: property.hxx:47
boost::function< void(bool)> OnSubscribers
Definition: signal.hpp:41
FutureSync< void > setValue(AutoAnyReference value) override
Definition: property.hxx:122
#define QI_API
Definition: api.hpp:33
SignalBase * signal() override
Definition: property.hpp:106
const T * operator->() const
Definition: property.hpp:203
ScopedLockReadWrite getLockedReadWrite()
Definition: property.hxx:131
typename ImplType::Getter Getter
Definition: property.hpp:123
Property(AutoAnyReference defaultValue, Getter getter=Getter(), Setter setter=Setter(), SignalBase::OnSubscribers onsubscribe=SignalBase::OnSubscribers())
Definition: property.hpp:133
typename ImplType::Getter Getter
Definition: property.hpp:88
UnsafeProperty(AutoAnyReference defaultValue, Getter getter=Getter(), Setter setter=Setter(), SignalBase::OnSubscribers onsubscribe=SignalBase::OnSubscribers())
Definition: property.hpp:96
typename ImplType::Setter Setter
Definition: property.hpp:124
FutureSync< void > setValue(AutoAnyReference value) override
Definition: property.hxx:92
qi::Signature signature() const override
Definition: property.hpp:228
boost::function< AnyValue(const AnyValue &)> Getter
Definition: property.hpp:44
UnsafeProperty(Getter getter=Getter(), Setter setter=Setter(), SignalBase::OnSubscribers onsubscribe=SignalBase::OnSubscribers())
Definition: property.hpp:91
GenericProperty(TypeInterface *type, Getter getter=Getter(), Setter setter=Setter())
Definition: property.hpp:215
virtual FutureSync< void > set(const T &v)=0
ScopedLockReadOnly(const Property< T > &property)
Definition: property.hpp:195
FutureSync< void > set(const T &v) override
Definition: property.hxx:107
Type-erased property, simulating a typed property but using AnyValue.
Definition: property.hpp:212
UnsafeProperty< T > & operator=(const T &v)
Definition: property.hpp:101
PropertyImpl(Getter getter=Getter(), Setter setter=Setter(), SignalBase::OnSubscribers onsubscribe=SignalBase::OnSubscribers())
Definition: property.hxx:27
Property(Getter getter=Getter(), Setter setter=Setter(), SignalBase::OnSubscribers onsubscribe=SignalBase::OnSubscribers())
Definition: property.hpp:128
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 >())
SignalBase * signal() override
Definition: property.hpp:67
FutureSync< void > set(const T &v) override
Definition: property.hxx:79
PropertyImpl< T > & operator=(const T &v)
Definition: property.hpp:65
ScopedLockReadWrite(Property< T > &property)
Definition: property.hpp:164
void setImpl(const T &v)
Definition: property.hxx:55
const T & operator*() const
Definition: property.hpp:202
typename ImplType::Setter Setter
Definition: property.hpp:89
boost::function< bool(AnyValue &, const AnyValue &)> Setter
Definition: property.hpp:43
const T & operator*() const
Definition: property.hpp:178
Property< T > & operator=(const T &v)
Definition: property.hpp:138
ScopedLockReadOnly getLockedReadOnly() const
Definition: property.hxx:138
FutureSync< AnyValue > value() const override
Definition: property.hxx:86
FutureSync< void > setValue(AutoAnyReference value) override
Definition: property.hpp:226
const T * operator->() const
Definition: property.hpp:179
SignalBase * signal() override
Definition: property.hpp:143