|
nros C++ API
Lightweight ROS 2 client for embedded real-time systems (C++ headers)
|
rclcpp::Result, rclcpp::ErrorCode, and the RCLCPP_TRY macro.
More...
#include <cstdint>#include "rclcpp/detail/traits.hpp"

Go to the source code of this file.
Classes | |
| class | rclcpp::ResultOf< T > |
| class | rclcpp::ResultOf< void > |
Namespaces | |
| namespace | rclcpp |
Macros | |
| #define | NROS_CPP_DEPRECATED_MSG(msg) |
| #define | NROS_NODISCARD |
| #define | RCLCPP_CHECK(expr) |
| #define | RCLCPP_TRY(expr) |
| #define | RCLCPP_TRY_LOG(file, line, expr, ret) ((void)(file), (void)(line), (void)(expr), (void)(ret)) |
| #define | RCLCPP_TRY_RET(expr, retval) |
Typedefs | |
| using | rclcpp::Result = ResultOf< void > |
Enumerations | |
| enum class | rclcpp::ErrorCode : int32_t { rclcpp::Ok = 0 , rclcpp::Error = -1 , rclcpp::Timeout = -2 , rclcpp::InvalidArgument = -3 , rclcpp::NotFound = -4 , rclcpp::AlreadyExists = -5 , rclcpp::Full = -6 , rclcpp::NotInitialized = -7 , rclcpp::BadSequence = -8 , rclcpp::ServiceFailed = -9 , rclcpp::PublishFailed = -10 , rclcpp::SubscriptionFailed = -11 , rclcpp::NotAllowed = -12 , rclcpp::Rejected = -13 , rclcpp::TryAgain = -14 , rclcpp::Reentrant = -15 , rclcpp::Unsupported = -16 , rclcpp::TransportError = -100 } |
rclcpp::Result, rclcpp::ErrorCode, and the RCLCPP_TRY macro.
See Error Codes for the full code table and recovery guidance.
| #define NROS_CPP_DEPRECATED_MSG | ( | msg | ) |
NROS_CPP_DEPRECATED_MSG(msg) — a deprecation that names its replacement.
Kept with no current user (phase-482 W6 retired every deprecated item) for the next forwarder, so it does not get re-invented with a second spelling. Declared here because result.hpp is the one header every other nros/*.hpp already pulls in — the same reason NROS_NODISCARD lives above.
Unlike NROS_NODISCARD this needs no compiler-specific carve-out: the deprecated attribute on a class and on a function is honoured by gcc and clang in both the [[…]] and GNU spellings, so the ordinary __has_cpp_attribute probe with a GNU fallback is enough.
| #define NROS_NODISCARD |
NROS_NODISCARD — [[nodiscard]] where the compiler has it, and nothing where it does not.
WHERE IT LIVES, and why here. The only things this tree marks are the two result types below, and result.hpp is the lowest C++ header we own — it includes <cstdint> and <utility> and nothing of ours, and every other nros/*.hpp reaches it. A header of its own would be one more file and one more include for one macro.
It is NOT a capability gate. It asks the COMPILER what attribute spelling it has, not the target what library it ships, and it never gates a MEMBER — so sizeof(Result) and sizeof(ResultOf<T>) are the same number in every configuration. That is the rule scripts/check-cpp-capability-layout.sh measures, both types are named in its list, and it is why phase-427 W8 forbids this header a third capability gate. The two it has stay two: <cstdio> for the default RCLCPP_TRY_LOG, and the body of that macro.
The spelling is measured, not assumed (gcc 12.3 and clang 14, -std=c++14, which is the standard the cpp lane compiles these headers with):
[[nodiscard]] on a CLASS works in C++14 mode on both compilers and fires -Wunused-result at a discarding call site. GCC says nothing about it being a C++17 attribute; clang reports -Wc++17-attribute-extensions, but only under -pedantic.__attribute__((warn_unused_result)) on a CLASS is a clang-only spelling. GCC 12 rejects it — "warn_unused_result attribute only
applies to function types" [-Wattributes] — and the class then carries no attribute at all, which is the silent-nothing outcome this macro exists to avoid.So: the GNU spelling on clang below C++17, where it costs no -pedantic diagnostic and clang honours it on types; [[nodiscard]] everywhere else the compiler advertises it; empty otherwise.
The outer #ifndef is there so a toolchain that already defines the name is not redefined out from under itself. It is not an invitation: defining it away is a decision to let failed operations be dropped in silence, and the cpp lane compiles a TU that proves the attribute reaches callers.
| #define RCLCPP_CHECK | ( | expr | ) |
Like RCLCPP_TRY but for void-returning callers (RTOS app_main(void), task entry points, …). Logs the failure via the same RCLCPP_TRY_LOG hook as RCLCPP_TRY_RET and bails with a bare return;.
| #define RCLCPP_TRY | ( | expr | ) |
Early-return macro for error propagation (replaces try/catch).
Usage:
| #define RCLCPP_TRY_LOG | ( | file, | |
| line, | |||
| expr, | |||
| ret | |||
| ) | ((void)(file), (void)(line), (void)(expr), (void)(ret)) |
Like RCLCPP_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 RCLCPP_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 RCLCPP_TRY_LOG(file, line, expr, ret) ((void)0).
| #define RCLCPP_TRY_RET | ( | expr, | |
| retval | |||
| ) |