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