ROS 2 rclcpp + rcl - jazzy  jazzy
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 <chrono>
19 #include <memory>
20 #include <string>
21 #include <type_traits>
22 #include <vector>
23 
24 #include "rclcpp/callback_group.hpp"
25 #include "rclcpp/detail/rmw_implementation_specific_subscription_payload.hpp"
26 #include "rclcpp/intra_process_buffer_type.hpp"
27 #include "rclcpp/intra_process_setting.hpp"
28 #include "rclcpp/qos.hpp"
29 #include "rclcpp/event_handler.hpp"
30 #include "rclcpp/qos_overriding_options.hpp"
31 #include "rclcpp/subscription_content_filter_options.hpp"
32 #include "rclcpp/topic_statistics_state.hpp"
33 #include "rclcpp/visibility_control.hpp"
34 
35 namespace rclcpp
36 {
37 
40 {
43 
45  bool use_default_callbacks = true;
46 
49 
52  rmw_unique_network_flow_endpoints_requirement_t require_unique_network_flow_endpoints =
53  RMW_UNIQUE_NETWORK_FLOW_ENDPOINTS_NOT_REQUIRED;
54 
56  rclcpp::CallbackGroup::SharedPtr callback_group = nullptr;
57 
60 
63 
65  std::shared_ptr<rclcpp::detail::RMWImplementationSpecificSubscriptionPayload>
67 
68  // Options to configure topic statistics collector in the subscription.
70  {
71  // Enable and disable topic statistics calculation and publication. Defaults to disabled.
73 
74  // Topic to which topic statistics get published when enabled. Defaults to /statistics.
75  std::string publish_topic = "/statistics";
76 
77  // Topic statistics publication period in ms. Defaults to one second.
78  // Only values greater than zero are allowed.
79  std::chrono::milliseconds publish_period{std::chrono::seconds(1)};
80 
81  // An optional QoS which can provide topic_statistics with a stable QoS separate from
82  // the subscription's current QoS settings which could be unstable.
83  // Explicitly set the enough depth to avoid missing the statistics messages.
85  };
86 
87  TopicStatisticsOptions topic_stats_options;
88 
89  QosOverridingOptions qos_overriding_options;
90 
91  ContentFilterOptions content_filter_options;
92 };
93 
95 template<typename Allocator>
97 {
98  static_assert(
99  std::is_void_v<typename std::allocator_traits<Allocator>::value_type>,
100  "Subscription allocator value type must be void");
101 
103  std::shared_ptr<Allocator> allocator = nullptr;
104 
106 
109  const SubscriptionOptionsBase & subscription_options_base)
110  : SubscriptionOptionsBase(subscription_options_base)
111  {}
112 
114 
120  {
122  result.allocator = this->get_rcl_allocator();
123  result.qos = qos.get_rmw_qos_profile();
124  result.rmw_subscription_options.ignore_local_publications = this->ignore_local_publications;
125  result.rmw_subscription_options.require_unique_network_flow_endpoints =
127 
128  // Apply payload to rcl_subscription_options if necessary.
129  if (rmw_implementation_payload && rmw_implementation_payload->has_been_customized()) {
130  rmw_implementation_payload->modify_rmw_subscription_options(result.rmw_subscription_options);
131  }
132 
133  // Copy content_filter_options into rcl_subscription_options.
134  if (!content_filter_options.filter_expression.empty()) {
135  std::vector<const char *> cstrings =
136  get_c_vector_string(content_filter_options.expression_parameters);
138  get_c_string(content_filter_options.filter_expression),
139  cstrings.size(),
140  cstrings.data(),
141  &result);
142  if (RCL_RET_OK != ret) {
143  rclcpp::exceptions::throw_from_rcl_error(
144  ret, "failed to set content_filter_options");
145  }
146  }
147 
148  return result;
149  }
150 
151  std::shared_ptr<Allocator>
152  get_allocator() const
153  {
154  if (!this->allocator) {
155  if (!allocator_storage_) {
156  allocator_storage_ = std::make_shared<Allocator>();
157  }
158  return allocator_storage_;
159  }
160  return this->allocator;
161  }
162 
163 private:
164  using PlainAllocator =
165  typename std::allocator_traits<Allocator>::template rebind_alloc<char>;
166 
168  get_rcl_allocator() const
169  {
170  if (!plain_allocator_storage_) {
171  plain_allocator_storage_ =
172  std::make_shared<PlainAllocator>(*this->get_allocator());
173  }
174  return rclcpp::allocator::get_rcl_allocator<char>(*plain_allocator_storage_);
175  }
176 
177  // This is a temporal workaround, to make sure that get_allocator()
178  // always returns a copy of the same allocator.
179  mutable std::shared_ptr<Allocator> allocator_storage_;
180 
181  // This is a temporal workaround, to keep the plain allocator that backs
182  // up the rcl allocator returned in rcl_subscription_options_t alive.
183  mutable std::shared_ptr<PlainAllocator> plain_allocator_storage_;
184 };
185 
186 using SubscriptionOptions = SubscriptionOptionsWithAllocator<std::allocator<void>>;
187 } // namespace rclcpp
188 
189 #endif // RCLCPP__SUBSCRIPTION_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
QoS & keep_last(size_t depth)
Set the history to keep last.
Definition: qos.cpp:128
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.
@ NodeDefault
Take topic statistics state from the node.
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:218
RCLCPP_PUBLIC const char * get_c_string(const char *string_in)
Return the given string.
Definition: utilities.cpp:206
Options available for a rcl subscription.
Definition: subscription.h:47
rcl_allocator_t allocator
Custom allocator for the subscription, used for incidental allocations.
Definition: subscription.h:52
rmw_qos_profile_t qos
Middleware quality of service settings for the subscription.
Definition: subscription.h:49
rmw_subscription_options_t rmw_subscription_options
rmw specific subscription options, e.g. the rmw implementation specific payload.
Definition: subscription.h:54
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.
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:227
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:278
#define RCL_RET_OK
Success return code.
Definition: types.h:27
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:24