ROS 2 rclcpp + rcl - lyrical  lyrical
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_type_ = other.clock_type_;
81  this->clock_qos_ = other.clock_qos_;
82  this->use_clock_thread_ = other.use_clock_thread_;
83  this->enable_logger_service_ = other.enable_logger_service_;
84  this->log_level_ = other.log_level_;
85  this->parameter_event_qos_ = other.parameter_event_qos_;
86  this->rosout_qos_ = other.rosout_qos_;
87  this->parameter_event_publisher_options_ = other.parameter_event_publisher_options_;
88  this->allow_undeclared_parameters_ = other.allow_undeclared_parameters_;
89  this->automatically_declare_parameters_from_overrides_ =
90  other.automatically_declare_parameters_from_overrides_;
91  this->allocator_ = other.allocator_;
92  }
93  return *this;
94 }
95 
96 const rcl_node_options_t *
98 {
99  // If it is nullptr, create it on demand.
100  if (!node_options_) {
101  node_options_.reset(new rcl_node_options_t);
102  *node_options_ = rcl_node_get_default_options();
103  node_options_->allocator = this->allocator_;
104  node_options_->use_global_arguments = this->use_global_arguments_;
105  node_options_->enable_rosout = this->enable_rosout_;
106  node_options_->rosout_qos = this->rosout_qos_.get_rmw_qos_profile();
107 
108  int c_argc = 0;
109  std::unique_ptr<const char *[]> c_argv;
110  if (!this->arguments_.empty()) {
111  if (this->arguments_.size() > static_cast<size_t>(std::numeric_limits<int>::max())) {
112  throw_from_rcl_error(RCL_RET_INVALID_ARGUMENT, "Too many args");
113  }
114 
115  c_argc = static_cast<int>(this->arguments_.size());
116  c_argv.reset(new const char *[c_argc]);
117 
118  for (std::size_t i = 0; i < this->arguments_.size(); ++i) {
119  c_argv[i] = this->arguments_[i].c_str();
120  }
121  }
122 
124  c_argc, c_argv.get(), this->allocator_, &(node_options_->arguments));
125 
126  if (RCL_RET_OK != ret) {
127  throw_from_rcl_error(ret, "failed to parse arguments");
128  }
129 
130  std::vector<std::string> unparsed_ros_arguments = detail::get_unparsed_ros_arguments(
131  c_argc, c_argv.get(), &(node_options_->arguments), this->allocator_);
132  if (!unparsed_ros_arguments.empty()) {
133  throw exceptions::UnknownROSArgsError(std::move(unparsed_ros_arguments));
134  }
135  }
136 
137  return node_options_.get();
138 }
139 
140 rclcpp::Context::SharedPtr
142 {
143  return this->context_;
144 }
145 
146 NodeOptions &
147 NodeOptions::context(const rclcpp::Context::SharedPtr & context)
148 {
149  this->context_ = context;
150  return *this;
151 }
152 
153 const std::vector<std::string> &
155 {
156  return this->arguments_;
157 }
158 
159 NodeOptions &
160 NodeOptions::arguments(const std::vector<std::string> & arguments)
161 {
162  this->node_options_.reset(); // reset node options to make it be recreated on next access.
163  this->arguments_ = arguments;
164  return *this;
165 }
166 
167 std::vector<rclcpp::Parameter> &
169 {
170  return this->parameter_overrides_;
171 }
172 
173 const std::vector<rclcpp::Parameter> &
175 {
176  return this->parameter_overrides_;
177 }
178 
179 NodeOptions &
180 NodeOptions::parameter_overrides(const std::vector<rclcpp::Parameter> & parameter_overrides)
181 {
182  this->parameter_overrides_ = parameter_overrides;
183  return *this;
184 }
185 
186 bool
188 {
189  return this->use_global_arguments_;
190 }
191 
192 NodeOptions &
193 NodeOptions::use_global_arguments(bool use_global_arguments)
194 {
195  this->node_options_.reset(); // reset node options to make it be recreated on next access.
196  this->use_global_arguments_ = use_global_arguments;
197  return *this;
198 }
199 
200 bool
202 {
203  return this->enable_rosout_;
204 }
205 
206 NodeOptions &
207 NodeOptions::enable_rosout(bool enable_rosout)
208 {
209  this->node_options_.reset(); // reset node options to make it be recreated on next access.
210  this->enable_rosout_ = enable_rosout;
211  return *this;
212 }
213 
214 bool
216 {
217  return this->use_intra_process_comms_;
218 }
219 
220 NodeOptions &
221 NodeOptions::use_intra_process_comms(bool use_intra_process_comms)
222 {
223  this->use_intra_process_comms_ = use_intra_process_comms;
224  return *this;
225 }
226 
227 bool
229 {
230  return this->enable_topic_statistics_;
231 }
232 
233 NodeOptions &
234 NodeOptions::enable_topic_statistics(bool enable_topic_statistics)
235 {
236  this->enable_topic_statistics_ = enable_topic_statistics;
237  return *this;
238 }
239 
240 bool
242 {
243  return this->start_parameter_services_;
244 }
245 
246 NodeOptions &
247 NodeOptions::start_parameter_services(bool start_parameter_services)
248 {
249  this->start_parameter_services_ = start_parameter_services;
250  return *this;
251 }
252 
253 bool
255 {
256  return this->enable_logger_service_;
257 }
258 
259 NodeOptions &
260 NodeOptions::enable_logger_service(bool enable_logger_service)
261 {
262  this->enable_logger_service_ = enable_logger_service;
263  return *this;
264 }
265 
268 {
269  return this->log_level_;
270 }
271 
272 NodeOptions &
274 {
275  this->log_level_ = log_level;
276  return *this;
277 }
278 
279 bool
281 {
282  return this->start_parameter_event_publisher_;
283 }
284 
285 NodeOptions &
286 NodeOptions::start_parameter_event_publisher(bool start_parameter_event_publisher)
287 {
288  this->start_parameter_event_publisher_ = start_parameter_event_publisher;
289  return *this;
290 }
291 
292 const rcl_clock_type_t &
294 {
295  return this->clock_type_;
296 }
297 
298 NodeOptions &
300 {
301  this->clock_type_ = clock_type;
302  return *this;
303 }
304 
305 const rclcpp::QoS &
307 {
308  return this->clock_qos_;
309 }
310 
311 NodeOptions &
313 {
314  this->clock_qos_ = clock_qos;
315  return *this;
316 }
317 
318 bool
320 {
321  return this->use_clock_thread_;
322 }
323 
324 NodeOptions &
325 NodeOptions::use_clock_thread(bool use_clock_thread)
326 {
327  this->use_clock_thread_ = use_clock_thread;
328  return *this;
329 }
330 
331 const rclcpp::QoS &
333 {
334  return this->parameter_event_qos_;
335 }
336 
337 NodeOptions &
338 NodeOptions::parameter_event_qos(const rclcpp::QoS & parameter_event_qos)
339 {
340  this->parameter_event_qos_ = parameter_event_qos;
341  return *this;
342 }
343 
344 const rclcpp::QoS &
346 {
347  return this->rosout_qos_;
348 }
349 
350 NodeOptions &
352 {
353  this->node_options_.reset();
354  this->rosout_qos_ = rosout_qos;
355  return *this;
356 }
357 
360 {
361  return parameter_event_publisher_options_;
362 }
363 
364 NodeOptions &
366  const rclcpp::PublisherOptionsBase & parameter_event_publisher_options)
367 {
368  parameter_event_publisher_options_ = parameter_event_publisher_options;
369  return *this;
370 }
371 
372 bool
374 {
375  return this->allow_undeclared_parameters_;
376 }
377 
378 NodeOptions &
379 NodeOptions::allow_undeclared_parameters(bool allow_undeclared_parameters)
380 {
381  this->allow_undeclared_parameters_ = allow_undeclared_parameters;
382  return *this;
383 }
384 
385 bool
387 {
388  return this->automatically_declare_parameters_from_overrides_;
389 }
390 
391 NodeOptions &
393  bool automatically_declare_parameters_from_overrides)
394 {
395  this->automatically_declare_parameters_from_overrides_ =
397  return *this;
398 }
399 
400 const rcl_allocator_t &
402 {
403  return this->allocator_;
404 }
405 
406 NodeOptions &
408 {
409  this->node_options_.reset(); // reset node options to make it be recreated on next access.
410  this->allocator_ = allocator;
411  return *this;
412 }
413 
414 } // 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.
Level
An enum for the type of logger level.
Definition: logger.hpp:98
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 NodeOptions & enable_logger_service(bool enable_log_service)
Set the enable_logger_service flag, return this for logger idiom.
RCLCPP_PUBLIC rclcpp::Logger::Level log_level() const
Return the log_level option.
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 enable_logger_service() const
Return the enable_logger_service flag.
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 const rcl_clock_type_t & clock_type() const
Return a reference to the clock type.
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:116
rmw_qos_profile_t & get_rmw_qos_profile()
Return the rmw qos profile.
Definition: qos.cpp:108
Thrown when unparsed ROS specific arguments are found.
Definition: exceptions.hpp:206
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:34
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:32
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:73
Structure which encapsulates the options for creating a rcl_node_t.
Definition: node_options.h:35
Non-templated part of PublisherOptionsWithAllocator<Allocator>.
enum rcl_clock_type_e rcl_clock_type_t
Time source type, used to indicate the source of a time measurement.
#define RCL_RET_OK
Success return code.
Definition: types.h:27
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
Definition: types.h:35
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:24