nros C++ API
Lightweight ROS 2 client for embedded real-time systems (C++ headers)
Loading...
Searching...
No Matches
nros.hpp
Go to the documentation of this file.
1// nros-cpp: Umbrella header
2// Include this single header to get the full nros C++ API.
3//
4// Freestanding C++ compatible — no STL, no exceptions, no RTTI required.
5
12#ifndef NROS_CPP_HPP
13#define NROS_CPP_HPP
14
15// Phase 118.D — pull cbindgen-generated FFI before any wrapper hpp so
16// `nros/qos.hpp`'s `#ifndef NROS_CPP_FFI_H` guard skips its local
17// fallback definitions in favor of the canonical types.
18#include "nros_cpp_ffi.h"
19
20#include "nros/log.hpp"
21#include "nros/result.hpp"
22// Issue 0789 — the clock / time / duration surface. `node.now()` and
23// `node.get_clock()->now()` are what a ported rclcpp publisher calls to
24// stamp a header, so the umbrella carries all three. Phase 379 W5 moved
25// `duration.hpp` ABOVE `qos.hpp`: the QoS deadline / lifespan / lease
26// accessors take and return `nros::Duration`. `qos.hpp` also includes it
27// directly, so this order is legibility rather than load-bearing.
28#include "nros/duration.hpp"
29#include "nros/time.hpp"
30#include "nros/clock.hpp"
31#include "nros/qos.hpp"
32#include "nros/options.hpp"
33#include "nros/future.hpp"
34#include "nros/stream.hpp"
35// RFC-0088 D5 — nros::SerializationFormat / format_of<M> / linked_format().
37// Phase 84.G8: node.hpp no longer pulls in the heavy entity headers —
38// each entity header carries its own out-of-line `Node::create_X<>()`
39// template definition. The umbrella pulls in every entity explicitly so
40// `#include <nros/nros.hpp>` still yields the full API.
41#include "nros/node.hpp"
42#include "nros/publisher.hpp"
43#include "nros/subscription.hpp"
44#include "nros/service.hpp"
45#include "nros/client.hpp"
48#include "nros/polling_action_server.hpp"
49#include "nros/polling_action_client.hpp"
50#include "nros/polling_subscription.hpp"
51#include "nros/parameter.hpp"
52#include "nros/tick_ctx.hpp"
53#include "nros/lifecycle.hpp"
54
55namespace nros {
56
63inline void* global_handle() {
64 if (!Node::global_initialized()) return nullptr;
65 return Node::global_storage();
66}
67
75inline Result spin_once(int32_t timeout_ms = 10) {
76 if (!Node::global_initialized()) {
78 }
79 return Result(nros_cpp_spin_once(Node::global_storage(), timeout_ms));
80}
81
102inline Result pre_shutdown(ShutdownCallback callback, void* context = nullptr,
103 PreShutdownCallbackHandle* out = nullptr) {
104 void* executor = global_handle();
105 if (executor == nullptr) {
107 }
108 nros_cpp_shutdown_callback_handle_t raw = NROS_CPP_SHUTDOWN_CALLBACK_HANDLE_INVALID;
109 nros_cpp_ret_t ret = nros_cpp_add_pre_shutdown_callback(executor, callback, context, &raw);
110 if (out != nullptr) *out = PreShutdownCallbackHandle(raw);
111 return Result(ret);
112}
113
121inline Result on_shutdown(ShutdownCallback callback, void* context = nullptr,
122 OnShutdownCallbackHandle* out = nullptr) {
123 void* executor = global_handle();
124 if (executor == nullptr) {
126 }
127 nros_cpp_shutdown_callback_handle_t raw = NROS_CPP_SHUTDOWN_CALLBACK_HANDLE_INVALID;
128 nros_cpp_ret_t ret = nros_cpp_add_on_shutdown_callback(executor, callback, context, &raw);
129 if (out != nullptr) *out = OnShutdownCallbackHandle(raw);
130 return Result(ret);
131}
132
136 void* executor = global_handle();
137 if (executor == nullptr) return false;
138 return nros_cpp_remove_pre_shutdown_callback(executor, handle.value()) == 0;
139}
140
144 void* executor = global_handle();
145 if (executor == nullptr) return false;
146 return nros_cpp_remove_on_shutdown_callback(executor, handle.value()) == 0;
147}
148
157inline Result spin() {
158 if (!Node::global_initialized()) {
160 }
161 Result last = Result::success();
162 while (ok()) {
163 last = Result(nros_cpp_spin_once(Node::global_storage(), 10));
164 if (!last.ok()) return last;
165 }
166 return last;
167}
168
177inline Result spin(uint32_t duration_ms, int32_t poll_ms = 10) {
178 if (!Node::global_initialized()) {
180 }
181 // Issue 0329 — forward to the single budgeted-spin CFFI entry point. This
182 // loop previously budgeted by ITERATION count (`elapsed += timeout`), which
183 // collapsed to milliseconds when `spin_once` returned early on a signaled
184 // wake — the exact bug `Executor::spin` fixed in Phase 118.C. The correct
185 // wall-clock budget now lives once, Rust-side, in `nros_cpp_spin_for`.
186 return Result(nros_cpp_spin_for(Node::global_storage(), duration_ms, poll_ms));
187}
188
189} // namespace nros
190
191#ifdef NROS_CPP_STD
192#include "nros/std_compat.hpp"
193#endif
194
195#endif // NROS_CPP_HPP
nros::ActionClient<A> — typed action client.
nros::ActionServer<A> — typed action server.
Definition executor.hpp:78
Definition executor.hpp:71
Definition result.hpp:90
static constexpr Result success()
Named constructors.
Definition result.hpp:112
bool ok() const
Returns true if the operation succeeded.
Definition result.hpp:100
nros_cpp_shutdown_callback_handle_t value() const
The raw FFI value, for passing to the C entry points directly.
Definition executor.hpp:57
nros::Client<S> — typed service client.
nros::Future<T> — single-shot deferred result.
int nros_cpp_ret_t
Definition future.hpp:21
nros_cpp_ret_t nros_cpp_spin_once(void *handle, int32_t timeout_ms)
Definition nros.hpp:55
Result spin()
Definition nros.hpp:157
bool remove_on_shutdown_callback(OnShutdownCallbackHandle handle)
Definition nros.hpp:143
Result spin_once(int32_t timeout_ms=10)
Definition nros.hpp:75
Result pre_shutdown(ShutdownCallback callback, void *context=nullptr, PreShutdownCallbackHandle *out=nullptr)
Definition nros.hpp:102
bool remove_pre_shutdown_callback(PreShutdownCallbackHandle handle)
Definition nros.hpp:135
bool ok()
Check if the nros session is initialized.
Definition node.hpp:997
void(*)(void *context) ShutdownCallback
Definition executor.hpp:34
void * global_handle()
Definition nros.hpp:63
Result on_shutdown(ShutdownCallback callback, void *context=nullptr, OnShutdownCallbackHandle *out=nullptr)
Definition nros.hpp:121
nros::Node and global session helpers.
nros::Publisher<M> — typed topic publisher.
nros::QoS — full DDS-shaped QoS settings (Phase 108.B.7).
nros::Result, nros::ErrorCode, and the NROS_TRY macro.
nros::format_of<M> — a message type's serialization format; nros::linked_format() — the linked backen...
nros::Service<S> — typed service server.
NROS_CPP_STD opt-in conveniences — std::function, std::string, std::chrono overloads.
nros::Stream<T> — multi-shot message receiver.
nros::Subscription<M> — typed topic subscriber.