ros2_control - humble
Loading...
Searching...
No Matches
accessor.hpp
1// Copyright 2020 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 TRANSMISSION_INTERFACE__ACCESSOR_HPP_
16#define TRANSMISSION_INTERFACE__ACCESSOR_HPP_
17
18#include <algorithm>
19#include <set>
20#include <string>
21#include <vector>
22
24{
25template <typename T>
26std::string to_string(const std::vector<T> & list)
27{
28 std::stringstream ss;
29 ss << "[";
30 for (const auto & elem : list)
31 {
32 ss << elem << ", ";
33 }
34
35 if (!list.empty())
36 {
37 ss.seekp(-2, std::ios_base::end); // remove last ", "
38 }
39 ss << "]";
40 return ss.str();
41}
42
43template <class T>
44std::vector<std::string> get_names(const std::vector<T> & handles)
45{
46 std::set<std::string> names;
47 std::transform(
48 handles.cbegin(), handles.cend(), std::inserter(names, names.end()),
49 [](const auto & handle) { return handle.get_prefix_name(); });
50 return std::vector<std::string>(names.begin(), names.end());
51}
52
53template <typename T>
54std::vector<T> get_ordered_handles(
55 const std::vector<T> & unordered_handles, const std::vector<std::string> & names,
56 const std::string & interface_type)
57{
58 std::vector<T> result;
59 for (const auto & name : names)
60 {
61 std::copy_if(
62 unordered_handles.cbegin(), unordered_handles.cend(), std::back_inserter(result),
63 [&](const auto & handle)
64 {
65 return (handle.get_prefix_name() == name) &&
66 (handle.get_interface_name() == interface_type) && handle;
67 });
68 }
69 return result;
70}
71
72} // namespace transmission_interface
73
74#endif // TRANSMISSION_INTERFACE__ACCESSOR_HPP_
Definition accessor.hpp:24