10#ifndef NROS_RCLCPP_EXECUTOR_HPP
11#define NROS_RCLCPP_EXECUTOR_HPP
21#include "rclcpp/logging.hpp"
22#include "rclcpp/detail/nros_cpp_config_generated.h"
24#include "rclcpp/detail/nros_cpp_ffi.h"
67 bool valid()
const {
return value_ != NROS_CPP_SHUTDOWN_CALLBACK_HANDLE_INVALID; }
70 nros_cpp_shutdown_callback_handle_t
value()
const {
return value_; }
73 nros_cpp_shutdown_callback_handle_t value_;
134 return create(out, locator, domain_id,
"nros_cpp");
144 const char* session_name) {
146 if (session_name ==
nullptr) {
154 const char* rmw = NROS_ENTRY_RMW;
156 const char* rmw =
nullptr;
159 nros_cpp_init_rmw(rmw, locator, domain_id, session_name,
nullptr, out.storage_);
161 out.initialized_ =
true;
175 uint8_t domain_id = 0,
const char* session_name =
"nros_cpp") {
176 if (session_name ==
nullptr) {
180 nros_cpp_init_rmw(rmw, locator, domain_id, session_name,
nullptr, out.storage_);
182 out.initialized_ =
true;
243 if (timeout_ms < 0) {
244 NROS_RCLCPP_SAY_REFUSED(NROS_RCLCPP_REFUSE_UNBOUNDED_SPIN_RUNTIME);
259 static_assert(::rclcpp::detail::refuse<T>::value, NROS_RCLCPP_REFUSE_UNBOUNDED_SPIN);
284 return Result(nros_cpp_spin_some(storage_, max_ms));
307 template <
typename Future>
319 template <
typename NodePtr>
void add_node(
const NodePtr& node,
bool notify =
true);
325 template <
typename NodePtr,
typename T =
void>
329 static_assert(::rclcpp::detail::refuse<T>::value, NROS_RCLCPP_REFUSE_REMOVE_NODE);
363 return Result(nros_cpp_executor_ping(storage_, timeout_ms));
385 return Result(nros_cpp_executor_get_node_names(storage_, visit, ctx));
396 return Result(nros_cpp_executor_get_topic_names_and_types(storage_, visit, ctx));
403 return Result(nros_cpp_executor_get_service_names_and_types(storage_, visit, ctx));
413 return Result(nros_cpp_executor_count_publishers(storage_, topic_name, out_count));
420 return Result(nros_cpp_executor_count_subscribers(storage_, topic_name, out_count));
428 return Result(nros_cpp_executor_count_clients(storage_, service_name, out_count));
435 return Result(nros_cpp_executor_count_services(storage_, service_name, out_count));
440 nros_cpp_names_and_types_visit_fn visit,
443 return Result(nros_cpp_executor_get_publisher_names_and_types_by_node(
444 storage_, node_name, node_namespace, visit, ctx));
456 const char* node_namespace,
457 nros_cpp_names_and_types_visit_fn visit,
460 return Result(nros_cpp_executor_get_subscription_names_and_types_by_node(
461 storage_, node_name, node_namespace, visit, ctx));
466 nros_cpp_names_and_types_visit_fn visit,
void* ctx) {
468 return Result(nros_cpp_executor_get_service_names_and_types_by_node(
469 storage_, node_name, node_namespace, visit, ctx));
474 nros_cpp_names_and_types_visit_fn visit,
void* ctx) {
476 return Result(nros_cpp_executor_get_client_names_and_types_by_node(
477 storage_, node_name, node_namespace, visit, ctx));
487 nros_cpp_endpoint_info_visit_fn visit,
void* ctx) {
490 nros_cpp_executor_get_publishers_info_by_topic(storage_, topic_name, visit, ctx));
495 nros_cpp_endpoint_info_visit_fn visit,
void* ctx) {
498 nros_cpp_executor_get_subscriptions_info_by_topic(storage_, topic_name, visit, ctx));
531 return Result(nros_cpp_spin(storage_, poll_ms));
555 return Result(nros_cpp_executor_cancel(storage_));
576 return nros_cpp_executor_is_spinning(
const_cast<uint8_t*
>(storage_));
593 return Result(nros_cpp_spin_for(storage_, duration_ms, poll_ms));
597 bool ok()
const {
return initialized_; }
633 nros_cpp_shutdown_callback_handle_t raw = NROS_CPP_SHUTDOWN_CALLBACK_HANDLE_INVALID;
634 nros_cpp_ret_t ret = nros_cpp_add_pre_shutdown_callback(storage_, callback, context, &raw);
651 nros_cpp_shutdown_callback_handle_t raw = NROS_CPP_SHUTDOWN_CALLBACK_HANDLE_INVALID;
652 nros_cpp_ret_t ret = nros_cpp_add_on_shutdown_callback(storage_, callback, context, &raw);
665 if (!initialized_)
return false;
666 return nros_cpp_remove_pre_shutdown_callback(storage_,
handle.value()) == 0;
673 if (!initialized_)
return false;
674 return nros_cpp_remove_on_shutdown_callback(storage_,
handle.value()) == 0;
681 initialized_ =
false;
688 nros_cpp_fini(storage_);
689 initialized_ =
false;
695 for (
unsigned i = 0; i <
sizeof(storage_); ++i) {
696 storage_[i] = other.storage_[i];
697 other.storage_[i] = 0;
699 other.initialized_ =
false;
703 if (
this != &other) {
705 nros_cpp_fini(storage_);
707 for (
unsigned i = 0; i <
sizeof(storage_); ++i) {
708 storage_[i] = other.storage_[i];
709 other.storage_[i] = 0;
711 initialized_ = other.initialized_;
712 other.initialized_ =
false;
721 alignas(8) uint8_t storage_[NROS_CPP_EXECUTOR_STORAGE_SIZE];
Definition executor.hpp:119
Result get_service_names_and_types(nros_cpp_names_and_types_visit_fn visit, void *ctx)
Definition executor.hpp:401
Result create_node(::rclcpp::Node &out, const char *name, const char *ns=nullptr)
Definition node.hpp:3617
bool ok() const
Check if the executor is initialized.
Definition executor.hpp:597
void add_node(const NodePtr &node, bool notify=true)
Definition rclcpp.hpp:1159
Result spin_once()
Definition executor.hpp:258
static Result create(Executor &out, const char *locator=nullptr, uint8_t domain_id=0)
Definition executor.hpp:133
Executor(Executor &&other)
Definition executor.hpp:694
Result spin(int32_t poll_ms=10)
Definition executor.hpp:529
Executor & operator=(Executor &&other)
Definition executor.hpp:702
Result spin_for(uint32_t duration_ms, int32_t poll_ms=10)
Definition executor.hpp:587
bool remove_pre_shutdown_callback(PreShutdownCallbackHandle handle)
Definition executor.hpp:664
Result get_publisher_names_and_types_by_node(const char *node_name, const char *node_namespace, nros_cpp_names_and_types_visit_fn visit, void *ctx)
phase-381 W4 — what one named node PUBLISHES, with the types.
Definition executor.hpp:439
Result spin_some(uint32_t max_ms=0)
Definition executor.hpp:282
Result count_publishers(const char *topic_name, size_t *out_count)
Definition executor.hpp:411
NodeBuilder node_builder(const char *name)
Definition node.hpp:3719
Result get_topic_names_and_types(nros_cpp_names_and_types_visit_fn visit, void *ctx)
Definition executor.hpp:394
Result add_on_shutdown_callback(ShutdownCallback callback, void *context=nullptr, OnShutdownCallbackHandle *out=nullptr)
Definition executor.hpp:648
Result get_node_names(nros_cpp_node_visit_fn visit, void *ctx)
Definition executor.hpp:383
void * handle()
Definition executor.hpp:605
Result get_subscription_names_and_types_by_node(const char *node_name, const char *node_namespace, nros_cpp_names_and_types_visit_fn visit, void *ctx)
Definition executor.hpp:455
bool remove_on_shutdown_callback(OnShutdownCallbackHandle handle)
Definition executor.hpp:672
Executor()
Default constructor — creates an uninitialized executor.
Definition executor.hpp:122
Result get_subscriptions_info_by_topic(const char *topic_name, nros_cpp_endpoint_info_visit_fn visit, void *ctx)
phase-381 W4 — the subscriptions on topic_name, one visit each.
Definition executor.hpp:494
void remove_node(const NodePtr &node, bool notify=true)
Definition executor.hpp:326
Result spin_all(uint32_t max_duration_ms)
Definition executor.hpp:296
FutureReturnCode spin_until_future_complete(Future &&future, int32_t timeout_ms=-1)
Definition rclcpp.hpp:1153
Result ping(int32_t timeout_ms)
Definition executor.hpp:361
Result cancel()
Definition executor.hpp:553
Result add_pre_shutdown_callback(ShutdownCallback callback, void *context=nullptr, PreShutdownCallbackHandle *out=nullptr)
Definition executor.hpp:630
Result get_publishers_info_by_topic(const char *topic_name, nros_cpp_endpoint_info_visit_fn visit, void *ctx)
Definition executor.hpp:486
Result shutdown()
Shut down the executor and close the middleware connection.
Definition executor.hpp:678
Result spin_once(int32_t timeout_ms)
Definition executor.hpp:231
Result count_subscribers(const char *topic_name, size_t *out_count)
Definition executor.hpp:418
Result spin_node_once(const NodePtr &node, int32_t timeout_ms)
Definition executor.hpp:345
bool is_spinning() const
Definition executor.hpp:575
Result get_client_names_and_types_by_node(const char *node_name, const char *node_namespace, nros_cpp_names_and_types_visit_fn visit, void *ctx)
phase-381 W4 — what services one named node CALLS, with the types.
Definition executor.hpp:473
Result count_clients(const char *service_name, size_t *out_count)
Definition executor.hpp:426
~Executor()
Destructor — shuts down if still active.
Definition executor.hpp:686
Result count_services(const char *service_name, size_t *out_count)
Definition executor.hpp:433
static Result create_with_rmw(Executor &out, const char *rmw, const char *locator=nullptr, uint8_t domain_id=0, const char *session_name="nros_cpp")
Definition executor.hpp:174
Result spin_node_some(const NodePtr &node)
Definition executor.hpp:336
static Result create(Executor &out, const char *locator, uint8_t domain_id, const char *session_name)
Definition executor.hpp:143
Result get_service_names_and_types_by_node(const char *node_name, const char *node_namespace, nros_cpp_names_and_types_visit_fn visit, void *ctx)
phase-381 W4 — what services one named node SERVES, with the types.
Definition executor.hpp:465
Definition future.hpp:337
Definition executor.hpp:91
ShutdownCallbackHandle()
Definition executor.hpp:60
Definition executor.hpp:84
ShutdownCallbackHandle()
Definition executor.hpp:60
static constexpr ResultOf success()
Named constructors.
Definition result.hpp:239
ShutdownCallbackHandle(nros_cpp_shutdown_callback_handle_t value)
Definition executor.hpp:64
bool valid() const
True when this handle came from a successful registration.
Definition executor.hpp:67
ShutdownCallbackHandle()
Definition executor.hpp:60
nros_cpp_shutdown_callback_handle_t value() const
The raw FFI value, for passing to the C entry points directly.
Definition executor.hpp:70
int nros_cpp_ret_t
Definition future.hpp:25
nros_cpp_ret_t nros_cpp_spin_once(void *handle, int32_t timeout_ms)
void(*)(void *context) ShutdownCallback
Definition executor.hpp:47
FutureReturnCode
Definition rclcpp.hpp:589
@ InvalidArgument
Null pointer, empty topic name, or out-of-range value.
Definition result.hpp:122
@ NotInitialized
Definition result.hpp:131
@ Unsupported
Operation not implemented by the active backend.
Definition result.hpp:152
ResultOf< void > Result
Definition result.hpp:247
rclcpp::Result, rclcpp::ErrorCode, and the RCLCPP_TRY macro.