#pragma once #include #include #include #include #include #include #include /// make boost::python understand std::shared_ptr namespace boost { template T* get_pointer(std::shared_ptr p) { return p.get(); } } namespace moveit { namespace python { template std::vector fromList(const boost::python::list& values) { boost::python::stl_input_iterator begin(values), end; return std::vector(begin, end); } template boost::python::list toList(const std::vector& v) { boost::python::list l; for (const T& value : v) l.append(value); return l; } template std::map fromDict(const boost::python::dict& values) { std::map m; for (boost::python::stl_input_iterator it(values.iteritems()), end; it != end; ++it) { const std::string& key = boost::python::extract((*it)[0]); const T& value = boost::python::extract((*it)[1]); m.insert(std::make_pair(key, value)); } return m; } template boost::python::dict toDict(const std::map& v) { boost::python::dict d; for (const std::pair& p : v) d[p.first] = p.second; return d; } /// convert a ROS msg to a string template std::string serializeMsg(const T& msg) { static_assert(sizeof(uint8_t) == sizeof(char), "Assuming char has same size as uint8_t"); std::size_t size = ros::serialization::serializationLength(msg); std::string result(size, '\0'); if (size) { ros::serialization::OStream stream(reinterpret_cast(&result[0]), size); ros::serialization::serialize(stream, msg); } return result; } /// convert a string to a ROS message template void deserializeMsg(const std::string& data, T& msg) { ros::serialization::IStream stream(const_cast(reinterpret_cast(&data[0])), data.size()); ros::serialization::deserialize(stream, msg); } /// convert a ROS message (from python) to a string std::string _serializeMsg(const boost::python::object& msg); /// convert a string to a python ROS message of given type boost::python::object _deserializeMsg(const std::string& data, const std::string& python_type_name); /// convert a python type name (extracted from __class__.__module__) to form package/msg std::string rosMsgName(const std::string& python_type_name); /// convert a ROS message from python to C++ template T fromPython(const boost::python::object &o) { std::string serialized = _serializeMsg(o); T result; deserializeMsg(serialized, result); return result; } /// convert a ROS message from C++ to python template boost::python::object toPython(const std::string& python_type_name, const T& msg) { std::string serialized = serializeMsg(msg); return _deserializeMsg(serialized, python_type_name); } } }