ROS 2 rclcpp + rcl - rolling  rolling-e7363f10
ROS 2 C++ Client Library with ROS Client Library
type_description_conversions.c
1 // Copyright 2023 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 <stdbool.h>
16 #include <stddef.h>
17 
18 #include "rcl/error_handling.h"
19 #include "rcl/type_description_conversions.h"
20 
21 #include "rosidl_runtime_c/string_functions.h"
22 #include "rosidl_runtime_c/type_description/field__functions.h"
23 #include "rosidl_runtime_c/type_description/individual_type_description__functions.h"
24 #include "rosidl_runtime_c/type_description/type_description__functions.h"
25 #include "rosidl_runtime_c/type_description/type_source__functions.h"
26 #include "type_description_interfaces/msg/detail/field__functions.h"
27 #include "type_description_interfaces/msg/individual_type_description.h"
28 
29 static bool individual_type_description_runtime_to_msg(
30  const rosidl_runtime_c__type_description__IndividualTypeDescription * in,
31  type_description_interfaces__msg__IndividualTypeDescription * out)
32 {
33  RCL_CHECK_ARGUMENT_FOR_NULL(in, false);
34  RCL_CHECK_ARGUMENT_FOR_NULL(out, false);
35 
36  const bool success =
37  rosidl_runtime_c__String__copy(&in->type_name, &out->type_name) &&
38  type_description_interfaces__msg__Field__Sequence__init(
39  &out->fields,
40  in->fields.size);
41  if (!success) {
42  goto error;
43  }
44 
45  for (size_t i = 0; i < in->fields.size; ++i) {
46  if (!rosidl_runtime_c__String__copy(
47  &(in->fields.data[i].name),
48  &(out->fields.data[i].name)))
49  {
50  goto error;
51  }
52 
53  if (in->fields.data[i].default_value.size) {
54  if (!rosidl_runtime_c__String__copy(
55  &(in->fields.data[i].default_value),
56  &(out->fields.data[i].default_value)))
57  {
58  goto error;
59  }
60  }
61 
62  // type_id
63  out->fields.data[i].type.type_id = in->fields.data[i].type.type_id;
64  // capacity
65  out->fields.data[i].type.capacity = in->fields.data[i].type.capacity;
66  // string_capacity
67  out->fields.data[i].type.string_capacity =
68  in->fields.data[i].type.string_capacity;
69 
70  // nested_type_name
71  if (in->fields.data[i].type.nested_type_name.size) {
72  if (!rosidl_runtime_c__String__copy(
73  &(in->fields.data[i].type.nested_type_name),
74  &(out->fields.data[i].type.nested_type_name)))
75  {
76  goto error;
77  }
78  }
79  }
80 
81  return true;
82 
83 error:
84  type_description_interfaces__msg__IndividualTypeDescription__fini(out);
85  return false;
86 }
87 
88 static bool individual_type_description_msg_to_runtime(
89  const type_description_interfaces__msg__IndividualTypeDescription * in,
90  rosidl_runtime_c__type_description__IndividualTypeDescription * out)
91 {
92  RCL_CHECK_ARGUMENT_FOR_NULL(in, false);
93  RCL_CHECK_ARGUMENT_FOR_NULL(out, false);
94 
95  const bool success =
96  rosidl_runtime_c__String__copy(&in->type_name, &out->type_name) &&
97  rosidl_runtime_c__type_description__Field__Sequence__init(
98  &out->fields, in->fields.size);
99  if (!success) {
100  goto error;
101  }
102 
103  for (size_t i = 0; i < in->fields.size; ++i) {
104  if (!rosidl_runtime_c__String__copy(
105  &(in->fields.data[i].name),
106  &(out->fields.data[i].name)))
107  {
108  goto error;
109  }
110 
111  if (in->fields.data[i].default_value.size) {
112  if (!rosidl_runtime_c__String__copy(
113  &(in->fields.data[i].default_value),
114  &(out->fields.data[i].default_value)))
115  {
116  goto error;
117  }
118  }
119 
120  // type_id
121  out->fields.data[i].type.type_id = in->fields.data[i].type.type_id;
122  // capacity
123  out->fields.data[i].type.capacity = in->fields.data[i].type.capacity;
124  // string_capacity
125  out->fields.data[i].type.string_capacity =
126  in->fields.data[i].type.string_capacity;
127 
128  // nested_type_name
129  if (in->fields.data[i].type.nested_type_name.size) {
130  if (!rosidl_runtime_c__String__copy(
131  &(in->fields.data[i].type.nested_type_name),
132  &(out->fields.data[i].type.nested_type_name)))
133  {
134  goto error;
135  }
136  }
137  }
138 
139  return true;
140 
141 error:
142  rosidl_runtime_c__type_description__IndividualTypeDescription__init(out);
143  return false;
144 }
145 
146 type_description_interfaces__msg__TypeDescription *
147 rcl_convert_type_description_runtime_to_msg(
148  const rosidl_runtime_c__type_description__TypeDescription * runtime_description)
149 {
150  RCL_CHECK_ARGUMENT_FOR_NULL(runtime_description, NULL);
151 
152  // Create the object
153  type_description_interfaces__msg__TypeDescription * out =
154  type_description_interfaces__msg__TypeDescription__create();
155  RCL_CHECK_ARGUMENT_FOR_NULL(out, NULL);
156 
157  // init referenced_type_descriptions with the correct size
158  if (!type_description_interfaces__msg__IndividualTypeDescription__Sequence__init(
159  &out->referenced_type_descriptions,
160  runtime_description->referenced_type_descriptions.size))
161  {
162  goto fail;
163  }
164 
165  // Convert individual type description
166  if (!individual_type_description_runtime_to_msg(
167  &runtime_description->type_description,
168  &out->type_description))
169  {
170  goto fail;
171  }
172 
173  // Convert referenced type descriptions
174  for (size_t i = 0; i < runtime_description->referenced_type_descriptions.size; ++i) {
175  if (!individual_type_description_runtime_to_msg(
176  &runtime_description->referenced_type_descriptions.data[i],
177  &out->referenced_type_descriptions.data[i]))
178  {
179  goto fail;
180  }
181  }
182 
183  return out;
184 
185 fail:
186  type_description_interfaces__msg__TypeDescription__destroy(out);
187  return NULL;
188 }
189 
190 rosidl_runtime_c__type_description__TypeDescription *
191 rcl_convert_type_description_msg_to_runtime(
192  const type_description_interfaces__msg__TypeDescription * description_msg)
193 {
194  RCL_CHECK_ARGUMENT_FOR_NULL(description_msg, NULL);
195 
196  // Create the object
197  rosidl_runtime_c__type_description__TypeDescription * out =
198  rosidl_runtime_c__type_description__TypeDescription__create();
199  RCL_CHECK_ARGUMENT_FOR_NULL(out, NULL);
200 
201  // init referenced_type_descriptions with the correct size
202  if (!rosidl_runtime_c__type_description__IndividualTypeDescription__Sequence__init(
203  &out->referenced_type_descriptions,
204  description_msg->referenced_type_descriptions.size))
205  {
206  goto fail;
207  }
208 
209  if (!individual_type_description_msg_to_runtime(
210  &description_msg->type_description,
211  &out->type_description))
212  {
213  goto fail;
214  }
215 
216  for (size_t i = 0; i < description_msg->referenced_type_descriptions.size; ++i) {
217  if (!individual_type_description_msg_to_runtime(
218  &description_msg->referenced_type_descriptions.data[i],
219  &out->referenced_type_descriptions.data[i]))
220  {
221  goto fail;
222  }
223  }
224 
225  return out;
226 
227 fail:
228  rosidl_runtime_c__type_description__TypeDescription__destroy(out);
229  return NULL;
230 }
231 
232 type_description_interfaces__msg__TypeSource__Sequence *
233 rcl_convert_type_source_sequence_runtime_to_msg(
234  const rosidl_runtime_c__type_description__TypeSource__Sequence * runtime_type_sources)
235 {
236  RCL_CHECK_ARGUMENT_FOR_NULL(runtime_type_sources, NULL);
237 
238  // Create the object
239  type_description_interfaces__msg__TypeSource__Sequence * out =
240  type_description_interfaces__msg__TypeSource__Sequence__create(runtime_type_sources->size);
241  RCL_CHECK_ARGUMENT_FOR_NULL(out, NULL);
242 
243  // Copy type sources
244  for (size_t i = 0; i < runtime_type_sources->size; ++i) {
245  // type_name
246  if (!rosidl_runtime_c__String__copy(
247  &(runtime_type_sources->data[i].type_name), &(out->data[i].type_name)))
248  {
249  goto fail;
250  }
251  // encoding
252  if (!rosidl_runtime_c__String__copy(
253  &(runtime_type_sources->data[i].encoding), &(out->data[i].encoding)))
254  {
255  goto fail;
256  }
257  // raw_file_contents
258  if (runtime_type_sources->data[i].raw_file_contents.size > 0) {
259  if (!rosidl_runtime_c__String__copy(
260  &(runtime_type_sources->data[i].raw_file_contents), &(out->data[i].raw_file_contents)))
261  {
262  goto fail;
263  }
264  }
265  }
266 
267  return out;
268 
269 fail:
270  type_description_interfaces__msg__TypeSource__Sequence__destroy(out);
271  return NULL;
272 }
273 
274 rosidl_runtime_c__type_description__TypeSource__Sequence *
275 rcl_convert_type_source_sequence_msg_to_runtime(
276  const type_description_interfaces__msg__TypeSource__Sequence * type_sources_msg)
277 {
278  RCL_CHECK_ARGUMENT_FOR_NULL(type_sources_msg, NULL);
279 
280  // Create the object
281  rosidl_runtime_c__type_description__TypeSource__Sequence * out =
282  rosidl_runtime_c__type_description__TypeSource__Sequence__create(type_sources_msg->size);
283  RCL_CHECK_ARGUMENT_FOR_NULL(out, NULL);
284 
285  // Copy type sources
286  for (size_t i = 0; i < type_sources_msg->size; ++i) {
287  // type_name
288  if (!rosidl_runtime_c__String__copy(
289  &(type_sources_msg->data[i].type_name), &(out->data[i].type_name)))
290  {
291  goto fail;
292  }
293  // encoding
294  if (!rosidl_runtime_c__String__copy(
295  &(type_sources_msg->data[i].encoding), &(out->data[i].encoding)))
296  {
297  goto fail;
298  }
299  // raw_file_contents
300  if (!rosidl_runtime_c__String__copy(
301  &(type_sources_msg->data[i].raw_file_contents), &(out->data[i].raw_file_contents)))
302  {
303  goto fail;
304  }
305  }
306 
307  return out;
308 
309 fail:
310  rosidl_runtime_c__type_description__TypeSource__Sequence__destroy(out);
311  return NULL;
312 }