blob: 438868734c350c60121beba3c0e5fd6a7cce0de8 [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 Schuh03803bd2019-12-30 18:08:17 -0800130 // Move constructor.
131 FlatbufferVector(Flatbuffer<T> &&other) : data_(std::move(other.data())) {}
132
Austin Schuhe309d2a2019-11-29 13:25:21 -0800133 // Copies the data from the other flatbuffer.
134 FlatbufferVector &operator=(const Flatbuffer<T> &other) {
135 data_ = std::vector<uint8_t>(other.data(), other.data() + other.size());
136 return *this;
137 }
138
Austin Schuh05b70472020-01-01 17:11:17 -0800139 // Constructs an empty flatbuffer of type T.
140 static FlatbufferVector<T> Empty() {
141 return FlatbufferVector<T>(std::vector<uint8_t>{});
142 }
143
Austin Schuhe309d2a2019-11-29 13:25:21 -0800144 virtual ~FlatbufferVector() override {}
145
146 const uint8_t *data() const override { return data_.data(); }
147 uint8_t *data() override { return data_.data(); }
148 size_t size() const override { return data_.size(); }
149
150 private:
151 std::vector<uint8_t> data_;
152};
153
Austin Schuhe93d8642019-10-13 15:27:07 -0700154// This object associates the message type with the memory storing the
155// flatbuffer. This only stores root tables.
156//
157// From a usage point of view, pointers to the data are very different than
158// pointers to the tables.
159template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800160class FlatbufferDetachedBuffer final : public Flatbuffer<T> {
Austin Schuhe93d8642019-10-13 15:27:07 -0700161 public:
162 // Builds a Flatbuffer by taking ownership of the buffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700163 FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
Austin Schuhe93d8642019-10-13 15:27:07 -0700164 : buffer_(::std::move(buffer)) {}
165
166 // Builds a flatbuffer by taking ownership of the buffer from the other
167 // flatbuffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700168 FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb)
169 : buffer_(::std::move(fb.buffer_)) {}
170 FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) {
Austin Schuhe93d8642019-10-13 15:27:07 -0700171 ::std::swap(buffer_, fb.buffer_);
172 return *this;
173 }
174
Alex Perrycb7da4b2019-08-28 19:35:56 -0700175 virtual ~FlatbufferDetachedBuffer() override {}
176
Austin Schuhe93d8642019-10-13 15:27:07 -0700177 // Constructs an empty flatbuffer of type T.
Austin Schuh40485ed2019-10-26 21:51:44 -0700178 static FlatbufferDetachedBuffer<T> Empty() {
Austin Schuhe93d8642019-10-13 15:27:07 -0700179 flatbuffers::FlatBufferBuilder fbb;
180 fbb.ForceDefaults(1);
181 const auto end = fbb.EndTable(fbb.StartTable());
182 fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh40485ed2019-10-26 21:51:44 -0700183 return FlatbufferDetachedBuffer<T>(fbb.Release());
Austin Schuhe93d8642019-10-13 15:27:07 -0700184 }
185
186 // Returns references to the buffer, and the data.
187 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
Austin Schuh40485ed2019-10-26 21:51:44 -0700188 const uint8_t *data() const override { return buffer_.data(); }
189 uint8_t *data() override { return buffer_.data(); }
190 size_t size() const override { return buffer_.size(); }
Austin Schuhe93d8642019-10-13 15:27:07 -0700191
192 private:
193 flatbuffers::DetachedBuffer buffer_;
194};
195
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800196// This object associates the message type with the memory storing the
197// flatbuffer. This only stores root tables.
198//
199// From a usage point of view, pointers to the data are very different than
200// pointers to the tables.
201template <typename T>
202class SizePrefixedFlatbufferDetachedBuffer final : public Flatbuffer<T> {
203 public:
204 // Builds a Flatbuffer by taking ownership of the buffer.
205 SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
206 : buffer_(::std::move(buffer)) {
207 CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
208 }
209
210 // Builds a flatbuffer by taking ownership of the buffer from the other
211 // flatbuffer.
212 SizePrefixedFlatbufferDetachedBuffer(
213 SizePrefixedFlatbufferDetachedBuffer &&fb)
214 : buffer_(::std::move(fb.buffer_)) {}
215 SizePrefixedFlatbufferDetachedBuffer &operator=(
216 SizePrefixedFlatbufferDetachedBuffer &&fb) {
217 ::std::swap(buffer_, fb.buffer_);
218 return *this;
219 }
220
221 virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
222
223 // Returns references to the buffer, and the data.
224 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
225 const uint8_t *data() const override {
226 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
227 }
228 uint8_t *data() override {
229 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
230 }
231 size_t size() const override {
232 return buffer_.size() - sizeof(flatbuffers::uoffset_t);
233 }
234
235 private:
236 flatbuffers::DetachedBuffer buffer_;
237};
Austin Schuhe93d8642019-10-13 15:27:07 -0700238// TODO(austin): Need a way to get our hands on the max size. Can start with
239// "large" for now.
240
241} // namespace aos
242
243#endif // AOS_FLATBUFFERS_H_