This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
utility_templates.hpp
296 lines (282 loc) · 10.9 KB
/
utility_templates.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2014-2016 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef UTILITY_TEMPLATES_HPP_
#define UTILITY_TEMPLATES_HPP_
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#ifndef _WIN32
# pragma GCC diagnostic push
# ifdef __clang__
# pragma clang diagnostic ignored "-Wdeprecated-register"
# pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
# endif
#endif
#include <ndds/ndds_cpp.h>
#ifndef _WIN32
# pragma GCC diagnostic pop
#endif
#include "rmw/error_handling.h"
#include "rosidl_typesupport_introspection_cpp/field_types.hpp"
#include "rosidl_typesupport_introspection_cpp/identifier.hpp"
#include "rosidl_typesupport_introspection_cpp/message_introspection.hpp"
#include "rosidl_typesupport_introspection_cpp/service_introspection.hpp"
#include "rosidl_typesupport_introspection_cpp/visibility_control.h"
#include "rosidl_typesupport_introspection_c/field_types.h"
#include "rosidl_typesupport_introspection_c/identifier.h"
#include "rosidl_typesupport_introspection_c/message_introspection.h"
#include "rosidl_typesupport_introspection_c/service_introspection.h"
#include "rosidl_typesupport_introspection_c/visibility_control.h"
#include "./macros.hpp"
template<typename MembersType>
ROSIDL_TYPESUPPORT_INTROSPECTION_CPP_LOCAL
inline std::string
_create_type_name(
const void * untyped_members,
const std::string & sep)
{
auto members = static_cast<const MembersType *>(untyped_members);
if (!members) {
RMW_SET_ERROR_MSG("members handle is null");
return "";
}
return
std::string(members->package_name_) + "::" + sep + "::dds_::" + members->message_name_ + "_";
}
template<typename ServiceType>
const void * get_request_ptr(const void * untyped_service_members)
{
auto service_members = static_cast<const ServiceType *>(untyped_service_members);
if (!service_members) {
RMW_SET_ERROR_MSG("service members handle is null");
return NULL;
}
return service_members->request_members_;
}
template<typename ServiceType>
const void * get_response_ptr(const void * untyped_service_members)
{
auto service_members = static_cast<const ServiceType *>(untyped_service_members);
if (!service_members) {
RMW_SET_ERROR_MSG("service members handle is null");
return NULL;
}
return service_members->response_members_;
}
template<typename MembersType>
DDS_TypeCode * create_type_code(
std::string type_name, const void * untyped_members, const char * introspection_identifier)
{
auto members = static_cast<const MembersType *>(untyped_members);
if (!members) {
RMW_SET_ERROR_MSG("members handle is null");
return NULL;
}
DDS_TypeCodeFactory * factory = DDS_TypeCodeFactory::get_instance();
if (!factory) {
RMW_SET_ERROR_MSG("failed to get typecode factory");
return NULL;
}
// Past this point, a failure results in unrolling code in the goto fail block.
DDS_StructMemberSeq struct_members;
DDS_ExceptionCode_t ex = DDS_NO_EXCEPTION_CODE;
// Start initializing elements.
DDS_TypeCode * type_code = factory->create_struct_tc(type_name.c_str(), struct_members, ex);
if (!type_code || ex != DDS_NO_EXCEPTION_CODE) {
RMW_SET_ERROR_MSG("failed to create struct typecode");
goto fail;
}
USING_INTROSPECTION_TYPEIDS()
for (uint32_t i = 0; i < members->member_count_; ++i) {
auto * member = members->members_ + i;
const DDS_TypeCode * member_type_code = nullptr;
DDS_TypeCode * member_type_code_non_const = nullptr;
switch (member->type_id_) {
case rosidl_typesupport_introspection_cpp::ROS_TYPE_BOOL:
member_type_code = factory->get_primitive_tc(DDS_TK_BOOLEAN);
break;
case rosidl_typesupport_introspection_cpp::ROS_TYPE_BYTE:
member_type_code = factory->get_primitive_tc(DDS_TK_OCTET);
break;
case rosidl_typesupport_introspection_cpp::ROS_TYPE_CHAR:
member_type_code = factory->get_primitive_tc(DDS_TK_CHAR);
break;
case rosidl_typesupport_introspection_cpp::ROS_TYPE_FLOAT32:
member_type_code = factory->get_primitive_tc(DDS_TK_FLOAT);
break;
case rosidl_typesupport_introspection_cpp::ROS_TYPE_FLOAT64:
member_type_code = factory->get_primitive_tc(DDS_TK_DOUBLE);
break;
case rosidl_typesupport_introspection_cpp::ROS_TYPE_INT8:
member_type_code = factory->get_primitive_tc(DDS_TK_OCTET);
break;
case rosidl_typesupport_introspection_cpp::ROS_TYPE_UINT8:
member_type_code = factory->get_primitive_tc(DDS_TK_OCTET);
break;
case rosidl_typesupport_introspection_cpp::ROS_TYPE_INT16:
member_type_code = factory->get_primitive_tc(DDS_TK_SHORT);
break;
case rosidl_typesupport_introspection_cpp::ROS_TYPE_UINT16:
member_type_code = factory->get_primitive_tc(DDS_TK_USHORT);
break;
case rosidl_typesupport_introspection_cpp::ROS_TYPE_INT32:
member_type_code = factory->get_primitive_tc(DDS_TK_LONG);
break;
case rosidl_typesupport_introspection_cpp::ROS_TYPE_UINT32:
member_type_code = factory->get_primitive_tc(DDS_TK_ULONG);
break;
case rosidl_typesupport_introspection_cpp::ROS_TYPE_INT64:
member_type_code = factory->get_primitive_tc(DDS_TK_LONGLONG);
break;
case rosidl_typesupport_introspection_cpp::ROS_TYPE_UINT64:
member_type_code = factory->get_primitive_tc(DDS_TK_ULONGLONG);
break;
case rosidl_typesupport_introspection_cpp::ROS_TYPE_STRING:
{
DDS_UnsignedLong max_string_size;
if (member->string_upper_bound_) {
if (member->string_upper_bound_ > (std::numeric_limits<DDS_UnsignedLong>::max)()) {
RMW_SET_ERROR_MSG(
"failed to create string typecode since the upper bound exceeds the DDS type");
goto fail;
}
max_string_size = static_cast<DDS_UnsignedLong>(member->string_upper_bound_);
} else {
max_string_size = RTI_INT32_MAX;
}
member_type_code_non_const = factory->create_string_tc(max_string_size, ex);
member_type_code = member_type_code_non_const;
}
if (!member_type_code || ex != DDS_NO_EXCEPTION_CODE) {
RMW_SET_ERROR_MSG("failed to create string typecode");
goto fail;
}
break;
case rosidl_typesupport_introspection_cpp::ROS_TYPE_MESSAGE:
{
if (!member->members_) {
RMW_SET_ERROR_MSG("members handle is null");
return NULL;
}
auto sub_members = static_cast<const MembersType *>(member->members_->data);
if (!sub_members) {
RMW_SET_ERROR_MSG("sub members handle is null");
return NULL;
}
std::string field_type_name = _create_type_name<MembersType>(sub_members, "msg");
member_type_code = create_type_code<MembersType>(field_type_name, sub_members,
introspection_identifier);
if (!member_type_code) {
// error string was set within the function
goto fail;
}
}
break;
default:
RMW_SET_ERROR_MSG(
(std::string("unknown type id ") + std::to_string(member->type_id_)).c_str());
goto fail;
}
if (!member_type_code) {
RMW_SET_ERROR_MSG("failed to create typecode");
goto fail;
}
if (member->is_array_) {
if (member->array_size_) {
if (member->array_size_ > (std::numeric_limits<DDS_UnsignedLong>::max)()) {
RMW_SET_ERROR_MSG("failed to create array typecode since the size exceeds the DDS type");
goto fail;
}
DDS_UnsignedLong array_size = static_cast<DDS_UnsignedLong>(member->array_size_);
if (!member->is_upper_bound_) {
member_type_code_non_const = factory->create_array_tc(array_size, member_type_code, ex);
member_type_code = member_type_code_non_const;
if (!member_type_code || ex != DDS_NO_EXCEPTION_CODE) {
RMW_SET_ERROR_MSG("failed to create array typecode");
goto fail;
}
} else {
member_type_code_non_const =
factory->create_sequence_tc(array_size, member_type_code, ex);
member_type_code = member_type_code_non_const;
if (!member_type_code || ex != DDS_NO_EXCEPTION_CODE) {
RMW_SET_ERROR_MSG("failed to create sequence typecode");
goto fail;
}
}
} else {
member_type_code_non_const = factory->create_sequence_tc(
RTI_INT32_MAX, member_type_code, ex);
member_type_code = member_type_code_non_const;
if (!member_type_code || ex != DDS_NO_EXCEPTION_CODE) {
RMW_SET_ERROR_MSG("failed to create sequence typecode");
goto fail;
}
}
}
auto zero_based_index = type_code->add_member(
(std::string(member->name_) + "_").c_str(),
i,
member_type_code,
DDS_TYPECODE_NONKEY_REQUIRED_MEMBER, ex);
if (ex != DDS_NO_EXCEPTION_CODE) {
RMW_SET_ERROR_MSG("failed to add member");
goto fail;
}
if (zero_based_index != i) {
RMW_SET_ERROR_MSG("unexpected member index");
return NULL;
}
}
// since empty message definitions are not supported
// we have to add the same dummy field as in rosidl_generator_dds_idl
if (members->member_count_ == 0) {
const DDS_TypeCode * member_type_code;
member_type_code = factory->get_primitive_tc(DDS_TK_BOOLEAN);
if (!member_type_code) {
RMW_SET_ERROR_MSG("failed to get primitive typecode");
goto fail;
}
type_code->add_member("_dummy", DDS_TYPECODE_MEMBER_ID_INVALID,
member_type_code,
DDS_TYPECODE_NONKEY_REQUIRED_MEMBER, ex);
if (ex != DDS_NO_EXCEPTION_CODE) {
RMW_SET_ERROR_MSG("failed to add member");
goto fail;
}
}
DDS_StructMemberSeq_finalize(&struct_members);
return type_code;
fail:
if (type_code) {
if (factory) {
DDS_ExceptionCode_t exc = DDS_NO_EXCEPTION_CODE;
factory->delete_tc(type_code, exc);
if (exc != DDS_NO_EXCEPTION_CODE) {
std::stringstream ss;
ss << "failed to delete type code during handling of failure at " <<
__FILE__ << ":" << __LINE__ << '\n';
(std::cerr << ss.str()).flush();
}
} else {
std::stringstream ss;
ss << "leaking type code during handling of failure at " <<
__FILE__ << ":" << __LINE__ << '\n';
(std::cerr << ss.str()).flush();
}
}
return nullptr;
}
#endif // UTILITY_TEMPLATES_HPP_