blob: 68a8ad5b3e11364e6e7907b30f6850ead1a8a6f1 [file] [log] [blame]
Austin Schuhe93d8642019-10-13 15:27:07 -07001#ifndef AOS_FLATBUFFERS_H_
2#define AOS_FLATBUFFERS_H_
3
Alex Perrycb7da4b2019-08-28 19:35:56 -07004#include <array>
James Kuszmaul3ae42262019-11-08 12:33:41 -08005#include <string_view>
Alex Perrycb7da4b2019-08-28 19:35:56 -07006
Austin Schuh6f3babe2020-01-26 20:34:50 -08007#include "absl/types/span.h"
Brian Silverman354697a2020-09-22 21:06:32 -07008#include "aos/containers/resizeable_buffer.h"
Tyler Chatow116edf12020-01-26 11:52:39 -08009#include "aos/macros.h"
Austin Schuhb8075812020-10-19 09:36:49 -070010#include "flatbuffers/flatbuffers.h" // IWYU pragma: export
Austin Schuhe84c3ed2019-12-14 15:29:48 -080011#include "glog/logging.h"
Austin Schuhe93d8642019-10-13 15:27:07 -070012
13namespace aos {
14
Austin Schuh40485ed2019-10-26 21:51:44 -070015// This class is a base class for all sizes of array backed allocators.
16class FixedAllocatorBase : public flatbuffers::Allocator {
17 public:
Brian Silvermana1652f32020-01-29 20:41:44 -080018 ~FixedAllocatorBase() override { CHECK(!is_allocated_); }
19
Austin Schuh40485ed2019-10-26 21:51:44 -070020 // TODO(austin): Read the contract for these.
21 uint8_t *allocate(size_t) override;
22
Brian Silverman1715d472020-08-12 22:54:15 -070023 void deallocate(uint8_t *allocated_data, size_t allocated_size) override {
24 DCHECK_LE(allocated_size, size());
25 DCHECK_EQ(allocated_data, data());
26 CHECK(is_allocated_);
27 is_allocated_ = false;
28 }
Austin Schuh40485ed2019-10-26 21:51:44 -070029
30 uint8_t *reallocate_downward(uint8_t *, size_t, size_t, size_t,
31 size_t) override;
32
33 virtual const uint8_t *data() const = 0;
34 virtual uint8_t *data() = 0;
35 virtual size_t size() const = 0;
36
Brian Silverman1715d472020-08-12 22:54:15 -070037 void Reset() {
38 CHECK(!is_allocated_);
39 is_allocated_ = false;
40 }
Brian Silvermana1652f32020-01-29 20:41:44 -080041 bool is_allocated() const { return is_allocated_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070042
Tyler Chatow116edf12020-01-26 11:52:39 -080043 bool allocated() { return is_allocated_; }
44
Austin Schuh40485ed2019-10-26 21:51:44 -070045 private:
46 bool is_allocated_ = false;
47};
48
49// This class is a fixed memory allocator which holds the data for a flatbuffer
Tyler Chatow116edf12020-01-26 11:52:39 -080050// in a vector.
Austin Schuh40485ed2019-10-26 21:51:44 -070051class FixedAllocator : public FixedAllocatorBase {
52 public:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080053 FixedAllocator(size_t size) : buffer_(size, 0) {}
54
Austin Schuh40485ed2019-10-26 21:51:44 -070055 uint8_t *data() override { return &buffer_[0]; }
56 const uint8_t *data() const override { return &buffer_[0]; }
57 size_t size() const override { return buffer_.size(); }
58
Austin Schuhe84c3ed2019-12-14 15:29:48 -080059 // Releases the data in the buffer.
60 std::vector<uint8_t> release() { return std::move(buffer_); }
61
Austin Schuh40485ed2019-10-26 21:51:44 -070062 private:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080063 std::vector<uint8_t> buffer_;
Austin Schuh40485ed2019-10-26 21:51:44 -070064};
65
Alex Perrycb7da4b2019-08-28 19:35:56 -070066// This class adapts a preallocated memory region to an Allocator.
67class PreallocatedAllocator : public FixedAllocatorBase {
68 public:
69 PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {}
Tyler Chatow116edf12020-01-26 11:52:39 -080070 PreallocatedAllocator(const PreallocatedAllocator &) = delete;
Brian Silvermana1652f32020-01-29 20:41:44 -080071 PreallocatedAllocator(PreallocatedAllocator &&other)
72 : data_(other.data_), size_(other.size_) {
Brian Silverman341b57e2020-06-23 16:23:18 -070073 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080074 CHECK(!other.is_allocated());
Alex Perrycb7da4b2019-08-28 19:35:56 -070075 }
Brian Silvermana1652f32020-01-29 20:41:44 -080076
77 PreallocatedAllocator &operator=(const PreallocatedAllocator &) = delete;
78 PreallocatedAllocator &operator=(PreallocatedAllocator &&other) {
Brian Silverman341b57e2020-06-23 16:23:18 -070079 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080080 CHECK(!other.is_allocated());
81 data_ = other.data_;
82 size_ = other.size_;
83 return *this;
84 }
85
86 uint8_t *data() final {
87 return reinterpret_cast<uint8_t *>(CHECK_NOTNULL(data_));
88 }
89 const uint8_t *data() const final {
90 return reinterpret_cast<const uint8_t *>(CHECK_NOTNULL(data_));
91 }
92 size_t size() const final { return size_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070093
94 private:
Brian Silvermana1652f32020-01-29 20:41:44 -080095 void *data_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070096 size_t size_;
97};
98
Austin Schuh40485ed2019-10-26 21:51:44 -070099// Base class representing an object which holds the memory representing a root
100// flatbuffer.
101template <typename T>
102class Flatbuffer {
103 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700104 virtual ~Flatbuffer() {}
Austin Schuh40485ed2019-10-26 21:51:44 -0700105 // Returns the MiniReflectTypeTable for T.
106 static const flatbuffers::TypeTable *MiniReflectTypeTable() {
107 return T::MiniReflectTypeTable();
108 }
109
110 // Returns a message from the buffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800111 virtual const T &message() const = 0;
Austin Schuh40485ed2019-10-26 21:51:44 -0700112 // Returns a mutable message. It can be mutated via the flatbuffer rules.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800113 virtual T *mutable_message() = 0;
Austin Schuh39580f12020-08-01 14:44:08 -0700114
115 // Wipes out the data buffer. This is handy to mark an instance as freed, and
116 // make attempts to use it fail more obviously.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800117 void Wipe() { memset(span().data(), 0, span().size()); }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800118
Austin Schuhadd6eb32020-11-09 21:24:26 -0800119 bool Verify() const {
Austin Schuh977a5ed2020-12-02 23:20:04 -0800120 if (span().size() < 4u) {
121 return false;
122 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800123 flatbuffers::Verifier v(span().data(), span().size());
Austin Schuha4fc60f2020-11-01 23:06:47 -0800124 return v.VerifyTable(&message());
125 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800126
127 protected:
128 virtual absl::Span<uint8_t> span() = 0;
129 virtual absl::Span<const uint8_t> span() const = 0;
130};
131
132// Base class for non-size prefixed flatbuffers. span() means different things
133// across the 2 types, so you end up with a different GetRoot.
134template <typename T>
135class NonSizePrefixedFlatbuffer : public Flatbuffer<T> {
136 public:
137 const T &message() const override {
138 return *flatbuffers::GetRoot<T>(
139 reinterpret_cast<const void *>(this->span().data()));
140 }
141 T *mutable_message() override {
142 return flatbuffers::GetMutableRoot<T>(
143 reinterpret_cast<void *>(this->span().data()));
144 }
145
146 absl::Span<uint8_t> span() override = 0;
147 absl::Span<const uint8_t> span() const override = 0;
Austin Schuha4fc60f2020-11-01 23:06:47 -0800148};
149
150// Non-owning Span backed flatbuffer.
151template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800152class FlatbufferSpan : public NonSizePrefixedFlatbuffer<T> {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800153 public:
154 // Builds a flatbuffer pointing to the contents of a span.
155 FlatbufferSpan(const absl::Span<const uint8_t> data) : data_(data) {}
156 // Builds a Flatbuffer pointing to the contents of another flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800157 FlatbufferSpan(const NonSizePrefixedFlatbuffer<T> &other) {
158 data_ = other.span();
159 }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800160
161 // Copies the data from the other flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800162 FlatbufferSpan &operator=(const NonSizePrefixedFlatbuffer<T> &other) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800163 data_ = other.span();
164 return *this;
165 }
166
167 virtual ~FlatbufferSpan() override {}
168
Austin Schuhadd6eb32020-11-09 21:24:26 -0800169 absl::Span<uint8_t> span() override {
170 LOG(FATAL) << "Unimplemented";
171 return absl::Span<uint8_t>(nullptr, 0);
Austin Schuha4fc60f2020-11-01 23:06:47 -0800172 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800173 absl::Span<const uint8_t> span() const override { return data_; }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800174
175 private:
176 absl::Span<const uint8_t> data_;
Austin Schuh40485ed2019-10-26 21:51:44 -0700177};
178
Austin Schuhadd6eb32020-11-09 21:24:26 -0800179// ResizeableBuffer backed flatbuffer.
Austin Schuhe309d2a2019-11-29 13:25:21 -0800180template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800181class FlatbufferVector : public NonSizePrefixedFlatbuffer<T> {
Austin Schuhe309d2a2019-11-29 13:25:21 -0800182 public:
Austin Schuhadd6eb32020-11-09 21:24:26 -0800183 // Builds a Flatbuffer around a ResizeableBuffer.
Brian Silverman354697a2020-09-22 21:06:32 -0700184 FlatbufferVector(ResizeableBuffer &&data) : data_(std::move(data)) {}
Austin Schuhe309d2a2019-11-29 13:25:21 -0800185
186 // Builds a Flatbuffer by copying the data from the other flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800187 FlatbufferVector(const NonSizePrefixedFlatbuffer<T> &other) {
188 data_.resize(other.span().size());
189 memcpy(data_.data(), other.span().data(), data_.size());
Brian Silverman354697a2020-09-22 21:06:32 -0700190 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800191
Austin Schuh5131bd02020-01-08 17:25:59 -0800192 // Copy constructor.
Brian Silverman354697a2020-09-22 21:06:32 -0700193 FlatbufferVector(const FlatbufferVector<T> &other) : data_(other.data_) {}
Austin Schuh5131bd02020-01-08 17:25:59 -0800194
Austin Schuh03803bd2019-12-30 18:08:17 -0800195 // Move constructor.
Austin Schuh5131bd02020-01-08 17:25:59 -0800196 FlatbufferVector(FlatbufferVector<T> &&other)
197 : data_(std::move(other.data_)) {}
Austin Schuh03803bd2019-12-30 18:08:17 -0800198
Austin Schuhe309d2a2019-11-29 13:25:21 -0800199 // Copies the data from the other flatbuffer.
Austin Schuh5131bd02020-01-08 17:25:59 -0800200 FlatbufferVector &operator=(const FlatbufferVector<T> &other) {
Brian Silverman354697a2020-09-22 21:06:32 -0700201 data_ = other.data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800202 return *this;
203 }
Austin Schuh5131bd02020-01-08 17:25:59 -0800204 FlatbufferVector &operator=(FlatbufferVector<T> &&other) {
205 data_ = std::move(other.data_);
206 return *this;
207 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800208
Austin Schuh05b70472020-01-01 17:11:17 -0800209 // Constructs an empty flatbuffer of type T.
210 static FlatbufferVector<T> Empty() {
Brian Silverman354697a2020-09-22 21:06:32 -0700211 return FlatbufferVector<T>(ResizeableBuffer());
Austin Schuh05b70472020-01-01 17:11:17 -0800212 }
213
Austin Schuhe309d2a2019-11-29 13:25:21 -0800214 virtual ~FlatbufferVector() override {}
215
Austin Schuhadd6eb32020-11-09 21:24:26 -0800216 absl::Span<uint8_t> span() override {
217 return absl::Span<uint8_t>(data_.data(), data_.size());
218 }
219 absl::Span<const uint8_t> span() const override {
220 return absl::Span<const uint8_t>(data_.data(), data_.size());
221 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800222
223 private:
Brian Silverman354697a2020-09-22 21:06:32 -0700224 ResizeableBuffer data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800225};
226
Austin Schuhe93d8642019-10-13 15:27:07 -0700227// This object associates the message type with the memory storing the
228// flatbuffer. This only stores root tables.
229//
230// From a usage point of view, pointers to the data are very different than
231// pointers to the tables.
232template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800233class FlatbufferDetachedBuffer final : public NonSizePrefixedFlatbuffer<T> {
Austin Schuhe93d8642019-10-13 15:27:07 -0700234 public:
235 // Builds a Flatbuffer by taking ownership of the buffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700236 FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
Austin Schuhe93d8642019-10-13 15:27:07 -0700237 : buffer_(::std::move(buffer)) {}
238
239 // Builds a flatbuffer by taking ownership of the buffer from the other
240 // flatbuffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700241 FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb)
242 : buffer_(::std::move(fb.buffer_)) {}
243 FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) {
Austin Schuhe93d8642019-10-13 15:27:07 -0700244 ::std::swap(buffer_, fb.buffer_);
245 return *this;
246 }
247
Alex Perrycb7da4b2019-08-28 19:35:56 -0700248 virtual ~FlatbufferDetachedBuffer() override {}
249
Austin Schuhe93d8642019-10-13 15:27:07 -0700250 // Constructs an empty flatbuffer of type T.
Austin Schuh40485ed2019-10-26 21:51:44 -0700251 static FlatbufferDetachedBuffer<T> Empty() {
Austin Schuhe93d8642019-10-13 15:27:07 -0700252 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800253 fbb.ForceDefaults(true);
Austin Schuhe93d8642019-10-13 15:27:07 -0700254 const auto end = fbb.EndTable(fbb.StartTable());
255 fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh40485ed2019-10-26 21:51:44 -0700256 return FlatbufferDetachedBuffer<T>(fbb.Release());
Austin Schuhe93d8642019-10-13 15:27:07 -0700257 }
258
259 // Returns references to the buffer, and the data.
260 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800261
262 absl::Span<uint8_t> span() override {
263 return absl::Span<uint8_t>(buffer_.data(), buffer_.size());
264 }
265 absl::Span<const uint8_t> span() const override {
266 return absl::Span<const uint8_t>(buffer_.data(), buffer_.size());
267 }
Austin Schuhe93d8642019-10-13 15:27:07 -0700268
269 private:
270 flatbuffers::DetachedBuffer buffer_;
271};
272
Tyler Chatow116edf12020-01-26 11:52:39 -0800273// Array backed flatbuffer which manages building of the flatbuffer.
274template <typename T, size_t Size>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800275class FlatbufferFixedAllocatorArray final
276 : public NonSizePrefixedFlatbuffer<T> {
Tyler Chatow116edf12020-01-26 11:52:39 -0800277 public:
Brian Silverman1715d472020-08-12 22:54:15 -0700278 FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {}
279
280 FlatbufferFixedAllocatorArray(const FlatbufferFixedAllocatorArray &) = delete;
Austin Schuhadd6eb32020-11-09 21:24:26 -0800281 void operator=(const NonSizePrefixedFlatbuffer<T> &) = delete;
Brian Silverman1715d472020-08-12 22:54:15 -0700282
Austin Schuhadd6eb32020-11-09 21:24:26 -0800283 void CopyFrom(const NonSizePrefixedFlatbuffer<T> &other) {
Brian Silverman1715d472020-08-12 22:54:15 -0700284 CHECK(!allocator_.is_allocated()) << ": May not overwrite while building";
Austin Schuh12e77842020-11-11 20:02:55 -0800285 CHECK_LE(other.span().size(), Size)
286 << ": Source flatbuffer is larger than the target.";
287 memcpy(buffer_.begin(), other.span().data(), other.span().size());
Brian Silverman1715d472020-08-12 22:54:15 -0700288 data_ = buffer_.begin();
Austin Schuh12e77842020-11-11 20:02:55 -0800289 size_ = other.span().size();
Tyler Chatow116edf12020-01-26 11:52:39 -0800290 }
291
Brian Silverman341b57e2020-06-23 16:23:18 -0700292 void Reset() {
Austin Schuh9dac21b2020-10-19 10:02:48 -0700293 CHECK(!allocator_.is_allocated() || data_ != nullptr)
294 << ": May not reset while building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700295 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
296 fbb_.ForceDefaults(true);
Austin Schuh9dac21b2020-10-19 10:02:48 -0700297 data_ = nullptr;
298 size_ = 0;
Brian Silverman341b57e2020-06-23 16:23:18 -0700299 }
300
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700301 flatbuffers::FlatBufferBuilder *fbb() {
Brian Silverman1715d472020-08-12 22:54:15 -0700302 CHECK(!allocator_.allocated())
303 << ": Array backed flatbuffer can only be built once";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700304 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
305 fbb_.ForceDefaults(true);
306 return &fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800307 }
308
309 void Finish(flatbuffers::Offset<T> root) {
Brian Silverman1715d472020-08-12 22:54:15 -0700310 CHECK(allocator_.allocated()) << ": Cannot finish if not building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700311 fbb_.Finish(root);
312 data_ = fbb_.GetBufferPointer();
313 size_ = fbb_.GetSize();
Brian Silverman1715d472020-08-12 22:54:15 -0700314 DCHECK_LE(size_, Size);
Tyler Chatow116edf12020-01-26 11:52:39 -0800315 }
316
Austin Schuhadd6eb32020-11-09 21:24:26 -0800317 absl::Span<uint8_t> span() override {
318 return absl::Span<uint8_t>(data_, size_);
Tyler Chatow116edf12020-01-26 11:52:39 -0800319 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800320 absl::Span<const uint8_t> span() const override {
321 return absl::Span<const uint8_t>(data_, size_);
Tyler Chatow116edf12020-01-26 11:52:39 -0800322 }
Tyler Chatow116edf12020-01-26 11:52:39 -0800323
324 private:
325 std::array<uint8_t, Size> buffer_;
326 PreallocatedAllocator allocator_;
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700327 flatbuffers::FlatBufferBuilder fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800328 uint8_t *data_ = nullptr;
329 size_t size_ = 0;
Tyler Chatow116edf12020-01-26 11:52:39 -0800330};
331
Austin Schuhadd6eb32020-11-09 21:24:26 -0800332template <typename T>
333class SizePrefixedFlatbuffer : public Flatbuffer<T> {
334 public:
335 const T &message() const override {
336 return *flatbuffers::GetSizePrefixedRoot<T>(
337 reinterpret_cast<const void *>(this->span().data()));
338 }
339
340 T *mutable_message() override {
341 return flatbuffers::GetMutableSizePrefixedRoot<T>(
342 reinterpret_cast<void *>(this->span().data()));
343 }
344
345 absl::Span<uint8_t> span() override = 0;
346 absl::Span<const uint8_t> span() const override = 0;
347};
348
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800349// This object associates the message type with the memory storing the
350// flatbuffer. This only stores root tables.
351//
352// From a usage point of view, pointers to the data are very different than
353// pointers to the tables.
354template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800355class SizePrefixedFlatbufferDetachedBuffer final
356 : public SizePrefixedFlatbuffer<T> {
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800357 public:
358 // Builds a Flatbuffer by taking ownership of the buffer.
359 SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
360 : buffer_(::std::move(buffer)) {
361 CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
362 }
363
364 // Builds a flatbuffer by taking ownership of the buffer from the other
365 // flatbuffer.
366 SizePrefixedFlatbufferDetachedBuffer(
367 SizePrefixedFlatbufferDetachedBuffer &&fb)
368 : buffer_(::std::move(fb.buffer_)) {}
369 SizePrefixedFlatbufferDetachedBuffer &operator=(
370 SizePrefixedFlatbufferDetachedBuffer &&fb) {
371 ::std::swap(buffer_, fb.buffer_);
372 return *this;
373 }
374
375 virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
376
Austin Schuh2f8fd752020-09-01 22:38:28 -0700377 static SizePrefixedFlatbufferDetachedBuffer<T> Empty() {
378 flatbuffers::FlatBufferBuilder fbb;
379 fbb.ForceDefaults(true);
380 const auto end = fbb.EndTable(fbb.StartTable());
381 fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end));
382 return SizePrefixedFlatbufferDetachedBuffer<T>(fbb.Release());
383 }
384
Austin Schuh8c399962020-12-25 21:51:45 -0800385 flatbuffers::DetachedBuffer Release() {
386 flatbuffers::FlatBufferBuilder fbb;
387 fbb.ForceDefaults(true);
388 const auto end = fbb.EndTable(fbb.StartTable());
Austin Schuhfd7b6312021-07-30 18:26:51 -0700389 fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh8c399962020-12-25 21:51:45 -0800390 flatbuffers::DetachedBuffer result = fbb.Release();
391 std::swap(result, buffer_);
392 return result;
393 }
394
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800395 // Returns references to the buffer, and the data.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800396 absl::Span<uint8_t> span() override {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700397 return absl::Span<uint8_t>(buffer_.data(), buffer_.size());
398 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800399 absl::Span<const uint8_t> span() const override {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700400 return absl::Span<const uint8_t>(buffer_.data(), buffer_.size());
401 }
402
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800403 private:
404 flatbuffers::DetachedBuffer buffer_;
405};
Austin Schuhe93d8642019-10-13 15:27:07 -0700406
Austin Schuhadd6eb32020-11-09 21:24:26 -0800407// ResizeableBuffer backed flatbuffer.
408template <typename T>
409class SizePrefixedFlatbufferVector : public SizePrefixedFlatbuffer<T> {
410 public:
411 // Builds a Flatbuffer around a ResizeableBuffer.
412 SizePrefixedFlatbufferVector(ResizeableBuffer &&data)
413 : data_(std::move(data)) {}
414
415 // Builds a Flatbuffer by copying the data from the other flatbuffer.
Austin Schuhb929c4e2021-07-12 15:32:53 -0700416 SizePrefixedFlatbufferVector(const SizePrefixedFlatbuffer<T> &other)
417 : SizePrefixedFlatbufferVector(other.span()) {}
418
419 // Builds a flatbuffer by copying the data from the provided span.
420 SizePrefixedFlatbufferVector(const absl::Span<const uint8_t> span) {
421 data_.resize(span.size());
422 memcpy(data_.data(), span.data(), data_.size());
Austin Schuhadd6eb32020-11-09 21:24:26 -0800423 }
424
425 // Copy constructor.
426 SizePrefixedFlatbufferVector(const SizePrefixedFlatbufferVector<T> &other)
427 : data_(other.data_) {}
428
429 // Move constructor.
430 SizePrefixedFlatbufferVector(SizePrefixedFlatbufferVector<T> &&other)
431 : data_(std::move(other.data_)) {}
432
433 // Copies the data from the other flatbuffer.
434 SizePrefixedFlatbufferVector &operator=(
435 const SizePrefixedFlatbufferVector<T> &other) {
436 data_ = other.data_;
437 return *this;
438 }
439 SizePrefixedFlatbufferVector &operator=(
440 SizePrefixedFlatbufferVector<T> &&other) {
441 data_ = std::move(other.data_);
442 return *this;
443 }
444
445 // Constructs an empty flatbuffer of type T.
446 static SizePrefixedFlatbufferVector<T> Empty() {
447 return SizePrefixedFlatbufferVector<T>(ResizeableBuffer());
448 }
449
450 virtual ~SizePrefixedFlatbufferVector() override {}
451
452 absl::Span<uint8_t> span() override {
453 return absl::Span<uint8_t>(data_.data(), data_.size());
454 }
455 absl::Span<const uint8_t> span() const override {
456 return absl::Span<const uint8_t>(data_.data(), data_.size());
457 }
458
459 private:
460 ResizeableBuffer data_;
461};
462
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800463// Non-owning Span backed flatbuffer.
464template <typename T>
465class SizePrefixedFlatbufferSpan : public SizePrefixedFlatbuffer<T> {
466 public:
467 // Builds a flatbuffer pointing to the contents of a span.
468 SizePrefixedFlatbufferSpan(const absl::Span<const uint8_t> data)
469 : data_(data) {}
470 // Builds a Flatbuffer pointing to the contents of another flatbuffer.
471 SizePrefixedFlatbufferSpan(const SizePrefixedFlatbuffer<T> &other) {
472 data_ = other.span();
473 }
474
475 // Points to the data in the other flatbuffer.
476 SizePrefixedFlatbufferSpan &operator=(
477 const SizePrefixedFlatbuffer<T> &other) {
478 data_ = other.span();
479 return *this;
480 }
481
482 ~SizePrefixedFlatbufferSpan() override {}
483
484 absl::Span<uint8_t> span() override {
485 LOG(FATAL) << "Unimplemented";
486 return absl::Span<uint8_t>(nullptr, 0);
487 }
488 absl::Span<const uint8_t> span() const override { return data_; }
489
490 private:
491 absl::Span<const uint8_t> data_;
492};
493
Brian Silvermanf51499a2020-09-21 12:49:08 -0700494inline flatbuffers::DetachedBuffer CopySpanAsDetachedBuffer(
495 absl::Span<const uint8_t> span) {
496 // Copy the data from the span.
497 uint8_t *buf = flatbuffers::DefaultAllocator().allocate(span.size());
498 memcpy(buf, span.data(), span.size());
499 // Then give it to a DetachedBuffer to manage.
500 return flatbuffers::DetachedBuffer(nullptr, false, buf, span.size(), buf,
501 span.size());
502}
503
Austin Schuhe93d8642019-10-13 15:27:07 -0700504} // namespace aos
505
506#endif // AOS_FLATBUFFERS_H_