scenario_simulator_v2 C++ API
subscriber_wrapper.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_WRAPPER_HPP_
16 #define CONCEALER__SUBSCRIBER_WRAPPER_HPP_
17 
18 #include <memory>
19 #include <rclcpp/rclcpp.hpp>
20 
21 namespace concealer
22 {
23 template <typename Message>
25 {
26  typename Message::ConstSharedPtr current_value = std::make_shared<const Message>();
27 
28  typename rclcpp::Subscription<Message>::SharedPtr subscription;
29 
30 public:
31  auto operator()() const -> const auto & { return *std::atomic_load(&current_value); }
32 
33  template <typename Autoware, typename Callback>
35  const std::string & topic, const rclcpp::QoS & quality_of_service, Autoware & autoware,
36  const Callback & callback)
37  : subscription(autoware.template create_subscription<Message>(
38  topic, quality_of_service,
39  [this, callback](const typename Message::ConstSharedPtr & message) {
40  if (std::atomic_store(&current_value, message); current_value) {
41  callback(*std::atomic_load(&current_value));
42  }
43  }))
44  {
45  }
46 
47  template <typename Autoware>
49  const std::string & topic, const rclcpp::QoS & quality_of_service, Autoware & autoware)
50  : subscription(autoware.template create_subscription<Message>(
51  topic, quality_of_service, [this](const typename Message::ConstSharedPtr & message) {
52  std::atomic_store(&current_value, message);
53  }))
54  {
55  }
56 };
57 } // namespace concealer
58 
59 #endif // CONCEALER__SUBSCRIBER_WRAPPER_HPP_
Definition: subscriber_wrapper.hpp:25
auto operator()() const -> const auto &
Definition: subscriber_wrapper.hpp:31
SubscriberWrapper(const std::string &topic, const rclcpp::QoS &quality_of_service, Autoware &autoware, const Callback &callback)
Definition: subscriber_wrapper.hpp:34
SubscriberWrapper(const std::string &topic, const rclcpp::QoS &quality_of_service, Autoware &autoware)
Definition: subscriber_wrapper.hpp:48
Definition: autoware_universe.hpp:40
std::string string
Definition: junit5.hpp:26