ROS 2 rclcpp + rcl - kilted  kilted
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  [[maybe_unused]] int argc, char const * const * argv,
35  rcl_arguments_t * arguments,
36  rcl_allocator_t allocator)
37 {
38  std::vector<std::string> unparsed_ros_arguments;
39  int unparsed_ros_args_count = rcl_arguments_get_count_unparsed_ros(arguments);
40  if (unparsed_ros_args_count > 0) {
41  int * unparsed_ros_args_indices = nullptr;
42  rcl_ret_t ret =
43  rcl_arguments_get_unparsed_ros(arguments, allocator, &unparsed_ros_args_indices);
44  if (RCL_RET_OK != ret) {
45  rclcpp::exceptions::throw_from_rcl_error(ret, "failed to get unparsed ROS arguments");
46  }
47  try {
48  for (int i = 0; i < unparsed_ros_args_count; ++i) {
49  assert(unparsed_ros_args_indices[i] >= 0);
50  assert(unparsed_ros_args_indices[i] < argc);
51  unparsed_ros_arguments.push_back(argv[unparsed_ros_args_indices[i]]);
52  }
53  allocator.deallocate(unparsed_ros_args_indices, allocator.state);
54  } catch (...) {
55  allocator.deallocate(unparsed_ros_args_indices, allocator.state);
56  throw;
57  }
58  }
59  return unparsed_ros_arguments;
60 }
61 
62 } // namespace detail
63 } // 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:27
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:24