ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
publisher_options.hpp
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__PUBLISHER_OPTIONS_HPP_
16 #define RCLCPP__PUBLISHER_OPTIONS_HPP_
17 
18 #include <memory>
19 #include <string>
20 #include <type_traits>
21 #include <vector>
22 
23 #include "rcl/publisher.h"
24 
25 #include "rclcpp/allocator/allocator_common.hpp"
26 #include "rclcpp/detail/rmw_implementation_specific_publisher_payload.hpp"
27 #include "rclcpp/intra_process_setting.hpp"
28 #include "rclcpp/qos.hpp"
29 #include "rclcpp/qos_event.hpp"
30 #include "rclcpp/qos_overriding_options.hpp"
31 
32 namespace rclcpp
33 {
34 
35 class CallbackGroup;
36 
39 {
42 
45 
47  bool use_default_callbacks = true;
48 
51  rmw_unique_network_flow_endpoints_requirement_t require_unique_network_flow_endpoints =
52  RMW_UNIQUE_NETWORK_FLOW_ENDPOINTS_NOT_REQUIRED;
53 
55  std::shared_ptr<rclcpp::CallbackGroup> callback_group;
56 
58  std::shared_ptr<rclcpp::detail::RMWImplementationSpecificPublisherPayload>
60 
61  QosOverridingOptions qos_overriding_options;
62 };
63 
65 template<typename Allocator>
67 {
68  static_assert(
69  std::is_void_v<typename std::allocator_traits<Allocator>::value_type>,
70  "Publisher allocator value type must be void");
71 
73  std::shared_ptr<Allocator> allocator = nullptr;
74 
76 
78  explicit PublisherOptionsWithAllocator(const PublisherOptionsBase & publisher_options_base)
79  : PublisherOptionsBase(publisher_options_base)
80  {}
81 
83  template<typename MessageT>
86  {
88  result.allocator = this->get_rcl_allocator();
89  result.qos = qos.get_rmw_qos_profile();
90  result.rmw_publisher_options.require_unique_network_flow_endpoints =
92 
93  // Apply payload to rcl_publisher_options if necessary.
94  if (rmw_implementation_payload && rmw_implementation_payload->has_been_customized()) {
95  rmw_implementation_payload->modify_rmw_publisher_options(result.rmw_publisher_options);
96  }
97 
98  return result;
99  }
100 
101 
103  std::shared_ptr<Allocator>
105  {
106  if (!this->allocator) {
107  if (!allocator_storage_) {
108  allocator_storage_ = std::make_shared<Allocator>();
109  }
110  return allocator_storage_;
111  }
112  return this->allocator;
113  }
114 
115 private:
116  using PlainAllocator =
117  typename std::allocator_traits<Allocator>::template rebind_alloc<char>;
118 
120  get_rcl_allocator() const
121  {
122  if (!plain_allocator_storage_) {
123  plain_allocator_storage_ =
124  std::make_shared<PlainAllocator>(*this->get_allocator());
125  }
126  return rclcpp::allocator::get_rcl_allocator<char>(*plain_allocator_storage_);
127  }
128 
129  // This is a temporal workaround, to make sure that get_allocator()
130  // always returns a copy of the same allocator.
131  mutable std::shared_ptr<Allocator> allocator_storage_;
132 
133  // This is a temporal workaround, to keep the plain allocator that backs
134  // up the rcl allocator returned in rcl_publisher_options_t alive.
135  mutable std::shared_ptr<PlainAllocator> plain_allocator_storage_;
136 };
137 
138 using PublisherOptions = PublisherOptionsWithAllocator<std::allocator<void>>;
139 
140 } // namespace rclcpp
141 
142 #endif // RCLCPP__PUBLISHER_OPTIONS_HPP_
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
Definition: allocator.h:31
Encapsulation of Quality of Service settings.
Definition: qos.hpp:111
rmw_qos_profile_t & get_rmw_qos_profile()
Return the rmw qos profile.
Definition: qos.cpp:88
Options that are passed in subscription/publisher constructor to specify QoSConfigurability.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
IntraProcessSetting
Used as argument in create_publisher and create_subscriber.
@ NodeDefault
Take intraprocess configuration from the node.
RCL_PUBLIC RCL_WARN_UNUSED rcl_publisher_options_t rcl_publisher_get_default_options(void)
Return the default publisher options in a rcl_publisher_options_t.
Definition: publisher.c:198
Options available for a rcl publisher.
Definition: publisher.h:44
rmw_qos_profile_t qos
Middleware quality of service settings for the publisher.
Definition: publisher.h:46
rmw_publisher_options_t rmw_publisher_options
rmw specific publisher options, e.g. the rmw implementation specific payload.
Definition: publisher.h:51
rcl_allocator_t allocator
Custom allocator for the publisher, used for incidental allocations.
Definition: publisher.h:49
Contains callbacks for various types of events a Publisher can receive from the middleware.
Definition: qos_event.hpp:59
Non-templated part of PublisherOptionsWithAllocator<Allocator>.
rmw_unique_network_flow_endpoints_requirement_t require_unique_network_flow_endpoints
PublisherEventCallbacks event_callbacks
Callbacks for various events related to publishers.
bool use_default_callbacks
Whether or not to use default callbacks when user doesn't supply any in event_callbacks.
std::shared_ptr< rclcpp::detail::RMWImplementationSpecificPublisherPayload > rmw_implementation_payload
Optional RMW implementation specific payload to be used during creation of the publisher.
IntraProcessSetting use_intra_process_comm
Setting to explicitly set intraprocess communications.
std::shared_ptr< rclcpp::CallbackGroup > callback_group
Callback group in which the waitable items from the publisher should be placed.
Structure containing optional configuration for Publishers.
PublisherOptionsWithAllocator(const PublisherOptionsBase &publisher_options_base)
Constructor using base class as input.
std::shared_ptr< Allocator > allocator
Optional custom allocator.
std::shared_ptr< Allocator > get_allocator() const
Get the allocator, creating one if needed.
rcl_publisher_options_t to_rcl_publisher_options(const rclcpp::QoS &qos) const
Convert this class, and a rclcpp::QoS, into an rcl_publisher_options_t.