blob: c1c0db02a50b56e410a6def9e732a9b0a927e34a [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 Schuhe93d8642019-10-13 15:27:07 -07007#include "flatbuffers/flatbuffers.h"
Austin Schuhe84c3ed2019-12-14 15:29:48 -08008#include "glog/logging.h"
Austin Schuhe93d8642019-10-13 15:27:07 -07009
10namespace aos {
11
Austin Schuh40485ed2019-10-26 21:51:44 -070012// This class is a base class for all sizes of array backed allocators.
13class FixedAllocatorBase : public flatbuffers::Allocator {
14 public:
15 // TODO(austin): Read the contract for these.
16 uint8_t *allocate(size_t) override;
17
18 void deallocate(uint8_t *, size_t) override { is_allocated_ = false; }
19
20 uint8_t *reallocate_downward(uint8_t *, size_t, size_t, size_t,
21 size_t) override;
22
23 virtual const uint8_t *data() const = 0;
24 virtual uint8_t *data() = 0;
25 virtual size_t size() const = 0;
26
Alex Perrycb7da4b2019-08-28 19:35:56 -070027 void Reset() { is_allocated_ = false; }
28
Austin Schuh40485ed2019-10-26 21:51:44 -070029 private:
30 bool is_allocated_ = false;
31};
32
33// This class is a fixed memory allocator which holds the data for a flatbuffer
34// in an array.
Austin Schuh40485ed2019-10-26 21:51:44 -070035class FixedAllocator : public FixedAllocatorBase {
36 public:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080037 FixedAllocator(size_t size) : buffer_(size, 0) {}
38
Austin Schuh40485ed2019-10-26 21:51:44 -070039 uint8_t *data() override { return &buffer_[0]; }
40 const uint8_t *data() const override { return &buffer_[0]; }
41 size_t size() const override { return buffer_.size(); }
42
Austin Schuhe84c3ed2019-12-14 15:29:48 -080043 // Releases the data in the buffer.
44 std::vector<uint8_t> release() { return std::move(buffer_); }
45
Austin Schuh40485ed2019-10-26 21:51:44 -070046 private:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080047 std::vector<uint8_t> buffer_;
Austin Schuh40485ed2019-10-26 21:51:44 -070048};
49
Alex Perrycb7da4b2019-08-28 19:35:56 -070050// This class adapts a preallocated memory region to an Allocator.
51class PreallocatedAllocator : public FixedAllocatorBase {
52 public:
53 PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {}
54 uint8_t *data() override { return reinterpret_cast<uint8_t *>(data_); }
55 const uint8_t *data() const override {
56 return reinterpret_cast<const uint8_t *>(data_);
57 }
58 size_t size() const override { return size_; }
59
60 private:
61 void* data_;
62 size_t size_;
63};
64
Austin Schuh40485ed2019-10-26 21:51:44 -070065// Base class representing an object which holds the memory representing a root
66// flatbuffer.
67template <typename T>
68class Flatbuffer {
69 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070070 virtual ~Flatbuffer() {}
Austin Schuh40485ed2019-10-26 21:51:44 -070071 // Returns the MiniReflectTypeTable for T.
72 static const flatbuffers::TypeTable *MiniReflectTypeTable() {
73 return T::MiniReflectTypeTable();
74 }
75
76 // Returns a message from the buffer.
77 const T &message() const {
78 return *flatbuffers::GetRoot<T>(reinterpret_cast<const void *>(data()));
79 }
80 // Returns a mutable message. It can be mutated via the flatbuffer rules.
81 T *mutable_message() {
82 return flatbuffers::GetMutableRoot<T>(reinterpret_cast<void *>(data()));
83 }
84
85 virtual const uint8_t *data() const = 0;
86 virtual uint8_t *data() = 0;
87 virtual size_t size() const = 0;
88};
89
Alex Perrycb7da4b2019-08-28 19:35:56 -070090// String backed flatbuffer.
91template <typename T>
92class FlatbufferString : public Flatbuffer<T> {
93 public:
94 // Builds a flatbuffer using the contents of the string.
James Kuszmaul3ae42262019-11-08 12:33:41 -080095 FlatbufferString(const std::string_view data) : data_(data) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -070096 // Builds a Flatbuffer by copying the data from the other flatbuffer.
97 FlatbufferString(const Flatbuffer<T> &other) {
98 data_ = std::string(other.data(), other.size());
99 }
100
101 // Coppies the data from the other flatbuffer.
102 FlatbufferString &operator=(const Flatbuffer<T> &other) {
103 data_ = std::string(other.data(), other.size());
104 return *this;
105 }
106
107 virtual ~FlatbufferString() override {}
108
109 const uint8_t *data() const override {
110 return reinterpret_cast<const uint8_t *>(data_.data());
111 }
James Kuszmaul872efd22019-12-03 20:59:09 -0800112 uint8_t *data() override { return reinterpret_cast<uint8_t *>(data_.data()); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700113 size_t size() const override { return data_.size(); }
114
115 private:
116 std::string data_;
117};
118
Austin Schuhe309d2a2019-11-29 13:25:21 -0800119// Vector backed flatbuffer.
120template <typename T>
121class FlatbufferVector : public Flatbuffer<T> {
122 public:
123 // Builds a Flatbuffer around a vector.
124 FlatbufferVector(std::vector<uint8_t> &&data) : data_(std::move(data)) {}
125
126 // Builds a Flatbuffer by copying the data from the other flatbuffer.
127 FlatbufferVector(const Flatbuffer<T> &other)
128 : data_(other.data(), other.data() + other.size()) {}
129
Austin Schuh5131bd02020-01-08 17:25:59 -0800130 // Copy constructor.
131 FlatbufferVector(const FlatbufferVector<T> &other)
132 : data_(other.data(), other.data() + other.size()) {}
133
Austin Schuh03803bd2019-12-30 18:08:17 -0800134 // Move constructor.
Austin Schuh5131bd02020-01-08 17:25:59 -0800135 FlatbufferVector(FlatbufferVector<T> &&other)
136 : data_(std::move(other.data_)) {}
Austin Schuh03803bd2019-12-30 18:08:17 -0800137
Austin Schuhe309d2a2019-11-29 13:25:21 -0800138 // Copies the data from the other flatbuffer.
Austin Schuh5131bd02020-01-08 17:25:59 -0800139 FlatbufferVector &operator=(const FlatbufferVector<T> &other) {
Austin Schuhe309d2a2019-11-29 13:25:21 -0800140 data_ = std::vector<uint8_t>(other.data(), other.data() + other.size());
141 return *this;
142 }
Austin Schuh5131bd02020-01-08 17:25:59 -0800143 FlatbufferVector &operator=(FlatbufferVector<T> &&other) {
144 data_ = std::move(other.data_);
145 return *this;
146 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800147
Austin Schuh05b70472020-01-01 17:11:17 -0800148 // Constructs an empty flatbuffer of type T.
149 static FlatbufferVector<T> Empty() {
150 return FlatbufferVector<T>(std::vector<uint8_t>{});
151 }
152
Austin Schuhe309d2a2019-11-29 13:25:21 -0800153 virtual ~FlatbufferVector() override {}
154
155 const uint8_t *data() const override { return data_.data(); }
156 uint8_t *data() override { return data_.data(); }
157 size_t size() const override { return data_.size(); }
158
159 private:
160 std::vector<uint8_t> data_;
161};
162
Austin Schuhe93d8642019-10-13 15:27:07 -0700163// This object associates the message type with the memory storing the
164// flatbuffer. This only stores root tables.
165//
166// From a usage point of view, pointers to the data are very different than
167// pointers to the tables.
168template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800169class FlatbufferDetachedBuffer final : public Flatbuffer<T> {
Austin Schuhe93d8642019-10-13 15:27:07 -0700170 public:
171 // Builds a Flatbuffer by taking ownership of the buffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700172 FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
Austin Schuhe93d8642019-10-13 15:27:07 -0700173 : buffer_(::std::move(buffer)) {}
174
175 // Builds a flatbuffer by taking ownership of the buffer from the other
176 // flatbuffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700177 FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb)
178 : buffer_(::std::move(fb.buffer_)) {}
179 FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) {
Austin Schuhe93d8642019-10-13 15:27:07 -0700180 ::std::swap(buffer_, fb.buffer_);
181 return *this;
182 }
183
Alex Perrycb7da4b2019-08-28 19:35:56 -0700184 virtual ~FlatbufferDetachedBuffer() override {}
185
Austin Schuhe93d8642019-10-13 15:27:07 -0700186 // Constructs an empty flatbuffer of type T.
Austin Schuh40485ed2019-10-26 21:51:44 -0700187 static FlatbufferDetachedBuffer<T> Empty() {
Austin Schuhe93d8642019-10-13 15:27:07 -0700188 flatbuffers::FlatBufferBuilder fbb;
189 fbb.ForceDefaults(1);
190 const auto end = fbb.EndTable(fbb.StartTable());
191 fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh40485ed2019-10-26 21:51:44 -0700192 return FlatbufferDetachedBuffer<T>(fbb.Release());
Austin Schuhe93d8642019-10-13 15:27:07 -0700193 }
194
195 // Returns references to the buffer, and the data.
196 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
Austin Schuh40485ed2019-10-26 21:51:44 -0700197 const uint8_t *data() const override { return buffer_.data(); }
198 uint8_t *data() override { return buffer_.data(); }
199 size_t size() const override { return buffer_.size(); }
Austin Schuhe93d8642019-10-13 15:27:07 -0700200
201 private:
202 flatbuffers::DetachedBuffer buffer_;
203};
204
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800205// This object associates the message type with the memory storing the
206// flatbuffer. This only stores root tables.
207//
208// From a usage point of view, pointers to the data are very different than
209// pointers to the tables.
210template <typename T>
211class SizePrefixedFlatbufferDetachedBuffer final : public Flatbuffer<T> {
212 public:
213 // Builds a Flatbuffer by taking ownership of the buffer.
214 SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
215 : buffer_(::std::move(buffer)) {
216 CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
217 }
218
219 // Builds a flatbuffer by taking ownership of the buffer from the other
220 // flatbuffer.
221 SizePrefixedFlatbufferDetachedBuffer(
222 SizePrefixedFlatbufferDetachedBuffer &&fb)
223 : buffer_(::std::move(fb.buffer_)) {}
224 SizePrefixedFlatbufferDetachedBuffer &operator=(
225 SizePrefixedFlatbufferDetachedBuffer &&fb) {
226 ::std::swap(buffer_, fb.buffer_);
227 return *this;
228 }
229
230 virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
231
232 // Returns references to the buffer, and the data.
233 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
234 const uint8_t *data() const override {
235 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
236 }
237 uint8_t *data() override {
238 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
239 }
240 size_t size() const override {
241 return buffer_.size() - sizeof(flatbuffers::uoffset_t);
242 }
243
244 private:
245 flatbuffers::DetachedBuffer buffer_;
246};
Austin Schuhe93d8642019-10-13 15:27:07 -0700247// TODO(austin): Need a way to get our hands on the max size. Can start with
248// "large" for now.
249
250} // namespace aos
251
252#endif // AOS_FLATBUFFERS_H_