ROS 2 rclcpp + rcl - lyrical  lyrical
ROS 2 C++ Client Library with ROS Client Library
executor_notify_waitable.hpp
1 // Copyright 2023 Open Source Robotics Foundation, Inc.
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 RCLCPP__EXECUTORS__EXECUTOR_NOTIFY_WAITABLE_HPP_
16 #define RCLCPP__EXECUTORS__EXECUTOR_NOTIFY_WAITABLE_HPP_
17 
18 #include <functional>
19 #include <memory>
20 #include <mutex>
21 #include <set>
22 #include <vector>
23 
24 #include "rclcpp/guard_condition.hpp"
25 #include "rclcpp/waitable.hpp"
26 
27 namespace rclcpp
28 {
29 namespace executors
30 {
31 
35 {
36 public:
37  RCLCPP_SMART_PTR_DEFINITIONS(ExecutorNotifyWaitable)
38 
39  // Constructor
44  RCLCPP_PUBLIC
45  explicit ExecutorNotifyWaitable(
46  const std::function<void(void)> & on_execute_callback = {},
47  const rclcpp::Context::SharedPtr & context =
48  rclcpp::contexts::get_global_default_context());
49 
50  // Destructor
51  RCLCPP_PUBLIC
52  ~ExecutorNotifyWaitable() override = default;
53 
54  RCLCPP_PUBLIC
56 
57  RCLCPP_PUBLIC
59 
61 
64  RCLCPP_PUBLIC
65  void
66  add_to_wait_set(rcl_wait_set_t & wait_set) override;
67 
69 
73  RCLCPP_PUBLIC
74  bool
75  is_ready(const rcl_wait_set_t & wait_set) override;
76 
78 
82  RCLCPP_PUBLIC
83  void
84  execute(const std::shared_ptr<void> & data) override;
85 
87 
90  RCLCPP_PUBLIC
91  std::shared_ptr<void>
92  take_data() override;
93 
95 
100  RCLCPP_PUBLIC
101  std::shared_ptr<void>
102  take_data_by_entity_id(size_t id) override;
103 
105 
109  RCLCPP_PUBLIC
110  void
111  set_on_ready_callback(std::function<void(size_t, int)> callback) override;
112 
114 
117  RCLCPP_PUBLIC
118  void
119  add_guard_condition(const rclcpp::GuardCondition::WeakPtr & guard_condition);
120 
122 
125  RCLCPP_PUBLIC
126  void
127  clear_on_ready_callback() override;
128 
130 
133  RCLCPP_PUBLIC
134  void
135  set_execute_callback(std::function<void(void)> on_execute_callback);
136 
138 
141  RCLCPP_PUBLIC
142  void
143  remove_guard_condition(const rclcpp::GuardCondition::WeakPtr & weak_guard_condition);
144 
146 
149  RCLCPP_PUBLIC
150  size_t
152 
154 
157  RCLCPP_PUBLIC
158  std::vector<std::shared_ptr<rclcpp::TimerBase>>
159  get_timers() const override;
160 
161 private:
163  std::function<void(void)> execute_callback_;
164 
166  std::mutex guard_condition_mutex_;
168  std::mutex execute_mutex_;
169 
170  std::function<void(size_t)> on_ready_callback_;
171 
173  std::set<rclcpp::GuardCondition::SharedPtr> notify_guard_conditions_;
174 
177  std::vector<size_t> idxs_of_added_guard_condition_;
178 
180  bool needs_processing = false;
181 
183  rclcpp::GuardCondition::SharedPtr guard_condition_;
184 };
185 
186 } // namespace executors
187 } // namespace rclcpp
188 
189 #endif // RCLCPP__EXECUTORS__EXECUTOR_NOTIFY_WAITABLE_HPP_
RCLCPP_PUBLIC ExecutorNotifyWaitable(const std::function< void(void)> &on_execute_callback={}, const rclcpp::Context::SharedPtr &context=rclcpp::contexts::get_global_default_context())
RCLCPP_PUBLIC void add_guard_condition(const rclcpp::GuardCondition::WeakPtr &guard_condition)
Add a guard condition to be waited on.
RCLCPP_PUBLIC void remove_guard_condition(const rclcpp::GuardCondition::WeakPtr &weak_guard_condition)
Remove a guard condition from being waited on.
RCLCPP_PUBLIC std::shared_ptr< void > take_data_by_entity_id(size_t id) override
Take the data from an entity ID so that it can be consumed with execute.
RCLCPP_PUBLIC void set_execute_callback(std::function< void(void)> on_execute_callback)
Set a new callback to be called whenever this waitable is executed.
RCLCPP_PUBLIC void execute(const std::shared_ptr< void > &data) override
Perform work associated with the waitable.
RCLCPP_PUBLIC size_t get_number_of_ready_guard_conditions() override
Get the number of ready guard_conditions.
RCLCPP_PUBLIC std::shared_ptr< void > take_data() override
Retrieve data to be used in the next execute call.
RCLCPP_PUBLIC void add_to_wait_set(rcl_wait_set_t &wait_set) override
Add conditions to the wait set.
RCLCPP_PUBLIC void set_on_ready_callback(std::function< void(size_t, int)> callback) override
Set a callback to be called whenever the waitable becomes ready.
RCLCPP_PUBLIC void clear_on_ready_callback() override
Unset any callback registered via set_on_ready_callback.
RCLCPP_PUBLIC bool is_ready(const rcl_wait_set_t &wait_set) override
Check conditions against the wait set.
RCLCPP_PUBLIC std::vector< std::shared_ptr< rclcpp::TimerBase > > get_timers() const override
Returns the number of used Timers.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Container for subscription's, guard condition's, etc to be waited on.
Definition: wait.h:42