15 #include "rclcpp/utilities.hpp"
22 #include "./signal_handler.hpp"
23 #include "rclcpp/contexts/default_context.hpp"
24 #include "rclcpp/detail/utilities.hpp"
25 #include "rclcpp/exceptions.hpp"
27 #include "rcl/error_handling.h"
36 char const *
const * argv,
40 using rclcpp::contexts::get_global_default_context;
41 get_global_default_context()->init(argc, argv, init_options);
72 std::vector<std::string>
73 _remove_ros_arguments(
74 char const *
const * argv,
80 const char ** nonros_argv = NULL;
91 exceptions::RCLError exc(ret, rcl_get_error_state(),
"");
93 if (NULL != nonros_argv) {
94 alloc.deallocate(nonros_argv, alloc.state);
99 std::vector<std::string> return_arguments(
static_cast<size_t>(nonros_argc));
101 for (
size_t ii = 0; ii < static_cast<size_t>(nonros_argc); ++ii) {
102 return_arguments[ii] = std::string(nonros_argv[ii]);
105 if (NULL != nonros_argv) {
106 alloc.deallocate(nonros_argv, alloc.state);
109 return return_arguments;
112 std::vector<std::string>
115 char const *
const * argv,
118 init(argc, argv, init_options);
120 using rclcpp::contexts::get_global_default_context;
121 auto rcl_context = get_global_default_context()->get_rcl_context();
125 std::vector<std::string>
135 exceptions::throw_from_rcl_error(ret,
"failed to parse arguments");
138 std::vector<std::string> return_arguments;
140 return_arguments = _remove_ros_arguments(argv, &parsed_args, alloc);
143 exc.formatted_message += std::string(
144 ", failed also to cleanup parsed arguments, leaking memory: ") +
145 rcl_get_error_string().str;
153 exceptions::throw_from_rcl_error(
154 ret,
"failed to cleanup parsed arguments, leaking memory");
157 return return_arguments;
161 ok(Context::SharedPtr context)
163 using rclcpp::contexts::get_global_default_context;
164 if (
nullptr == context) {
165 context = get_global_default_context();
167 return context->is_valid();
171 shutdown(Context::SharedPtr context,
const std::string & reason)
173 using rclcpp::contexts::get_global_default_context;
174 auto default_context = get_global_default_context();
175 if (
nullptr == context) {
176 context = default_context;
178 bool ret = context->shutdown(reason);
179 if (context == default_context) {
186 on_shutdown(std::function<
void()> callback, Context::SharedPtr context)
188 using rclcpp::contexts::get_global_default_context;
189 if (
nullptr == context) {
190 context = get_global_default_context();
192 context->on_shutdown(callback);
196 sleep_for(
const std::chrono::nanoseconds & nanoseconds, Context::SharedPtr context)
198 using rclcpp::contexts::get_global_default_context;
199 if (
nullptr == context) {
200 context = get_global_default_context();
202 return context->sleep_for(nanoseconds);
214 return string_in.c_str();
217 std::vector<const char *>
220 std::vector<const char *> cstrings;
221 cstrings.reserve(strings_in.size());
223 for (
size_t i = 0; i < strings_in.size(); ++i) {
224 cstrings.push_back(strings_in[i].c_str());
#define rcl_get_default_allocator
Return a properly initialized rcl_allocator_t with default values.
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
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.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_arguments_fini(rcl_arguments_t *args)
Reclaim resources held inside rcl_arguments_t structure.
RCL_PUBLIC RCL_WARN_UNUSED rcl_arguments_t rcl_get_zero_initialized_arguments(void)
Return a rcl_arguments_t struct with members initialized to NULL.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_remove_ros_arguments(const char *const *argv, const rcl_arguments_t *args, rcl_allocator_t allocator, int *nonros_argc, const char ***nonros_argv)
Return a list of arguments with ROS-specific arguments removed.
Encapsulation of options for initializing rclcpp.
bool is_installed()
Return true if installed, false otherwise.
static SignalHandler & get_global_signal_handler()
Return the global singleton of this class.
bool install(SignalHandlerOptions signal_handler_options=SignalHandlerOptions::All)
Install the signal handler for SIGINT/SIGTERM and start the dedicated signal handling thread.
rclcpp::SignalHandlerOptions get_current_signal_handler_options()
Get the current signal handler options.
Created when the return code does not match one of the other specialized exceptions.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC void init(int argc, char const *const *argv, const InitOptions &init_options=InitOptions(), SignalHandlerOptions signal_handler_options=SignalHandlerOptions::All)
Initialize communications via the rmw implementation and set up a global signal handler.
SignalHandlerOptions
Option to indicate which signal handlers rclcpp should install.
RCLCPP_PUBLIC bool signal_handlers_installed()
Return true if the signal handlers are installed, otherwise false.
RCLCPP_PUBLIC void on_shutdown(std::function< void()> callback, rclcpp::Context::SharedPtr context=nullptr)
Register a function to be called when shutdown is called on the context.
RCLCPP_PUBLIC bool sleep_for(const std::chrono::nanoseconds &nanoseconds, rclcpp::Context::SharedPtr context=nullptr)
Use the global condition variable to block for the specified amount of time.
RCLCPP_PUBLIC std::vector< std::string > remove_ros_arguments(int argc, char const *const *argv)
Remove ROS-specific arguments from argument vector.
RCLCPP_PUBLIC std::vector< std::string > init_and_remove_ros_arguments(int argc, char const *const *argv, const InitOptions &init_options=InitOptions())
Initialize communications via the rmw implementation and set up a global signal handler.
RCLCPP_PUBLIC std::vector< const char * > get_c_vector_string(const std::vector< std::string > &strings_in)
Return the std::vector of C string from the given std::vector<std::string>.
RCLCPP_PUBLIC bool install_signal_handlers(SignalHandlerOptions signal_handler_options=SignalHandlerOptions::All)
Install the global signal handler for rclcpp.
RCLCPP_PUBLIC bool shutdown(rclcpp::Context::SharedPtr context=nullptr, const std::string &reason="user called rclcpp::shutdown()")
Shutdown rclcpp context, invalidating it for derived entities.
RCLCPP_PUBLIC const char * get_c_string(const char *string_in)
Return the given string.
RCLCPP_PUBLIC bool ok(rclcpp::Context::SharedPtr context=nullptr)
Check rclcpp's status.
RCLCPP_PUBLIC SignalHandlerOptions get_current_signal_handler_options()
Get the current signal handler options.
RCLCPP_PUBLIC bool uninstall_signal_handlers()
Uninstall the global signal handler for rclcpp.
Hold output of parsing command line arguments.
#define RCL_RET_OK
Success return code.
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.