65 static handle
cast(
const T& src, return_value_policy , handle )
68 rclcpp::Serialization<T> serializer;
69 rclcpp::SerializedMessage serialized_msg;
70 serializer.serialize_message(&src, &serialized_msg);
71 py::bytes bytes = py::bytes(
reinterpret_cast<const char*
>(serialized_msg.get_rcl_serialized_message().buffer),
72 serialized_msg.get_rcl_serialized_message().buffer_length);
75 const std::string ros_msg_name = rosidl_generator_traits::name<T>();
78 std::size_t pos1 = ros_msg_name.find(
'/');
79 std::size_t pos2 = ros_msg_name.find(
'/', pos1 + 1);
80 py::module m = py::module::import((ros_msg_name.substr(0, pos1) +
".msg").c_str());
83 py::object cls = m.attr(ros_msg_name.substr(pos2 + 1).c_str());
86 py::module rclpy = py::module::import(
"rclpy.serialization");
87 py::object msg = rclpy.attr(
"deserialize_message")(bytes, cls);
93 bool load(handle src,
bool )
100 py::module rclpy = py::module::import(
"rclpy.serialization");
101 py::bytes bytes = rclpy.attr(
"serialize_message")(src);
104 rcl_serialized_message_t rcl_serialized_msg = rmw_get_zero_initialized_serialized_message();
105 char* serialized_buffer;
107 if (PYBIND11_BYTES_AS_STRING_AND_SIZE(bytes.ptr(), &serialized_buffer, &length))
109 throw py::error_already_set();
113 throw py::error_already_set();
115 rcl_serialized_msg.buffer_capacity = length;
116 rcl_serialized_msg.buffer_length = length;
117 rcl_serialized_msg.buffer =
reinterpret_cast<uint8_t*
>(serialized_buffer);
119 rmw_deserialize(&rcl_serialized_msg, rosidl_typesupport_cpp::get_message_type_support_handle<T>(), &value);
120 if (RMW_RET_OK != rmw_ret)
122 throw std::runtime_error(
"failed to deserialize ROS message");