MTC
properties.h
1 #pragma once
2 
3 #include <pybind11/smart_holder.h>
4 #include <py_binding_tools/ros_msg_typecasters.h>
5 #include <moveit/task_constructor/properties.h>
6 #include <boost/any.hpp>
7 #include <typeindex>
8 
9 PYBIND11_SMART_HOLDER_TYPE_CASTERS(moveit::task_constructor::Property)
10 PYBIND11_SMART_HOLDER_TYPE_CASTERS(moveit::task_constructor::PropertyMap)
11 
12 namespace moveit {
13 namespace python {
14 
15 class PYBIND11_EXPORT PropertyConverterBase
16 {
17 public:
18  using to_python_converter_function = pybind11::object (*)(const boost::any&);
19  using from_python_converter_function = boost::any (*)(const pybind11::object&);
20 
21 protected:
22  static bool insert(const std::type_index& type_index, const std::string& ros_msg_name,
23  to_python_converter_function to, from_python_converter_function from);
24 };
25 
27 template <typename T>
29 {
30 public:
31  PropertyConverter() { insert(typeid(T), rosMsgName<T>(), &toPython, &fromPython); }
32 
33 private:
34  static pybind11::object toPython(const boost::any& value) { return pybind11::cast(boost::any_cast<T>(value)); }
35  static boost::any fromPython(const pybind11::object& po) { return pybind11::cast<T>(po); }
36 
37  template <class Q = T>
38  typename std::enable_if<ros::message_traits::IsMessage<Q>::value, std::string>::type rosMsgName() {
39  return ros::message_traits::DataType<T>::value();
40  }
41 
42  template <class Q = T>
43  typename std::enable_if<!ros::message_traits::IsMessage<Q>::value, std::string>::type rosMsgName() {
44  return std::string();
45  }
46 };
47 
48 namespace properties {
49 
55 template <typename type_, typename... options>
56 class class_ : public pybind11::classh<type_, options...> // NOLINT(readability-identifier-naming)
57 {
58  using base_class_ = pybind11::classh<type_, options...>;
59 
60 public:
61  // forward all constructors
62  using base_class_::classh;
63 
64  template <typename PropertyType, typename... Extra>
65  class_& property(const char* name, const Extra&... extra) {
66  PropertyConverter<PropertyType>(); // register corresponding property converter
67  auto getter = [name](type_& self) -> PropertyType& {
68  moveit::task_constructor::PropertyMap& props = self.properties();
69  return const_cast<PropertyType&>(props.get<PropertyType>(name));
70  };
71  auto setter = [name](type_& self, const PropertyType& value) {
72  moveit::task_constructor::PropertyMap& props = self.properties();
73  props.set(name, boost::any(value));
74  };
75  base_class_::def_property(name, getter, setter, pybind11::return_value_policy::reference_internal, extra...);
76  return *this;
77  }
78 };
79 } // namespace properties
80 } // namespace python
81 } // namespace moveit
Definition: properties.h:16
utility class to register C++ / Python converters for a property of type T
Definition: properties.h:29
Definition: properties.h:257
const boost::any & get(const std::string &name) const
Get the value of a property. Throws undeclared if unknown name.
Definition: properties.cpp:254
void set(const std::string &name, const T &value)
set (and, if neccessary, declare) the value of a property
Definition: properties.h:304
Definition: properties.h:70