|
| 1 | +// Copyright 2025 PAL Robotics S.L. |
| 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 HARDWARE_INTERFACE__HELPERS_HPP_ |
| 16 | +#define HARDWARE_INTERFACE__HELPERS_HPP_ |
| 17 | + |
| 18 | +#include <algorithm> |
| 19 | +#include <functional> |
| 20 | +#include <map> |
| 21 | +#include <string> |
| 22 | +#include <unordered_map> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +namespace ros2_control |
| 26 | +{ |
| 27 | + |
| 28 | +/** |
| 29 | + * @brief Get the iterator to the item in the container. |
| 30 | + * @param container The container to search in. |
| 31 | + * @param item The item to search for. |
| 32 | + * @return Iterator to the item in the container. |
| 33 | + * |
| 34 | + * @note Only std::vector, std::map and std::unordered_map are supported. |
| 35 | + */ |
| 36 | +template <typename Container, typename T> |
| 37 | +[[nodiscard]] auto get_item_iterator(const Container & container, const T & item) |
| 38 | +{ |
| 39 | + if constexpr (std::is_same_v<Container, std::vector<T>>) |
| 40 | + { |
| 41 | + return std::find(container.begin(), container.end(), item); |
| 42 | + } |
| 43 | + else if constexpr ( |
| 44 | + std::is_same_v<Container, std::map<T, typename Container::mapped_type>> || |
| 45 | + std::is_same_v<Container, std::unordered_map<T, typename Container::mapped_type>>) |
| 46 | + { |
| 47 | + return container.find(item); |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + using is_vector = std::is_same<Container, std::vector<T>>; |
| 52 | + using is_map = std::is_same<Container, std::map<T, typename Container::mapped_type>>; |
| 53 | + using is_unordered_map = |
| 54 | + std::is_same<Container, std::unordered_map<T, typename Container::mapped_type>>; |
| 55 | + // Handle unsupported container types |
| 56 | + static_assert( |
| 57 | + is_vector::value || is_map::value || is_unordered_map::value, |
| 58 | + "Only std::vector, std::map and std::unordered_map are supported."); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * @brief Check if the item is in the container. |
| 64 | + * @param container The container to search in. |
| 65 | + * @param item The item to search for. |
| 66 | + * @return True if the item is in the container, false otherwise. |
| 67 | + */ |
| 68 | +template <typename Container, typename T> |
| 69 | +[[nodiscard]] bool has_item(const Container & container, const T & item) |
| 70 | +{ |
| 71 | + return get_item_iterator(container, item) != container.end(); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * @brief Add the item to the container if it is not already in it. |
| 76 | + * @param vector The container to add the item to. |
| 77 | + * @param item The item to add. |
| 78 | + */ |
| 79 | +template <typename T> |
| 80 | +void add_item(std::vector<T> & vector, const T & item) |
| 81 | +{ |
| 82 | + if (!has_item(vector, item)) |
| 83 | + { |
| 84 | + vector.push_back(item); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * @brief Remove the item from the container if it is in it. |
| 90 | + * @param container The container to remove the item from. |
| 91 | + * @param item The item to remove. |
| 92 | + * @return True if the item was removed, false otherwise. If the item was not in the container, it |
| 93 | + * returns false. |
| 94 | + */ |
| 95 | +template <typename Container> |
| 96 | +[[nodiscard]] bool remove_item(Container & container, typename Container::const_reference item) |
| 97 | +{ |
| 98 | + auto it = get_item_iterator(container, item); |
| 99 | + if (it != container.end()) |
| 100 | + { |
| 101 | + container.erase(it); |
| 102 | + return true; |
| 103 | + } |
| 104 | + return false; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * @brief Check if the container has any of the items. |
| 109 | + * @param container The container to search in. |
| 110 | + * @param items The items to search for. |
| 111 | + * @return True if the container has any of the items, false otherwise. |
| 112 | + */ |
| 113 | +template <typename Container> |
| 114 | +[[nodiscard]] bool has_any_item( |
| 115 | + const Container & container, const std::vector<typename Container::key_type> & items) |
| 116 | +{ |
| 117 | + return std::any_of( |
| 118 | + items.begin(), items.end(), |
| 119 | + [&container](const typename Container::key_type & item) { return has_item(container, item); }); |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * @brief Check if the container has any of the items. |
| 124 | + * @param container The container to search in. |
| 125 | + * @param items The items to search for. |
| 126 | + * @return True if the container has any of the items, false otherwise. |
| 127 | + */ |
| 128 | +template <typename T> |
| 129 | +[[nodiscard]] bool has_any_item(const std::vector<T> & container, const std::vector<T> & items) |
| 130 | +{ |
| 131 | + return std::any_of( |
| 132 | + items.begin(), items.end(), [&container](const T & item) { return has_item(container, item); }); |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * @brief Check if the container has all of the items. |
| 137 | + * @param container The container to search in. |
| 138 | + * @param items The items to search for. |
| 139 | + * @return True if the container has all of the items, false otherwise. |
| 140 | + */ |
| 141 | +template <typename Container> |
| 142 | +[[nodiscard]] bool has_all_items( |
| 143 | + const Container & container, const std::vector<typename Container::key_type> & items) |
| 144 | +{ |
| 145 | + return std::all_of( |
| 146 | + items.begin(), items.end(), |
| 147 | + [&container](const typename Container::key_type & item) { return has_item(container, item); }); |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * @brief Check if the container has all of the items. |
| 152 | + * @param container The container to search in. |
| 153 | + * @param items The items to search for. |
| 154 | + * @return True if the container has all of the items, false otherwise. |
| 155 | + */ |
| 156 | +template <typename T> |
| 157 | +[[nodiscard]] bool has_all_items(const std::vector<T> & container, const std::vector<T> & items) |
| 158 | +{ |
| 159 | + return std::all_of( |
| 160 | + items.begin(), items.end(), [&container](const T & item) { return has_item(container, item); }); |
| 161 | +} |
| 162 | + |
| 163 | +/** |
| 164 | + * @brief Check if the container has all unique items. |
| 165 | + * @param container The container to search in. |
| 166 | + * @return True if the container has all unique items, false otherwise. |
| 167 | + * |
| 168 | + * @note The container must be sortable. |
| 169 | + * @note The container must have the begin() and end() methods. |
| 170 | + */ |
| 171 | +template <typename Container> |
| 172 | +[[nodiscard]] bool is_unique(Container container) |
| 173 | +{ |
| 174 | + std::sort(container.begin(), container.end()); |
| 175 | + return std::adjacent_find(container.cbegin(), container.cend()) == container.cend(); |
| 176 | +} |
| 177 | + |
| 178 | +} // namespace ros2_control |
| 179 | + |
| 180 | +#endif // HARDWARE_INTERFACE__HELPERS_HPP_ |
0 commit comments