ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
ROS 2 C++ Client Library with ROS Client Library
callback_group.hpp
1 // Copyright 2014 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__CALLBACK_GROUP_HPP_
16 #define RCLCPP__CALLBACK_GROUP_HPP_
17 
18 #include <atomic>
19 #include <functional>
20 #include <memory>
21 #include <mutex>
22 #include <vector>
23 
24 #include "rclcpp/client.hpp"
25 #include "rclcpp/context.hpp"
26 #include "rclcpp/guard_condition.hpp"
27 #include "rclcpp/publisher_base.hpp"
28 #include "rclcpp/service.hpp"
29 #include "rclcpp/subscription_base.hpp"
30 #include "rclcpp/timer.hpp"
31 #include "rclcpp/visibility_control.hpp"
32 #include "rclcpp/waitable.hpp"
33 
34 namespace rclcpp
35 {
36 
37 // Forward declarations for friend statement in class CallbackGroup
38 namespace node_interfaces
39 {
40 class NodeServices;
41 class NodeTimers;
42 class NodeTopics;
43 class NodeWaitables;
44 } // namespace node_interfaces
45 
46 enum class CallbackGroupType
47 {
48  MutuallyExclusive,
49  Reentrant
50 };
51 
53 {
58 
59 public:
60  RCLCPP_SMART_PTR_DEFINITIONS(CallbackGroup)
61 
62 
97  RCLCPP_PUBLIC
98  explicit CallbackGroup(
99  CallbackGroupType group_type,
100  rclcpp::Context::WeakPtr context,
102 
104  RCLCPP_PUBLIC
105  ~CallbackGroup();
106 
107  template<typename Function>
108  rclcpp::SubscriptionBase::SharedPtr
109  find_subscription_ptrs_if(Function func) const
110  {
111  return _find_ptrs_if_impl<rclcpp::SubscriptionBase, Function>(func, subscription_ptrs_);
112  }
113 
114  template<typename Function>
115  rclcpp::TimerBase::SharedPtr
116  find_timer_ptrs_if(Function func) const
117  {
118  return _find_ptrs_if_impl<rclcpp::TimerBase, Function>(func, timer_ptrs_);
119  }
120 
121  template<typename Function>
122  rclcpp::ServiceBase::SharedPtr
123  find_service_ptrs_if(Function func) const
124  {
125  return _find_ptrs_if_impl<rclcpp::ServiceBase, Function>(func, service_ptrs_);
126  }
127 
128  template<typename Function>
129  rclcpp::ClientBase::SharedPtr
130  find_client_ptrs_if(Function func) const
131  {
132  return _find_ptrs_if_impl<rclcpp::ClientBase, Function>(func, client_ptrs_);
133  }
134 
135  template<typename Function>
136  rclcpp::Waitable::SharedPtr
137  find_waitable_ptrs_if(Function func) const
138  {
139  return _find_ptrs_if_impl<rclcpp::Waitable, Function>(func, waitable_ptrs_);
140  }
141 
143 
146  RCLCPP_PUBLIC
147  size_t
148  size() const;
149 
151 
159  RCLCPP_PUBLIC
160  std::atomic_bool &
162 
164 
167  RCLCPP_PUBLIC
168  const CallbackGroupType &
169  type() const;
170 
172 
179  RCLCPP_PUBLIC
180  void
182  std::function<void(const rclcpp::SubscriptionBase::SharedPtr &)> sub_func,
183  std::function<void(const rclcpp::ServiceBase::SharedPtr &)> service_func,
184  std::function<void(const rclcpp::ClientBase::SharedPtr &)> client_func,
185  std::function<void(const rclcpp::TimerBase::SharedPtr &)> timer_func,
186  std::function<void(const rclcpp::Waitable::SharedPtr &)> waitable_func) const;
187 
189 
200  RCLCPP_PUBLIC
201  std::atomic_bool &
203 
205 
209  RCLCPP_PUBLIC
210  bool
212 
214 
217  RCLCPP_PUBLIC
218  rclcpp::GuardCondition::SharedPtr
220 
222  RCLCPP_PUBLIC
223  void
225 
226 protected:
227  RCLCPP_DISABLE_COPY(CallbackGroup)
228 
229  RCLCPP_PUBLIC
230  void
231  add_publisher(const rclcpp::PublisherBase::SharedPtr publisher_ptr);
232 
233  RCLCPP_PUBLIC
234  void
235  add_subscription(const rclcpp::SubscriptionBase::SharedPtr subscription_ptr);
236 
237  RCLCPP_PUBLIC
238  void
239  add_timer(const rclcpp::TimerBase::SharedPtr timer_ptr);
240 
241  RCLCPP_PUBLIC
242  void
243  add_service(const rclcpp::ServiceBase::SharedPtr service_ptr);
244 
245  RCLCPP_PUBLIC
246  void
247  add_client(const rclcpp::ClientBase::SharedPtr client_ptr);
248 
249  RCLCPP_PUBLIC
250  void
251  add_waitable(const rclcpp::Waitable::SharedPtr waitable_ptr);
252 
253  RCLCPP_PUBLIC
254  void
255  remove_waitable(const rclcpp::Waitable::SharedPtr waitable_ptr) noexcept;
256 
257  CallbackGroupType type_;
258  // Mutex to protect the subsequent vectors of pointers.
259  mutable std::mutex mutex_;
260  std::atomic_bool associated_with_executor_;
261  std::vector<rclcpp::SubscriptionBase::WeakPtr> subscription_ptrs_;
262  std::vector<rclcpp::TimerBase::WeakPtr> timer_ptrs_;
263  std::vector<rclcpp::ServiceBase::WeakPtr> service_ptrs_;
264  std::vector<rclcpp::ClientBase::WeakPtr> client_ptrs_;
265  std::vector<rclcpp::Waitable::WeakPtr> waitable_ptrs_;
266  std::atomic_bool can_be_taken_from_;
267  const bool automatically_add_to_executor_with_node_;
268  // defer the creation of the guard condition
269  std::shared_ptr<rclcpp::GuardCondition> notify_guard_condition_ = nullptr;
270  std::recursive_mutex notify_guard_condition_mutex_;
271 
272  rclcpp::Context::WeakPtr context_;
273 
274 private:
275  template<typename TypeT, typename Function>
276  typename TypeT::SharedPtr _find_ptrs_if_impl(
277  Function func, const std::vector<typename TypeT::WeakPtr> & vect_ptrs) const
278  {
279  std::lock_guard<std::mutex> lock(mutex_);
280  for (auto & weak_ptr : vect_ptrs) {
281  auto ref_ptr = weak_ptr.lock();
282  if (ref_ptr && func(ref_ptr)) {
283  return ref_ptr;
284  }
285  }
286  return typename TypeT::SharedPtr();
287  }
288 };
289 
290 } // namespace rclcpp
291 
292 #endif // RCLCPP__CALLBACK_GROUP_HPP_
RCLCPP_PUBLIC ~CallbackGroup()
Default destructor.
RCLCPP_PUBLIC rclcpp::GuardCondition::SharedPtr get_notify_guard_condition()
Retrieve the guard condition used to signal changes to this callback group.
RCLCPP_PUBLIC std::atomic_bool & get_associated_with_executor_atomic()
Return a reference to the 'associated with executor' atomic boolean.
RCLCPP_PUBLIC void trigger_notify_guard_condition()
Trigger the notify guard condition.
RCLCPP_PUBLIC void collect_all_ptrs(std::function< void(const rclcpp::SubscriptionBase::SharedPtr &)> sub_func, std::function< void(const rclcpp::ServiceBase::SharedPtr &)> service_func, std::function< void(const rclcpp::ClientBase::SharedPtr &)> client_func, std::function< void(const rclcpp::TimerBase::SharedPtr &)> timer_func, std::function< void(const rclcpp::Waitable::SharedPtr &)> waitable_func) const
Collect all of the entity pointers contained in this callback group.
RCLCPP_PUBLIC CallbackGroup(CallbackGroupType group_type, rclcpp::Context::WeakPtr context, bool automatically_add_to_executor_with_node=true)
Constructor for CallbackGroup.
RCLCPP_PUBLIC bool automatically_add_to_executor_with_node() const
Return true if this callback group should be automatically added to an executor by the node.
RCLCPP_PUBLIC const CallbackGroupType & type() const
Get the group type.
RCLCPP_PUBLIC std::atomic_bool & can_be_taken_from()
Return a reference to the 'can be taken' atomic boolean.
RCLCPP_PUBLIC size_t size() const
Get the total number of entities in this callback group.
Implementation of the NodeServices part of the Node API.
Implementation of the NodeTimers part of the Node API.
Definition: node_timers.hpp:32
Implementation of the NodeTopics part of the Node API.
Definition: node_topics.hpp:42
Implementation of the NodeWaitables part of the Node API.
RCLCPP_PUBLIC void remove_waitable(rclcpp::Waitable::SharedPtr waitable_ptr, rclcpp::CallbackGroup::SharedPtr group) noexcept override
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.