ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
serialization.cpp
1 // Copyright 2020 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "rclcpp/serialization.hpp"
16 
17 #include <memory>
18 #include <string>
19 
20 #include "rclcpp/exceptions.hpp"
21 #include "rclcpp/serialized_message.hpp"
22 
23 #include "rcpputils/asserts.hpp"
24 
25 #include "rmw/rmw.h"
26 
27 namespace rclcpp
28 {
29 
30 SerializationBase::SerializationBase(const rosidl_message_type_support_t * type_support)
31 : type_support_(type_support)
32 {
33  rcpputils::check_true(nullptr != type_support, "Typesupport is nullpointer.");
34 }
35 
37  const void * ros_message, SerializedMessage * serialized_message) const
38 {
39  rcpputils::check_true(nullptr != ros_message, "ROS message is nullpointer.");
40  rcpputils::check_true(nullptr != serialized_message, "Serialized message is nullpointer.");
41 
42  const auto ret = rmw_serialize(
43  ros_message,
44  type_support_,
45  &serialized_message->get_rcl_serialized_message());
46  if (ret != RMW_RET_OK) {
47  rclcpp::exceptions::throw_from_rcl_error(ret, "Failed to serialize ROS message.");
48  }
49 }
50 
52  const SerializedMessage * serialized_message, void * ros_message) const
53 {
54  rcpputils::check_true(nullptr != serialized_message, "Serialized message is nullpointer.");
55  rcpputils::check_true(
56  0u != serialized_message->capacity(),
57  "Wrongly initialized. Serialized message has a capacity of zero.");
58  rcpputils::check_true(
59  0u != serialized_message->size(),
60  "Wrongly initialized. Serialized message has a size of zero.");
61  rcpputils::check_true(nullptr != ros_message, "ROS message is a nullpointer.");
62 
63  const auto ret = rmw_deserialize(
64  &serialized_message->get_rcl_serialized_message(), type_support_, ros_message);
65  if (ret != RMW_RET_OK) {
66  rclcpp::exceptions::throw_from_rcl_error(ret, "Failed to deserialize ROS message.");
67  }
68 }
69 
70 } // namespace rclcpp
SerializationBase(const rosidl_message_type_support_t *type_support)
Constructor of SerializationBase.
void serialize_message(const void *ros_message, SerializedMessage *serialized_message) const
Serialize a ROS2 message to a serialized stream.
void deserialize_message(const SerializedMessage *serialized_message, void *ros_message) const
Deserialize a serialized stream to a ROS message.
Object oriented version of rcl_serialized_message_t with destructor to avoid memory leaks.
size_t size() const
Get the size of the serialized data buffer.
rcl_serialized_message_t & get_rcl_serialized_message()
Get the underlying rcl_serialized_t handle.
size_t capacity() const
Get the size of allocated memory for the data buffer.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.