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) {
62 RCL_SET_ERROR_MSG(
"Failed to initialize type cache hash map");
75 rosidl_type_hash_t key;
77 rcutils_ret_t hash_map_ret = rcutils_hash_map_get_next_key_and_data(
78 &node->
impl->registered_types_by_type_hash, NULL, &key,
79 &type_info_with_registrations);
81 if (RCUTILS_RET_NOT_INITIALIZED == hash_map_ret) {
85 while (RCUTILS_RET_OK == hash_map_ret) {
86 hash_map_ret = rcutils_hash_map_unset(
87 &node->
impl->registered_types_by_type_hash, &key);
88 if (hash_map_ret != RCUTILS_RET_OK) {
89 RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
90 "Failed to clear out type informations [%s] during shutdown; memory "
92 rcutils_get_error_string().str);
96 type_description_interfaces__msg__TypeDescription__destroy(
97 type_info_with_registrations.
type_info.type_description);
98 type_description_interfaces__msg__TypeSource__Sequence__destroy(
99 type_info_with_registrations.
type_info.type_sources);
101 hash_map_ret = rcutils_hash_map_get_next_key_and_data(
102 &node->
impl->registered_types_by_type_hash, NULL, &key,
103 &type_info_with_registrations);
106 rcutils_ret_t rcutils_ret =
107 rcutils_hash_map_fini(&node->
impl->registered_types_by_type_hash);
112 rcl_ret_t rcl_node_type_cache_get_type_info(
114 const rosidl_type_hash_t * type_hash,
125 rcutils_hash_map_get(
126 &node->
impl->registered_types_by_type_hash,
127 type_hash, &type_info_with_registrations);
128 if (RCUTILS_RET_OK == ret) {
129 *type_info = type_info_with_registrations.
type_info;
131 }
else if (RCUTILS_RET_NOT_INITIALIZED == ret) {
138 rcl_ret_t rcl_node_type_cache_register_type(
139 const rcl_node_t * node,
const rosidl_type_hash_t * type_hash,
140 const rosidl_runtime_c__type_description__TypeDescription * type_description,
141 const rosidl_runtime_c__type_description__TypeSource__Sequence * type_description_sources)
151 const rcutils_ret_t rcutils_ret = rcutils_hash_map_get(
152 &node->
impl->registered_types_by_type_hash,
153 type_hash, &type_info_with_registrations);
155 if (RCUTILS_RET_OK == rcutils_ret) {
159 }
else if (RCUTILS_RET_NOT_FOUND == rcutils_ret) {
164 type_info_with_registrations.
type_info.type_description =
165 rcl_convert_type_description_runtime_to_msg(type_description);
166 if (type_info_with_registrations.
type_info.type_description == NULL) {
172 type_info_with_registrations.
type_info.type_sources =
173 rcl_convert_type_source_sequence_runtime_to_msg(type_description_sources);
174 if (type_info_with_registrations.
type_info.type_sources == NULL) {
176 type_description_interfaces__msg__TypeDescription__destroy(
177 type_info_with_registrations.
type_info.type_description);
185 if (RCUTILS_RET_OK !=
186 rcutils_hash_map_set(
187 &node->
impl->registered_types_by_type_hash,
188 type_hash, &type_info_with_registrations))
190 RCL_SET_ERROR_MSG(
"Failed to update type info");
191 type_description_interfaces__msg__TypeDescription__destroy(
192 type_info_with_registrations.
type_info.type_description);
193 type_description_interfaces__msg__TypeSource__Sequence__destroy(
194 type_info_with_registrations.
type_info.type_sources);
201 rcl_ret_t rcl_node_type_cache_unregister_type(
203 const rosidl_type_hash_t * type_hash)
211 if (RCUTILS_RET_OK !=
212 rcutils_hash_map_get(
213 &node->
impl->registered_types_by_type_hash,
214 type_hash, &type_info))
216 RCL_SET_ERROR_MSG(
"Failed to unregister type, hash not present in map.");
221 if (RCUTILS_RET_OK !=
222 rcutils_hash_map_set(
223 &node->
impl->registered_types_by_type_hash,
224 type_hash, &type_info))
226 RCL_SET_ERROR_MSG(
"Failed to update type info");
230 if (RCUTILS_RET_OK !=
231 rcutils_hash_map_unset(
232 &node->
impl->registered_types_by_type_hash,
235 RCL_SET_ERROR_MSG(
"Failed to unregister type info");
239 type_description_interfaces__msg__TypeDescription__destroy(
241 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.