ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
node_options.cpp
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 #include "rclcpp/node_options.hpp"
16 
17 #include <limits>
18 #include <memory>
19 #include <string>
20 #include <vector>
21 #include <utility>
22 
23 #include "rclcpp/detail/utilities.hpp"
24 #include "rclcpp/exceptions.hpp"
25 #include "rclcpp/logging.hpp"
26 #include "rclcpp/publisher_options.hpp"
27 #include "rclcpp/qos.hpp"
28 
29 using rclcpp::exceptions::throw_from_rcl_error;
30 
31 namespace rclcpp
32 {
33 
34 namespace detail
35 {
36 static
37 void
38 rcl_node_options_t_destructor(rcl_node_options_t * node_options)
39 {
40  if (node_options) {
41  rcl_ret_t ret = rcl_node_options_fini(node_options);
42  if (RCL_RET_OK != ret) {
43  // Cannot throw here, as it may be called in the destructor.
44  RCLCPP_ERROR(
45  rclcpp::get_logger("rclcpp"),
46  "failed to finalize rcl node options: %s", rcl_get_error_string().str);
47  rcl_reset_error();
48  }
49 
50  delete node_options;
51  node_options = nullptr;
52  }
53 }
54 } // namespace detail
55 
57 : node_options_(nullptr, detail::rcl_node_options_t_destructor), allocator_(allocator)
58 {}
59 
61 : node_options_(nullptr, detail::rcl_node_options_t_destructor)
62 {
63  *this = other;
64 }
65 
68 {
69  if (this != &other) {
70  this->node_options_.reset();
71  this->context_ = other.context_;
72  this->arguments_ = other.arguments_;
73  this->parameter_overrides_ = other.parameter_overrides_;
74  this->use_global_arguments_ = other.use_global_arguments_;
75  this->enable_rosout_ = other.enable_rosout_;
76  this->use_intra_process_comms_ = other.use_intra_process_comms_;
77  this->enable_topic_statistics_ = other.enable_topic_statistics_;
78  this->start_parameter_services_ = other.start_parameter_services_;
79  this->start_parameter_event_publisher_ = other.start_parameter_event_publisher_;
80  this->clock_qos_ = other.clock_qos_;
81  this->use_clock_thread_ = other.use_clock_thread_;
82  this->parameter_event_qos_ = other.parameter_event_qos_;
83  this->rosout_qos_ = other.rosout_qos_;
84  this->parameter_event_publisher_options_ = other.parameter_event_publisher_options_;
85  this->allow_undeclared_parameters_ = other.allow_undeclared_parameters_;
86  this->automatically_declare_parameters_from_overrides_ =
87  other.automatically_declare_parameters_from_overrides_;
88  this->allocator_ = other.allocator_;
89  }
90  return *this;
91 }
92 
93 const rcl_node_options_t *
95 {
96  // If it is nullptr, create it on demand.
97  if (!node_options_) {
98  node_options_.reset(new rcl_node_options_t);
99  *node_options_ = rcl_node_get_default_options();
100  node_options_->allocator = this->allocator_;
101  node_options_->use_global_arguments = this->use_global_arguments_;
102  node_options_->enable_rosout = this->enable_rosout_;
103  node_options_->rosout_qos = this->rosout_qos_.get_rmw_qos_profile();
104 
105  int c_argc = 0;
106  std::unique_ptr<const char *[]> c_argv;
107  if (!this->arguments_.empty()) {
108  if (this->arguments_.size() > static_cast<size_t>(std::numeric_limits<int>::max())) {
109  throw_from_rcl_error(RCL_RET_INVALID_ARGUMENT, "Too many args");
110  }
111 
112  c_argc = static_cast<int>(this->arguments_.size());
113  c_argv.reset(new const char *[c_argc]);
114 
115  for (std::size_t i = 0; i < this->arguments_.size(); ++i) {
116  c_argv[i] = this->arguments_[i].c_str();
117  }
118  }
119 
121  c_argc, c_argv.get(), this->allocator_, &(node_options_->arguments));
122 
123  if (RCL_RET_OK != ret) {
124  throw_from_rcl_error(ret, "failed to parse arguments");
125  }
126 
127  std::vector<std::string> unparsed_ros_arguments = detail::get_unparsed_ros_arguments(
128  c_argc, c_argv.get(), &(node_options_->arguments), this->allocator_);
129  if (!unparsed_ros_arguments.empty()) {
130  throw exceptions::UnknownROSArgsError(std::move(unparsed_ros_arguments));
131  }
132  }
133 
134  return node_options_.get();
135 }
136 
137 rclcpp::Context::SharedPtr
139 {
140  return this->context_;
141 }
142 
143 NodeOptions &
144 NodeOptions::context(rclcpp::Context::SharedPtr context)
145 {
146  this->context_ = context;
147  return *this;
148 }
149 
150 const std::vector<std::string> &
152 {
153  return this->arguments_;
154 }
155 
156 NodeOptions &
157 NodeOptions::arguments(const std::vector<std::string> & arguments)
158 {
159  this->node_options_.reset(); // reset node options to make it be recreated on next access.
160  this->arguments_ = arguments;
161  return *this;
162 }
163 
164 std::vector<rclcpp::Parameter> &
166 {
167  return this->parameter_overrides_;
168 }
169 
170 const std::vector<rclcpp::Parameter> &
172 {
173  return this->parameter_overrides_;
174 }
175 
176 NodeOptions &
177 NodeOptions::parameter_overrides(const std::vector<rclcpp::Parameter> & parameter_overrides)
178 {
179  this->parameter_overrides_ = parameter_overrides;
180  return *this;
181 }
182 
183 bool
185 {
186  return this->use_global_arguments_;
187 }
188 
189 NodeOptions &
190 NodeOptions::use_global_arguments(bool use_global_arguments)
191 {
192  this->node_options_.reset(); // reset node options to make it be recreated on next access.
193  this->use_global_arguments_ = use_global_arguments;
194  return *this;
195 }
196 
197 bool
199 {
200  return this->enable_rosout_;
201 }
202 
203 NodeOptions &
204 NodeOptions::enable_rosout(bool enable_rosout)
205 {
206  this->node_options_.reset(); // reset node options to make it be recreated on next access.
207  this->enable_rosout_ = enable_rosout;
208  return *this;
209 }
210 
211 bool
213 {
214  return this->use_intra_process_comms_;
215 }
216 
217 NodeOptions &
218 NodeOptions::use_intra_process_comms(bool use_intra_process_comms)
219 {
220  this->use_intra_process_comms_ = use_intra_process_comms;
221  return *this;
222 }
223 
224 bool
226 {
227  return this->enable_topic_statistics_;
228 }
229 
230 NodeOptions &
231 NodeOptions::enable_topic_statistics(bool enable_topic_statistics)
232 {
233  this->enable_topic_statistics_ = enable_topic_statistics;
234  return *this;
235 }
236 
237 bool
239 {
240  return this->start_parameter_services_;
241 }
242 
243 NodeOptions &
244 NodeOptions::start_parameter_services(bool start_parameter_services)
245 {
246  this->start_parameter_services_ = start_parameter_services;
247  return *this;
248 }
249 
250 bool
252 {
253  return this->start_parameter_event_publisher_;
254 }
255 
256 NodeOptions &
257 NodeOptions::start_parameter_event_publisher(bool start_parameter_event_publisher)
258 {
259  this->start_parameter_event_publisher_ = start_parameter_event_publisher;
260  return *this;
261 }
262 
263 const rclcpp::QoS &
265 {
266  return this->clock_qos_;
267 }
268 
269 NodeOptions &
271 {
272  this->clock_qos_ = clock_qos;
273  return *this;
274 }
275 
276 bool
278 {
279  return this->use_clock_thread_;
280 }
281 
282 NodeOptions &
283 NodeOptions::use_clock_thread(bool use_clock_thread)
284 {
285  this->use_clock_thread_ = use_clock_thread;
286  return *this;
287 }
288 
289 const rclcpp::QoS &
291 {
292  return this->parameter_event_qos_;
293 }
294 
295 NodeOptions &
296 NodeOptions::parameter_event_qos(const rclcpp::QoS & parameter_event_qos)
297 {
298  this->parameter_event_qos_ = parameter_event_qos;
299  return *this;
300 }
301 
302 const rclcpp::QoS &
304 {
305  return this->rosout_qos_;
306 }
307 
308 NodeOptions &
310 {
311  this->node_options_.reset();
312  this->rosout_qos_ = rosout_qos;
313  return *this;
314 }
315 
318 {
319  return parameter_event_publisher_options_;
320 }
321 
322 NodeOptions &
324  const rclcpp::PublisherOptionsBase & parameter_event_publisher_options)
325 {
326  parameter_event_publisher_options_ = parameter_event_publisher_options;
327  return *this;
328 }
329 
330 bool
332 {
333  return this->allow_undeclared_parameters_;
334 }
335 
336 NodeOptions &
337 NodeOptions::allow_undeclared_parameters(bool allow_undeclared_parameters)
338 {
339  this->allow_undeclared_parameters_ = allow_undeclared_parameters;
340  return *this;
341 }
342 
343 bool
345 {
346  return this->automatically_declare_parameters_from_overrides_;
347 }
348 
349 NodeOptions &
351  bool automatically_declare_parameters_from_overrides)
352 {
353  this->automatically_declare_parameters_from_overrides_ =
355  return *this;
356 }
357 
358 const rcl_allocator_t &
360 {
361  return this->allocator_;
362 }
363 
364 NodeOptions &
366 {
367  this->node_options_.reset(); // reset node options to make it be recreated on next access.
368  this->allocator_ = allocator;
369  return *this;
370 }
371 
372 } // namespace rclcpp
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
Definition: allocator.h:31
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_parse_arguments(int argc, const char *const *argv, rcl_allocator_t allocator, rcl_arguments_t *args_output)
Parse command line arguments into a structure usable by code.
Encapsulation of options for node initialization.
RCLCPP_PUBLIC bool start_parameter_event_publisher() const
Return the start_parameter_event_publisher flag.
RCLCPP_PUBLIC const std::vector< std::string > & arguments() const
Return a reference to the list of arguments for the node.
RCLCPP_PUBLIC bool use_clock_thread() const
Return the use_clock_thread flag.
RCLCPP_PUBLIC bool use_global_arguments() const
Return the use_global_arguments flag.
RCLCPP_PUBLIC const rclcpp::QoS & parameter_event_qos() const
Return a reference to the parameter_event_qos QoS.
RCLCPP_PUBLIC const rclcpp::QoS & rosout_qos() const
Return a reference to the rosout QoS.
RCLCPP_PUBLIC bool enable_rosout() const
Return the enable_rosout flag.
RCLCPP_PUBLIC const rclcpp::PublisherOptionsBase & parameter_event_publisher_options() const
Return a reference to the parameter_event_publisher_options.
RCLCPP_PUBLIC bool allow_undeclared_parameters() const
Return the allow_undeclared_parameters flag.
RCLCPP_PUBLIC bool automatically_declare_parameters_from_overrides() const
Return the automatically_declare_parameters_from_overrides flag.
RCLCPP_PUBLIC const rclcpp::QoS & clock_qos() const
Return a reference to the clock QoS.
RCLCPP_PUBLIC NodeOptions(rcl_allocator_t allocator=rcl_get_default_allocator())
Create NodeOptions with default values, optionally specifying the allocator to use.
RCLCPP_PUBLIC bool start_parameter_services() const
Return the start_parameter_services flag.
RCLCPP_PUBLIC bool use_intra_process_comms() const
Return the use_intra_process_comms flag.
RCLCPP_PUBLIC std::vector< rclcpp::Parameter > & parameter_overrides()
Return a reference to the list of parameter overrides.
RCLCPP_PUBLIC const rcl_allocator_t & allocator() const
Return the rcl_allocator_t to be used.
RCLCPP_PUBLIC rclcpp::Context::SharedPtr context() const
Return the context to be used by the node.
RCLCPP_PUBLIC bool enable_topic_statistics() const
Return the enable_topic_statistics flag.
RCLCPP_PUBLIC const rcl_node_options_t * get_rcl_node_options() const
Return the rcl_node_options used by the node.
RCLCPP_PUBLIC NodeOptions & operator=(const NodeOptions &other)
Assignment operator.
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
Thrown when unparsed ROS specific arguments are found.
Definition: exceptions.hpp:197
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC Logger get_logger(const std::string &name)
Return a named logger.
Definition: logger.cpp:27
RCL_PUBLIC rcl_node_options_t rcl_node_get_default_options(void)
Return the default node options in a rcl_node_options_t.
Definition: node_options.c:30
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_node_options_fini(rcl_node_options_t *options)
Finalize the given node_options.
Definition: node_options.c:71
Structure which encapsulates the options for creating a rcl_node_t.
Definition: node_options.h:35
Non-templated part of PublisherOptionsWithAllocator<Allocator>.
#define RCL_RET_OK
Success return code.
Definition: types.h:26
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
Definition: types.h:34
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:23