nros C++ API
Lightweight ROS 2 client for embedded real-time systems (C++ headers)
Loading...
Searching...
No Matches
Classes | Namespaces | Macros | Enumerations
result.hpp File Reference

nros::Result, nros::ErrorCode, and the NROS_TRY macro. More...

#include <cstdint>
#include <utility>
Include dependency graph for result.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  nros::Expected< T >
 
class  nros::Result
 

Namespaces

namespace  nros
 

Macros

#define NROS_CHECK(expr)
 
#define NROS_TRY(expr)
 
#define NROS_TRY_LOG(file, line, expr, ret)   ((void)(file), (void)(line), (void)(expr), (void)(ret))
 
#define NROS_TRY_RET(expr, retval)
 

Enumerations

enum class  nros::ErrorCode : int32_t {
  nros::Ok = 0 , nros::Error = -1 , nros::Timeout = -2 , nros::InvalidArgument = -3 ,
  nros::NotInitialized = -4 , nros::Full = -5 , nros::TryAgain = -6 , nros::Reentrant = -7 ,
  nros::TransportError = -100
}
 

Detailed Description

nros::Result, nros::ErrorCode, and the NROS_TRY macro.

See Error Codes for the full code table and recovery guidance.

Macro Definition Documentation

◆ NROS_CHECK

#define NROS_CHECK (   expr)
Value:
do { \
::nros::Result _nros_r = (expr); \
if (!_nros_r.ok()) { \
NROS_TRY_LOG(__FILE__, __LINE__, #expr, _nros_r.raw()); \
return; \
} \
} while (0)
Definition result.hpp:52
int32_t raw() const
Get the raw integer code (for FFI interop).
Definition result.hpp:71
bool ok() const
Returns true if the operation succeeded.
Definition result.hpp:62

Like NROS_TRY but for void-returning callers (RTOS app_main(void), task entry points, …). Logs the failure via the same NROS_TRY_LOG hook as NROS_TRY_RET and bails with a bare return;.

◆ NROS_TRY

#define NROS_TRY (   expr)
Value:
do { \
::nros::Result _nros_r = (expr); \
if (!_nros_r.ok()) return _nros_r; \
} while (0)

Early-return macro for error propagation (replaces try/catch).

Usage:

nros::Result do_stuff() {
NROS_TRY(node.create_publisher(pub, "/topic"));
}
static constexpr Result success()
Named constructors.
Definition result.hpp:74
Result init(const char *locator=nullptr, uint8_t domain_id=0)
Definition node.hpp:568
#define NROS_TRY(expr)
Definition result.hpp:90

◆ NROS_TRY_LOG

#define NROS_TRY_LOG (   file,
  line,
  expr,
  ret 
)    ((void)(file), (void)(line), (void)(expr), (void)(ret))

Like NROS_TRY but for callers that need a custom return value (e.g. int main examples returning 1 on failure).

Phase 123.B.1 — when NROS_CPP_STD is defined (POSIX / Zephyr native_sim / threadx-linux + any host with <cstdio>), the default logger writes [nros] <file>:<line> <expr> -> <ret> to stderr so first-time users see failures immediately. Embedded builds without stdio fall through to the silent default.

Override NROS_TRY_LOG(file, line, expr, ret) before including this header to attach a custom logger (Zephyr's LOG_ERR, semihosting, defmt, etc.). Opt out entirely with #define NROS_TRY_LOG(file, line, expr, ret) ((void)0).

◆ NROS_TRY_RET

#define NROS_TRY_RET (   expr,
  retval 
)
Value:
do { \
::nros::Result _nros_r = (expr); \
if (!_nros_r.ok()) { \
NROS_TRY_LOG(__FILE__, __LINE__, #expr, _nros_r.raw()); \
return (retval); \
} \
} while (0)