10#ifndef NROS_CPP_NODE_HPP
11#define NROS_CPP_NODE_HPP
16#if defined(NROS_CPP_STD) || (__STDC_HOSTED__ + 0)
18#if defined(NROS_CPP_STD) || (__STDC_HOSTED__ + 0)
26#include "nros_cpp_ffi.h"
29#include "nros/nros_cpp_config_generated.h"
34#include "nros/options.hpp"
48#include "nros/callback_group.hpp"
50#ifdef NROS_RMW_CYCLONEDDS
51extern "C" int32_t nros_rmw_cyclonedds_register(
void);
53#if defined(NROS_RMW_XRCE) || defined(NROS_RMW_XRCE_CFFI)
54extern "C" int32_t nros_rmw_xrce_register(
void);
56#ifdef NROS_RMW_ZENOH_CFFI
57extern "C" int32_t nros_rmw_zenoh_register(
void);
60extern "C" int32_t nros_rmw_uorb_register(
void);
66static_assert(NROS_CPP_RET_NOT_FOUND == -4 && NROS_CPP_RET_ALREADY_EXISTS == -5 &&
67 NROS_CPP_RET_FULL == -6 && NROS_CPP_RET_NOT_INIT == -7 &&
68 NROS_CPP_RET_TRY_AGAIN == -14 && NROS_CPP_RET_REENTRANT == -15 &&
69 NROS_CPP_RET_UNSUPPORTED == -16,
70 "nros_cpp_ret_t diverged from the shared numbering (issue #229)");
78template <
typename M>
class Publisher;
79template <
typename M>
class Subscription;
80template <
typename S>
class Service;
81template <
typename S>
class Client;
82template <
typename A>
class ActionServer;
83template <
typename A>
class ActionClient;
193 if (!
out.executor_handle_) {
200 out.initialized_ =
true;
207 if (!initialized_)
return "";
213 if (!initialized_)
return "";
224 if (!initialized_)
return nullptr;
260 ::nros_cpp_node_set_qos_overrides(&handle_,
overrides, len);
278 template <
typename M>
294 template <
typename M>
304 template <
typename M>
320 template <
typename M>
343 typename M,
typename F,
344 typename =
typename std::enable_if<std::is_convertible<
F,
void (*)(
const M&)>::value>::type>
356 template <
typename M,
typename F,
357 typename =
typename std::enable_if<
358 std::is_convertible<F, void (*)(
const M&,
const uint8_t*, size_t)>::value>::type>
361 const SubscriptionOptions& options = {});
363#if defined(NANO_ROS_SAFETY_E2E)
378 template <
typename M,
typename F,
379 typename =
typename std::enable_if<std::is_convertible<
380 F, void (*)(
const M&,
const nros_cpp_integrity_status_t&)>::value>::type>
381 Result create_subscription_with_safety(Subscription<M>& out,
const char* topic, F callback,
383 const SubscriptionOptions& options = {});
392 template <
typename S>
408 template <
typename S,
typename F,
409 typename =
typename std::enable_if<std::is_convertible<
410 F, void (*)(
const typename S::Request&,
typename S::Response&)>::value>::type>
411 Result
create_service(Service<S>& out,
const char* service_name, F callback,
412 const QoS& qos =
QoS::services(),
const ServiceOptions& options = {});
420 template <
typename S>
421 Result
create_client(Client<S>& out,
const char* service_name,
434 template <
typename S,
typename F,
435 typename =
typename std::enable_if<
436 std::is_convertible<F, void (*)(
const typename S::Response&)>::value>::type>
437 Result
create_client(Client<S>& out,
const char* service_name, F callback,
438 const QoS& qos =
QoS::services(),
const ClientOptions& options = {});
450 template <
typename A>
453 const ActionServerOptions& options = {});
461 template <
typename A>
468 template <
typename A>
472 template <
typename A>
484 void* context =
nullptr) {
486 size_t handle_id = 0;
490 out.executor_ = executor_handle_;
491 out.handle_id_ = handle_id;
492 out.initialized_ =
true;
506 void* context =
nullptr) {
508 size_t handle_id = 0;
510 context, &handle_id);
512 out.executor_ = executor_handle_;
513 out.handle_id_ = handle_id;
514 out.initialized_ =
true;
545 size_t handle_id = 0;
549 out.executor_ = executor_handle_;
550 out.handle_id_ = handle_id;
551 out.initialized_ =
true;
571 typename M,
typename F,
572 typename =
typename std::enable_if<std::is_convertible<
F,
void (*)(
const M&)>::value>::type>
589 template <
typename M>
603 void* context =
nullptr) {
608 out.initialized_ =
true;
617 initialized_ =
false;
623 : handle_(
other.handle_), initialized_(
other.initialized_),
624 executor_handle_(
other.executor_handle_) {
625 other.initialized_ =
false;
626 other.executor_handle_ =
nullptr;
630 if (
this != &
other) {
634 handle_ =
other.handle_;
635 initialized_ =
other.initialized_;
636 executor_handle_ =
other.executor_handle_;
637 other.initialized_ =
false;
638 other.executor_handle_ =
nullptr;
649 void* executor_handle_;
675 template <
int = 0>
struct GlobalStorageHolder {
677 static bool initialized;
679 static uint8_t* global_storage() {
return GlobalStorageHolder<>::storage; }
680 static bool& global_initialized() {
return GlobalStorageHolder<>::initialized; }
687alignas(8) uint8_t Node::GlobalStorageHolder<N>::storage[NROS_CPP_EXECUTOR_STORAGE_SIZE] = {};
688template <
int N>
bool Node::GlobalStorageHolder<N>::initialized =
false;
693#if defined(NROS_CPP_STD) || (__STDC_HOSTED__ + 0)
710#ifdef NROS_ENTRY_LOCATOR
711 if (locator ==
nullptr) {
715#if defined(NROS_CPP_STD) || (__STDC_HOSTED__ + 0)
716 if (locator ==
nullptr) {
717 locator =
"tcp/127.0.0.1:7447";
721 return init(locator, domain_id,
"node");
731#if defined(NROS_CPP_STD) || (__STDC_HOSTED__ + 0)
751#ifdef NROS_ENTRY_LOCATOR
752 if (locator ==
nullptr) {
756#ifdef NROS_ENTRY_DOMAIN_ID
760 if (domain_id == 0) {
764#if defined(NROS_CPP_STD) || (__STDC_HOSTED__ + 0)
766 if (locator ==
nullptr) {
767 locator =
"tcp/127.0.0.1:7447";
783 Node::global_initialized() =
true;
789 if (!Node::global_initialized()) {
793 Node::global_initialized() =
false;
822 if (
path ==
nullptr) {
825#if defined(NROS_CPP_STD) || (__STDC_HOSTED__ + 0)
843 return Node::global_initialized();
854 if (!Node::global_initialized()) {
857 out.executor_handle_ = Node::global_storage();
873 const char*
ns =
nullptr) {
874 if (executor_handle ==
nullptr) {
877 out.executor_handle_ = executor_handle;
895 out.executor_handle_ = storage_;
919 : executor_handle_(executor_handle), name_(
name),
941 options_.domain_id_override =
id;
955 options_.sched_context_id =
sc_id;
962 out.executor_handle_ = executor_handle_;
966 out.initialized_ =
true;
974 if (
src !=
nullptr) {
981 for (
size_t i = n; i < cap; ++i) {
987 void* executor_handle_;
989 nros_cpp_node_options_t options_;
NodeBuilder node_builder(const char *name)
Definition node.hpp:992
void * handle()
Definition executor.hpp:166
Result create_node(Node &out, const char *name, const char *ns=nullptr)
Definition node.hpp:893
ErrorCode error() const
Definition result.hpp:218
bool ok() const
Definition result.hpp:211
Definition guard_condition.hpp:45
NodeBuilder & domain_id(uint32_t id)
Definition node.hpp:940
NodeBuilder & sched(uint8_t sc_id)
Definition node.hpp:954
NodeBuilder(void *executor_handle, const char *name)
Definition node.hpp:918
NodeBuilder & rmw(const char *name)
Definition node.hpp:926
NodeBuilder & locator(const char *loc)
Definition node.hpp:933
NodeBuilder & namespace_(const char *ns)
Definition node.hpp:947
Result build(Node &out) const
Materialize the Node.
Definition node.hpp:960
friend Result init(const char *locator, uint8_t domain_id)
Definition node.hpp:692
Result create_subscription_in(const CallbackGroup &group, Subscription< M > &out, const char *topic, F callback, const QoS &qos=QoS::default_profile(), const SubscriptionOptions &options={})
Definition subscription.hpp:622
Result create_polling_action_server(PollingActionServer< A > &out, const char *action_name)
Result create_client(Client< S > &out, const char *service_name, const QoS &qos=QoS::services())
Definition client.hpp:240
friend class ComponentNode
Definition node.hpp:653
Result create_action_server(ActionServer< A > &out, const char *action_name, const QoS &qos=QoS::services(), const ActionServerOptions &options={})
Definition action_server.hpp:350
friend class NodeBuilder
Definition node.hpp:652
const void * get_logger() const
Definition node.hpp:223
friend bool ok()
Check if the nros session is initialized.
Definition node.hpp:842
const nros_cpp_node_t * ffi_handle() const
Definition node.hpp:246
const char * get_namespace() const
Get the node namespace.
Definition node.hpp:212
CallbackGroup create_callback_group(const char *name)
Definition node.hpp:529
Result create_publisher_in(const CallbackGroup &, Publisher< M > &out, const char *topic, const QoS &qos=QoS::default_profile())
Definition node.hpp:590
friend Result spin()
Definition nros.hpp:78
Result create_subscription(Subscription< M > &out, const char *topic, const QoS &qos=QoS::default_profile())
Definition subscription.hpp:513
Result create_action_client(ActionClient< A > &out, const char *action_name, const QoS &qos=QoS::services())
Definition action_client.hpp:399
void * executor_handle() const
Definition node.hpp:270
friend Result create_node(Node &out, const char *name, const char *ns)
Definition node.hpp:853
Result create_timer(Timer &out, uint64_t period_ms, nros_cpp_timer_callback_t callback, void *context=nullptr)
Definition node.hpp:483
friend class Executor
Definition node.hpp:651
Node(Node &&other)
Definition node.hpp:622
Result create_service(Service< S > &out, const char *service_name, const QoS &qos=QoS::services())
Definition service.hpp:214
~Node()
Destructor — releases node resources.
Definition node.hpp:614
Result create_publisher(Publisher< M > &out, const char *topic, const QoS &qos=QoS::default_profile())
Definition publisher.hpp:272
static Result create(Node &out, const char *name, const char *ns=nullptr)
Definition node.hpp:192
Result create_subscription_with_info(Subscription< M > &out, const char *topic, F callback, const QoS &qos=QoS::default_profile(), const SubscriptionOptions &options={})
Definition subscription.hpp:663
Node()
Default constructor — creates an uninitialized node.
Definition node.hpp:184
bool is_valid() const
Check if the node is initialized and valid.
Definition node.hpp:229
Result create_polling_action_client(PollingActionClient< A > &out, const char *action_name)
Phase 122.3.d.b — Create an L1 polling-mode action client.
friend void * global_handle()
Definition nros.hpp:51
friend Result spin_once(int32_t timeout_ms)
Definition nros.hpp:63
Result create_timer_oneshot(Timer &out, uint64_t delay_ms, nros_cpp_timer_callback_t callback, void *context=nullptr)
Definition node.hpp:505
Result create_timer_in(const CallbackGroup &group, Timer &out, uint64_t period_ms, nros_cpp_timer_callback_t callback, void *context=nullptr)
Definition node.hpp:542
Result create_guard_condition(GuardCondition &out, nros_cpp_guard_callback_t callback, void *context=nullptr)
Definition node.hpp:602
void set_qos_overrides(const nros_cpp_qos_override_t *overrides, size_t len)
Definition node.hpp:258
friend Result shutdown()
Definition node.hpp:788
friend Result create_node_on(Node &out, void *executor_handle, const char *name, const char *ns)
Definition node.hpp:872
const char * get_name() const
Get the node name.
Definition node.hpp:206
Node & operator=(Node &&other)
Definition node.hpp:629
static constexpr QoS default_profile()
Default profile: RELIABLE + VOLATILE + KEEP_LAST(10).
Definition qos.hpp:170
static constexpr QoS services()
Services profile: RELIABLE + VOLATILE + KEEP_LAST(10).
Definition qos.hpp:176
static constexpr Result success()
Named constructors.
Definition result.hpp:109
nros::Executor — drives transport I/O and dispatches callbacks.
int nros_cpp_ret_t
Definition future.hpp:20
nros::GuardCondition — cross-thread wake source.
Result init_with_launch(const char *path, int argc=0, char **argv=nullptr, const char *session_name=nullptr)
Definition node.hpp:818
Expected< Node > make_node(const char *name, const char *ns=nullptr)
Definition node.hpp:884
Result shutdown()
Definition node.hpp:788
bool ok()
Check if the nros session is initialized.
Definition node.hpp:842
constexpr uint8_t kDomainIdExplicitZero
Definition node.hpp:100
Result init_with_launch_auto(int argc=0, char **argv=nullptr, const char *session_name=nullptr)
Definition node.hpp:804
Result init(const char *locator=nullptr, uint8_t domain_id=0)
Definition node.hpp:692
Result create_node(Node &out, const char *name, const char *ns=nullptr)
Definition node.hpp:853
Result create_node_on(Node &out, void *executor_handle, const char *name, const char *ns=nullptr)
Definition node.hpp:872
nros::QoS — full DDS-shaped QoS settings (Phase 108.B.7).
nros::Result, nros::ErrorCode, and the NROS_TRY macro.
nros::Timer — periodic callback driven by the executor.