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 
60  explicit CallbackGroup(CallbackGroupType group_type);
61 
62  template<typename Function>
63  rclcpp::SubscriptionBase::SharedPtr
64  find_subscription_ptrs_if(Function func) const
65  {
66  return _find_ptrs_if_impl<rclcpp::SubscriptionBase, Function>(func, subscription_ptrs_);
67  }
68 
69  template<typename Function>
70  rclcpp::TimerBase::SharedPtr
71  find_timer_ptrs_if(Function func) const
72  {
73  return _find_ptrs_if_impl<rclcpp::TimerBase, Function>(func, timer_ptrs_);
74  }
75 
76  template<typename Function>
77  rclcpp::ServiceBase::SharedPtr
78  find_service_ptrs_if(Function func) const
79  {
80  return _find_ptrs_if_impl<rclcpp::ServiceBase, Function>(func, service_ptrs_);
81  }
82 
83  template<typename Function>
84  rclcpp::ClientBase::SharedPtr
85  find_client_ptrs_if(Function func) const
86  {
87  return _find_ptrs_if_impl<rclcpp::ClientBase, Function>(func, client_ptrs_);
88  }
89 
90  template<typename Function>
91  rclcpp::Waitable::SharedPtr
92  find_waitable_ptrs_if(Function func) const
93  {
94  return _find_ptrs_if_impl<rclcpp::Waitable, Function>(func, waitable_ptrs_);
95  }
96 
98  std::atomic_bool &
100 
102  const CallbackGroupType &
103  type() const;
104 
105 protected:
107 
109  void
110  add_publisher(const rclcpp::PublisherBase::SharedPtr publisher_ptr);
111 
113  void
114  add_subscription(const rclcpp::SubscriptionBase::SharedPtr subscription_ptr);
115 
117  void
118  add_timer(const rclcpp::TimerBase::SharedPtr timer_ptr);
119 
121  void
122  add_service(const rclcpp::ServiceBase::SharedPtr service_ptr);
123 
125  void
126  add_client(const rclcpp::ClientBase::SharedPtr client_ptr);
127 
129  void
130  add_waitable(const rclcpp::Waitable::SharedPtr waitable_ptr);
131 
133  void
134  remove_waitable(const rclcpp::Waitable::SharedPtr waitable_ptr) noexcept;
135 
137  // Mutex to protect the subsequent vectors of pointers.
144  std::atomic_bool can_be_taken_from_;
145 
146 private:
147  template<typename TypeT, typename Function>
148  typename TypeT::SharedPtr _find_ptrs_if_impl(
149  Function func, const std::vector<typename TypeT::WeakPtr> & vect_ptrs) const
150  {
152  for (auto & weak_ptr : vect_ptrs) {
153  auto ref_ptr = weak_ptr.lock();
154  if (ref_ptr && func(ref_ptr)) {
155  return ref_ptr;
156  }
157  }
158  return typename TypeT::SharedPtr();
159  }
160 };
161 
162 namespace callback_group
163 {
164 
165 using CallbackGroupType [[deprecated("use rclcpp::CallbackGroupType instead")]] = CallbackGroupType;
166 using CallbackGroup [[deprecated("use rclcpp::CallbackGroup instead")]] = CallbackGroup;
167 
168 } // namespace callback_group
169 } // namespace rclcpp
170 
171 #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:140
client.hpp
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:139
rclcpp::CallbackGroup::add_timer
void add_timer(const rclcpp::TimerBase::SharedPtr timer_ptr)
rclcpp::CallbackGroup::find_subscription_ptrs_if
rclcpp::SubscriptionBase::SharedPtr find_subscription_ptrs_if(Function func) const
Definition: callback_group.hpp:64
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:85
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:142
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:92
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:136
RCLCPP_SMART_PTR_DEFINITIONS
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
rclcpp::CallbackGroup::mutex_
std::mutex mutex_
Definition: callback_group.hpp:138
rclcpp::node_interfaces::NodeTopics
Implementation of the NodeTopics part of the Node API.
Definition: node_topics.hpp:37
rclcpp::CallbackGroup::find_service_ptrs_if
rclcpp::ServiceBase::SharedPtr find_service_ptrs_if(Function func) const
Definition: callback_group.hpp:78
rclcpp::CallbackGroup::waitable_ptrs_
std::vector< rclcpp::Waitable::WeakPtr > waitable_ptrs_
Definition: callback_group.hpp:143
rclcpp::callback_group::CallbackGroup
CallbackGroup CallbackGroup
Definition: callback_group.hpp:166
rclcpp::callback_group::CallbackGroupType
CallbackGroupType CallbackGroupType
Definition: callback_group.hpp:165
publisher_base.hpp
rclcpp::CallbackGroup::service_ptrs_
std::vector< rclcpp::ServiceBase::WeakPtr > service_ptrs_
Definition: callback_group.hpp:141
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:32
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:71
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::CallbackGroup
CallbackGroup(CallbackGroupType group_type)
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:144
rclcpp::node_interfaces::NodeWaitables
Implementation of the NodeWaitables part of the Node API.
Definition: node_waitables.hpp:31
service.hpp
waitable.hpp