scenario_simulator_v2 C++ API
subscriber.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
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 CONCEALER__SUBSCRIBER_HPP_
16 #define CONCEALER__SUBSCRIBER_HPP_
17 
18 #include <concealer/convert.hpp>
20 #include <memory>
21 #include <rclcpp/rclcpp.hpp>
23 
24 namespace concealer
25 {
26 template <typename...>
27 struct Subscriber;
28 
29 template <typename Message>
31 {
32  typename Message::ConstSharedPtr current_value = std::make_shared<const Message>();
33 
34  typename rclcpp::Subscription<Message>::SharedPtr subscription;
35 
36  auto operator()() const -> Message { return *std::atomic_load(&current_value); }
37 
38  template <typename Autoware, typename Callback>
39  explicit Subscriber(
40  const std::string & topic, const rclcpp::QoS & quality_of_service, Autoware & autoware,
41  const Callback & callback)
42  : subscription(
43  available<Message>() ? autoware.template create_subscription<Message>(
44  topic, quality_of_service,
45  [this, callback](const typename Message::ConstSharedPtr & message) {
46  if (std::atomic_store(&current_value, message); current_value) {
47  callback((*this)());
48  }
49  })
50  : nullptr)
51  {
52  }
53 
54  template <typename Autoware>
55  explicit Subscriber(
56  const std::string & topic, const rclcpp::QoS & quality_of_service, Autoware & autoware)
57  : subscription(
58  available<Message>() ? autoware.template create_subscription<Message>(
59  topic, quality_of_service,
60  [this](const typename Message::ConstSharedPtr & message) {
61  std::atomic_store(&current_value, message);
62  })
63  : nullptr)
64  {
65  }
66 };
67 
68 template <typename Message, typename T, typename... Ts>
69 struct Subscriber<Message, T, Ts...> : public Subscriber<T, Ts...>
70 {
71  typename Message::ConstSharedPtr current_value = nullptr;
72 
73  typename rclcpp::Subscription<Message>::SharedPtr subscription;
74 
75  auto operator()() const -> Message
76  {
77  if (auto value = std::atomic_load(&current_value)) {
78  return *value;
79  } else {
80  return convert<Message>(Subscriber<T, Ts...>::operator()());
81  }
82  }
83 
84  template <typename Autoware, typename Callback>
85  explicit Subscriber(
86  const std::string & topic, const rclcpp::QoS & quality_of_service, Autoware & autoware,
87  const Callback & callback)
88  : Subscriber<T, Ts...>(topic, quality_of_service, autoware, callback),
89  subscription(
90  available<Message>() ? autoware.template create_subscription<Message>(
91  topic, quality_of_service,
92  [this, callback](const typename Message::ConstSharedPtr & message) {
93  if (std::atomic_store(&current_value, message); current_value) {
94  callback((*this)());
95  }
96  })
97  : nullptr)
98  {
99  }
100 
101  template <typename Autoware>
102  explicit Subscriber(
103  const std::string & topic, const rclcpp::QoS & quality_of_service, Autoware & autoware)
104  : Subscriber<T, Ts...>(topic, quality_of_service, autoware),
105  subscription(
106  available<Message>() ? autoware.template create_subscription<Message>(
107  topic, quality_of_service,
108  [this](const typename Message::ConstSharedPtr & message) {
109  std::atomic_store(&current_value, message);
110  })
111  : nullptr)
112  {
113  }
114 };
115 
116 template <typename... Messages>
117 struct Subscriber<std::tuple<Messages...>> : public Subscriber<Messages...>
118 {
119  using type = std::tuple<Messages...>;
120 
121  using primary_type = std::tuple_element_t<0, type>;
122 
123  template <typename F, typename T, typename... Ts>
124  constexpr auto any(F f, const Subscriber<T, Ts...> & x) -> bool
125  {
126  if constexpr (0 < sizeof...(Ts)) {
127  return f(x) or any(f, static_cast<const Subscriber<Ts...> &>(x));
128  } else {
129  return f(x);
130  }
131  }
132 
133  template <typename... Ts>
134  explicit Subscriber(const std::string & topic, Ts &&... xs)
135  : Subscriber<Messages...>(topic, std::forward<decltype(xs)>(xs)...)
136  {
137  auto subscription_available = [](const auto & x) { return static_cast<bool>(x.subscription); };
138 
139  if (not any(subscription_available, static_cast<const Subscriber<Messages...> &>(*this))) {
141  "No viable subscription for topic ", std::quoted(topic), " in ",
142  getParameter<std::string>("architecture_type"), ".");
143  }
144  }
145 };
146 } // namespace concealer
147 
148 #endif // CONCEALER__SUBSCRIBER_HPP_
Definition: autoware_universe.hpp:40
constexpr auto available() -> bool
Definition: available.hpp:21
Definition: lanelet_wrapper.hpp:39
auto any(F f, T &&x, Ts &&... xs)
Definition: follow_trajectory.cpp:36
Definition: junit5.hpp:25
std::string string
Definition: junit5.hpp:26
Subscriber(const std::string &topic, const rclcpp::QoS &quality_of_service, Autoware &autoware, const Callback &callback)
Definition: subscriber.hpp:85
auto operator()() const -> Message
Definition: subscriber.hpp:75
rclcpp::Subscription< Message >::SharedPtr subscription
Definition: subscriber.hpp:73
Subscriber(const std::string &topic, const rclcpp::QoS &quality_of_service, Autoware &autoware)
Definition: subscriber.hpp:102
Definition: subscriber.hpp:31
auto operator()() const -> Message
Definition: subscriber.hpp:36
Subscriber(const std::string &topic, const rclcpp::QoS &quality_of_service, Autoware &autoware, const Callback &callback)
Definition: subscriber.hpp:39
rclcpp::Subscription< Message >::SharedPtr subscription
Definition: subscriber.hpp:34
Subscriber(const std::string &topic, const rclcpp::QoS &quality_of_service, Autoware &autoware)
Definition: subscriber.hpp:55
std::tuple< Messages... > type
Definition: subscriber.hpp:119
std::tuple_element_t< 0, type > primary_type
Definition: subscriber.hpp:121
Subscriber(const std::string &topic, Ts &&... xs)
Definition: subscriber.hpp:134
constexpr auto any(F f, const Subscriber< T, Ts... > &x) -> bool
Definition: subscriber.hpp:124
Definition: subscriber.hpp:27