15 #include "rcl/node_type_cache.h"
16 #include "rcl/type_description_conversions.h"
18 #include "rcl/error_handling.h"
19 #include "rcutils/logging_macros.h"
20 #include "rcutils/types/hash_map.h"
22 #include "./context_impl.h"
23 #include "./node_impl.h"
34 static size_t get_type_hash_hashmap_key(
const void * key)
37 const rosidl_type_hash_t * type_hash = key;
38 return *(
size_t *)type_hash->value;
41 static int cmp_type_hash(
const void * val1,
const void * val2)
43 return memcmp(val1, val2,
sizeof(rosidl_type_hash_t));
50 if (NULL != node->
impl->registered_types_by_type_hash.impl) {
55 rcutils_ret_t ret = rcutils_hash_map_init(
56 &node->
impl->registered_types_by_type_hash, 2,
sizeof(rosidl_type_hash_t),
58 get_type_hash_hashmap_key, cmp_type_hash,
61 if (RCUTILS_RET_OK != ret) {
63 RCL_SET_ERROR_MSG(
"Failed to initialize type cache hash map");
76 rosidl_type_hash_t key;
78 rcutils_ret_t hash_map_ret = rcutils_hash_map_get_next_key_and_data(
79 &node->
impl->registered_types_by_type_hash, NULL, &key,
80 &type_info_with_registrations);
82 if (RCUTILS_RET_NOT_INITIALIZED == hash_map_ret) {
86 while (RCUTILS_RET_OK == hash_map_ret) {
87 hash_map_ret = rcutils_hash_map_unset(
88 &node->
impl->registered_types_by_type_hash, &key);
89 if (hash_map_ret != RCUTILS_RET_OK) {
90 RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
91 "Failed to clear out type informations [%s] during shutdown; memory "
93 rcutils_get_error_string().str);
97 type_description_interfaces__msg__TypeDescription__destroy(
98 type_info_with_registrations.
type_info.type_description);
99 type_description_interfaces__msg__TypeSource__Sequence__destroy(
100 type_info_with_registrations.
type_info.type_sources);
102 hash_map_ret = rcutils_hash_map_get_next_key_and_data(
103 &node->
impl->registered_types_by_type_hash, NULL, &key,
104 &type_info_with_registrations);
107 rcutils_ret_t rcutils_ret =
108 rcutils_hash_map_fini(&node->
impl->registered_types_by_type_hash);
113 rcl_ret_t rcl_node_type_cache_get_type_info(
115 const rosidl_type_hash_t * type_hash,
126 rcutils_hash_map_get(
127 &node->
impl->registered_types_by_type_hash,
128 type_hash, &type_info_with_registrations);
129 if (RCUTILS_RET_OK == ret) {
130 *type_info = type_info_with_registrations.
type_info;
132 }
else if (RCUTILS_RET_NOT_INITIALIZED == ret) {
139 rcl_ret_t rcl_node_type_cache_register_type(
140 const rcl_node_t * node,
const rosidl_type_hash_t * type_hash,
141 const rosidl_runtime_c__type_description__TypeDescription * type_description,
142 const rosidl_runtime_c__type_description__TypeSource__Sequence * type_description_sources)
152 const rcutils_ret_t rcutils_ret = rcutils_hash_map_get(
153 &node->
impl->registered_types_by_type_hash,
154 type_hash, &type_info_with_registrations);
156 if (RCUTILS_RET_OK == rcutils_ret) {
160 }
else if (RCUTILS_RET_NOT_FOUND == rcutils_ret) {
165 type_info_with_registrations.
type_info.type_description =
166 rcl_convert_type_description_runtime_to_msg(type_description);
167 if (type_info_with_registrations.
type_info.type_description == NULL) {
173 type_info_with_registrations.
type_info.type_sources =
174 rcl_convert_type_source_sequence_runtime_to_msg(type_description_sources);
175 if (type_info_with_registrations.
type_info.type_sources == NULL) {
177 type_description_interfaces__msg__TypeDescription__destroy(
178 type_info_with_registrations.
type_info.type_description);
186 if (RCUTILS_RET_OK !=
187 rcutils_hash_map_set(
188 &node->
impl->registered_types_by_type_hash,
189 type_hash, &type_info_with_registrations))
192 rcutils_reset_error();
193 RCL_SET_ERROR_MSG(
"Failed to update type info");
194 type_description_interfaces__msg__TypeDescription__destroy(
195 type_info_with_registrations.
type_info.type_description);
196 type_description_interfaces__msg__TypeSource__Sequence__destroy(
197 type_info_with_registrations.
type_info.type_sources);
204 rcl_ret_t rcl_node_type_cache_unregister_type(
206 const rosidl_type_hash_t * type_hash)
214 if (RCUTILS_RET_OK !=
215 rcutils_hash_map_get(
216 &node->
impl->registered_types_by_type_hash,
217 type_hash, &type_info))
219 RCL_SET_ERROR_MSG(
"Failed to unregister type, hash not present in map.");
224 if (RCUTILS_RET_OK !=
225 rcutils_hash_map_set(
226 &node->
impl->registered_types_by_type_hash,
227 type_hash, &type_info))
229 RCL_SET_ERROR_MSG(
"Failed to update type info");
233 if (RCUTILS_RET_OK !=
234 rcutils_hash_map_unset(
235 &node->
impl->registered_types_by_type_hash,
238 RCL_SET_ERROR_MSG(
"Failed to unregister type info");
242 type_description_interfaces__msg__TypeDescription__destroy(
244 type_description_interfaces__msg__TypeSource__Sequence__destroy(
rcl_allocator_t allocator
Allocator used during init and shutdown.
rcl_context_impl_t * impl
Implementation specific pointer.
Structure which encapsulates a ROS Node.
rcl_node_impl_t * impl
Private implementation pointer.
rcl_context_t * context
Context associated with this node.
size_t num_registrations
Counter to keep track of registrations.
rcl_type_info_t type_info
The actual type info.
#define RCL_RET_NOT_INIT
rcl_init() not yet called return code.
#define RCL_RET_OK
Success return code.
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
#define RCL_RET_ERROR
Unspecified error return code.
#define RCL_RET_NODE_INVALID
Invalid rcl_node_t given return code.
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.