ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
utilities.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/detail/utilities.hpp"
16 
17 #include <cassert>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 #include "rclcpp/exceptions.hpp"
23 
24 #include "rcl/allocator.h"
25 #include "rcl/arguments.h"
26 
27 namespace rclcpp
28 {
29 namespace detail
30 {
31 
32 std::vector<std::string>
33 get_unparsed_ros_arguments(
34  int argc, char const * const * argv,
35  rcl_arguments_t * arguments,
36  rcl_allocator_t allocator)
37 {
38  (void)argc;
39  std::vector<std::string> unparsed_ros_arguments;
40  int unparsed_ros_args_count = rcl_arguments_get_count_unparsed_ros(arguments);
41  if (unparsed_ros_args_count > 0) {
42  int * unparsed_ros_args_indices = nullptr;
43  rcl_ret_t ret =
44  rcl_arguments_get_unparsed_ros(arguments, allocator, &unparsed_ros_args_indices);
45  if (RCL_RET_OK != ret) {
46  rclcpp::exceptions::throw_from_rcl_error(ret, "failed to get unparsed ROS arguments");
47  }
48  try {
49  for (int i = 0; i < unparsed_ros_args_count; ++i) {
50  assert(unparsed_ros_args_indices[i] >= 0);
51  assert(unparsed_ros_args_indices[i] < argc);
52  unparsed_ros_arguments.push_back(argv[unparsed_ros_args_indices[i]]);
53  }
54  allocator.deallocate(unparsed_ros_args_indices, allocator.state);
55  } catch (...) {
56  allocator.deallocate(unparsed_ros_args_indices, allocator.state);
57  throw;
58  }
59  }
60  return unparsed_ros_arguments;
61 }
62 
63 } // namespace detail
64 } // 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_arguments_get_unparsed_ros(const rcl_arguments_t *args, rcl_allocator_t allocator, int **output_unparsed_ros_indices)
Return a list of indices to unknown ROS specific arguments that were left unparsed.
RCL_PUBLIC RCL_WARN_UNUSED int rcl_arguments_get_count_unparsed_ros(const rcl_arguments_t *args)
Return the number of ROS specific arguments that were not successfully parsed.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Hold output of parsing command line arguments.
Definition: arguments.h:36
#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