ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
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  std::function<void(void)> on_execute_callback = {}, const rclcpp::Context::SharedPtr & context =
47  rclcpp::contexts::get_global_default_context());
48 
49  // Destructor
50  RCLCPP_PUBLIC
51  ~ExecutorNotifyWaitable() override = default;
52 
53  RCLCPP_PUBLIC
55 
56  RCLCPP_PUBLIC
58 
60 
63  RCLCPP_PUBLIC
64  void
65  add_to_wait_set(rcl_wait_set_t & wait_set) override;
66 
68 
72  RCLCPP_PUBLIC
73  bool
74  is_ready(const rcl_wait_set_t & wait_set) override;
75 
77 
81  RCLCPP_PUBLIC
82  void
83  execute(const std::shared_ptr<void> & data) override;
84 
86 
89  RCLCPP_PUBLIC
90  std::shared_ptr<void>
91  take_data() override;
92 
94 
99  RCLCPP_PUBLIC
100  std::shared_ptr<void>
101  take_data_by_entity_id(size_t id) override;
102 
104 
108  RCLCPP_PUBLIC
109  void
110  set_on_ready_callback(std::function<void(size_t, int)> callback) override;
111 
113 
116  RCLCPP_PUBLIC
117  void
118  add_guard_condition(rclcpp::GuardCondition::WeakPtr guard_condition);
119 
121 
124  RCLCPP_PUBLIC
125  void
126  clear_on_ready_callback() override;
127 
129 
132  RCLCPP_PUBLIC
133  void
134  set_execute_callback(std::function<void(void)> on_execute_callback);
135 
137 
140  RCLCPP_PUBLIC
141  void
142  remove_guard_condition(rclcpp::GuardCondition::WeakPtr weak_guard_condition);
143 
145 
148  RCLCPP_PUBLIC
149  size_t
151 
153 
156  RCLCPP_PUBLIC
157  std::vector<std::shared_ptr<rclcpp::TimerBase>>
158  get_timers() const override;
159 
160 private:
162  std::function<void(void)> execute_callback_;
163 
165  std::mutex guard_condition_mutex_;
167  std::mutex execute_mutex_;
168 
169  std::function<void(size_t)> on_ready_callback_;
170 
172  std::set<rclcpp::GuardCondition::SharedPtr> notify_guard_conditions_;
173 
176  std::vector<size_t> idxs_of_added_guard_condition_;
177 
179  bool needs_processing = false;
180 
182  rclcpp::GuardCondition::SharedPtr guard_condition_;
183 };
184 
185 } // namespace executors
186 } // namespace rclcpp
187 
188 #endif // RCLCPP__EXECUTORS__EXECUTOR_NOTIFY_WAITABLE_HPP_
RCLCPP_PUBLIC ExecutorNotifyWaitable(std::function< void(void)> on_execute_callback={}, const rclcpp::Context::SharedPtr &context=rclcpp::contexts::get_global_default_context())
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 void add_guard_condition(rclcpp::GuardCondition::WeakPtr guard_condition)
Add a guard condition to be waited on.
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.
RCLCPP_PUBLIC void remove_guard_condition(rclcpp::GuardCondition::WeakPtr weak_guard_condition)
Remove a guard condition from being waited on.
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