libqi-api  2.4.3.28
 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_DETAILS_PROPERTY_HXX_
8 #define _QITYPE_DETAILS_PROPERTY_HXX_
9 
10 
11 namespace qi
12 {
13 
14  inline void GenericProperty::set(const AnyValue& v)
15  {
16  std::pair<AnyReference, bool> conv = v.convert(_type);
17  if (!conv.first.type())
18  throw std::runtime_error(std::string("Failed converting ") + v.type()->infoString() + " to " + _type->infoString());
19 
20  Property<AnyValue>::set(AnyValue(conv.first, false, false));
21  if (conv.second)
22  conv.first.destroy();
23  }
24 
25  template<typename T>
27  SignalBase::OnSubscribers onsubscribe)
28  : SignalF<void(const T&)>(onsubscribe)
29  , _getter(getter)
30  , _setter(setter)
31  {
32  }
33 
34  template<typename T>
36  {
37  if (_getter)
38  return _getter(_value);
39  else
40  return _value;
41  }
42  template<typename T>
43  void PropertyImpl<T>::set(const T& v)
44  {
45  qiLogDebug("qitype.property") << "set " << this << " " << (!!_setter);
46  if (_setter)
47  {
48  bool ok = _setter(_value, v);
49  if (ok)
50  (*this)(_value);
51  }
52  else
53  {
54  _value = v;
55  (*this)(_value);
56  }
57  }
58 }
59 
60 #endif // _QITYPE_DETAILS_PROPERTY_HXX_
std::pair< AnyReference, bool > convert(TypeInterface *targetType) const
#define qiLogDebug(...)
Definition: log.hpp:69
T get() const
Definition: property.hxx:35
TypeInterface * type() const
void set(const AnyValue &v)
Definition: property.hxx:14
PropertyImpl(Getter getter=Getter(), Setter setter=Setter(), SignalBase::OnSubscribers onsubscribe=SignalBase::OnSubscribers())
Definition: property.hxx:26
const char * infoString()
boost::function< void(bool)> OnSubscribers
Definition: signal.hpp:41
boost::function< T(const T &)> Getter
Definition: property.hpp:43
boost::function< bool(T &, const T &)> Setter
Definition: property.hpp:42
void set(const T &v)
Definition: property.hxx:43