ROS 2 rclcpp + rcl - humble  humble
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/qos_event.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 
82  TopicStatisticsOptions topic_stats_options;
83 
84  QosOverridingOptions qos_overriding_options;
85 
86  ContentFilterOptions content_filter_options;
87 };
88 
90 template<typename Allocator>
92 {
93  static_assert(
94  std::is_void_v<typename std::allocator_traits<Allocator>::value_type>,
95  "Subscription allocator value type must be void");
96 
98  std::shared_ptr<Allocator> allocator = nullptr;
99 
101 
104  const SubscriptionOptionsBase & subscription_options_base)
105  : SubscriptionOptionsBase(subscription_options_base)
106  {}
107 
109 
113  template<typename MessageT>
116  {
118  result.allocator = this->get_rcl_allocator();
119  result.qos = qos.get_rmw_qos_profile();
120  result.rmw_subscription_options.ignore_local_publications = this->ignore_local_publications;
121  result.rmw_subscription_options.require_unique_network_flow_endpoints =
123 
124  // Apply payload to rcl_subscription_options if necessary.
125  if (rmw_implementation_payload && rmw_implementation_payload->has_been_customized()) {
126  rmw_implementation_payload->modify_rmw_subscription_options(result.rmw_subscription_options);
127  }
128 
129  // Copy content_filter_options into rcl_subscription_options.
130  if (!content_filter_options.filter_expression.empty()) {
131  std::vector<const char *> cstrings =
132  get_c_vector_string(content_filter_options.expression_parameters);
134  get_c_string(content_filter_options.filter_expression),
135  cstrings.size(),
136  cstrings.data(),
137  &result);
138  if (RCL_RET_OK != ret) {
139  rclcpp::exceptions::throw_from_rcl_error(
140  ret, "failed to set content_filter_options");
141  }
142  }
143 
144  return result;
145  }
146 
147  std::shared_ptr<Allocator>
148  get_allocator() const
149  {
150  if (!this->allocator) {
151  if (!allocator_storage_) {
152  allocator_storage_ = std::make_shared<Allocator>();
153  }
154  return allocator_storage_;
155  }
156  return this->allocator;
157  }
158 
159 private:
160  using PlainAllocator =
161  typename std::allocator_traits<Allocator>::template rebind_alloc<char>;
162 
164  get_rcl_allocator() const
165  {
166  if (!plain_allocator_storage_) {
167  plain_allocator_storage_ =
168  std::make_shared<PlainAllocator>(*this->get_allocator());
169  }
170  return rclcpp::allocator::get_rcl_allocator<char>(*plain_allocator_storage_);
171  }
172 
173  // This is a temporal workaround, to make sure that get_allocator()
174  // always returns a copy of the same allocator.
175  mutable std::shared_ptr<Allocator> allocator_storage_;
176 
177  // This is a temporal workaround, to keep the plain allocator that backs
178  // up the rcl allocator returned in rcl_subscription_options_t alive.
179  mutable std::shared_ptr<PlainAllocator> plain_allocator_storage_;
180 };
181 
182 using SubscriptionOptions = SubscriptionOptionsWithAllocator<std::allocator<void>>;
183 } // namespace rclcpp
184 
185 #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: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.
@ 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:46
rcl_allocator_t allocator
Custom allocator for the subscription, used for incidental allocations.
Definition: subscription.h:51
rmw_qos_profile_t qos
Middleware quality of service settings for the subscription.
Definition: subscription.h:48
rmw_subscription_options_t rmw_subscription_options
rmw specific subscription options, e.g. the rmw implementation specific payload.
Definition: subscription.h:53
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.
Definition: qos_event.hpp:67
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.
std::shared_ptr< Allocator > allocator
Optional custom allocator.
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.
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:202
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:236
#define RCL_RET_OK
Success return code.
Definition: types.h:26
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:23