nros C++ API
Lightweight ROS 2 client for embedded real-time systems (C++ headers)
Loading...
Searching...
No Matches
executor.hpp
Go to the documentation of this file.
1// nros-cpp: Executor class
2// Freestanding C++ — no exceptions, no STL required
3
10#ifndef NROS_CPP_EXECUTOR_HPP
11#define NROS_CPP_EXECUTOR_HPP
12
13#include <cstdint>
14#include <cstddef>
15
16#include "nros/result.hpp"
17#include "nros/nros_cpp_config_generated.h"
18
19#include "nros_cpp_ffi.h"
20
21namespace nros {
22
23// Forward declarations
24class Node;
25class NodeBuilder;
26
50class Executor {
51 public:
53 Executor() : storage_(), initialized_(false) {}
54
64 static Result create(Executor& out, const char* locator = nullptr, uint8_t domain_id = 0) {
65 return create(out, locator, domain_id, "nros_cpp");
66 }
67
74 static Result create(Executor& out, const char* locator, uint8_t domain_id,
75 const char* session_name) {
76 // -3 = NROS_CPP_RET_INVALID_ARGUMENT (generated header).
77 if (session_name == nullptr) {
78 return Result(-3);
79 }
80 nros_cpp_ret_t ret = nros_cpp_init(locator, domain_id, session_name, nullptr, out.storage_);
81 if (ret == 0) {
82 out.initialized_ = true;
83 }
84 return Result(ret);
85 }
86
93 Result create_node(Node& out, const char* name, const char* ns = nullptr);
94
102 NodeBuilder node_builder(const char* name);
103
112 if (!initialized_) return Result(ErrorCode::NotInitialized);
113 return Result(nros_cpp_spin_once(storage_, timeout_ms));
114 }
115
128 if (!initialized_) return Result(ErrorCode::NotInitialized);
129 return Result(nros_cpp_executor_ping(storage_, timeout_ms));
130 }
131
140 if (!initialized_) return Result(ErrorCode::NotInitialized);
141 // Phase 118.C: budget by wall-clock. Iteration-count budgeting
142 // (`elapsed += timeout`) breaks when `nros_cpp_spin_once` returns
143 // early on a signaled condvar — the loop collapses into
144 // milliseconds. Same fix Future::wait() got in Phase 89.2.
146 const uint64_t budget_ns = static_cast<uint64_t>(duration_ms) * 1000000ULL;
148 while (true) {
150 if (!last.ok()) return last;
152 if (now_ns - start_ns >= budget_ns) break;
153 }
154 return last;
155 }
156
158 bool ok() const { return initialized_; }
159
166 void* handle() { return storage_; }
167
170 if (!initialized_) return Result::success();
171 nros_cpp_ret_t ret = nros_cpp_fini(storage_);
172 initialized_ = false;
173 return Result(ret);
174 }
175
178 if (initialized_) {
179 nros_cpp_fini(storage_);
180 initialized_ = false;
181 }
182 }
183
184 // Move semantics (non-copyable)
185 Executor(Executor&& other) : initialized_(other.initialized_) {
186 for (unsigned i = 0; i < sizeof(storage_); ++i) {
187 storage_[i] = other.storage_[i];
188 other.storage_[i] = 0;
189 }
190 other.initialized_ = false;
191 }
192
194 if (this != &other) {
195 if (initialized_) {
196 nros_cpp_fini(storage_);
197 }
198 for (unsigned i = 0; i < sizeof(storage_); ++i) {
199 storage_[i] = other.storage_[i];
200 other.storage_[i] = 0;
201 }
202 initialized_ = other.initialized_;
203 other.initialized_ = false;
204 }
205 return *this;
206 }
207
208 private:
209 Executor(const Executor&) = delete;
210 Executor& operator=(const Executor&) = delete;
211
212 alignas(8) uint8_t storage_[NROS_CPP_EXECUTOR_STORAGE_SIZE];
213 bool initialized_;
214};
215
216} // namespace nros
217
218#endif // NROS_CPP_EXECUTOR_HPP
Definition executor.hpp:50
Executor()
Default constructor — creates an uninitialized executor.
Definition executor.hpp:53
Result ping(int32_t timeout_ms)
Definition executor.hpp:127
NodeBuilder node_builder(const char *name)
Definition node.hpp:847
Executor & operator=(Executor &&other)
Definition executor.hpp:193
~Executor()
Destructor — shuts down if still active.
Definition executor.hpp:177
static Result create(Executor &out, const char *locator=nullptr, uint8_t domain_id=0)
Definition executor.hpp:64
bool ok() const
Check if the executor is initialized.
Definition executor.hpp:158
void * handle()
Definition executor.hpp:166
Result shutdown()
Shut down the executor and close the middleware connection.
Definition executor.hpp:169
Result spin_once(int32_t timeout_ms=10)
Definition executor.hpp:111
static Result create(Executor &out, const char *locator, uint8_t domain_id, const char *session_name)
Definition executor.hpp:74
Result spin(uint32_t duration_ms, int32_t poll_ms=10)
Definition executor.hpp:139
Executor(Executor &&other)
Definition executor.hpp:185
Result create_node(Node &out, const char *name, const char *ns=nullptr)
Definition node.hpp:748
Definition future.hpp:40
Definition node.hpp:771
Definition node.hpp:158
Definition result.hpp:52
static constexpr Result success()
Named constructors.
Definition result.hpp:74
int nros_cpp_ret_t
Definition future.hpp:20
nros_cpp_ret_t nros_cpp_spin_once(void *handle, int32_t timeout_ms)
uint64_t nros_cpp_time_ns(void)
Definition nros.hpp:42
nros::Result, nros::ErrorCode, and the NROS_TRY macro.