10#ifndef NROS_CPP_FUTURE_HPP
11#define NROS_CPP_FUTURE_HPP
17#include "nros/size_bound.hpp"
49template <typename T, size_t Cap = ::nros::rx_buffer_capacity<T>::value>
class Future {
53 if (slot_ < 0 || !take_fn_)
return false;
54 if (ready_)
return true;
57 nros_cpp_ret_t ret = take_fn_(client_storage_, buf,
sizeof(buf), &len);
58 if (ret == 0 && len > 0) {
60 cached_len_ = len <
sizeof(cached_buf_) ? len :
sizeof(cached_buf_);
61 for (
size_t i = 0; i < cached_len_; ++i)
62 cached_buf_[i] = buf[i];
76 if (T::ffi_deserialize(cached_buf_, cached_len_, &out) != 0) {
93 Result wait(
void* executor_handle, uint32_t timeout_ms, T& out, uint32_t poll_ms = 10) {
95 if (poll_ms == 0) poll_ms = 1;
102 const uint64_t budget_ns =
static_cast<uint64_t
>(timeout_ms) * 1000000ULL;
115 if (now_ns - start_ns >= budget_ns)
break;
128 : client_storage_(other.client_storage_), take_fn_(other.take_fn_), slot_(other.slot_),
129 ready_(other.ready_), cached_len_(other.cached_len_) {
130 for (
size_t i = 0; i < cached_len_; ++i)
131 cached_buf_[i] = other.cached_buf_[i];
133 other.ready_ =
false;
137 if (
this != &other) {
138 client_storage_ = other.client_storage_;
139 take_fn_ = other.take_fn_;
141 ready_ = other.ready_;
142 cached_len_ = other.cached_len_;
143 for (
size_t i = 0; i < cached_len_; ++i)
144 cached_buf_[i] = other.cached_buf_[i];
146 other.ready_ =
false;
155 : client_storage_(nullptr), take_fn_(nullptr), slot_(-1), ready_(false), cached_len_(0) {}
161 template <
typename S>
friend class Client;
164 using TryRecvFn =
nros_cpp_ret_t (*)(
void*, uint8_t*, size_t,
size_t*);
166 Future(
void* storage, TryRecvFn fn,
int slot)
167 : client_storage_(storage), take_fn_(fn), slot_(slot), ready_(false), cached_len_(0) {}
169 void* client_storage_;
174 uint8_t cached_buf_[Cap];
Definition action_client.hpp:73
Result wait(void *executor_handle, uint32_t timeout_ms, T &out, uint32_t poll_ms=10)
Definition future.hpp:93
void cancel()
Cancel the pending operation (idempotent).
Definition future.hpp:121
Future & operator=(Future &&other) noexcept
Definition future.hpp:136
~Future()
Definition future.hpp:151
Result try_take(T &out)
Definition future.hpp:72
Future(Future &&other) noexcept
Definition future.hpp:127
Future()
Default constructor – creates an empty/consumed future.
Definition future.hpp:154
bool is_consumed() const
Check if the future has been consumed or cancelled.
Definition future.hpp:124
bool is_ready()
Check if the result has arrived (non-blocking).
Definition future.hpp:52
static constexpr Result success()
Named constructors.
Definition result.hpp:112
int nros_cpp_ret_t
Definition future.hpp:21
nros_cpp_ret_t nros_cpp_spin_once(void *handle, int32_t timeout_ms)
uint64_t nros_cpp_time_ns(void)
@ Error
Generic failure not covered by a more specific code.
@ Timeout
Operation deadline elapsed before completion.
@ TryAgain
Transient — no data ready yet (non-blocking take). Retry later.
nros::Result, nros::ErrorCode, and the NROS_TRY macro.