nros C++ API
Lightweight ROS 2 client for embedded real-time systems (C++ headers)
Loading...
Searching...
No Matches
result.hpp
Go to the documentation of this file.
1// nros-cpp: Result type for error handling
2// Freestanding C++ — no exceptions, no STL required
3
12#ifndef NROS_CPP_RESULT_HPP
13#define NROS_CPP_RESULT_HPP
14
15#include <cstdint>
16#include <utility>
17#if defined(NROS_CPP_STD) || (__STDC_HOSTED__ + 0)
18#include <cstdio>
19#endif
20
21namespace nros {
22
26enum class ErrorCode : int32_t {
28 Ok = 0,
30 Error = -1,
32 Timeout = -2,
34 InvalidArgument = -3,
37 NotInitialized = -4,
39 Full = -5,
41 TryAgain = -6,
43 Reentrant = -7,
45 TransportError = -100,
46};
47
52class Result {
53 public:
55 constexpr Result() : code_(ErrorCode::Ok) {}
57 constexpr Result(ErrorCode code) : code_(code) {}
59 constexpr Result(int32_t raw) : code_(static_cast<ErrorCode>(raw)) {}
60
62 bool ok() const { return code_ == ErrorCode::Ok; }
63
65 explicit operator bool() const { return ok(); }
66
68 ErrorCode code() const { return code_; }
69
71 int32_t raw() const { return static_cast<int32_t>(code_); }
72
74 static constexpr Result success() { return Result(ErrorCode::Ok); }
75
76 private:
77 ErrorCode code_;
78};
79
90#define NROS_TRY(expr) \
91 do { \
92 ::nros::Result _nros_r = (expr); \
93 if (!_nros_r.ok()) return _nros_r; \
94 } while (0)
95
109#ifndef NROS_TRY_LOG
110#if defined(NROS_CPP_STD) || (__STDC_HOSTED__ + 0)
111#define NROS_TRY_LOG(file, line, expr, ret) \
112 ::std::fprintf(stderr, "[nros] %s:%d %s -> %d\n", (file), (line), (expr), (int)(ret))
113#else
114#define NROS_TRY_LOG(file, line, expr, ret) ((void)(file), (void)(line), (void)(expr), (void)(ret))
115#endif
116#endif
117
118#define NROS_TRY_RET(expr, retval) \
119 do { \
120 ::nros::Result _nros_r = (expr); \
121 if (!_nros_r.ok()) { \
122 NROS_TRY_LOG(__FILE__, __LINE__, #expr, _nros_r.raw()); \
123 return (retval); \
124 } \
125 } while (0)
126
130#define NROS_CHECK(expr) \
131 do { \
132 ::nros::Result _nros_r = (expr); \
133 if (!_nros_r.ok()) { \
134 NROS_TRY_LOG(__FILE__, __LINE__, #expr, _nros_r.raw()); \
135 return; \
136 } \
137 } while (0)
138
160template <typename T> class Expected {
161 public:
162 static Expected ok(T value) {
163 Expected e;
164 e.ok_ = true;
165 e.value_ = ::std::move(value);
166 return e;
167 }
168 static Expected error(ErrorCode code) {
169 Expected e;
170 e.ok_ = false;
171 e.error_ = code;
172 return e;
173 }
174 static Expected error(const Result& r) { return error(r.code()); }
175
176 bool ok() const { return ok_; }
177 explicit operator bool() const { return ok_; }
178
179 T& value() & { return value_; }
180 const T& value() const& { return value_; }
181 T&& value() && { return ::std::move(value_); }
182
183 ErrorCode error() const { return error_; }
184 Result error_as_result() const { return Result(error_); }
185
186 private:
187 Expected() : ok_(false), error_(ErrorCode::Error), value_() {}
188
189 bool ok_;
190 ErrorCode error_;
191 T value_;
192};
193
194} // namespace nros
195
196#endif // NROS_CPP_RESULT_HPP
Definition result.hpp:160
Result error_as_result() const
Definition result.hpp:184
T & value() &
Definition result.hpp:179
const T & value() const &
Definition result.hpp:180
static Expected error(ErrorCode code)
Definition result.hpp:168
static Expected error(const Result &r)
Definition result.hpp:174
static Expected ok(T value)
Definition result.hpp:162
ErrorCode error() const
Definition result.hpp:183
T && value() &&
Definition result.hpp:181
bool ok() const
Definition result.hpp:176
Definition future.hpp:40
Definition result.hpp:52
static constexpr Result success()
Named constructors.
Definition result.hpp:74
int32_t raw() const
Get the raw integer code (for FFI interop).
Definition result.hpp:71
ErrorCode code() const
Get the underlying error code.
Definition result.hpp:68
constexpr Result(ErrorCode code)
Construct from a typed code.
Definition result.hpp:57
constexpr Result(int32_t raw)
Construct from a raw FFI return value (int32_t).
Definition result.hpp:59
bool ok() const
Returns true if the operation succeeded.
Definition result.hpp:62
constexpr Result()
Default-construct a success.
Definition result.hpp:55
Definition nros.hpp:42
ErrorCode
Definition result.hpp:26
@ InvalidArgument
Null pointer, empty topic name, or out-of-range value.
@ Reentrant
A blocking call was made from inside a callback.
@ Error
Generic failure not covered by a more specific code.
@ TransportError
Underlying zenoh-pico / DDS transport rejected the operation.
@ Full
Static pool exhausted (executor slots, subscription buffers, …).
@ Timeout
Operation deadline elapsed before completion.
@ TryAgain
Transient — no data ready yet (non-blocking take). Retry later.