ROS 2 rclcpp + rcl - rolling  rolling-e7363f10
ROS 2 C++ Client Library with ROS Client Library
subscription_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__SUBSCRIPTION_OPTIONS_HPP_
16 #define RCLCPP__SUBSCRIPTION_OPTIONS_HPP_
17 
18 #include <memory>
19 #include <string>
20 #include <type_traits>
21 #include <vector>
22 
23 #include "rclcpp/callback_group.hpp"
24 #include "rclcpp/detail/rmw_implementation_specific_subscription_payload.hpp"
25 #include "rclcpp/intra_process_buffer_type.hpp"
26 #include "rclcpp/intra_process_setting.hpp"
27 #include "rclcpp/qos.hpp"
28 #include "rclcpp/event_handler.hpp"
29 #include "rclcpp/qos_overriding_options.hpp"
30 #include "rclcpp/subscription_content_filter_options.hpp"
31 
32 namespace rclcpp
33 {
34 
37 {
40 
42  bool use_default_callbacks = true;
43 
46 
49  rmw_unique_network_flow_endpoints_requirement_t require_unique_network_flow_endpoints =
50  RMW_UNIQUE_NETWORK_FLOW_ENDPOINTS_NOT_REQUIRED;
51 
53  rclcpp::CallbackGroup::SharedPtr callback_group = nullptr;
54 
57 
60 
62  std::shared_ptr<rclcpp::detail::RMWImplementationSpecificSubscriptionPayload>
64 
65  QosOverridingOptions qos_overriding_options;
66 
67  ContentFilterOptions content_filter_options;
68 
70 
76  std::string acceptable_buffer_backends{"cpu"};
77 };
78 
80 template<typename Allocator>
82 {
83  static_assert(
84  std::is_void_v<typename std::allocator_traits<Allocator>::value_type>,
85  "Subscription allocator value type must be void");
86 
88  std::shared_ptr<Allocator> allocator = nullptr;
89 
91 
94  const SubscriptionOptionsBase & subscription_options_base)
95  : SubscriptionOptionsBase(subscription_options_base)
96  {}
97 
99 
105  {
107  result.allocator = this->get_rcl_allocator();
108  result.qos = qos.get_rmw_qos_profile();
109  result.rmw_subscription_options.ignore_local_publications = this->ignore_local_publications;
110  result.rmw_subscription_options.require_unique_network_flow_endpoints =
112 
113  // Apply payload to rcl_subscription_options if necessary.
114  if (rmw_implementation_payload && rmw_implementation_payload->has_been_customized()) {
115  rmw_implementation_payload->modify_rmw_subscription_options(result.rmw_subscription_options);
116  }
117 
118  // Copy content_filter_options into rcl_subscription_options.
119  if (!content_filter_options.filter_expression.empty()) {
120  std::vector<const char *> cstrings =
121  get_c_vector_string(content_filter_options.expression_parameters);
123  get_c_string(content_filter_options.filter_expression),
124  cstrings.size(),
125  cstrings.data(),
126  &result);
127  if (RCL_RET_OK != ret) {
128  rclcpp::exceptions::throw_from_rcl_error(
129  ret, "failed to set content_filter_options");
130  }
131  }
132 
133  if (!acceptable_buffer_backends.empty()) {
136  &result);
137  if (RCL_RET_OK != ret) {
138  rclcpp::exceptions::throw_from_rcl_error(
139  ret, "failed to set acceptable_buffer_backends");
140  }
141  }
142 
143  return result;
144  }
145 
146  std::shared_ptr<Allocator>
147  get_allocator() const
148  {
149  if (!this->allocator) {
150  if (!allocator_storage_) {
151  allocator_storage_ = std::make_shared<Allocator>();
152  }
153  return allocator_storage_;
154  }
155  return this->allocator;
156  }
157 
158 private:
159  using PlainAllocator =
160  typename std::allocator_traits<Allocator>::template rebind_alloc<char>;
161 
163  get_rcl_allocator() const
164  {
165  if constexpr (std::is_same_v<Allocator, std::allocator<void>>) {
166  return rcl_get_default_allocator();
167  } else {
168  if constexpr (rclcpp::allocator::has_get_rcl_allocator_v<Allocator>) {
169  return get_allocator()->get_rcl_allocator();
170  } else {
171  if (!plain_allocator_storage_) {
172  plain_allocator_storage_ =
173  std::make_shared<PlainAllocator>(*this->get_allocator());
174  }
175 
176  return rclcpp::allocator::get_rcl_allocator<char>(*plain_allocator_storage_);
177  }
178  }
179  }
180 
181  // This is a temporal workaround, to make sure that get_allocator()
182  // always returns a copy of the same allocator.
183  mutable std::shared_ptr<Allocator> allocator_storage_;
184 
185  // This is a temporal workaround, to keep the plain allocator that backs
186  // up the rcl allocator returned in rcl_subscription_options_t alive.
187  mutable std::shared_ptr<PlainAllocator> plain_allocator_storage_;
188 };
189 
190 using SubscriptionOptions = SubscriptionOptionsWithAllocator<std::allocator<void>>;
191 } // namespace rclcpp
192 
193 #endif // RCLCPP__SUBSCRIPTION_OPTIONS_HPP_
#define rcl_get_default_allocator
Return a properly initialized rcl_allocator_t with default values.
Definition: allocator.h:37
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
Definition: allocator.h:31
Encapsulation of Quality of Service settings.
Definition: qos.hpp:114
rmw_qos_profile_t & get_rmw_qos_profile()
Return the rmw qos profile.
Definition: qos.cpp:109
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.
@ CallbackDefault
Set the data type used in the intra-process buffer as the same used in the callback.
RCLCPP_PUBLIC std::vector< const char * > get_c_vector_string(const std::vector< std::string > &strings_in)
Return the std::vector of C string from the given std::vector<std::string>.
Definition: utilities.cpp:214
RCLCPP_PUBLIC const char * get_c_string(const char *string_in)
Return the given string.
Definition: utilities.cpp:202
Options available for a rcl subscription.
Definition: subscription.h:55
rcl_allocator_t allocator
Custom allocator for the subscription, used for incidental allocations.
Definition: subscription.h:60
rmw_qos_profile_t qos
Middleware quality of service settings for the subscription.
Definition: subscription.h:57
rmw_subscription_options_t rmw_subscription_options
rmw specific subscription options, e.g. the rmw implementation specific payload.
Definition: subscription.h:62
Options to configure content filtered topic in the subscription.
std::string filter_expression
Filter expression is similar to the WHERE part of an SQL clause.
Contains callbacks for non-message events that a Subscription can receive from the middleware.
Non-template base class for subscription options.
IntraProcessSetting use_intra_process_comm
Setting to explicitly set intraprocess communications.
std::shared_ptr< rclcpp::detail::RMWImplementationSpecificSubscriptionPayload > rmw_implementation_payload
Optional RMW implementation specific payload to be used during creation of the subscription.
SubscriptionEventCallbacks event_callbacks
Callbacks for events related to this subscription.
rmw_unique_network_flow_endpoints_requirement_t require_unique_network_flow_endpoints
bool use_default_callbacks
Whether or not to use default callbacks when user doesn't supply any in event_callbacks.
bool ignore_local_publications
True to ignore local publications.
rclcpp::CallbackGroup::SharedPtr callback_group
The callback group for this subscription. NULL to use the default callback group.
IntraProcessBufferType intra_process_buffer_type
Setting the data-type stored in the intraprocess buffer.
std::string acceptable_buffer_backends
Acceptable buffer backend names for this subscription.
Structure containing optional configuration for Subscriptions.
SubscriptionOptionsWithAllocator(const SubscriptionOptionsBase &subscription_options_base)
Constructor using base class as input.
rcl_subscription_options_t to_rcl_subscription_options(const rclcpp::QoS &qos) const
Convert this class, with a rclcpp::QoS, into an rcl_subscription_options_t.
std::shared_ptr< Allocator > allocator
Optional custom allocator.
RCL_PUBLIC RCL_WARN_UNUSED rcl_subscription_options_t rcl_subscription_get_default_options(void)
Return the default subscription options in a rcl_subscription_options_t.
Definition: subscription.c:240
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_subscription_options_set_content_filter_options(const char *filter_expression, size_t expression_parameters_argc, const char *expression_parameter_argv[], rcl_subscription_options_t *options)
Set the content filter options for the given subscription options.
Definition: subscription.c:328
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_subscription_options_set_acceptable_buffer_backends(const char *acceptable_buffer_backends, rcl_subscription_options_t *options)
Set the acceptable buffer backends for the given subscription options.
Definition: subscription.c:298
#define RCL_RET_OK
Success return code.
Definition: types.h:28
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:25