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 (freestanding C++14)
2//
3// Provides nros::Span<T> and nros::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// These types are used by generated borrowed message structs to reference
8// variable-length data in the CDR receive buffer without copying.
9
16#ifndef NROS_CPP_SPAN_HPP
17#define NROS_CPP_SPAN_HPP
18
19#include <cstddef>
20#include <cstdint>
21#include <string.h>
22
23namespace nros {
24
30template <typename T> struct Span {
32 const T* ptr;
34 size_t len;
35
37 constexpr const T* data() const { return ptr; }
39 constexpr size_t size() const { return len; }
41 constexpr bool empty() const { return len == 0; }
43 constexpr const T& operator[](size_t i) const { return ptr[i]; }
45 constexpr const T* begin() const { return ptr; }
47 constexpr const T* end() const { return ptr + len; }
48};
49
54struct StringView {
56 const char* ptr;
58 size_t len;
59
61 constexpr const char* data() const { return ptr; }
63 constexpr size_t size() const { return len; }
65 constexpr bool empty() const { return len == 0; }
67 constexpr char operator[](size_t i) const { return ptr[i]; }
69 constexpr const char* begin() const { return ptr; }
71 constexpr const char* end() const { return ptr + len; }
72
74 bool equals(const char* cstr) const {
75 size_t clen = strlen(cstr);
76 return clen == len && memcmp(ptr, cstr, len) == 0;
77 }
78};
79
88template <typename T> struct LeSpan {
90 const uint8_t* bytes;
92 size_t count;
93
95 constexpr size_t size() const { return count; }
97 constexpr bool empty() const { return count == 0; }
98
101 T operator[](size_t i) const {
102 const uint8_t* p = bytes + i * sizeof(T);
103 unsigned char tmp[sizeof(T)];
104#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
105 __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
106 for (size_t b = 0; b < sizeof(T); ++b)
107 tmp[b] = p[sizeof(T) - 1 - b];
108#else
109 for (size_t b = 0; b < sizeof(T); ++b)
110 tmp[b] = p[b];
111#endif
112 T out;
113 memcpy(&out, tmp, sizeof(T));
114 return out;
115 }
116};
117
118} // namespace nros
119
120#endif // NROS_CPP_SPAN_HPP
Definition future.hpp:40
Definition nros.hpp:42
Definition span.hpp:88
constexpr size_t size() const
Number of elements.
Definition span.hpp:95
size_t count
Number of elements.
Definition span.hpp:92
constexpr bool empty() const
True if the view contains zero elements.
Definition span.hpp:97
const uint8_t * bytes
Pointer to the first element's little-endian bytes. Borrowed.
Definition span.hpp:90
T operator[](size_t i) const
Definition span.hpp:101
Definition span.hpp:30
constexpr const T * data() const
Pointer to the underlying storage.
Definition span.hpp:37
constexpr bool empty() const
True if the view contains zero elements.
Definition span.hpp:41
const T * ptr
Pointer to the first element. Borrowed — caller-owned storage.
Definition span.hpp:32
constexpr size_t size() const
Number of elements (alias for len).
Definition span.hpp:39
constexpr const T * end() const
Iterator past the last element.
Definition span.hpp:47
constexpr const T * begin() const
Iterator to the first element.
Definition span.hpp:45
constexpr const T & operator[](size_t i) const
Element access; no bounds check.
Definition span.hpp:43
size_t len
Number of elements in the view.
Definition span.hpp:34
Definition span.hpp:54
constexpr size_t size() const
Number of bytes.
Definition span.hpp:63
const char * ptr
Pointer to the first byte. Not null-terminated.
Definition span.hpp:56
constexpr char operator[](size_t i) const
Byte access; no bounds check.
Definition span.hpp:67
bool equals(const char *cstr) const
True if cstr (null-terminated) has the same length and bytes.
Definition span.hpp:74
constexpr const char * data() const
Pointer to the underlying bytes.
Definition span.hpp:61
constexpr const char * begin() const
Iterator to the first byte.
Definition span.hpp:69
constexpr bool empty() const
True if the view contains zero bytes.
Definition span.hpp:65
size_t len
Number of bytes in the view.
Definition span.hpp:58
constexpr const char * end() const
Iterator past the last byte.
Definition span.hpp:71