ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
ROS 2 C++ Client Library with ROS Client Library
executor_entities_collection.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_ENTITIES_COLLECTION_HPP_
16 #define RCLCPP__EXECUTORS__EXECUTOR_ENTITIES_COLLECTION_HPP_
17 
18 #include <deque>
19 #include <functional>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include <rclcpp/any_executable.hpp>
24 #include <rclcpp/node_interfaces/node_base.hpp>
25 #include <rclcpp/callback_group.hpp>
26 #include <rclcpp/executors/executor_notify_waitable.hpp>
27 #include <rclcpp/visibility_control.hpp>
28 #include <rclcpp/wait_result.hpp>
29 #include <rclcpp/wait_set.hpp>
30 
31 namespace rclcpp
32 {
33 namespace executors
34 {
35 
37 template<typename EntityValueType>
39 {
41  using EntityWeakPtr = typename EntityValueType::WeakPtr;
43  using EntitySharedPtr = typename EntityValueType::SharedPtr;
44 
47 
49  rclcpp::CallbackGroup::WeakPtr callback_group;
50 };
51 
53 /*
54  * Iterates update_from and update_to to see which entities have been added/removed between
55  * the two collections.
56  *
57  * For each new entry (in update_from, but not in update_to),
58  * add the entity and fire the on_added callback
59  * For each removed entry (in update_to, but not in update_from),
60  * remove the entity and fire the on_removed callback.
61  *
62  * \param[in] update_from The collection representing the next iteration's state
63  * \param[inout] update_to The collection representing the current iteration's state
64  * \param[in] on_added Callback fired when a new entity is detected
65  * \param[in] on_removed Callback fired when an entity is removed
66  */
67 template<typename CollectionType>
68 void update_entities(
69  const CollectionType & update_from,
70  CollectionType & update_to,
71  std::function<void(const typename CollectionType::EntitySharedPtr &)> on_added,
72  std::function<void(const typename CollectionType::EntitySharedPtr &)> on_removed
73 )
74 {
75  for (auto it = update_to.begin(); it != update_to.end(); ) {
76  if (update_from.count(it->first) == 0) {
77  auto entity = it->second.entity.lock();
78  if (entity) {
79  on_removed(entity);
80  }
81  it = update_to.erase(it);
82  } else {
83  ++it;
84  }
85  }
86  for (auto it = update_from.begin(); it != update_from.end(); ++it) {
87  if (update_to.count(it->first) == 0) {
88  auto entity = it->second.entity.lock();
89  if (entity) {
90  on_added(entity);
91  }
92  update_to.insert(*it);
93  }
94  }
95 }
96 
98 template<typename EntityKeyType, typename EntityValueType>
100  : public std::unordered_map<const EntityKeyType *, CollectionEntry<EntityValueType>>
101 {
102 public:
104  using Key = const EntityKeyType *;
105 
107  using EntityWeakPtr = typename EntityValueType::WeakPtr;
108 
110  using EntitySharedPtr = typename EntityValueType::SharedPtr;
111 
113 
121  void update(
123  std::function<void(const EntitySharedPtr &)> on_added,
124  std::function<void(const EntitySharedPtr &)> on_removed)
125  {
126  update_entities(other, *this, on_added, on_removed);
127  }
128 };
129 
131 
136 {
139 
142 
145 
148 
151 
154 
157 
160 
163 
166 
169 
172 
174 
177  bool empty() const;
178 
180  void clear();
181 
183 
186  size_t remove_expired_entities();
187 };
188 
190 
196 void
197 build_entities_collection(
198  const std::vector<rclcpp::CallbackGroup::WeakPtr> & callback_groups,
199  ExecutorEntitiesCollection & collection);
200 
202 
210 size_t
211 ready_executables(
212  const ExecutorEntitiesCollection & collection,
214  std::deque<rclcpp::AnyExecutable> & executables
215 );
216 } // namespace executors
217 } // namespace rclcpp
218 
219 #endif // RCLCPP__EXECUTORS__EXECUTOR_ENTITIES_COLLECTION_HPP_
Interface for introspecting a wait set after waiting on it.
Definition: wait_result.hpp:63
A collection of entities, indexed by their corresponding handles.
typename EntityValueType::WeakPtr EntityWeakPtr
Weak pointer to entity type.
const EntityKeyType * Key
Key type of the map.
typename EntityValueType::SharedPtr EntitySharedPtr
Shared pointer to entity type.
void update(const EntityCollection< EntityKeyType, EntityValueType > &other, std::function< void(const EntitySharedPtr &)> on_added, std::function< void(const EntitySharedPtr &)> on_removed)
Update this collection based on the contents of another collection.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Structure to represent a single entity's entry in a collection.
rclcpp::CallbackGroup::WeakPtr callback_group
If relevant, the entity's corresponding callback_group.
typename EntityValueType::WeakPtr EntityWeakPtr
Weak pointer to entity type.
typename EntityValueType::SharedPtr EntitySharedPtr
Shared pointer to entity type.
Represent the total set of entities for a single executor.
TimerCollection timers
Collection of timers currently in use by the executor.
size_t remove_expired_entities()
Remove entities that have expired weak ownership.
GuardConditionCollection guard_conditions
Collection of guard conditions currently in use by the executor.
ServiceCollection services
Collection of services currently in use by the executor.
SubscriptionCollection subscriptions
Collection of subscriptions currently in use by the executor.
WaitableCollection waitables
Collection of waitables currently in use by the executor.
bool empty() const
Check if the entities collection is empty.
ClientCollection clients
Collection of clients currently in use by the executor.