nros C++ API
Lightweight ROS 2 client for embedded real-time systems (C++ headers)
Loading...
Searching...
No Matches
fixed_sequence.hpp
Go to the documentation of this file.
1// nros-cpp: FixedSequence — fixed-capacity sequence container
2// Freestanding C++14 — no exceptions, no STL required
3//
4// Memory layout is repr(C) compatible: identical to { uint32_t size; T data[N]; }.
5// Used by codegen-generated message structs for sequence fields.
6
13#ifndef NROS_CPP_FIXED_SEQUENCE_HPP
14#define NROS_CPP_FIXED_SEQUENCE_HPP
15
16#include <cstddef>
17#include <cstdint>
18
19namespace nros {
20
36template <typename T, size_t N> struct FixedSequence {
39
41 FixedSequence() : size(0), data{} {}
42
44 bool push_back(const T& val) {
45 if (size >= N) return false;
46 data[size++] = val;
47 return true;
48 }
49
51 T& operator[](size_t i) { return data[i]; }
52 const T& operator[](size_t i) const { return data[i]; }
53
55 uint32_t length() const { return size; }
56
58 static constexpr size_t max_size() { return N; }
59
61 T* begin() { return data; }
62 T* end() { return data + size; }
63 const T* begin() const { return data; }
64 const T* end() const { return data + size; }
65};
66
67} // namespace nros
68
69#endif // NROS_CPP_FIXED_SEQUENCE_HPP
Definition future.hpp:40
Definition nros.hpp:42
Definition fixed_sequence.hpp:36
const T & operator[](size_t i) const
Definition fixed_sequence.hpp:52
const T * begin() const
Definition fixed_sequence.hpp:63
const T * end() const
Definition fixed_sequence.hpp:64
T data[N]
Definition fixed_sequence.hpp:38
bool push_back(const T &val)
Append an element. Returns false if the sequence is full.
Definition fixed_sequence.hpp:44
uint32_t size
Definition fixed_sequence.hpp:37
T * begin()
Iterator support.
Definition fixed_sequence.hpp:61
uint32_t length() const
Current number of elements.
Definition fixed_sequence.hpp:55
FixedSequence()
Default constructor — empty sequence.
Definition fixed_sequence.hpp:41
T * end()
Definition fixed_sequence.hpp:62
static constexpr size_t max_size()
Maximum capacity.
Definition fixed_sequence.hpp:58
T & operator[](size_t i)
Access element by index (no bounds check).
Definition fixed_sequence.hpp:51