ROS 2 rclcpp + rcl - lyrical  lyrical
ROS 2 C++ Client Library with ROS Client Library
ready_entity.hpp
1 // Copyright 2024 Cellumation GmbH.
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 #pragma once
16 
17 #include <utility>
18 
19 #include "scheduler.hpp"
20 #include "global_event_id_provider.hpp"
21 #include "rclcpp/executors/events_cbg_executor/events_cbg_executor.hpp"
22 
23 namespace rclcpp
24 {
25 namespace executors
26 {
27 namespace cbg_executor
28 {
30 {
32  {
33  rclcpp::TimerBase::WeakPtr timer_ptr;
34  // must be called by the after executing the timer callback
35  std::function<void()> timer_was_executed;
36 
37  bool expired() const
38  {
39  return timer_ptr.expired();
40  }
41  };
42 
43  std::variant<rclcpp::SubscriptionBase::WeakPtr, ReadyTimerWithExecutedCallback,
44  rclcpp::ServiceBase::WeakPtr, rclcpp::ClientBase::WeakPtr, CBGScheduler::WaitableWithEventType,
46 
47  explicit ReadyEntity(const rclcpp::SubscriptionBase::WeakPtr ptr)
48  : entity(ptr), id(GlobalEventIdProvider::get_next_id()) {}
49  explicit ReadyEntity(const ReadyTimerWithExecutedCallback & timer)
50  : entity(timer), id(GlobalEventIdProvider::get_next_id()) {}
51  explicit ReadyEntity(const rclcpp::ServiceBase::WeakPtr ptr)
52  : entity(ptr), id(GlobalEventIdProvider::get_next_id()) {}
53  explicit ReadyEntity(const rclcpp::ClientBase::WeakPtr ptr)
54  : entity(ptr), id(GlobalEventIdProvider::get_next_id()) {}
55  explicit ReadyEntity(const CBGScheduler::WaitableWithEventType & ev)
56  : entity(ev), id(GlobalEventIdProvider::get_next_id()) {}
57  explicit ReadyEntity(const CBGScheduler::CallbackEventType & ev)
58  : entity(ev), id(GlobalEventIdProvider::get_next_id()) {}
59 
60  std::function<void()> get_execute_function() const
61  {
62  return std::visit([](auto && entity) -> std::function<void()> {
63  using T = std::decay_t<decltype(entity)>;
64  if constexpr (std::is_same_v<T, rclcpp::SubscriptionBase::WeakPtr>) {
65  rclcpp::SubscriptionBase::SharedPtr shr_ptr = entity.lock();
66  if (!shr_ptr) {
67  return std::function<void()>();
68  }
69  return [shr_ptr = std::move(shr_ptr)]() {
71  };
72  } else if constexpr (std::is_same_v<T, ReadyTimerWithExecutedCallback>) {
73  auto shr_ptr = entity.timer_ptr.lock();
74  if (!shr_ptr) {
75  return std::function<void()>();
76  }
77 
78  return [shr_ptr = std::move(shr_ptr),
79  timer_executed_cb = entity.timer_was_executed]() {
80  auto data = shr_ptr->call();
81  if (!data) {
82  // timer was cancelled, skip it.
83  return;
84  }
85 
87 
88  // readd the timer to the timers manager
89  timer_executed_cb();
90  };
91  } else if constexpr (std::is_same_v<T, rclcpp::ServiceBase::WeakPtr>) {
92  auto shr_ptr = entity.lock();
93  if (!shr_ptr) {
94  return std::function<void()>();
95  }
96  return [shr_ptr = std::move(shr_ptr)]() {
98  };
99  } else if constexpr (std::is_same_v<T, rclcpp::ClientBase::WeakPtr>) {
100  auto shr_ptr = entity.lock();
101  if (!shr_ptr) {
102  return std::function<void()>();
103  }
104  return [shr_ptr = std::move(shr_ptr)]() {
106  };
107  } else if constexpr (std::is_same_v<T, CBGScheduler::WaitableWithEventType>) {
108  auto shr_ptr_in = entity.waitable.lock();
109  if (!shr_ptr_in) {
110  return std::function<void()>();
111  }
112 
113  return [shr_ptr = std::move(shr_ptr_in),
114  event_type = entity.internal_event_type]() {
115  auto data = shr_ptr->take_data_by_entity_id(event_type);
116  shr_ptr->execute(data);
117  };
118  } else if constexpr (std::is_same_v<T, CBGScheduler::CallbackEventType>) {
119  return entity.callback;
120  }
121  }, entity);
122  }
123 
124  GlobalEventIdProvider::MonotonicId id;
125 
129  bool expired() const
130  {
131  return std::visit([](const auto & entity) {return entity.expired();}, entity);
132  }
133 };
134 } // namespace cbg_executor
135 } // namespace executors
136 } // namespace rclcpp
static RCLCPP_PUBLIC void execute_timer(const rclcpp::TimerBase::SharedPtr &timer, const std::shared_ptr< void > &data_ptr)
Run timer executable.
Definition: executor.cpp:658
static RCLCPP_PUBLIC void execute_client(const rclcpp::ClientBase::SharedPtr &client)
Run service client executable.
Definition: executor.cpp:678
static RCLCPP_PUBLIC void execute_service(const rclcpp::ServiceBase::SharedPtr &service)
Run service server executable.
Definition: executor.cpp:666
static RCLCPP_PUBLIC void execute_subscription(const rclcpp::SubscriptionBase::SharedPtr &subscription)
Run subscription executable.
Definition: executor.cpp:555
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.