ROS 2 rclcpp + rcl - kilted  kilted
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_buffer_type.hpp"
28 #include "rclcpp/intra_process_setting.hpp"
29 #include "rclcpp/qos.hpp"
30 #include "rclcpp/event_handler.hpp"
31 #include "rclcpp/qos_overriding_options.hpp"
32 
33 namespace rclcpp
34 {
35 
36 class CallbackGroup;
37 
40 {
43 
46 
49 
51  bool use_default_callbacks = true;
52 
55  rmw_unique_network_flow_endpoints_requirement_t require_unique_network_flow_endpoints =
56  RMW_UNIQUE_NETWORK_FLOW_ENDPOINTS_NOT_REQUIRED;
57 
59  std::shared_ptr<rclcpp::CallbackGroup> callback_group;
60 
62  std::shared_ptr<rclcpp::detail::RMWImplementationSpecificPublisherPayload>
64 
65  QosOverridingOptions qos_overriding_options;
66 };
67 
69 template<typename Allocator>
71 {
72  static_assert(
73  std::is_void_v<typename std::allocator_traits<Allocator>::value_type>,
74  "Publisher allocator value type must be void");
75 
77  std::shared_ptr<Allocator> allocator = nullptr;
78 
80 
82  explicit PublisherOptionsWithAllocator(const PublisherOptionsBase & publisher_options_base)
83  : PublisherOptionsBase(publisher_options_base)
84  {}
85 
87  template<typename MessageT>
90  {
92  result.allocator = this->get_rcl_allocator();
93  result.qos = qos.get_rmw_qos_profile();
94  result.rmw_publisher_options.require_unique_network_flow_endpoints =
96 
97  // Apply payload to rcl_publisher_options if necessary.
98  if (rmw_implementation_payload && rmw_implementation_payload->has_been_customized()) {
99  rmw_implementation_payload->modify_rmw_publisher_options(result.rmw_publisher_options);
100  }
101 
102  return result;
103  }
104 
105 
107  std::shared_ptr<Allocator>
109  {
110  if (!this->allocator) {
111  if (!allocator_storage_) {
112  allocator_storage_ = std::make_shared<Allocator>();
113  }
114  return allocator_storage_;
115  }
116  return this->allocator;
117  }
118 
119 private:
120  using PlainAllocator =
121  typename std::allocator_traits<Allocator>::template rebind_alloc<char>;
122 
124  get_rcl_allocator() const
125  {
126  if (!plain_allocator_storage_) {
127  plain_allocator_storage_ =
128  std::make_shared<PlainAllocator>(*this->get_allocator());
129  }
130  return rclcpp::allocator::get_rcl_allocator<char>(*plain_allocator_storage_);
131  }
132 
133  // This is a temporal workaround, to make sure that get_allocator()
134  // always returns a copy of the same allocator.
135  mutable std::shared_ptr<Allocator> allocator_storage_;
136 
137  // This is a temporal workaround, to keep the plain allocator that backs
138  // up the rcl allocator returned in rcl_publisher_options_t alive.
139  mutable std::shared_ptr<PlainAllocator> plain_allocator_storage_;
140 };
141 
142 using PublisherOptions = PublisherOptionsWithAllocator<std::allocator<void>>;
143 
144 } // namespace rclcpp
145 
146 #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:116
rmw_qos_profile_t & get_rmw_qos_profile()
Return the rmw qos profile.
Definition: qos.cpp:102
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.
@ SharedPtr
Set the data type used in the intra-process buffer as std::shared_ptr<MessageT>
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:220
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.
Non-templated part of PublisherOptionsWithAllocator<Allocator>.
rmw_unique_network_flow_endpoints_requirement_t require_unique_network_flow_endpoints
IntraProcessBufferType intra_process_buffer_type
Setting the data-type stored in the intraprocess buffer.
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.