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 
44 {
46  Reentrant
47 };
48 
50 {
55 
56 public:
58 
59 
60 
94  explicit CallbackGroup(
95  CallbackGroupType group_type,
97 
98  template<typename Function>
99  rclcpp::SubscriptionBase::SharedPtr
100  find_subscription_ptrs_if(Function func) const
101  {
102  return _find_ptrs_if_impl<rclcpp::SubscriptionBase, Function>(func, subscription_ptrs_);
103  }
104 
105  template<typename Function>
106  rclcpp::TimerBase::SharedPtr
107  find_timer_ptrs_if(Function func) const
108  {
109  return _find_ptrs_if_impl<rclcpp::TimerBase, Function>(func, timer_ptrs_);
110  }
111 
112  template<typename Function>
113  rclcpp::ServiceBase::SharedPtr
114  find_service_ptrs_if(Function func) const
115  {
116  return _find_ptrs_if_impl<rclcpp::ServiceBase, Function>(func, service_ptrs_);
117  }
118 
119  template<typename Function>
120  rclcpp::ClientBase::SharedPtr
121  find_client_ptrs_if(Function func) const
122  {
123  return _find_ptrs_if_impl<rclcpp::ClientBase, Function>(func, client_ptrs_);
124  }
125 
126  template<typename Function>
127  rclcpp::Waitable::SharedPtr
128  find_waitable_ptrs_if(Function func) const
129  {
130  return _find_ptrs_if_impl<rclcpp::Waitable, Function>(func, waitable_ptrs_);
131  }
132 
134  std::atomic_bool &
136 
138  const CallbackGroupType &
139  type() const;
140 
142 
154  std::atomic_bool &
156 
158 
163  bool
165 
166 protected:
168 
170  void
171  add_publisher(const rclcpp::PublisherBase::SharedPtr publisher_ptr);
172 
174  void
175  add_subscription(const rclcpp::SubscriptionBase::SharedPtr subscription_ptr);
176 
178  void
179  add_timer(const rclcpp::TimerBase::SharedPtr timer_ptr);
180 
182  void
183  add_service(const rclcpp::ServiceBase::SharedPtr service_ptr);
184 
186  void
187  add_client(const rclcpp::ClientBase::SharedPtr client_ptr);
188 
190  void
191  add_waitable(const rclcpp::Waitable::SharedPtr waitable_ptr);
192 
194  void
195  remove_waitable(const rclcpp::Waitable::SharedPtr waitable_ptr) noexcept;
196 
198  // Mutex to protect the subsequent vectors of pointers.
200  std::atomic_bool associated_with_executor_;
206  std::atomic_bool can_be_taken_from_;
208 
209 private:
210  template<typename TypeT, typename Function>
211  typename TypeT::SharedPtr _find_ptrs_if_impl(
212  Function func, const std::vector<typename TypeT::WeakPtr> & vect_ptrs) const
213  {
215  for (auto & weak_ptr : vect_ptrs) {
216  auto ref_ptr = weak_ptr.lock();
217  if (ref_ptr && func(ref_ptr)) {
218  return ref_ptr;
219  }
220  }
221  return typename TypeT::SharedPtr();
222  }
223 };
224 
225 } // namespace rclcpp
226 
227 #endif // RCLCPP__CALLBACK_GROUP_HPP_
rclcpp::CallbackGroup::add_waitable
void add_waitable(const rclcpp::Waitable::SharedPtr waitable_ptr)
rclcpp::CallbackGroup::timer_ptrs_
std::vector< rclcpp::TimerBase::WeakPtr > timer_ptrs_
Definition: callback_group.hpp:202
client.hpp
rclcpp::CallbackGroup::automatically_add_to_executor_with_node
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_DISABLE_COPY
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
rclcpp::CallbackGroup::subscription_ptrs_
std::vector< rclcpp::SubscriptionBase::WeakPtr > subscription_ptrs_
Definition: callback_group.hpp:201
rclcpp::CallbackGroup::add_timer
void add_timer(const rclcpp::TimerBase::SharedPtr timer_ptr)
rclcpp::CallbackGroup::get_associated_with_executor_atomic
std::atomic_bool & get_associated_with_executor_atomic()
Return a reference to the 'associated with executor' atomic boolean.
rclcpp::CallbackGroup::find_subscription_ptrs_if
rclcpp::SubscriptionBase::SharedPtr find_subscription_ptrs_if(Function func) const
Definition: callback_group.hpp:100
std::vector< rclcpp::SubscriptionBase::WeakPtr >
rclcpp::CallbackGroup::find_client_ptrs_if
rclcpp::ClientBase::SharedPtr find_client_ptrs_if(Function func) const
Definition: callback_group.hpp:121
rclcpp::node_interfaces::NodeTimers
Implementation of the NodeTimers part of the Node API.
Definition: node_timers.hpp:31
rclcpp::CallbackGroup::type
const CallbackGroupType & type() const
rclcpp::CallbackGroup::client_ptrs_
std::vector< rclcpp::ClientBase::WeakPtr > client_ptrs_
Definition: callback_group.hpp:204
std::lock_guard
rclcpp::CallbackGroupType::MutuallyExclusive
@ MutuallyExclusive
rclcpp::CallbackGroup::find_waitable_ptrs_if
rclcpp::Waitable::SharedPtr find_waitable_ptrs_if(Function func) const
Definition: callback_group.hpp:128
subscription_base.hpp
rclcpp::CallbackGroupType::Reentrant
@ Reentrant
rclcpp::CallbackGroupType
CallbackGroupType
Definition: callback_group.hpp:43
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
timer.hpp
RCLCPP_PUBLIC
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
rclcpp::CallbackGroup::type_
CallbackGroupType type_
Definition: callback_group.hpp:197
RCLCPP_SMART_PTR_DEFINITIONS
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
rclcpp::CallbackGroup::mutex_
std::mutex mutex_
Definition: callback_group.hpp:199
rclcpp::node_interfaces::NodeTopics
Implementation of the NodeTopics part of the Node API.
Definition: node_topics.hpp:37
rclcpp::CallbackGroup::CallbackGroup
CallbackGroup(CallbackGroupType group_type, bool automatically_add_to_executor_with_node=true)
Constructor for CallbackGroup.
rclcpp::CallbackGroup::find_service_ptrs_if
rclcpp::ServiceBase::SharedPtr find_service_ptrs_if(Function func) const
Definition: callback_group.hpp:114
rclcpp::CallbackGroup::waitable_ptrs_
std::vector< rclcpp::Waitable::WeakPtr > waitable_ptrs_
Definition: callback_group.hpp:205
publisher_base.hpp
rclcpp::CallbackGroup::service_ptrs_
std::vector< rclcpp::ServiceBase::WeakPtr > service_ptrs_
Definition: callback_group.hpp:203
rclcpp::CallbackGroup::add_client
void add_client(const rclcpp::ClientBase::SharedPtr client_ptr)
rclcpp::CallbackGroup::remove_waitable
void remove_waitable(const rclcpp::Waitable::SharedPtr waitable_ptr) noexcept
rclcpp::node_interfaces::NodeServices
Implementation of the NodeServices part of the Node API.
Definition: node_services.hpp:34
rclcpp::CallbackGroup::add_subscription
void add_subscription(const rclcpp::SubscriptionBase::SharedPtr subscription_ptr)
rclcpp::CallbackGroup::find_timer_ptrs_if
rclcpp::TimerBase::SharedPtr find_timer_ptrs_if(Function func) const
Definition: callback_group.hpp:107
visibility_control.hpp
rclcpp::CallbackGroup::can_be_taken_from
std::atomic_bool & can_be_taken_from()
std::mutex
rclcpp::CallbackGroup::add_publisher
void add_publisher(const rclcpp::PublisherBase::SharedPtr publisher_ptr)
rclcpp::CallbackGroup
Definition: callback_group.hpp:49
rclcpp::CallbackGroup::add_service
void add_service(const rclcpp::ServiceBase::SharedPtr service_ptr)
rclcpp::CallbackGroup::can_be_taken_from_
std::atomic_bool can_be_taken_from_
Definition: callback_group.hpp:206
rclcpp::CallbackGroup::automatically_add_to_executor_with_node_
const bool automatically_add_to_executor_with_node_
Definition: callback_group.hpp:207
rclcpp::node_interfaces::NodeWaitables
Implementation of the NodeWaitables part of the Node API.
Definition: node_waitables.hpp:31
service.hpp
waitable.hpp
rclcpp::CallbackGroup::associated_with_executor_
std::atomic_bool associated_with_executor_
Definition: callback_group.hpp:200