ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
static_executor_entities_collector.hpp
1 // Copyright 2020 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__STATIC_EXECUTOR_ENTITIES_COLLECTOR_HPP_
16 #define RCLCPP__EXECUTORS__STATIC_EXECUTOR_ENTITIES_COLLECTOR_HPP_
17 
18 #include <chrono>
19 #include <list>
20 #include <map>
21 #include <memory>
22 #include <vector>
23 
24 #include "rcl/guard_condition.h"
25 #include "rcl/wait.h"
26 
27 #include "rclcpp/experimental/executable_list.hpp"
28 #include "rclcpp/macros.hpp"
29 #include "rclcpp/memory_strategy.hpp"
30 #include "rclcpp/visibility_control.hpp"
31 #include "rclcpp/waitable.hpp"
32 
33 namespace rclcpp
34 {
35 namespace executors
36 {
37 typedef std::map<rclcpp::CallbackGroup::WeakPtr,
38  rclcpp::node_interfaces::NodeBaseInterface::WeakPtr,
39  std::owner_less<rclcpp::CallbackGroup::WeakPtr>> WeakCallbackGroupsToNodesMap;
40 
42  : public rclcpp::Waitable,
43  public std::enable_shared_from_this<StaticExecutorEntitiesCollector>
44 {
45 public:
46  RCLCPP_SMART_PTR_DEFINITIONS(StaticExecutorEntitiesCollector)
47 
48  // Constructor
49  RCLCPP_PUBLIC
51 
52  // Destructor
53  RCLCPP_PUBLIC
55 
57 
62  RCLCPP_PUBLIC
63  void
64  init(
65  rcl_wait_set_t * p_wait_set,
66  rclcpp::memory_strategy::MemoryStrategy::SharedPtr memory_strategy);
67 
69  RCLCPP_PUBLIC
70  bool
71  is_init() {return initialized_;}
72 
73  RCLCPP_PUBLIC
74  void
75  fini();
76 
78  RCLCPP_PUBLIC
79  void
80  execute(std::shared_ptr<void> & data) override;
81 
83 
87  RCLCPP_PUBLIC
88  std::shared_ptr<void>
89  take_data() override;
90 
92 
97  RCLCPP_PUBLIC
98  void
99  refresh_wait_set(std::chrono::nanoseconds timeout = std::chrono::nanoseconds(-1));
100 
104  RCLCPP_PUBLIC
105  void
106  add_to_wait_set(rcl_wait_set_t * wait_set) override;
107 
108  RCLCPP_PUBLIC
109  size_t
111 
113 
116  RCLCPP_PUBLIC
117  bool
119  rclcpp::CallbackGroup::SharedPtr group_ptr,
120  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
121 
123 
127  RCLCPP_PUBLIC
128  bool
130  rclcpp::CallbackGroup::SharedPtr group_ptr,
131  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
132  WeakCallbackGroupsToNodesMap & weak_groups_to_nodes);
133 
135 
138  RCLCPP_PUBLIC
139  bool
141  rclcpp::CallbackGroup::SharedPtr group_ptr);
142 
144 
147  RCLCPP_PUBLIC
148  bool
150  rclcpp::CallbackGroup::SharedPtr group_ptr,
151  WeakCallbackGroupsToNodesMap & weak_groups_to_nodes);
152 
157  RCLCPP_PUBLIC
158  bool
159  add_node(
160  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
161 
166  RCLCPP_PUBLIC
167  bool
168  remove_node(
169  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
170 
171  RCLCPP_PUBLIC
172  std::vector<rclcpp::CallbackGroup::WeakPtr>
173  get_all_callback_groups();
174 
176 
179  RCLCPP_PUBLIC
180  std::vector<rclcpp::CallbackGroup::WeakPtr>
182 
184 
187  RCLCPP_PUBLIC
188  std::vector<rclcpp::CallbackGroup::WeakPtr>
190 
192 
197  RCLCPP_PUBLIC
198  bool
199  is_ready(rcl_wait_set_t * wait_set) override;
200 
202 
205  RCLCPP_PUBLIC
206  size_t
207  get_number_of_timers() {return exec_list_.number_of_timers;}
208 
210 
213  RCLCPP_PUBLIC
214  size_t
215  get_number_of_subscriptions() {return exec_list_.number_of_subscriptions;}
216 
218 
221  RCLCPP_PUBLIC
222  size_t
223  get_number_of_services() {return exec_list_.number_of_services;}
224 
226 
229  RCLCPP_PUBLIC
230  size_t
231  get_number_of_clients() {return exec_list_.number_of_clients;}
232 
234 
237  RCLCPP_PUBLIC
238  size_t
239  get_number_of_waitables() {return exec_list_.number_of_waitables;}
240 
246  RCLCPP_PUBLIC
247  rclcpp::SubscriptionBase::SharedPtr
248  get_subscription(size_t i) {return exec_list_.subscription[i];}
249 
255  RCLCPP_PUBLIC
256  rclcpp::TimerBase::SharedPtr
257  get_timer(size_t i) {return exec_list_.timer[i];}
258 
264  RCLCPP_PUBLIC
265  rclcpp::ServiceBase::SharedPtr
266  get_service(size_t i) {return exec_list_.service[i];}
267 
273  RCLCPP_PUBLIC
274  rclcpp::ClientBase::SharedPtr
275  get_client(size_t i) {return exec_list_.client[i];}
276 
282  RCLCPP_PUBLIC
283  rclcpp::Waitable::SharedPtr
284  get_waitable(size_t i) {return exec_list_.waitable[i];}
285 
286 private:
288 
291  void
292  prepare_wait_set();
293 
294  void
295  fill_executable_list();
296 
297  void
298  fill_memory_strategy();
299 
301 
306  bool
307  has_node(
308  const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
309  const WeakCallbackGroupsToNodesMap & weak_groups_to_nodes) const;
310 
314 
317  void
318  add_callback_groups_from_nodes_associated_to_executor();
319 
320  void
321  fill_executable_list_from_map(const WeakCallbackGroupsToNodesMap & weak_groups_to_nodes);
322 
324  rclcpp::memory_strategy::MemoryStrategy::SharedPtr memory_strategy_;
325 
326  // maps callback groups to nodes.
327  WeakCallbackGroupsToNodesMap weak_groups_associated_with_executor_to_nodes_;
328  // maps callback groups to nodes.
329  WeakCallbackGroupsToNodesMap weak_groups_to_nodes_associated_with_executor_;
330 
331  typedef std::map<rclcpp::node_interfaces::NodeBaseInterface::WeakPtr,
332  const rclcpp::GuardCondition *,
333  std::owner_less<rclcpp::node_interfaces::NodeBaseInterface::WeakPtr>>
334  WeakNodesToGuardConditionsMap;
335  WeakNodesToGuardConditionsMap weak_nodes_to_guard_conditions_;
336 
338  std::list<rclcpp::node_interfaces::NodeBaseInterface::WeakPtr> weak_nodes_;
339 
340  // Mutex to protect vector of new nodes.
341  std::mutex new_nodes_mutex_;
342  std::vector<rclcpp::node_interfaces::NodeBaseInterface::WeakPtr> new_nodes_;
343 
345  rcl_wait_set_t * p_wait_set_ = nullptr;
346 
349 
351  bool initialized_ = false;
352 };
353 
354 } // namespace executors
355 } // namespace rclcpp
356 
357 #endif // RCLCPP__EXECUTORS__STATIC_EXECUTOR_ENTITIES_COLLECTOR_HPP_
A condition that can be waited on in a single wait set and asynchronously triggered.
RCLCPP_PUBLIC std::vector< rclcpp::CallbackGroup::WeakPtr > get_automatically_added_callback_groups_from_nodes()
Get callback groups that belong to executor.
RCLCPP_PUBLIC std::vector< rclcpp::CallbackGroup::WeakPtr > get_manually_added_callback_groups()
Get callback groups that belong to executor.
RCLCPP_PUBLIC bool remove_callback_group_from_map(rclcpp::CallbackGroup::SharedPtr group_ptr, WeakCallbackGroupsToNodesMap &weak_groups_to_nodes)
Remove a callback group from the executor.
RCLCPP_PUBLIC std::shared_ptr< void > take_data() override
Take the data so that it can be consumed with execute.
RCLCPP_PUBLIC rclcpp::ClientBase::SharedPtr get_client(size_t i)
RCLCPP_PUBLIC void refresh_wait_set(std::chrono::nanoseconds timeout=std::chrono::nanoseconds(-1))
Function to add_handles_to_wait_set and wait for work and.
RCLCPP_PUBLIC size_t get_number_of_clients()
Return number of clients.
RCLCPP_PUBLIC rclcpp::Waitable::SharedPtr get_waitable(size_t i)
RCLCPP_PUBLIC size_t get_number_of_subscriptions()
Return number of subscriptions.
RCLCPP_PUBLIC size_t get_number_of_ready_guard_conditions() override
Get the number of ready guard_conditions.
RCLCPP_PUBLIC bool add_callback_group(rclcpp::CallbackGroup::SharedPtr group_ptr, rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr)
Add a callback group to an executor.
RCLCPP_PUBLIC rclcpp::ServiceBase::SharedPtr get_service(size_t i)
RCLCPP_PUBLIC bool is_ready(rcl_wait_set_t *wait_set) override
Complete all available queued work without blocking.
RCLCPP_PUBLIC size_t get_number_of_timers()
Return number of timers.
RCLCPP_PUBLIC bool remove_callback_group(rclcpp::CallbackGroup::SharedPtr group_ptr)
Remove a callback group from the executor.
RCLCPP_PUBLIC size_t get_number_of_waitables()
Return number of waitables.
RCLCPP_PUBLIC size_t get_number_of_services()
Return number of services.
RCLCPP_PUBLIC bool is_init()
Finalize StaticExecutorEntitiesCollector to clear resources.
RCLCPP_PUBLIC void add_to_wait_set(rcl_wait_set_t *wait_set) override
RCLCPP_PUBLIC rclcpp::TimerBase::SharedPtr get_timer(size_t i)
RCLCPP_PUBLIC void init(rcl_wait_set_t *p_wait_set, rclcpp::memory_strategy::MemoryStrategy::SharedPtr memory_strategy)
Initialize StaticExecutorEntitiesCollector.
RCLCPP_PUBLIC bool add_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr)
RCLCPP_PUBLIC void execute(std::shared_ptr< void > &data) override
Execute the waitable.
RCLCPP_PUBLIC bool remove_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr)
RCLCPP_PUBLIC rclcpp::SubscriptionBase::SharedPtr get_subscription(size_t i)
This class contains subscriptionbase, timerbase, etc. which can be used to run callbacks.
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