ROS 2 rclcpp + rcl - jazzy  jazzy
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->parameter_event_qos_ = other.parameter_event_qos_;
85  this->rosout_qos_ = other.rosout_qos_;
86  this->parameter_event_publisher_options_ = other.parameter_event_publisher_options_;
87  this->allow_undeclared_parameters_ = other.allow_undeclared_parameters_;
88  this->automatically_declare_parameters_from_overrides_ =
89  other.automatically_declare_parameters_from_overrides_;
90  this->allocator_ = other.allocator_;
91  }
92  return *this;
93 }
94 
95 const rcl_node_options_t *
97 {
98  // If it is nullptr, create it on demand.
99  if (!node_options_) {
100  node_options_.reset(new rcl_node_options_t);
101  *node_options_ = rcl_node_get_default_options();
102  node_options_->allocator = this->allocator_;
103  node_options_->use_global_arguments = this->use_global_arguments_;
104  node_options_->enable_rosout = this->enable_rosout_;
105  node_options_->rosout_qos = this->rosout_qos_.get_rmw_qos_profile();
106 
107  int c_argc = 0;
108  std::unique_ptr<const char *[]> c_argv;
109  if (!this->arguments_.empty()) {
110  if (this->arguments_.size() > static_cast<size_t>(std::numeric_limits<int>::max())) {
111  throw_from_rcl_error(RCL_RET_INVALID_ARGUMENT, "Too many args");
112  }
113 
114  c_argc = static_cast<int>(this->arguments_.size());
115  c_argv.reset(new const char *[c_argc]);
116 
117  for (std::size_t i = 0; i < this->arguments_.size(); ++i) {
118  c_argv[i] = this->arguments_[i].c_str();
119  }
120  }
121 
123  c_argc, c_argv.get(), this->allocator_, &(node_options_->arguments));
124 
125  if (RCL_RET_OK != ret) {
126  throw_from_rcl_error(ret, "failed to parse arguments");
127  }
128 
129  std::vector<std::string> unparsed_ros_arguments = detail::get_unparsed_ros_arguments(
130  c_argc, c_argv.get(), &(node_options_->arguments), this->allocator_);
131  if (!unparsed_ros_arguments.empty()) {
132  throw exceptions::UnknownROSArgsError(std::move(unparsed_ros_arguments));
133  }
134  }
135 
136  return node_options_.get();
137 }
138 
139 rclcpp::Context::SharedPtr
141 {
142  return this->context_;
143 }
144 
145 NodeOptions &
146 NodeOptions::context(rclcpp::Context::SharedPtr context)
147 {
148  this->context_ = context;
149  return *this;
150 }
151 
152 const std::vector<std::string> &
154 {
155  return this->arguments_;
156 }
157 
158 NodeOptions &
159 NodeOptions::arguments(const std::vector<std::string> & arguments)
160 {
161  this->node_options_.reset(); // reset node options to make it be recreated on next access.
162  this->arguments_ = arguments;
163  return *this;
164 }
165 
166 std::vector<rclcpp::Parameter> &
168 {
169  return this->parameter_overrides_;
170 }
171 
172 const std::vector<rclcpp::Parameter> &
174 {
175  return this->parameter_overrides_;
176 }
177 
178 NodeOptions &
179 NodeOptions::parameter_overrides(const std::vector<rclcpp::Parameter> & parameter_overrides)
180 {
181  this->parameter_overrides_ = parameter_overrides;
182  return *this;
183 }
184 
185 bool
187 {
188  return this->use_global_arguments_;
189 }
190 
191 NodeOptions &
192 NodeOptions::use_global_arguments(bool use_global_arguments)
193 {
194  this->node_options_.reset(); // reset node options to make it be recreated on next access.
195  this->use_global_arguments_ = use_global_arguments;
196  return *this;
197 }
198 
199 bool
201 {
202  return this->enable_rosout_;
203 }
204 
205 NodeOptions &
206 NodeOptions::enable_rosout(bool enable_rosout)
207 {
208  this->node_options_.reset(); // reset node options to make it be recreated on next access.
209  this->enable_rosout_ = enable_rosout;
210  return *this;
211 }
212 
213 bool
215 {
216  return this->use_intra_process_comms_;
217 }
218 
219 NodeOptions &
220 NodeOptions::use_intra_process_comms(bool use_intra_process_comms)
221 {
222  this->use_intra_process_comms_ = use_intra_process_comms;
223  return *this;
224 }
225 
226 bool
228 {
229  return this->enable_topic_statistics_;
230 }
231 
232 NodeOptions &
233 NodeOptions::enable_topic_statistics(bool enable_topic_statistics)
234 {
235  this->enable_topic_statistics_ = enable_topic_statistics;
236  return *this;
237 }
238 
239 bool
241 {
242  return this->start_parameter_services_;
243 }
244 
245 NodeOptions &
246 NodeOptions::start_parameter_services(bool start_parameter_services)
247 {
248  this->start_parameter_services_ = start_parameter_services;
249  return *this;
250 }
251 
252 bool
254 {
255  return this->enable_logger_service_;
256 }
257 
258 NodeOptions &
259 NodeOptions::enable_logger_service(bool enable_logger_service)
260 {
261  this->enable_logger_service_ = enable_logger_service;
262  return *this;
263 }
264 
265 bool
267 {
268  return this->start_parameter_event_publisher_;
269 }
270 
271 NodeOptions &
272 NodeOptions::start_parameter_event_publisher(bool start_parameter_event_publisher)
273 {
274  this->start_parameter_event_publisher_ = start_parameter_event_publisher;
275  return *this;
276 }
277 
278 const rcl_clock_type_t &
280 {
281  return this->clock_type_;
282 }
283 
284 NodeOptions &
286 {
287  this->clock_type_ = clock_type;
288  return *this;
289 }
290 
291 const rclcpp::QoS &
293 {
294  return this->clock_qos_;
295 }
296 
297 NodeOptions &
299 {
300  this->clock_qos_ = clock_qos;
301  return *this;
302 }
303 
304 bool
306 {
307  return this->use_clock_thread_;
308 }
309 
310 NodeOptions &
311 NodeOptions::use_clock_thread(bool use_clock_thread)
312 {
313  this->use_clock_thread_ = use_clock_thread;
314  return *this;
315 }
316 
317 const rclcpp::QoS &
319 {
320  return this->parameter_event_qos_;
321 }
322 
323 NodeOptions &
324 NodeOptions::parameter_event_qos(const rclcpp::QoS & parameter_event_qos)
325 {
326  this->parameter_event_qos_ = parameter_event_qos;
327  return *this;
328 }
329 
330 const rclcpp::QoS &
332 {
333  return this->rosout_qos_;
334 }
335 
336 NodeOptions &
338 {
339  this->node_options_.reset();
340  this->rosout_qos_ = rosout_qos;
341  return *this;
342 }
343 
346 {
347  return parameter_event_publisher_options_;
348 }
349 
350 NodeOptions &
352  const rclcpp::PublisherOptionsBase & parameter_event_publisher_options)
353 {
354  parameter_event_publisher_options_ = parameter_event_publisher_options;
355  return *this;
356 }
357 
358 bool
360 {
361  return this->allow_undeclared_parameters_;
362 }
363 
364 NodeOptions &
365 NodeOptions::allow_undeclared_parameters(bool allow_undeclared_parameters)
366 {
367  this->allow_undeclared_parameters_ = allow_undeclared_parameters;
368  return *this;
369 }
370 
371 bool
373 {
374  return this->automatically_declare_parameters_from_overrides_;
375 }
376 
377 NodeOptions &
379  bool automatically_declare_parameters_from_overrides)
380 {
381  this->automatically_declare_parameters_from_overrides_ =
383  return *this;
384 }
385 
386 const rcl_allocator_t &
388 {
389  return this->allocator_;
390 }
391 
392 NodeOptions &
394 {
395  this->node_options_.reset(); // reset node options to make it be recreated on next access.
396  this->allocator_ = allocator;
397  return *this;
398 }
399 
400 } // 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 NodeOptions & enable_logger_service(bool enable_log_service)
Set the enable_logger_service flag, return this for logger idiom.
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:102
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:33
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>.
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