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