nros C++ API
Lightweight ROS 2 client for embedded real-time systems (C++ headers)
Loading...
Searching...
No Matches
span.hpp
Go to the documentation of this file.
1// nros-cpp: Lightweight non-owning view types
2//
3// Provides rclcpp::Span<T> and rclcpp::StringView as zero-overhead alternatives
4// to std::span (C++20) and std::string_view (C++17). Compatible with GCC 5+,
5// Clang 3.5+, and all embedded toolchains.
6//
7// Written in the freestanding C++14 subset, which is a property of this
8// FILE and not the project's minimum: nano-ros declares C++17 (issue 1118 —
9// `target_compile_features(nros-cpp-headers INTERFACE cxx_std_17)`), because
10// that is what ROS 2 Humble's own `rclcpp` exports (phase-426 W4 retired the
11// construct-specific half of this claim: `component_node.hpp`'s `if constexpr`
12// left with `adopt_launch_seed_`). `just check cpp` still parses
13// this header at `-std=c++14 -ffreestanding`, so the subset is enforced and
14// not merely claimed.
15//
16// These types are used by generated borrowed message structs to reference
17// variable-length data in the CDR receive buffer without copying.
18
25#ifndef NROS_RCLCPP_SPAN_HPP
26#define NROS_RCLCPP_SPAN_HPP
27
28#include <cstddef>
29#include <cstdint>
30#include <string.h>
31
32namespace rclcpp {
33
39template <typename T> struct Span {
41 const T* ptr;
43 size_t len;
44
46 constexpr const T* data() const { return ptr; }
48 constexpr size_t size() const { return len; }
50 constexpr bool empty() const { return len == 0; }
52 constexpr const T& operator[](size_t i) const { return ptr[i]; }
54 constexpr const T* begin() const { return ptr; }
56 constexpr const T* end() const { return ptr + len; }
57};
58
63struct StringView {
65 const char* ptr;
67 size_t len;
68
70 constexpr const char* data() const { return ptr; }
72 constexpr size_t size() const { return len; }
74 constexpr bool empty() const { return len == 0; }
76 constexpr char operator[](size_t i) const { return ptr[i]; }
78 constexpr const char* begin() const { return ptr; }
80 constexpr const char* end() const { return ptr + len; }
81
83 bool equals(const char* cstr) const {
84 size_t clen = strlen(cstr);
85 return clen == len && memcmp(ptr, cstr, len) == 0;
86 }
87};
88
97template <typename T> struct LeSpan {
99 const uint8_t* bytes;
101 size_t count;
102
104 constexpr size_t size() const { return count; }
106 constexpr bool empty() const { return count == 0; }
107
110 T operator[](size_t i) const {
111 const uint8_t* p = bytes + i * sizeof(T);
112 unsigned char tmp[sizeof(T)];
113#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
114 __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
115 for (size_t b = 0; b < sizeof(T); ++b)
116 tmp[b] = p[sizeof(T) - 1 - b];
117#else
118 for (size_t b = 0; b < sizeof(T); ++b)
119 tmp[b] = p[b];
120#endif
121 T out;
122 memcpy(&out, tmp, sizeof(T));
123 return out;
124 }
125};
126
127} // namespace rclcpp
128
129#endif // NROS_RCLCPP_SPAN_HPP
Definition client.hpp:129
Definition rclcpp.hpp:75
Definition span.hpp:97
T operator[](size_t i) const
Definition span.hpp:110
constexpr size_t size() const
Number of elements.
Definition span.hpp:104
constexpr bool empty() const
True if the view contains zero elements.
Definition span.hpp:106
const uint8_t * bytes
Pointer to the first element's little-endian bytes. Borrowed.
Definition span.hpp:99
size_t count
Number of elements.
Definition span.hpp:101
Definition span.hpp:39
constexpr const T * data() const
Pointer to the underlying storage.
Definition span.hpp:46
constexpr bool empty() const
True if the view contains zero elements.
Definition span.hpp:50
constexpr size_t size() const
Number of elements (alias for len).
Definition span.hpp:48
constexpr const T * begin() const
Iterator to the first element.
Definition span.hpp:54
const T * ptr
Pointer to the first element. Borrowed — caller-owned storage.
Definition span.hpp:41
constexpr const T * end() const
Iterator past the last element.
Definition span.hpp:56
constexpr const T & operator[](size_t i) const
Element access; no bounds check.
Definition span.hpp:52
size_t len
Number of elements in the view.
Definition span.hpp:43
Definition span.hpp:63
constexpr size_t size() const
Number of bytes.
Definition span.hpp:72
bool equals(const char *cstr) const
True if cstr (null-terminated) has the same length and bytes.
Definition span.hpp:83
constexpr const char * end() const
Iterator past the last byte.
Definition span.hpp:80
constexpr char operator[](size_t i) const
Byte access; no bounds check.
Definition span.hpp:76
const char * ptr
Pointer to the first byte. Not null-terminated.
Definition span.hpp:65
constexpr const char * data() const
Pointer to the underlying bytes.
Definition span.hpp:70
constexpr const char * begin() const
Iterator to the first byte.
Definition span.hpp:78
constexpr bool empty() const
True if the view contains zero bytes.
Definition span.hpp:74
size_t len
Number of bytes in the view.
Definition span.hpp:67