nros C API
Lightweight ROS 2 client for embedded real-time systems
Loading...
Searching...
No Matches
serialization_format.h
Go to the documentation of this file.
1
37#ifndef NROS_SERIALIZATION_FORMAT_H
38#define NROS_SERIALIZATION_FORMAT_H
39
40#include "nros/nros_generated.h"
41
52#if defined(__cplusplus)
53#define NROS_STATIC_ASSERT(cond, msg) static_assert(cond, msg)
54#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
55#define NROS_STATIC_ASSERT(cond, msg) static_assert(cond, msg)
56#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
57#define NROS_STATIC_ASSERT(cond, msg) _Static_assert(cond, msg)
58#else
59/* Pre-C11 fallback: a negative array size. The tag must be unique within the
60 translation unit, and `__LINE__` is NOT — two generated message headers
61 included in one TU collide whenever the assertion lands on the same line
62 number in both. `__COUNTER__` (gcc, clang, armcc) is unique per expansion;
63 `__LINE__` is the last resort for a compiler with neither. */
64#define NROS_STATIC_ASSERT_CAT_(a, b) a##b
65#define NROS_STATIC_ASSERT_CAT(a, b) NROS_STATIC_ASSERT_CAT_(a, b)
66#ifdef __COUNTER__
67#define NROS_STATIC_ASSERT_TAG_ __COUNTER__
68#else
69#define NROS_STATIC_ASSERT_TAG_ __LINE__
70#endif
71#define NROS_STATIC_ASSERT(cond, msg) \
72 typedef char NROS_STATIC_ASSERT_CAT(nros_static_assert_, \
73 NROS_STATIC_ASSERT_TAG_)[(cond) ? 1 : -1]
74#endif
75
76/* RFC-0088 D2 — reserved discriminants for the in-tree formats. Low values for
77 readability, and nothing more: the allocation is per image, so these are the
78 values THIS image's `nros_serdes::format::SerializationFormatId` uses, not a
79 registry any other image is bound by. */
80#define NROS_SERIALIZATION_FORMAT_ID_CDR 1
81#define NROS_SERIALIZATION_FORMAT_ID_UORB 2
82
83#ifndef NROS_SERIALIZATION_FORMAT_ID
84#error \
85 "NROS_SERIALIZATION_FORMAT_ID is missing — <nros/nros_generated.h> did not supply it. Regenerate the committed cbindgen headers (`cargo run -p nros-cbindgen-headers`)."
86#endif
87
96#if NROS_SERIALIZATION_FORMAT_ID == NROS_SERIALIZATION_FORMAT_ID_CDR
97#define NROS_SERIALIZATION_FORMAT "cdr"
98#elif NROS_SERIALIZATION_FORMAT_ID == NROS_SERIALIZATION_FORMAT_ID_UORB
99#define NROS_SERIALIZATION_FORMAT "uorb"
100#else
101#error \
102 "NROS_SERIALIZATION_FORMAT_ID names a format this header has no name for — add its row here and in `nros_serdes::format::SerializationFormatId` together (RFC-0088 D2)."
103#endif
104
113#define NROS_ASSERT_MESSAGE_FORMAT(msg_format_id, type_literal) \
114 NROS_STATIC_ASSERT((msg_format_id) == NROS_SERIALIZATION_FORMAT_ID, \
115 "RFC-0088: " type_literal \
116 " is not encoded in the format the linked backend speaks " \
117 "(NROS_SERIALIZATION_FORMAT) — one image, one backend, one encoding")
118
119#endif /* NROS_SERIALIZATION_FORMAT_H */