blob: e556c0fee84afd70685f66f70748498ec16bf255 [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"
Austin Schuhe93d8642019-10-13 15:27:07 -07008#include "flatbuffers/flatbuffers.h"
Austin Schuhe84c3ed2019-12-14 15:29:48 -08009#include "glog/logging.h"
Austin Schuhe93d8642019-10-13 15:27:07 -070010
11namespace aos {
12
Austin Schuh40485ed2019-10-26 21:51:44 -070013// This class is a base class for all sizes of array backed allocators.
14class FixedAllocatorBase : public flatbuffers::Allocator {
15 public:
Brian Silvermana1652f32020-01-29 20:41:44 -080016 ~FixedAllocatorBase() override { CHECK(!is_allocated_); }
17
Austin Schuh40485ed2019-10-26 21:51:44 -070018 // TODO(austin): Read the contract for these.
19 uint8_t *allocate(size_t) override;
20
21 void deallocate(uint8_t *, size_t) override { is_allocated_ = false; }
22
23 uint8_t *reallocate_downward(uint8_t *, size_t, size_t, size_t,
24 size_t) override;
25
26 virtual const uint8_t *data() const = 0;
27 virtual uint8_t *data() = 0;
28 virtual size_t size() const = 0;
29
Alex Perrycb7da4b2019-08-28 19:35:56 -070030 void Reset() { is_allocated_ = false; }
Brian Silvermana1652f32020-01-29 20:41:44 -080031 bool is_allocated() const { return is_allocated_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070032
Austin Schuh40485ed2019-10-26 21:51:44 -070033 private:
34 bool is_allocated_ = false;
35};
36
37// This class is a fixed memory allocator which holds the data for a flatbuffer
38// in an array.
Austin Schuh40485ed2019-10-26 21:51:44 -070039class FixedAllocator : public FixedAllocatorBase {
40 public:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080041 FixedAllocator(size_t size) : buffer_(size, 0) {}
42
Austin Schuh40485ed2019-10-26 21:51:44 -070043 uint8_t *data() override { return &buffer_[0]; }
44 const uint8_t *data() const override { return &buffer_[0]; }
45 size_t size() const override { return buffer_.size(); }
46
Austin Schuhe84c3ed2019-12-14 15:29:48 -080047 // Releases the data in the buffer.
48 std::vector<uint8_t> release() { return std::move(buffer_); }
49
Austin Schuh40485ed2019-10-26 21:51:44 -070050 private:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080051 std::vector<uint8_t> buffer_;
Austin Schuh40485ed2019-10-26 21:51:44 -070052};
53
Alex Perrycb7da4b2019-08-28 19:35:56 -070054// This class adapts a preallocated memory region to an Allocator.
55class PreallocatedAllocator : public FixedAllocatorBase {
56 public:
57 PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {}
Brian Silvermana1652f32020-01-29 20:41:44 -080058 PreallocatedAllocator(const PreallocatedAllocator&) = delete;
59 PreallocatedAllocator(PreallocatedAllocator &&other)
60 : data_(other.data_), size_(other.size_) {
61 CHECK(!is_allocated());
62 CHECK(!other.is_allocated());
Alex Perrycb7da4b2019-08-28 19:35:56 -070063 }
Brian Silvermana1652f32020-01-29 20:41:44 -080064
65 PreallocatedAllocator &operator=(const PreallocatedAllocator &) = delete;
66 PreallocatedAllocator &operator=(PreallocatedAllocator &&other) {
67 CHECK(!is_allocated());
68 CHECK(!other.is_allocated());
69 data_ = other.data_;
70 size_ = other.size_;
71 return *this;
72 }
73
74 uint8_t *data() final {
75 return reinterpret_cast<uint8_t *>(CHECK_NOTNULL(data_));
76 }
77 const uint8_t *data() const final {
78 return reinterpret_cast<const uint8_t *>(CHECK_NOTNULL(data_));
79 }
80 size_t size() const final { return size_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070081
82 private:
Brian Silvermana1652f32020-01-29 20:41:44 -080083 void *data_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070084 size_t size_;
85};
86
Austin Schuh40485ed2019-10-26 21:51:44 -070087// Base class representing an object which holds the memory representing a root
88// flatbuffer.
89template <typename T>
90class Flatbuffer {
91 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070092 virtual ~Flatbuffer() {}
Austin Schuh40485ed2019-10-26 21:51:44 -070093 // Returns the MiniReflectTypeTable for T.
94 static const flatbuffers::TypeTable *MiniReflectTypeTable() {
95 return T::MiniReflectTypeTable();
96 }
97
98 // Returns a message from the buffer.
99 const T &message() const {
100 return *flatbuffers::GetRoot<T>(reinterpret_cast<const void *>(data()));
101 }
102 // Returns a mutable message. It can be mutated via the flatbuffer rules.
103 T *mutable_message() {
104 return flatbuffers::GetMutableRoot<T>(reinterpret_cast<void *>(data()));
105 }
106
107 virtual const uint8_t *data() const = 0;
108 virtual uint8_t *data() = 0;
109 virtual size_t size() const = 0;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800110
111 absl::Span<uint8_t> span() { return absl::Span<uint8_t>(data(), size()); }
112 absl::Span<const uint8_t> span() const {
113 return absl::Span<const uint8_t>(data(), size());
114 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700115};
116
Alex Perrycb7da4b2019-08-28 19:35:56 -0700117// String backed flatbuffer.
118template <typename T>
119class FlatbufferString : public Flatbuffer<T> {
120 public:
121 // Builds a flatbuffer using the contents of the string.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800122 FlatbufferString(const std::string_view data) : data_(data) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700123 // Builds a Flatbuffer by copying the data from the other flatbuffer.
124 FlatbufferString(const Flatbuffer<T> &other) {
125 data_ = std::string(other.data(), other.size());
126 }
127
128 // Coppies the data from the other flatbuffer.
129 FlatbufferString &operator=(const Flatbuffer<T> &other) {
130 data_ = std::string(other.data(), other.size());
131 return *this;
132 }
133
134 virtual ~FlatbufferString() override {}
135
136 const uint8_t *data() const override {
137 return reinterpret_cast<const uint8_t *>(data_.data());
138 }
James Kuszmaul872efd22019-12-03 20:59:09 -0800139 uint8_t *data() override { return reinterpret_cast<uint8_t *>(data_.data()); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700140 size_t size() const override { return data_.size(); }
141
142 private:
143 std::string data_;
144};
145
Austin Schuhe309d2a2019-11-29 13:25:21 -0800146// Vector backed flatbuffer.
147template <typename T>
148class FlatbufferVector : public Flatbuffer<T> {
149 public:
150 // Builds a Flatbuffer around a vector.
151 FlatbufferVector(std::vector<uint8_t> &&data) : data_(std::move(data)) {}
152
153 // Builds a Flatbuffer by copying the data from the other flatbuffer.
154 FlatbufferVector(const Flatbuffer<T> &other)
155 : data_(other.data(), other.data() + other.size()) {}
156
Austin Schuh5131bd02020-01-08 17:25:59 -0800157 // Copy constructor.
158 FlatbufferVector(const FlatbufferVector<T> &other)
159 : data_(other.data(), other.data() + other.size()) {}
160
Austin Schuh03803bd2019-12-30 18:08:17 -0800161 // Move constructor.
Austin Schuh5131bd02020-01-08 17:25:59 -0800162 FlatbufferVector(FlatbufferVector<T> &&other)
163 : data_(std::move(other.data_)) {}
Austin Schuh03803bd2019-12-30 18:08:17 -0800164
Austin Schuhe309d2a2019-11-29 13:25:21 -0800165 // Copies the data from the other flatbuffer.
Austin Schuh5131bd02020-01-08 17:25:59 -0800166 FlatbufferVector &operator=(const FlatbufferVector<T> &other) {
Austin Schuhe309d2a2019-11-29 13:25:21 -0800167 data_ = std::vector<uint8_t>(other.data(), other.data() + other.size());
168 return *this;
169 }
Austin Schuh5131bd02020-01-08 17:25:59 -0800170 FlatbufferVector &operator=(FlatbufferVector<T> &&other) {
171 data_ = std::move(other.data_);
172 return *this;
173 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800174
Austin Schuh05b70472020-01-01 17:11:17 -0800175 // Constructs an empty flatbuffer of type T.
176 static FlatbufferVector<T> Empty() {
177 return FlatbufferVector<T>(std::vector<uint8_t>{});
178 }
179
Austin Schuhe309d2a2019-11-29 13:25:21 -0800180 virtual ~FlatbufferVector() override {}
181
182 const uint8_t *data() const override { return data_.data(); }
183 uint8_t *data() override { return data_.data(); }
184 size_t size() const override { return data_.size(); }
185
186 private:
187 std::vector<uint8_t> data_;
188};
189
Austin Schuhe93d8642019-10-13 15:27:07 -0700190// This object associates the message type with the memory storing the
191// flatbuffer. This only stores root tables.
192//
193// From a usage point of view, pointers to the data are very different than
194// pointers to the tables.
195template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800196class FlatbufferDetachedBuffer final : public Flatbuffer<T> {
Austin Schuhe93d8642019-10-13 15:27:07 -0700197 public:
198 // Builds a Flatbuffer by taking ownership of the buffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700199 FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
Austin Schuhe93d8642019-10-13 15:27:07 -0700200 : buffer_(::std::move(buffer)) {}
201
202 // Builds a flatbuffer by taking ownership of the buffer from the other
203 // flatbuffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700204 FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb)
205 : buffer_(::std::move(fb.buffer_)) {}
206 FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) {
Austin Schuhe93d8642019-10-13 15:27:07 -0700207 ::std::swap(buffer_, fb.buffer_);
208 return *this;
209 }
210
Alex Perrycb7da4b2019-08-28 19:35:56 -0700211 virtual ~FlatbufferDetachedBuffer() override {}
212
Austin Schuhe93d8642019-10-13 15:27:07 -0700213 // Constructs an empty flatbuffer of type T.
Austin Schuh40485ed2019-10-26 21:51:44 -0700214 static FlatbufferDetachedBuffer<T> Empty() {
Austin Schuhe93d8642019-10-13 15:27:07 -0700215 flatbuffers::FlatBufferBuilder fbb;
216 fbb.ForceDefaults(1);
217 const auto end = fbb.EndTable(fbb.StartTable());
218 fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh40485ed2019-10-26 21:51:44 -0700219 return FlatbufferDetachedBuffer<T>(fbb.Release());
Austin Schuhe93d8642019-10-13 15:27:07 -0700220 }
221
222 // Returns references to the buffer, and the data.
223 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
Austin Schuh40485ed2019-10-26 21:51:44 -0700224 const uint8_t *data() const override { return buffer_.data(); }
225 uint8_t *data() override { return buffer_.data(); }
226 size_t size() const override { return buffer_.size(); }
Austin Schuhe93d8642019-10-13 15:27:07 -0700227
228 private:
229 flatbuffers::DetachedBuffer buffer_;
230};
231
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800232// This object associates the message type with the memory storing the
233// flatbuffer. This only stores root tables.
234//
235// From a usage point of view, pointers to the data are very different than
236// pointers to the tables.
237template <typename T>
238class SizePrefixedFlatbufferDetachedBuffer final : public Flatbuffer<T> {
239 public:
240 // Builds a Flatbuffer by taking ownership of the buffer.
241 SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
242 : buffer_(::std::move(buffer)) {
243 CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
244 }
245
246 // Builds a flatbuffer by taking ownership of the buffer from the other
247 // flatbuffer.
248 SizePrefixedFlatbufferDetachedBuffer(
249 SizePrefixedFlatbufferDetachedBuffer &&fb)
250 : buffer_(::std::move(fb.buffer_)) {}
251 SizePrefixedFlatbufferDetachedBuffer &operator=(
252 SizePrefixedFlatbufferDetachedBuffer &&fb) {
253 ::std::swap(buffer_, fb.buffer_);
254 return *this;
255 }
256
257 virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
258
259 // Returns references to the buffer, and the data.
260 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
261 const uint8_t *data() const override {
262 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
263 }
264 uint8_t *data() override {
265 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
266 }
267 size_t size() const override {
268 return buffer_.size() - sizeof(flatbuffers::uoffset_t);
269 }
270
271 private:
272 flatbuffers::DetachedBuffer buffer_;
273};
Austin Schuhe93d8642019-10-13 15:27:07 -0700274// TODO(austin): Need a way to get our hands on the max size. Can start with
275// "large" for now.
276
277} // namespace aos
278
279#endif // AOS_FLATBUFFERS_H_