rclcpp  master
C++ ROS Client Library API
subscription_base.hpp
Go to the documentation of this file.
1 // Copyright 2019 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__SUBSCRIPTION_BASE_HPP_
16 #define RCLCPP__SUBSCRIPTION_BASE_HPP_
17 
18 #include <atomic>
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 #include <vector>
23 #include <utility>
24 
25 #include "rcl/subscription.h"
26 
27 #include "rmw/rmw.h"
28 
32 #include "rclcpp/macros.hpp"
33 #include "rclcpp/message_info.hpp"
35 #include "rclcpp/qos.hpp"
36 #include "rclcpp/qos_event.hpp"
40 
41 namespace rclcpp
42 {
43 
44 namespace node_interfaces
45 {
46 class NodeBaseInterface;
47 } // namespace node_interfaces
48 
49 namespace experimental
50 {
55 class IntraProcessManager;
56 } // namespace experimental
57 
60 class SubscriptionBase : public std::enable_shared_from_this<SubscriptionBase>
61 {
62 public:
64 
65 
66 
79  const rosidl_message_type_support_t & type_support_handle,
80  const std::string & topic_name,
81  const rcl_subscription_options_t & subscription_options,
82  bool is_serialized = false);
83 
86  virtual ~SubscriptionBase();
87 
90  const char *
91  get_topic_name() const;
92 
96 
100 
102 
105  get_event_handlers() const;
106 
108 
121  get_actual_qos() const;
122 
124 
141  bool
142  take_type_erased(void * message_out, rclcpp::MessageInfo & message_info_out);
143 
145 
159  bool
160  take_serialized(rclcpp::SerializedMessage & message_out, rclcpp::MessageInfo & message_info_out);
161 
163 
165  virtual
167  create_message() = 0;
168 
170 
172  virtual
175 
177 
182  virtual
183  void
184  handle_message(std::shared_ptr<void> & message, const rclcpp::MessageInfo & message_info) = 0;
185 
187  virtual
188  void
189  handle_loaned_message(void * loaned_message, const rclcpp::MessageInfo & message_info) = 0;
190 
192 
194  virtual
195  void
196  return_message(std::shared_ptr<void> & message) = 0;
197 
199 
201  virtual
202  void
204 
206  const rosidl_message_type_support_t &
208 
210 
214  bool
215  is_serialized() const;
216 
218 
220  size_t
221  get_publisher_count() const;
222 
224 
231  bool
232  can_loan_messages() const;
233 
236 
239  void
241  uint64_t intra_process_subscription_id,
242  IntraProcessManagerWeakPtr weak_ipm);
243 
245 
250  rclcpp::Waitable::SharedPtr
252 
254 
267  bool
268  exchange_in_use_by_wait_set_state(void * pointer_to_subscription_part, bool in_use_state);
269 
271 
278 
279 protected:
280  template<typename EventCallbackT>
281  void
283  const EventCallbackT & callback,
284  const rcl_subscription_event_type_t event_type)
285  {
286  auto handler = std::make_shared<QOSEventHandler<EventCallbackT,
288  callback,
291  event_type);
292  qos_events_in_use_by_wait_set_.insert(std::make_pair(handler.get(), false));
293  event_handlers_.emplace_back(handler);
294  }
295 
298 
300  bool
301  matches_any_intra_process_publishers(const rmw_gid_t * sender_gid) const;
302 
304 
308 
310 
314 
315 private:
317 
318  rosidl_message_type_support_t type_support_;
319  bool is_serialized_;
320 
321  std::atomic<bool> subscription_in_use_by_wait_set_{false};
322  std::atomic<bool> intra_process_subscription_waitable_in_use_by_wait_set_{false};
324  std::atomic<bool>> qos_events_in_use_by_wait_set_;
325 };
326 
327 } // namespace rclcpp
328 
329 #endif // RCLCPP__SUBSCRIPTION_BASE_HPP_
rclcpp::SubscriptionBase::get_event_handlers
const std::vector< std::shared_ptr< rclcpp::QOSEventHandlerBase > > & get_event_handlers() const
Get all the QoS event handlers associated with this subscription.
rcl_subscription_event_type_t
rcl_subscription_event_type_t
rclcpp::SubscriptionBase::handle_loaned_message
virtual void handle_loaned_message(void *loaned_message, const rclcpp::MessageInfo &message_info)=0
rclcpp::SubscriptionBase::setup_intra_process
void setup_intra_process(uint64_t intra_process_subscription_id, IntraProcessManagerWeakPtr weak_ipm)
Implemenation detail.
rclcpp::SubscriptionBase::intra_process_subscription_handle_
std::shared_ptr< rcl_subscription_t > intra_process_subscription_handle_
Definition: subscription_base.hpp:307
rclcpp::SubscriptionBase::get_message_type_support_handle
const rosidl_message_type_support_t & get_message_type_support_handle() const
rclcpp::SubscriptionBase::node_base_
rclcpp::node_interfaces::NodeBaseInterface *const node_base_
Definition: subscription_base.hpp:303
std::string
std::shared_ptr< rcl_subscription_t >
rclcpp::SubscriptionBase::use_intra_process_
bool use_intra_process_
Definition: subscription_base.hpp:311
message_info.hpp
network_flow_endpoint.hpp
RCLCPP_DISABLE_COPY
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
rcl_subscription_event_init
rcl_ret_t rcl_subscription_event_init(rcl_event_t *event, const rcl_subscription_t *subscription, const rcl_subscription_event_type_t event_type)
rmw.h
rclcpp::SubscriptionBase::return_message
virtual void return_message(std::shared_ptr< void > &message)=0
Return the message borrowed in create_message.
rclcpp::SubscriptionBase::weak_ipm_
IntraProcessManagerWeakPtr weak_ipm_
Definition: subscription_base.hpp:312
std::make_shared
T make_shared(T... args)
std::vector
rclcpp::SubscriptionBase::event_handlers_
std::vector< std::shared_ptr< rclcpp::QOSEventHandlerBase > > event_handlers_
Definition: subscription_base.hpp:309
rclcpp::SubscriptionBase::can_loan_messages
bool can_loan_messages() const
Check if subscription instance can loan messages.
rclcpp::SubscriptionBase::get_subscription_handle
std::shared_ptr< rcl_subscription_t > get_subscription_handle()
rclcpp::SubscriptionBase
Definition: subscription_base.hpp:60
rclcpp::SubscriptionBase::get_publisher_count
size_t get_publisher_count() const
Get matching publisher count.
subscription.h
rclcpp::SubscriptionBase::intra_process_subscription_id_
uint64_t intra_process_subscription_id_
Definition: subscription_base.hpp:313
rclcpp::SubscriptionBase::subscription_handle_
std::shared_ptr< rcl_subscription_t > subscription_handle_
Definition: subscription_base.hpp:306
rclcpp::SubscriptionBase::default_incompatible_qos_callback
void default_incompatible_qos_callback(QOSRequestedIncompatibleQoSInfo &info) const
rclcpp::SubscriptionBase::create_serialized_message
virtual std::shared_ptr< rclcpp::SerializedMessage > create_serialized_message()=0
Borrow a new serialized message.
rclcpp::SubscriptionBase::SubscriptionBase
SubscriptionBase(rclcpp::node_interfaces::NodeBaseInterface *node_base, const rosidl_message_type_support_t &type_support_handle, const std::string &topic_name, const rcl_subscription_options_t &subscription_options, bool is_serialized=false)
Constructor.
serialized_message.hpp
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
subscription_intra_process_base.hpp
qos.hpp
RCLCPP_PUBLIC
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
rclcpp::node_interfaces::NodeBaseInterface
Pure virtual interface class for the NodeBase part of the Node API.
Definition: node_base_interface.hpp:36
rclcpp::QoS
Encapsulation of Quality of Service settings.
Definition: qos.hpp:110
rclcpp::SubscriptionBase::get_network_flow_endpoints
std::vector< rclcpp::NetworkFlowEndpoint > get_network_flow_endpoints() const
Get network flow endpoints.
macros.hpp
rclcpp::SubscriptionBase::is_serialized
bool is_serialized() const
Return if the subscription is serialized.
rclcpp::SubscriptionBase::get_topic_name
const char * get_topic_name() const
Get the topic that this subscription is subscribed on.
rclcpp::SubscriptionBase::take_serialized
bool take_serialized(rclcpp::SerializedMessage &message_out, rclcpp::MessageInfo &message_info_out)
Take the next inter-process message, in its serialized form, from the subscription.
rclcpp::SubscriptionBase::handle_message
virtual void handle_message(std::shared_ptr< void > &message, const rclcpp::MessageInfo &message_info)=0
Check if we need to handle the message, and execute the callback if we do.
rclcpp::SubscriptionBase::create_message
virtual std::shared_ptr< void > create_message()=0
Borrow a new message.
std::enable_shared_from_this
std::atomic< bool >
intra_process_manager.hpp
any_subscription_callback.hpp
rcl_subscription_options_t
std::weak_ptr< rclcpp::experimental::IntraProcessManager >
rclcpp::SubscriptionBase::take_type_erased
bool take_type_erased(void *message_out, rclcpp::MessageInfo &message_info_out)
Take the next inter-process message from the subscription as a type erased pointer.
std::vector::emplace_back
T emplace_back(T... args)
rclcpp::MessageInfo
Additional meta data about messages taken from subscriptions.
Definition: message_info.hpp:26
rmw_gid_t
rclcpp::SubscriptionBase::return_serialized_message
virtual void return_serialized_message(std::shared_ptr< rclcpp::SerializedMessage > &message)=0
Return the message borrowed in create_serialized_message.
type_support_decl.hpp
visibility_control.hpp
std::unordered_map::insert
T insert(T... args)
rmw_qos_incompatible_event_status_t
rclcpp::SerializedMessage
Object oriented version of rcl_serialized_message_t with destructor to avoid memory leaks.
Definition: serialized_message.hpp:27
rclcpp::SubscriptionBase::get_intra_process_waitable
rclcpp::Waitable::SharedPtr get_intra_process_waitable() const
Return the waitable for intra-process.
RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE
#define RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(...)
Definition: macros.hpp:51
rclcpp::SubscriptionBase::~SubscriptionBase
virtual ~SubscriptionBase()
Destructor.
rclcpp::SubscriptionBase::matches_any_intra_process_publishers
bool matches_any_intra_process_publishers(const rmw_gid_t *sender_gid) const
std::make_pair
T make_pair(T... args)
rclcpp::SubscriptionBase::exchange_in_use_by_wait_set_state
bool exchange_in_use_by_wait_set_state(void *pointer_to_subscription_part, bool in_use_state)
Exchange state of whether or not a part of the subscription is used by a wait set.
rclcpp::SubscriptionBase::node_handle_
std::shared_ptr< rcl_node_t > node_handle_
Definition: subscription_base.hpp:305
rclcpp::QOSEventHandlerBase
Definition: qos_event.hpp:84
rclcpp::QOSEventHandler
Definition: qos_event.hpp:111
std::unordered_map
rclcpp::SubscriptionBase::add_event_handler
void add_event_handler(const EventCallbackT &callback, const rcl_subscription_event_type_t event_type)
Definition: subscription_base.hpp:282
qos_event.hpp
rclcpp::SubscriptionBase::get_actual_qos
rclcpp::QoS get_actual_qos() const
Get the actual QoS settings, after the defaults have been determined.