rclcpp  master
C++ ROS Client Library API
callback_group.hpp
Go to the documentation of this file.
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 <mutex>
20 #include <string>
21 #include <vector>
22 
23 #include "rclcpp/client.hpp"
25 #include "rclcpp/service.hpp"
27 #include "rclcpp/timer.hpp"
29 #include "rclcpp/waitable.hpp"
30 
31 namespace rclcpp
32 {
33 
34 // Forward declarations for friend statement in class CallbackGroup
35 namespace node_interfaces
36 {
37 class NodeServices;
38 class NodeTimers;
39 class NodeTopics;
40 class NodeWaitables;
41 } // namespace node_interfaces
42 
43 namespace callback_group
44 {
45 
47 {
49  Reentrant
50 };
51 
53 {
58 
59 public:
61 
63  explicit CallbackGroup(CallbackGroupType group_type);
64 
65  template<typename Function>
67  find_subscription_ptrs_if(Function func) const
68  {
69  return _find_ptrs_if_impl<rclcpp::SubscriptionBase, Function>(func, subscription_ptrs_);
70  }
71 
72  template<typename Function>
74  find_timer_ptrs_if(Function func) const
75  {
76  return _find_ptrs_if_impl<rclcpp::TimerBase, Function>(func, timer_ptrs_);
77  }
78 
79  template<typename Function>
81  find_service_ptrs_if(Function func) const
82  {
83  return _find_ptrs_if_impl<rclcpp::ServiceBase, Function>(func, service_ptrs_);
84  }
85 
86  template<typename Function>
88  find_client_ptrs_if(Function func) const
89  {
90  return _find_ptrs_if_impl<rclcpp::ClientBase, Function>(func, client_ptrs_);
91  }
92 
93  template<typename Function>
95  find_waitable_ptrs_if(Function func) const
96  {
97  return _find_ptrs_if_impl<rclcpp::Waitable, Function>(func, waitable_ptrs_);
98  }
99 
101  std::atomic_bool &
102  can_be_taken_from();
103 
105  const CallbackGroupType &
106  type() const;
107 
108 protected:
110 
112  void
113  add_publisher(const rclcpp::PublisherBase::SharedPtr publisher_ptr);
114 
116  void
117  add_subscription(const rclcpp::SubscriptionBase::SharedPtr subscription_ptr);
118 
120  void
121  add_timer(const rclcpp::TimerBase::SharedPtr timer_ptr);
122 
124  void
125  add_service(const rclcpp::ServiceBase::SharedPtr service_ptr);
126 
128  void
129  add_client(const rclcpp::ClientBase::SharedPtr client_ptr);
130 
132  void
133  add_waitable(const rclcpp::Waitable::SharedPtr waitable_ptr);
134 
136  void
137  remove_waitable(const rclcpp::Waitable::SharedPtr waitable_ptr) noexcept;
138 
140  // Mutex to protect the subsequent vectors of pointers.
147  std::atomic_bool can_be_taken_from_;
148 
149 private:
150  template<typename TypeT, typename Function>
151  typename TypeT::SharedPtr _find_ptrs_if_impl(
152  Function func, const std::vector<typename TypeT::WeakPtr> & vect_ptrs) const
153  {
154  std::lock_guard<std::mutex> lock(mutex_);
155  for (auto & weak_ptr : vect_ptrs) {
156  auto ref_ptr = weak_ptr.lock();
157  if (ref_ptr && func(ref_ptr)) {
158  return ref_ptr;
159  }
160  }
161  return typename TypeT::SharedPtr();
162  }
163 };
164 
165 } // namespace callback_group
166 } // namespace rclcpp
167 
168 #endif // RCLCPP__CALLBACK_GROUP_HPP_
rclcpp::ServiceBase::SharedPtr find_service_ptrs_if(Function func) const
Definition: callback_group.hpp:81
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
std::vector< rclcpp::ServiceBase::WeakPtr > service_ptrs_
Definition: callback_group.hpp:144
rclcpp::TimerBase::SharedPtr find_timer_ptrs_if(Function func) const
Definition: callback_group.hpp:74
CallbackGroupType type_
Definition: callback_group.hpp:139
std::mutex mutex_
Definition: callback_group.hpp:141
CallbackGroupType
Definition: callback_group.hpp:46
std::vector< rclcpp::ClientBase::WeakPtr > client_ptrs_
Definition: callback_group.hpp:145
Implementation of the NodeTimers part of the Node API.
Definition: node_timers.hpp:31
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
std::atomic_bool can_be_taken_from_
Definition: callback_group.hpp:147
Implementation of the NodeServices part of the Node API.
Definition: node_services.hpp:32
std::vector< rclcpp::SubscriptionBase::WeakPtr > subscription_ptrs_
Definition: callback_group.hpp:142
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
Definition: callback_group.hpp:52
rclcpp::Waitable::SharedPtr find_waitable_ptrs_if(Function func) const
Definition: callback_group.hpp:95
std::vector< rclcpp::TimerBase::WeakPtr > timer_ptrs_
Definition: callback_group.hpp:143
virtual void remove_waitable(rclcpp::Waitable::SharedPtr waitable_ptr, rclcpp::callback_group::CallbackGroup::SharedPtr group) noexcept
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
Set the data type used in the intra-process buffer as std::shared_ptr<MessageT>
rclcpp::SubscriptionBase::SharedPtr find_subscription_ptrs_if(Function func) const
Definition: callback_group.hpp:67
std::vector< rclcpp::Waitable::WeakPtr > waitable_ptrs_
Definition: callback_group.hpp:146
Implementation of the NodeTopics part of the Node API.
Definition: node_topics.hpp:36
rclcpp::ClientBase::SharedPtr find_client_ptrs_if(Function func) const
Definition: callback_group.hpp:88
Implementation of the NodeWaitables part of the Node API.
Definition: node_waitables.hpp:32
virtual void add_waitable(rclcpp::Waitable::SharedPtr waitable_base_ptr, rclcpp::callback_group::CallbackGroup::SharedPtr group)