Skip to content

Commit e34f4dc

Browse files
committed
Introduce method to clear expired entities from a collection
Signed-off-by: Michael Carroll <[email protected]>
1 parent b812790 commit e34f4dc

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

rclcpp/include/rclcpp/executors/executor_entities_collection.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ struct ExecutorEntitiesCollection
178178

179179
/// Clear the entities collection
180180
void clear();
181+
182+
/// Remove entities that have expired weak ownership
183+
/**
184+
* \return The total number of removed entities
185+
*/
186+
size_t remove_expired_entities();
181187
};
182188

183189
/// Build an entities collection from callback groups

rclcpp/src/rclcpp/executors/executor_entities_collection.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,31 @@ void ExecutorEntitiesCollection::clear()
3939
waitables.clear();
4040
}
4141

42+
size_t ExecutorEntitiesCollection::remove_expired_entities()
43+
{
44+
auto remove_entities = [](auto & collection) -> size_t {
45+
size_t removed = 0;
46+
for (auto it = collection.begin(); it != collection.end(); )
47+
{
48+
if (it->second.entity.expired()) {
49+
++removed;
50+
it = collection.erase(it);
51+
} else {
52+
++it;
53+
}
54+
}
55+
return removed;
56+
};
57+
58+
return (
59+
remove_entities(subscriptions) +
60+
remove_entities(timers) +
61+
remove_entities(guard_conditions) +
62+
remove_entities(clients) +
63+
remove_entities(services) +
64+
remove_entities(waitables));
65+
}
66+
4267
void
4368
build_entities_collection(
4469
const std::vector<rclcpp::CallbackGroup::WeakPtr> & callback_groups,

0 commit comments

Comments
 (0)