|
| 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 | +#ifndef RCL__NODE_TYPE_CACHE_H_ |
| 16 | +#define RCL__NODE_TYPE_CACHE_H_ |
| 17 | + |
| 18 | +#include "rcl/node.h" |
| 19 | +#include "rcl/types.h" |
| 20 | +#include "rcl/visibility_control.h" |
| 21 | +#include "rosidl_runtime_c/type_description/type_description__struct.h" |
| 22 | +#include "rosidl_runtime_c/type_description/type_source__struct.h" |
| 23 | +#include "type_description_interfaces/msg/type_description.h" |
| 24 | +#include "type_description_interfaces/msg/type_source.h" |
| 25 | + |
| 26 | +#ifdef __cplusplus |
| 27 | +extern "C" { |
| 28 | +#endif |
| 29 | + |
| 30 | +typedef struct rcl_type_info_t |
| 31 | +{ |
| 32 | + type_description_interfaces__msg__TypeDescription * type_description; |
| 33 | + type_description_interfaces__msg__TypeSource__Sequence * type_sources; |
| 34 | +} rcl_type_info_t; |
| 35 | + |
| 36 | +/// Initialize the node's type cache. |
| 37 | +/** |
| 38 | + * This function initializes hash map of the node's type cache such that types |
| 39 | + * can be registered and retrieved. |
| 40 | + * Note that to correctly capture all types used by a node, this needs to be called |
| 41 | + * before any "builtin" publishers or services are created. |
| 42 | + * |
| 43 | + * <hr> |
| 44 | + * Attribute | Adherence |
| 45 | + * ------------------ | ------------- |
| 46 | + * Allocates Memory | Yes |
| 47 | + * Thread-Safe | No |
| 48 | + * Uses Atomics | No |
| 49 | + * Lock-Free | Yes |
| 50 | + * |
| 51 | + * \param[in] node the handle to the node whose type cache should be initialized |
| 52 | + * \return #RCL_RET_OK if the node's type cache was successfully initialized, or |
| 53 | + * \return #RCL_RET_INVALID_ARGUMENT if any arguments are invalid, or |
| 54 | + * \return #RCL_RET_NODE_INVALID if the given `node` is invalid, or |
| 55 | + * \return #RCL_RET_ERROR if an unspecified error occurs. |
| 56 | + */ |
| 57 | +RCL_WARN_UNUSED |
| 58 | +rcl_ret_t rcl_node_type_cache_init(rcl_node_t * node); |
| 59 | + |
| 60 | +/// Finalize the node's type cache. |
| 61 | +/** |
| 62 | + * This function clears the hash map of the node's type cache and deallocates |
| 63 | + * used memory. |
| 64 | + * |
| 65 | + * <hr> |
| 66 | + * Attribute | Adherence |
| 67 | + * ------------------ | ------------- |
| 68 | + * Allocates Memory | No |
| 69 | + * Thread-Safe | No |
| 70 | + * Uses Atomics | No |
| 71 | + * Lock-Free | Yes |
| 72 | + * |
| 73 | + * \param[in] node the handle to the node whose type cache should be finalized |
| 74 | + * \return #RCL_RET_OK if the node's type cache was successfully finalized, or |
| 75 | + * \return #RCL_RET_INVALID_ARGUMENT if any arguments are invalid, or |
| 76 | + * \return #RCL_RET_NODE_INVALID if the given `node` is invalid, or |
| 77 | + * \return #RCL_RET_ERROR if an unspecified error occurs. |
| 78 | + */ |
| 79 | +RCL_WARN_UNUSED |
| 80 | +rcl_ret_t rcl_node_type_cache_fini(rcl_node_t * node); |
| 81 | + |
| 82 | +/// Register a type with the node's type cache. |
| 83 | +/** |
| 84 | + * This function registers the given type, uniquely identified by the type_hash, |
| 85 | + * with the node with the node's type cache. Multiple registrations of the same |
| 86 | + * type will increment its registration count. |
| 87 | + * |
| 88 | + * <hr> |
| 89 | + * Attribute | Adherence |
| 90 | + * ------------------ | ------------- |
| 91 | + * Allocates Memory | Yes |
| 92 | + * Thread-Safe | No |
| 93 | + * Uses Atomics | No |
| 94 | + * Lock-Free | Yes |
| 95 | + * |
| 96 | + * \param[in] node the handle to the node whose type cache should be finalized |
| 97 | + * \param[in] type_hash hash of the type |
| 98 | + * \param[in] type_description type description struct |
| 99 | + * \param[in] type_description_sources type description sources struct |
| 100 | + * \return #RCL_RET_OK if the type was successfully registered, or |
| 101 | + * \return #RCL_RET_INVALID_ARGUMENT if any arguments are invalid, or |
| 102 | + * \return #RCL_RET_NODE_INVALID if the given `node` is invalid, or |
| 103 | + * \return #RCL_RET_ERROR if an unspecified error occurs. |
| 104 | + */ |
| 105 | +RCL_PUBLIC |
| 106 | +RCL_WARN_UNUSED |
| 107 | +rcl_ret_t rcl_node_type_cache_register_type( |
| 108 | + const rcl_node_t * node, const rosidl_type_hash_t * type_hash, |
| 109 | + const rosidl_runtime_c__type_description__TypeDescription * type_description, |
| 110 | + const rosidl_runtime_c__type_description__TypeSource__Sequence * type_description_sources |
| 111 | +); |
| 112 | + |
| 113 | +/// Unregister a message type from the node's type cache. |
| 114 | +/** |
| 115 | + * This function uses the given `type_hash` to unregister the associated type in |
| 116 | + * the node's type cache. If the type has been registered multiple times, the |
| 117 | + * type will only be removed if its registration count reaches 0. |
| 118 | + * |
| 119 | + * <hr> |
| 120 | + * Attribute | Adherence |
| 121 | + * ------------------ | ------------- |
| 122 | + * Allocates Memory | Yes |
| 123 | + * Thread-Safe | No |
| 124 | + * Uses Atomics | No |
| 125 | + * Lock-Free | Yes |
| 126 | + * |
| 127 | + * \param[in] node the handle to the node whose type cache should be finalized |
| 128 | + * \param[in] type_hash type hash |
| 129 | + * \return #RCL_RET_OK if the type was successfully registered, or |
| 130 | + * \return #RCL_RET_INVALID_ARGUMENT if any arguments are invalid, or |
| 131 | + * \return #RCL_RET_NODE_INVALID if the given `node` is invalid, or |
| 132 | + * \return #RCL_RET_ERROR if an unspecified error occurs. |
| 133 | + */ |
| 134 | +RCL_PUBLIC |
| 135 | +RCL_WARN_UNUSED |
| 136 | +rcl_ret_t rcl_node_type_cache_unregister_type( |
| 137 | + const rcl_node_t * node, const rosidl_type_hash_t * type_hash); |
| 138 | + |
| 139 | +/// Retrieve type information from the node's type cache. |
| 140 | +/** |
| 141 | + * This function returns the desired type information from the node's type cache |
| 142 | + * |
| 143 | + * The `type_info` field must point to an allocated `rcl_type_info_t` object to |
| 144 | + * which the type information will be written. |
| 145 | + * |
| 146 | + * <hr> |
| 147 | + * Attribute | Adherence |
| 148 | + * ------------------ | ------------- |
| 149 | + * Allocates Memory | No |
| 150 | + * Thread-Safe | No |
| 151 | + * Uses Atomics | No |
| 152 | + * Lock-Free | Yes |
| 153 | + * |
| 154 | + * \param[in] node the handle to the node whose type cache should be queried |
| 155 | + * \param[in] type_hash type hash |
| 156 | + * \param[out] type_info pointer to the type info struct that will be populated |
| 157 | + * \return #RCL_RET_OK if type information was retrieved successfully |
| 158 | + * \return #RCL_RET_INVALID_ARGUMENT if any arguments are invalid, or |
| 159 | + * \return #RCL_RET_NODE_INVALID if the given `node` is invalid, or |
| 160 | + * \return #RCL_RET_NOT_INIT if node's type cache has not been initialized, or |
| 161 | + * \return #RCL_RET_ERROR if an unspecified error occurs. |
| 162 | + */ |
| 163 | +RCL_PUBLIC |
| 164 | +RCL_WARN_UNUSED |
| 165 | +rcl_ret_t rcl_node_type_cache_get_type_info( |
| 166 | + const rcl_node_t * node, |
| 167 | + const rosidl_type_hash_t * type_hash, |
| 168 | + rcl_type_info_t * type_info); |
| 169 | + |
| 170 | +#ifdef __cplusplus |
| 171 | +} |
| 172 | +#endif |
| 173 | + |
| 174 | +#endif // RCL__NODE_TYPE_CACHE_H_ |
0 commit comments