blob: 7349a8d647c54493a7643eba0211e2d39375b8c9 [file] [log] [blame]
James Kuszmaulf5eb4682023-09-22 17:16:59 -07001#ifndef AOS_FLATBUFFERS_STATIC_VECTOR_H_
2#define AOS_FLATBUFFERS_STATIC_VECTOR_H_
3#include <span>
4
5#include "flatbuffers/base.h"
James Kuszmaule65fb402024-01-13 14:10:51 -08006#include "flatbuffers/vector.h"
James Kuszmaulf5eb4682023-09-22 17:16:59 -07007#include "glog/logging.h"
8
9#include "aos/containers/inlined_vector.h"
10#include "aos/containers/sized_array.h"
11#include "aos/flatbuffers/base.h"
12
13namespace aos::fbs {
14
15namespace internal {
16// Helper class for managing how we specialize the Vector object for different
17// contained types.
18// Users of the Vector class should never need to care about this.
19// Template arguments:
20// T: The type that the vector stores.
21// kInline: Whether the type in question is stored inline or not.
22// Enable: Used for SFINAE around struct values; can be ignored.
23// The struct provides the following types:
24// Type: The type of the data that will be stored inline in the vector.
25// ObjectType: The type of the actual data (only used for non-inline objects).
26// FlatbufferType: The type used by flatbuffers::Vector to store this type.
27// ConstFlatbufferType: The type used by a const flatbuffers::Vector to store
28// this type.
29// kDataAlign: Alignment required by the stored type.
30// kDataSize: Nominal size required by each non-inline data member. This is
31// what will be initially allocated; once created, individual members may
32// grow to accommodate dynamically lengthed vectors.
33template <typename T, bool kInline, class Enable = void>
34struct InlineWrapper;
35} // namespace internal
36
37// This Vector class provides a mutable, resizeable, flatbuffer vector.
38//
39// Upon creation, the Vector will start with enough space allocated for
40// kStaticLength elements, and must be provided with a memory buffer that
41// is large enough to serialize all the kStaticLength members (kStaticLength may
42// be zero).
43//
44// Once created, the Vector may be grown using calls to reserve().
45// This will result in the Vector attempting to allocate memory via its
46// parent object; such calls may fail if there is no space available in the
47// allocator.
48//
49// Note that if you are using the Vector class in a realtime context (and thus
50// must avoid dynamic memory allocations) you must only be using a Vector of
51// inline data (i.e., scalars, enums, or structs). Flatbuffer tables and strings
52// require overhead to manage and so require some form of dynamic memory
53// allocation. If we discover a strong use-case for such things, then we may
54// provide some interface that allows managing said metadata on the stack or
55// in another realtime-safe manner.
56//
57// Template arguments:
58// T: Type contained by the vector; either a scalar/struct/enum type or a
59// static flatbuffer type of some sort (a String or an implementation of
60// aos::fbs::Table).
61// kStaticLength: Number of elements to statically allocate memory for.
62// May be zero.
63// kInline: Whether the type T will be stored inline in the vector.
64// kForceAlign: Alignment to force for the start of the vector (e.g., for
65// byte arrays it may be desirable to have the entire array aligned).
66// kNullTerminate: Whether to reserve an extra byte past the end of
67// the inline data for null termination. Not included in kStaticLength,
68// so if e.g. you want to store the string "abc" then kStaticLength can
69// be 3 and kNullTerminate can be true and the vector data will take
70// up 4 bytes of memory.
71//
72// Vector buffer memory layout:
73// * Requirements:
74// * Minimum alignment of 4 bytes (for element count).
75// * The start of the vector data must be aligned to either
76// alignof(InlineType) or a user-specified number.
77// * The element count for the vector must immediately precede the vector
78// data (and so may itself not be aligned to alignof(InlineType)).
79// * For non-inlined types, the individual types must be aligned to
80// their own alignment.
81// * In order to accommodate this, the vector buffer as a whole must
82// generally be aligned to the greatest of the above alignments. There
83// are two reasonable ways one could do this:
84// * Require that the 4th byte of the buffer provided by aligned to
85// the maximum alignment of its contents.
86// * Require that the buffer itself by aligned, and provide padding
87// ourselves. The Vector would then have to expose its own offset
88// because it would not start at the start of the buffer.
89// The former requires that the wrapping code understand the internals
90// of how vectors work; the latter generates extra padding and adds
91// extra logic around handling non-zero offsets.
92// To maintain general simplicity, we will use the second condition and eat
93// the cost of the potential extra few bytes of padding.
94// * The layout of the buffer will thus be:
95// [padding; element_count; inline_data; padding; offset_data]
96// The first padding will be of size max(0, kAlign - 4).
97// The element_count is of size 4.
98// The inline_data is of size sizeof(InlineType) * kStaticLength.
99// The second padding is of size
100// (kAlign - ((sizeof(InlineType) * kStaticLength) % kAlign)).
101// The remaining data is only present if kInline is false.
102// The offset data is of size T::kSize * kStaticLength. T::kSize % T::kAlign
103// must be zero.
104// Note that no padding is required on the end because T::kAlign will always
105// end up being equal to the alignment (this can only be violated if
106// kForceAlign is used, but we do not allow that).
James Kuszmaul1c9693f2023-12-08 09:45:26 -0800107// The Vector class leaves any padding uninitialized. Until and unless we
108// determine that it is a performance issue, it is the responsibility of the
109// parent of this object to zero-initialize the memory.
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700110template <typename T, size_t kStaticLength, bool kInline,
111 size_t kForceAlign = 0, bool kNullTerminate = false>
112class Vector : public ResizeableObject {
James Kuszmaul22448052023-12-14 15:55:14 -0800113 template <typename VectorType, typename ValueType>
114 class generic_iterator {
115 public:
116 using iterator_category = std::random_access_iterator_tag;
117 using value_type = ValueType;
118 using difference_type = std::ptrdiff_t;
119 using pointer = value_type *;
120 using reference = value_type &;
121
122 explicit generic_iterator(VectorType *vector, size_t index)
123 : vector_(vector), index_(index) {}
124 generic_iterator(const generic_iterator &) = default;
125 generic_iterator() : vector_(nullptr), index_(0) {}
126 generic_iterator &operator=(const generic_iterator &) = default;
127
128 generic_iterator &operator++() {
129 ++index_;
130 return *this;
131 }
132 generic_iterator operator++(int) {
133 generic_iterator retval = *this;
134 ++(*this);
135 return retval;
136 }
137 generic_iterator &operator--() {
138 --index_;
139 return *this;
140 }
141 generic_iterator operator--(int) {
142 generic_iterator retval = *this;
143 --(*this);
144 return retval;
145 }
146 bool operator==(const generic_iterator &other) const {
147 CHECK_EQ(other.vector_, vector_);
148 return index_ == other.index_;
149 }
150 std::strong_ordering operator<=>(const generic_iterator &other) const {
151 CHECK_EQ(other.vector_, vector_);
152 return index_ <=> other.index_;
153 }
154 reference operator*() const { return vector_->at(index_); }
155 difference_type operator-(const generic_iterator &other) const {
156 CHECK_EQ(other.vector_, vector_);
157 return index_ - other.index_;
158 }
159 generic_iterator operator-(difference_type decrement) const {
160 return generic_iterator(vector_, index_ - decrement);
161 }
162 friend generic_iterator operator-(difference_type decrement,
163 const generic_iterator &rhs) {
164 return rhs - decrement;
165 }
166 generic_iterator operator+(difference_type increment) const {
167 return generic_iterator(vector_, index_ + increment);
168 }
169 friend generic_iterator operator+(difference_type increment,
170 const generic_iterator &rhs) {
171 return rhs + increment;
172 }
173 generic_iterator &operator+=(difference_type increment) {
174 index_ += increment;
175 return *this;
176 }
177 generic_iterator &operator-=(difference_type increment) {
178 index_ -= increment;
179 return *this;
180 }
181 reference operator[](difference_type index) const {
182 return *(*this + index);
183 }
184
185 private:
186 VectorType *vector_;
187 size_t index_;
188 };
189
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700190 public:
James Kuszmaul22448052023-12-14 15:55:14 -0800191 using iterator = generic_iterator<Vector, T>;
192 using const_iterator = generic_iterator<const Vector, const T>;
193
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700194 static_assert(kInline || !kNullTerminate,
195 "It does not make sense to null-terminate vectors of objects.");
196 // Type stored inline in the serialized vector (offsets for tables/strings; T
197 // otherwise).
198 using InlineType = typename internal::InlineWrapper<T, kInline>::Type;
199 // OUt-of-line type for out-of-line T.
200 using ObjectType = typename internal::InlineWrapper<T, kInline>::ObjectType;
201 // Type used as the template parameter to flatbuffers::Vector<>.
202 using FlatbufferType =
203 typename internal::InlineWrapper<T, kInline>::FlatbufferType;
204 using ConstFlatbufferType =
205 typename internal::InlineWrapper<T, kInline>::ConstFlatbufferType;
206 // flatbuffers::Vector type that corresponds to this Vector.
207 typedef flatbuffers::Vector<FlatbufferType> Flatbuffer;
208 typedef const flatbuffers::Vector<ConstFlatbufferType> ConstFlatbuffer;
209 // Alignment of the inline data.
210 static constexpr size_t kInlineAlign =
211 std::max(kForceAlign, alignof(InlineType));
212 // Type used for serializing the length of the vector.
213 typedef uint32_t LengthType;
214 // Overall alignment of this type, and required alignment of the buffer that
215 // must be provided to the Vector.
216 static constexpr size_t kAlign =
217 std::max({alignof(LengthType), kInlineAlign,
218 internal::InlineWrapper<T, kInline>::kDataAlign});
219 // Padding inserted prior to the length element of the vector (to manage
220 // alignment of the data properly; see class comment)
221 static constexpr size_t kPadding1 =
222 std::max<size_t>(0, kAlign - sizeof(LengthType));
223 // Size of the vector length field.
224 static constexpr size_t kLengthSize = sizeof(LengthType);
225 // Size of all the inline vector data, including null termination (prior to
226 // any dynamic increases in size).
227 static constexpr size_t kInlineSize =
228 sizeof(InlineType) * (kStaticLength + (kNullTerminate ? 1 : 0));
229 // Per-element size of any out-of-line data.
230 static constexpr size_t kDataElementSize =
231 internal::InlineWrapper<T, kInline>::kDataSize;
232 // Padding between the inline data and any out-of-line data, to manage
233 // mismatches in alignment between the two.
234 static constexpr size_t kPadding2 = kAlign - (kInlineSize % kAlign);
235 // Total statically allocated space for any out-of-line data ("offset data")
236 // (prior to any dynamic increases in size).
237 static constexpr size_t kOffsetOffsetDataSize =
238 kInline ? 0 : (kStaticLength * kDataElementSize);
239 // Total nominal size of the Vector.
240 static constexpr size_t kSize =
241 kPadding1 + kLengthSize + kInlineSize + kPadding2 + kOffsetOffsetDataSize;
242 // Offset from the start of the provided buffer to where the actual start of
243 // the vector is.
244 static constexpr size_t kOffset = kPadding1;
245 // Constructors; the provided buffer must be aligned to kAlign and be kSize in
246 // length. parent must be non-null.
247 Vector(std::span<uint8_t> buffer, ResizeableObject *parent)
248 : ResizeableObject(buffer, parent) {
249 CHECK_EQ(0u, reinterpret_cast<size_t>(buffer.data()) % kAlign);
250 CHECK_EQ(kSize, buffer.size());
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700251 SetLength(0u);
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700252 if (!kInline) {
253 // Initialize the offsets for any sub-tables. These are used to track
254 // where each table will get serialized in memory as memory gets
255 // resized/moved around.
256 for (size_t index = 0; index < kStaticLength; ++index) {
257 object_absolute_offsets_.emplace_back(kPadding1 + kLengthSize +
258 kInlineSize + kPadding2 +
259 index * kDataElementSize);
260 }
261 }
262 }
263 Vector(const Vector &) = delete;
264 Vector &operator=(const Vector &) = delete;
265 virtual ~Vector() {}
266 // Current allocated length of this vector.
267 // Does not include null termination.
268 size_t capacity() const { return allocated_length_; }
269 // Current length of the vector.
270 // Does not include null termination.
271 size_t size() const { return length_; }
272
273 // Appends an element to the Vector. Used when kInline is false. Returns
274 // nullptr if the append failed due to insufficient capacity. If you need to
275 // increase the capacity() of the vector, call reserve().
276 [[nodiscard]] T *emplace_back();
277 // Appends an element to the Vector. Used when kInline is true. Returns false
278 // if there is insufficient capacity for a new element.
279 [[nodiscard]] bool emplace_back(T element) {
280 static_assert(kInline);
281 return AddInlineElement(element);
282 }
283
284 // Adjusts the allocated size of the vector (does not affect the actual
285 // current length as returned by size()). Returns true on success, and false
286 // if the allocation failed for some reason.
287 // Note that reductions in size will not currently result in the allocated
288 // size actually changing.
289 [[nodiscard]] bool reserve(size_t new_length) {
290 if (new_length > allocated_length_) {
291 const size_t new_elements = new_length - allocated_length_;
292 // First, we must add space for our new inline elements.
293 if (!InsertBytes(
294 inline_data() + allocated_length_ + (kNullTerminate ? 1 : 0),
295 new_elements * sizeof(InlineType), SetZero::kYes)) {
296 return false;
297 }
298 if (!kInline) {
299 // For non-inline objects, create the space required for all the new
300 // object data.
301 const size_t insertion_point = buffer_.size();
302 if (!InsertBytes(buffer_.data() + insertion_point,
303 new_elements * kDataElementSize, SetZero::kYes)) {
304 return false;
305 }
306 for (size_t index = 0; index < new_elements; ++index) {
307 // Note that the already-allocated data may be arbitrarily-sized, so
308 // we cannot use the same static calculation that we do in the
309 // constructor.
310 object_absolute_offsets_.emplace_back(insertion_point +
311 index * kDataElementSize);
312 }
313 objects_.reserve(new_length);
314 }
315 allocated_length_ = new_length;
316 }
317 return true;
318 }
319
320 // Accessors for using the Vector as a flatbuffers::Vector.
321 // Note that these pointers will be unstable if any memory allocations occur
322 // that cause memory to get shifted around.
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700323 ConstFlatbuffer *AsFlatbufferVector() const {
324 return reinterpret_cast<const Flatbuffer *>(vector_buffer().data());
325 }
326
327 // Copies the contents of the provided vector into this; returns false on
328 // failure (e.g., if the provided vector is too long for the amount of space
329 // we can allocate through reserve()).
James Kuszmaul710883b2023-12-14 14:34:48 -0800330 // This is a deep copy, and will call FromFlatbuffer on any constituent
331 // objects.
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700332 [[nodiscard]] bool FromFlatbuffer(ConstFlatbuffer *vector);
333
334 // Returns the element at the provided index. index must be less than size().
335 const T &at(size_t index) const {
336 CHECK_LT(index, length_);
337 return unsafe_at(index);
338 }
339
340 // Same as at(), except that bounds checks are only performed in non-optimized
341 // builds.
342 // TODO(james): The GetInlineElement() call itself does some bounds-checking;
343 // consider down-grading that.
344 const T &unsafe_at(size_t index) const {
345 DCHECK_LT(index, length_);
346 if (kInline) {
347 // This reinterpret_cast is extremely wrong if T != InlineType (this is
348 // fine because we only do this if kInline is true).
349 // TODO(james): Get the templating improved so that we can get away with
350 // specializing at() instead of using if statements. Resolving this will
351 // also allow deduplicating the Resize() calls.
352 // This specialization is difficult because you cannot partially
353 // specialize a templated class method (online things seem to suggest e.g.
354 // using a struct as the template parameter rather than having separate
355 // parameters).
356 return reinterpret_cast<const T &>(GetInlineElement(index));
357 } else {
358 return objects_[index].t;
359 }
360 }
361
362 // Returns a mutable pointer to the element at the provided index. index must
363 // be less than size().
364 T &at(size_t index) {
365 CHECK_LT(index, length_);
366 return unsafe_at(index);
367 }
368
369 // Same as at(), except that bounds checks are only performed in non-optimized
370 // builds.
371 // TODO(james): The GetInlineElement() call itself does some bounds-checking;
372 // consider down-grading that.
373 T &unsafe_at(size_t index) {
374 DCHECK_LT(index, length_);
375 if (kInline) {
376 // This reinterpret_cast is extremely wrong if T != InlineType (this is
377 // fine because we only do this if kInline is true).
378 // TODO(james): Get the templating improved so that we can get away with
379 // specializing at() instead of using if statements. Resolving this will
380 // also allow deduplicating the Resize() calls.
381 // This specialization is difficult because you cannot partially
382 // specialize a templated class method (online things seem to suggest e.g.
383 // using a struct as the template parameter rather than having separate
384 // parameters).
385 return reinterpret_cast<T &>(GetInlineElement(index));
386 } else {
387 return objects_[index].t;
388 }
389 }
390
391 const T &operator[](size_t index) const { return at(index); }
392 T &operator[](size_t index) { return at(index); }
393
394 // Resizes the vector to the requested size.
395 // size must be less than or equal to the current capacity() of the vector.
396 // Does not allocate additional memory (call reserve() to allocate additional
397 // memory).
398 // Zero-initializes all inline element; initializes all subtable/string
399 // elements to extant but empty objects.
400 void resize(size_t size);
401
402 // Resizes an inline vector to the requested size.
403 // When changing the size of the vector, the removed/inserted elements will be
404 // set to zero if requested. Otherwise, they will be left uninitialized.
405 void resize_inline(size_t size, SetZero set_zero) {
406 CHECK_LE(size, allocated_length_);
407 static_assert(
408 kInline,
409 "Vector::resize_inline() only works for inline vector types (scalars, "
410 "enums, structs).");
411 if (size == length_) {
412 return;
413 }
414 if (set_zero == SetZero::kYes) {
415 memset(
416 reinterpret_cast<void *>(inline_data() + std::min(size, length_)), 0,
417 std::abs(static_cast<ssize_t>(length_) - static_cast<ssize_t>(size)) *
418 sizeof(InlineType));
419 }
420 length_ = size;
421 SetLength(length_);
422 }
423 // Resizes a vector of offsets to the requested size.
424 // If the size is increased, the new elements will be initialized
425 // to empty but extant objects for non-inlined types (so, zero-length
426 // vectors/strings; objects that exist but have no fields populated).
427 // Note that this is always equivalent to resize().
428 void resize_not_inline(size_t size) {
429 CHECK_LE(size, allocated_length_);
430 static_assert(!kInline,
431 "Vector::resize_not_inline() only works for offset vector "
432 "types (objects, strings).");
433 if (size == length_) {
434 return;
435 } else if (length_ > size) {
436 // TODO: Remove any excess allocated memory.
437 length_ = size;
438 SetLength(length_);
439 return;
440 } else {
441 while (length_ < size) {
442 CHECK_NOTNULL(emplace_back());
443 }
444 }
445 }
446
447 // Accessors directly to the inline data of a vector.
448 const T *data() const {
449 static_assert(kInline,
450 "If you have a use-case for directly accessing the "
451 "flatbuffer data pointer for vectors of "
452 "objects/strings, please start a discussion.");
453 return inline_data();
454 }
455
456 T *data() {
457 static_assert(kInline,
458 "If you have a use-case for directly accessing the "
459 "flatbuffer data pointer for vectors of "
460 "objects/strings, please start a discussion.");
461 return inline_data();
462 }
463
James Kuszmaul22448052023-12-14 15:55:14 -0800464 // Iterators to allow easy use with standard C++ features.
465 iterator begin() { return iterator(this, 0); }
466 iterator end() { return iterator(this, size()); }
467 const_iterator begin() const { return const_iterator(this, 0); }
468 const_iterator end() const { return const_iterator(this, size()); }
469
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700470 std::string SerializationDebugString() const {
471 std::stringstream str;
472 str << "Raw Size: " << kSize << " alignment: " << kAlign
473 << " allocated length: " << allocated_length_ << " inline alignment "
474 << kInlineAlign << " kPadding1 " << kPadding1 << "\n";
475 str << "Observed length " << GetLength() << " (expected " << length_
476 << ")\n";
477 str << "Inline Size " << kInlineSize << " Inline bytes/value:\n";
478 // TODO(james): Get pretty-printing for structs so we can provide better
479 // debug.
480 internal::DebugBytes(
481 internal::GetSubSpan(vector_buffer(), kLengthSize,
482 sizeof(InlineType) * allocated_length_),
483 str);
484 str << "kPadding2 " << kPadding2 << " offset data size "
485 << kOffsetOffsetDataSize << "\n";
486 return str.str();
487 }
488
489 protected:
490 friend struct internal::TableMover<
491 Vector<T, kStaticLength, kInline, kForceAlign, kNullTerminate>>;
492 // protected so that the String class can access the move constructor.
493 Vector(Vector &&) = default;
494
495 private:
496 // See kAlign and kOffset.
497 size_t Alignment() const final { return kAlign; }
498 size_t AbsoluteOffsetOffset() const override { return kOffset; }
499 // Returns a buffer that starts at the start of the vector itself (past any
500 // padding).
501 std::span<uint8_t> vector_buffer() {
502 return internal::GetSubSpan(buffer(), kPadding1);
503 }
504 std::span<const uint8_t> vector_buffer() const {
505 return internal::GetSubSpan(buffer(), kPadding1);
506 }
507
508 bool AddInlineElement(InlineType e) {
509 if (length_ == allocated_length_) {
510 return false;
511 }
512 SetInlineElement(length_, e);
513 ++length_;
514 SetLength(length_);
515 return true;
516 }
517
518 void SetInlineElement(size_t index, InlineType value) {
519 CHECK_LT(index, allocated_length_);
520 inline_data()[index] = value;
521 }
522
523 InlineType &GetInlineElement(size_t index) {
524 CHECK_LT(index, allocated_length_);
525 return inline_data()[index];
526 }
527
528 const InlineType &GetInlineElement(size_t index) const {
529 CHECK_LT(index, allocated_length_);
530 return inline_data()[index];
531 }
532
533 // Returns a pointer to the start of the inline data itself.
534 InlineType *inline_data() {
535 return reinterpret_cast<InlineType *>(vector_buffer().data() + kLengthSize);
536 }
537 const InlineType *inline_data() const {
538 return reinterpret_cast<const InlineType *>(vector_buffer().data() +
539 kLengthSize);
540 }
541
542 // Updates the length of the vector to match the provided length. Does not set
543 // the length_ member.
544 void SetLength(LengthType length) {
545 *reinterpret_cast<LengthType *>(vector_buffer().data()) = length;
546 if (kNullTerminate) {
547 memset(reinterpret_cast<void *>(inline_data() + length), 0,
548 sizeof(InlineType));
549 }
550 }
551 LengthType GetLength() const {
552 return *reinterpret_cast<const LengthType *>(vector_buffer().data());
553 }
554
555 // Overrides to allow ResizeableObject to manage memory adjustments.
556 size_t NumberOfSubObjects() const final {
557 return kInline ? 0 : allocated_length_;
558 }
559 using ResizeableObject::SubObject;
560 SubObject GetSubObject(size_t index) final {
561 return SubObject{
562 reinterpret_cast<uoffset_t *>(&GetInlineElement(index)),
563 // In order to let this compile regardless of whether type T is an
564 // object type or not, we just use a reinterpret_cast.
565 (index < length_)
566 ? reinterpret_cast<ResizeableObject *>(&objects_[index].t)
567 : nullptr,
568 &object_absolute_offsets_[index]};
569 }
570 // Implementation that handles copying from a flatbuffers::Vector of an inline
571 // data type.
572 [[nodiscard]] bool FromInlineFlatbuffer(ConstFlatbuffer *vector) {
573 if (!reserve(CHECK_NOTNULL(vector)->size())) {
574 return false;
575 }
576
577 // We will be overwriting the whole vector very shortly; there is no need to
578 // clear the buffer to zero.
579 resize_inline(vector->size(), SetZero::kNo);
580
581 memcpy(inline_data(), vector->Data(), size() * sizeof(InlineType));
582 return true;
583 }
584
585 // Implementation that handles copying from a flatbuffers::Vector of a
586 // not-inline data type.
587 [[nodiscard]] bool FromNotInlineFlatbuffer(const Flatbuffer *vector) {
588 if (!reserve(vector->size())) {
589 return false;
590 }
591 // "Clear" the vector.
592 resize_not_inline(0);
593
594 for (const typename T::Flatbuffer *entry : *vector) {
595 if (!CHECK_NOTNULL(emplace_back())->FromFlatbuffer(entry)) {
596 return false;
597 }
598 }
599 return true;
600 }
601
602 // In order to allow for easy partial template specialization, we use a
603 // non-member class to call FromInline/FromNotInlineFlatbuffer and
604 // resize_inline/resize_not_inline. There are not actually any great ways to
605 // do this with just our own class member functions, so instead we make these
606 // methods members of a friend of the Vector class; we then partially
607 // specialize the entire InlineWrapper class and use it to isolate anything
608 // that needs to have a common user interface while still having separate
609 // actual logic.
610 template <typename T_, bool kInline_, class Enable_>
611 friend struct internal::InlineWrapper;
612
613 // Note: The objects here really want to be owned by this object (as opposed
614 // to e.g. returning a stack-allocated object from the emplace_back() methods
615 // that the user then owns). There are two main challenges with have the user
616 // own the object on question:
617 // 1. We can't have >1 reference floating around, or else one object's state
618 // can become out of date. This forces us to do ref-counting and could
619 // make certain types of code obnoxious to write.
620 // 2. Once the user-created object goes out of scope, we lose all of its
621 // internal state. In _theory_ it should be possible to reconstruct most
622 // of the relevant state by examining the contents of the buffer, but
623 // doing so would be cumbersome.
624 aos::InlinedVector<internal::TableMover<ObjectType>,
625 kInline ? 0 : kStaticLength>
626 objects_;
627 aos::InlinedVector<size_t, kInline ? 0 : kStaticLength>
628 object_absolute_offsets_;
629 // Current actual length of the vector.
630 size_t length_ = 0;
631 // Current length that we have allocated space available for.
632 size_t allocated_length_ = kStaticLength;
633};
634
635template <typename T, size_t kStaticLength, bool kInline, size_t kForceAlign,
636 bool kNullTerminate>
637T *Vector<T, kStaticLength, kInline, kForceAlign,
638 kNullTerminate>::emplace_back() {
639 static_assert(!kInline);
640 if (length_ >= allocated_length_) {
641 return nullptr;
642 }
643 const size_t object_start = object_absolute_offsets_[length_];
644 std::span<uint8_t> object_buffer =
645 internal::GetSubSpan(buffer(), object_start, T::kSize);
646 objects_.emplace_back(object_buffer, this);
647 const uoffset_t offset =
648 object_start - (reinterpret_cast<size_t>(&GetInlineElement(length_)) -
649 reinterpret_cast<size_t>(buffer().data()));
650 CHECK(AddInlineElement(offset));
651 return &objects_[objects_.size() - 1].t;
652}
653
654// The String class is a special version of the Vector that is always
655// null-terminated, always contains 1-byte character elements, and which has a
656// few extra methods for convenient string access.
657template <size_t kStaticLength>
658class String : public Vector<char, kStaticLength, true, 0, true> {
659 public:
660 typedef Vector<char, kStaticLength, true, 0, true> VectorType;
661 typedef flatbuffers::String Flatbuffer;
662 String(std::span<uint8_t> buffer, ResizeableObject *parent)
663 : VectorType(buffer, parent) {}
664 virtual ~String() {}
665 void SetString(std::string_view string) {
666 CHECK_LT(string.size(), VectorType::capacity());
667 VectorType::resize_inline(string.size(), SetZero::kNo);
668 memcpy(VectorType::data(), string.data(), string.size());
669 }
670 std::string_view string_view() const {
671 return std::string_view(VectorType::data(), VectorType::size());
672 }
673 std::string str() const {
674 return std::string(VectorType::data(), VectorType::size());
675 }
676 const char *c_str() const { return VectorType::data(); }
677
678 private:
679 friend struct internal::TableMover<String<kStaticLength>>;
680 String(String &&) = default;
681};
682
683namespace internal {
684// Specialization for all non-inline vector types. All of these types will just
685// use offsets for their inline data and have appropriate member types/constants
686// for the remaining fields.
687template <typename T>
688struct InlineWrapper<T, false, void> {
689 typedef uoffset_t Type;
690 typedef T ObjectType;
691 typedef flatbuffers::Offset<typename T::Flatbuffer> FlatbufferType;
692 typedef flatbuffers::Offset<typename T::Flatbuffer> ConstFlatbufferType;
693 static_assert((T::kSize % T::kAlign) == 0);
694 static constexpr size_t kDataAlign = T::kAlign;
695 static constexpr size_t kDataSize = T::kSize;
696 template <typename StaticVector>
697 static bool FromFlatbuffer(
698 StaticVector *to, const typename StaticVector::ConstFlatbuffer *from) {
699 return to->FromNotInlineFlatbuffer(from);
700 }
701 template <typename StaticVector>
702 static void ResizeVector(StaticVector *target, size_t size) {
703 target->resize_not_inline(size);
704 }
705};
706// Specialization for "normal" scalar inline data (ints, floats, doubles,
707// enums).
708template <typename T>
709struct InlineWrapper<T, true,
710 typename std::enable_if_t<!std::is_class<T>::value>> {
711 typedef T Type;
712 typedef T ObjectType;
713 typedef T FlatbufferType;
714 typedef T ConstFlatbufferType;
715 static constexpr size_t kDataAlign = alignof(T);
716 static constexpr size_t kDataSize = sizeof(T);
717 template <typename StaticVector>
718 static bool FromFlatbuffer(
719 StaticVector *to, const typename StaticVector::ConstFlatbuffer *from) {
720 return to->FromInlineFlatbuffer(from);
721 }
722 template <typename StaticVector>
723 static void ResizeVector(StaticVector *target, size_t size) {
724 target->resize_inline(size, SetZero::kYes);
725 }
726};
727// Specialization for booleans, given that flatbuffers uses uint8_t's for bools.
728template <>
729struct InlineWrapper<bool, true, void> {
730 typedef uint8_t Type;
731 typedef uint8_t ObjectType;
732 typedef uint8_t FlatbufferType;
733 typedef uint8_t ConstFlatbufferType;
734 static constexpr size_t kDataAlign = 1u;
735 static constexpr size_t kDataSize = 1u;
736 template <typename StaticVector>
737 static bool FromFlatbuffer(
738 StaticVector *to, const typename StaticVector::ConstFlatbuffer *from) {
739 return to->FromInlineFlatbuffer(from);
740 }
741 template <typename StaticVector>
742 static void ResizeVector(StaticVector *target, size_t size) {
743 target->resize_inline(size, SetZero::kYes);
744 }
745};
746// Specialization for flatbuffer structs.
747// The flatbuffers codegen uses struct pointers rather than references or the
748// such, so it needs to be treated special.
749template <typename T>
750struct InlineWrapper<T, true,
751 typename std::enable_if_t<std::is_class<T>::value>> {
752 typedef T Type;
753 typedef T ObjectType;
754 typedef T *FlatbufferType;
755 typedef const T *ConstFlatbufferType;
756 static constexpr size_t kDataAlign = alignof(T);
757 static constexpr size_t kDataSize = sizeof(T);
758 template <typename StaticVector>
759 static bool FromFlatbuffer(
760 StaticVector *to, const typename StaticVector::ConstFlatbuffer *from) {
761 return to->FromInlineFlatbuffer(from);
762 }
763 template <typename StaticVector>
764 static void ResizeVector(StaticVector *target, size_t size) {
765 target->resize_inline(size, SetZero::kYes);
766 }
767};
768} // namespace internal
769 //
770template <typename T, size_t kStaticLength, bool kInline, size_t kForceAlign,
771 bool kNullTerminate>
772bool Vector<T, kStaticLength, kInline, kForceAlign,
773 kNullTerminate>::FromFlatbuffer(ConstFlatbuffer *vector) {
774 return internal::InlineWrapper<T, kInline>::FromFlatbuffer(this, vector);
775}
776
777template <typename T, size_t kStaticLength, bool kInline, size_t kForceAlign,
778 bool kNullTerminate>
779void Vector<T, kStaticLength, kInline, kForceAlign, kNullTerminate>::resize(
780 size_t size) {
781 internal::InlineWrapper<T, kInline>::ResizeVector(this, size);
782}
783
784} // namespace aos::fbs
785#endif // AOS_FLATBUFFERS_STATIC_VECTOR_H_