ros2_control - jazzy
Loading...
Searching...
No Matches
helpers.hpp
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
25namespace ros2_control
26{
27
36template <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
68template <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
79template <typename T>
80void 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
95template <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
113template <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
128template <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
141template <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
156template <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
171template <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_