Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 1 | #ifndef AOS_FLATBUFFERS_H_ |
| 2 | #define AOS_FLATBUFFERS_H_ |
| 3 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 4 | #include <array> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 5 | #include <string_view> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 6 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 7 | #include "absl/types/span.h" |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 8 | #include "aos/containers/resizeable_buffer.h" |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 9 | #include "aos/macros.h" |
Austin Schuh | b807581 | 2020-10-19 09:36:49 -0700 | [diff] [blame] | 10 | #include "flatbuffers/flatbuffers.h" // IWYU pragma: export |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 11 | #include "glog/logging.h" |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 12 | |
| 13 | namespace aos { |
| 14 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 15 | // This class is a base class for all sizes of array backed allocators. |
| 16 | class FixedAllocatorBase : public flatbuffers::Allocator { |
| 17 | public: |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 18 | ~FixedAllocatorBase() override { CHECK(!is_allocated_); } |
| 19 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 20 | // TODO(austin): Read the contract for these. |
| 21 | uint8_t *allocate(size_t) override; |
| 22 | |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 23 | 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 Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 29 | |
| 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 Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 37 | void Reset() { |
| 38 | CHECK(!is_allocated_); |
| 39 | is_allocated_ = false; |
| 40 | } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 41 | bool is_allocated() const { return is_allocated_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 42 | |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 43 | bool allocated() { return is_allocated_; } |
| 44 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 45 | private: |
| 46 | bool is_allocated_ = false; |
| 47 | }; |
| 48 | |
| 49 | // This class is a fixed memory allocator which holds the data for a flatbuffer |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 50 | // in a vector. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 51 | class FixedAllocator : public FixedAllocatorBase { |
| 52 | public: |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 53 | FixedAllocator(size_t size) : buffer_(size, 0) {} |
| 54 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 55 | 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 Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 59 | // Releases the data in the buffer. |
| 60 | std::vector<uint8_t> release() { return std::move(buffer_); } |
| 61 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 62 | private: |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 63 | std::vector<uint8_t> buffer_; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 66 | // This class adapts a preallocated memory region to an Allocator. |
| 67 | class PreallocatedAllocator : public FixedAllocatorBase { |
| 68 | public: |
| 69 | PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {} |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 70 | PreallocatedAllocator(const PreallocatedAllocator &) = delete; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 71 | PreallocatedAllocator(PreallocatedAllocator &&other) |
| 72 | : data_(other.data_), size_(other.size_) { |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 73 | CHECK(!is_allocated()) << ": May not overwrite in-use allocator"; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 74 | CHECK(!other.is_allocated()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 75 | } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 76 | |
| 77 | PreallocatedAllocator &operator=(const PreallocatedAllocator &) = delete; |
| 78 | PreallocatedAllocator &operator=(PreallocatedAllocator &&other) { |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 79 | CHECK(!is_allocated()) << ": May not overwrite in-use allocator"; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 80 | 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 93 | |
| 94 | private: |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 95 | void *data_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 96 | size_t size_; |
| 97 | }; |
| 98 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 99 | // Base class representing an object which holds the memory representing a root |
| 100 | // flatbuffer. |
| 101 | template <typename T> |
| 102 | class Flatbuffer { |
| 103 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 104 | virtual ~Flatbuffer() {} |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 105 | // 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 Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 111 | virtual const T &message() const = 0; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 112 | // Returns a mutable message. It can be mutated via the flatbuffer rules. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 113 | virtual T *mutable_message() = 0; |
Austin Schuh | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 114 | |
| 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 Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 117 | void Wipe() { memset(span().data(), 0, span().size()); } |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 118 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 119 | bool Verify() const { |
| 120 | flatbuffers::Verifier v(span().data(), span().size()); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 121 | return v.VerifyTable(&message()); |
| 122 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 123 | |
| 124 | protected: |
| 125 | virtual absl::Span<uint8_t> span() = 0; |
| 126 | virtual absl::Span<const uint8_t> span() const = 0; |
| 127 | }; |
| 128 | |
| 129 | // Base class for non-size prefixed flatbuffers. span() means different things |
| 130 | // across the 2 types, so you end up with a different GetRoot. |
| 131 | template <typename T> |
| 132 | class NonSizePrefixedFlatbuffer : public Flatbuffer<T> { |
| 133 | public: |
| 134 | const T &message() const override { |
| 135 | return *flatbuffers::GetRoot<T>( |
| 136 | reinterpret_cast<const void *>(this->span().data())); |
| 137 | } |
| 138 | T *mutable_message() override { |
| 139 | return flatbuffers::GetMutableRoot<T>( |
| 140 | reinterpret_cast<void *>(this->span().data())); |
| 141 | } |
| 142 | |
| 143 | absl::Span<uint8_t> span() override = 0; |
| 144 | absl::Span<const uint8_t> span() const override = 0; |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | // Non-owning Span backed flatbuffer. |
| 148 | template <typename T> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 149 | class FlatbufferSpan : public NonSizePrefixedFlatbuffer<T> { |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 150 | public: |
| 151 | // Builds a flatbuffer pointing to the contents of a span. |
| 152 | FlatbufferSpan(const absl::Span<const uint8_t> data) : data_(data) {} |
| 153 | // Builds a Flatbuffer pointing to the contents of another flatbuffer. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 154 | FlatbufferSpan(const NonSizePrefixedFlatbuffer<T> &other) { |
| 155 | data_ = other.span(); |
| 156 | } |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 157 | |
| 158 | // Copies the data from the other flatbuffer. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 159 | FlatbufferSpan &operator=(const NonSizePrefixedFlatbuffer<T> &other) { |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 160 | data_ = other.span(); |
| 161 | return *this; |
| 162 | } |
| 163 | |
| 164 | virtual ~FlatbufferSpan() override {} |
| 165 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 166 | absl::Span<uint8_t> span() override { |
| 167 | LOG(FATAL) << "Unimplemented"; |
| 168 | return absl::Span<uint8_t>(nullptr, 0); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 169 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 170 | absl::Span<const uint8_t> span() const override { return data_; } |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 171 | |
| 172 | private: |
| 173 | absl::Span<const uint8_t> data_; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 176 | // String backed flatbuffer. |
| 177 | template <typename T> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 178 | class FlatbufferString : public NonSizePrefixedFlatbuffer<T> { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 179 | public: |
| 180 | // Builds a flatbuffer using the contents of the string. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 181 | FlatbufferString(const std::string_view data) : data_(data) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 182 | // Builds a Flatbuffer by copying the data from the other flatbuffer. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 183 | FlatbufferString(const NonSizePrefixedFlatbuffer<T> &other) { |
| 184 | absl::Span<const uint8_t> d = other.span(); |
| 185 | data_ = std::string(reinterpret_cast<const char *>(d.data()), d.size()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Jim Ostrowski | 46d2523 | 2020-09-07 18:44:17 -0700 | [diff] [blame] | 188 | // Copies the data from the other flatbuffer. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 189 | FlatbufferString &operator=(const NonSizePrefixedFlatbuffer<T> &other) { |
| 190 | absl::Span<const uint8_t> d = other.span(); |
| 191 | data_ = std::string(reinterpret_cast<const char *>(d.data()), d.size()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 192 | return *this; |
| 193 | } |
| 194 | |
| 195 | virtual ~FlatbufferString() override {} |
| 196 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 197 | absl::Span<uint8_t> span() override { |
| 198 | return absl::Span<uint8_t>(reinterpret_cast<uint8_t *>(data_.data()), |
| 199 | data_.size()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 200 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 201 | absl::Span<const uint8_t> span() const override { |
| 202 | return absl::Span<const uint8_t>( |
| 203 | reinterpret_cast<const uint8_t *>(data_.data()), data_.size()); |
| 204 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 205 | |
| 206 | private: |
| 207 | std::string data_; |
| 208 | }; |
| 209 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 210 | // ResizeableBuffer backed flatbuffer. |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 211 | template <typename T> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 212 | class FlatbufferVector : public NonSizePrefixedFlatbuffer<T> { |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 213 | public: |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 214 | // Builds a Flatbuffer around a ResizeableBuffer. |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 215 | FlatbufferVector(ResizeableBuffer &&data) : data_(std::move(data)) {} |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 216 | |
| 217 | // Builds a Flatbuffer by copying the data from the other flatbuffer. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 218 | FlatbufferVector(const NonSizePrefixedFlatbuffer<T> &other) { |
| 219 | data_.resize(other.span().size()); |
| 220 | memcpy(data_.data(), other.span().data(), data_.size()); |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 221 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 222 | |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 223 | // Copy constructor. |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 224 | FlatbufferVector(const FlatbufferVector<T> &other) : data_(other.data_) {} |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 225 | |
Austin Schuh | 03803bd | 2019-12-30 18:08:17 -0800 | [diff] [blame] | 226 | // Move constructor. |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 227 | FlatbufferVector(FlatbufferVector<T> &&other) |
| 228 | : data_(std::move(other.data_)) {} |
Austin Schuh | 03803bd | 2019-12-30 18:08:17 -0800 | [diff] [blame] | 229 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 230 | // Copies the data from the other flatbuffer. |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 231 | FlatbufferVector &operator=(const FlatbufferVector<T> &other) { |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 232 | data_ = other.data_; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 233 | return *this; |
| 234 | } |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 235 | FlatbufferVector &operator=(FlatbufferVector<T> &&other) { |
| 236 | data_ = std::move(other.data_); |
| 237 | return *this; |
| 238 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 239 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 240 | // Constructs an empty flatbuffer of type T. |
| 241 | static FlatbufferVector<T> Empty() { |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 242 | return FlatbufferVector<T>(ResizeableBuffer()); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 245 | virtual ~FlatbufferVector() override {} |
| 246 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 247 | absl::Span<uint8_t> span() override { |
| 248 | return absl::Span<uint8_t>(data_.data(), data_.size()); |
| 249 | } |
| 250 | absl::Span<const uint8_t> span() const override { |
| 251 | return absl::Span<const uint8_t>(data_.data(), data_.size()); |
| 252 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 253 | |
| 254 | private: |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 255 | ResizeableBuffer data_; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 256 | }; |
| 257 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 258 | // This object associates the message type with the memory storing the |
| 259 | // flatbuffer. This only stores root tables. |
| 260 | // |
| 261 | // From a usage point of view, pointers to the data are very different than |
| 262 | // pointers to the tables. |
| 263 | template <typename T> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 264 | class FlatbufferDetachedBuffer final : public NonSizePrefixedFlatbuffer<T> { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 265 | public: |
| 266 | // Builds a Flatbuffer by taking ownership of the buffer. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 267 | FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer) |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 268 | : buffer_(::std::move(buffer)) {} |
| 269 | |
| 270 | // Builds a flatbuffer by taking ownership of the buffer from the other |
| 271 | // flatbuffer. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 272 | FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb) |
| 273 | : buffer_(::std::move(fb.buffer_)) {} |
| 274 | FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 275 | ::std::swap(buffer_, fb.buffer_); |
| 276 | return *this; |
| 277 | } |
| 278 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 279 | virtual ~FlatbufferDetachedBuffer() override {} |
| 280 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 281 | // Constructs an empty flatbuffer of type T. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 282 | static FlatbufferDetachedBuffer<T> Empty() { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 283 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 284 | fbb.ForceDefaults(true); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 285 | const auto end = fbb.EndTable(fbb.StartTable()); |
| 286 | fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end)); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 287 | return FlatbufferDetachedBuffer<T>(fbb.Release()); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // Returns references to the buffer, and the data. |
| 291 | const flatbuffers::DetachedBuffer &buffer() const { return buffer_; } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 292 | |
| 293 | absl::Span<uint8_t> span() override { |
| 294 | return absl::Span<uint8_t>(buffer_.data(), buffer_.size()); |
| 295 | } |
| 296 | absl::Span<const uint8_t> span() const override { |
| 297 | return absl::Span<const uint8_t>(buffer_.data(), buffer_.size()); |
| 298 | } |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 299 | |
| 300 | private: |
| 301 | flatbuffers::DetachedBuffer buffer_; |
| 302 | }; |
| 303 | |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 304 | // Array backed flatbuffer which manages building of the flatbuffer. |
| 305 | template <typename T, size_t Size> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 306 | class FlatbufferFixedAllocatorArray final |
| 307 | : public NonSizePrefixedFlatbuffer<T> { |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 308 | public: |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 309 | FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {} |
| 310 | |
| 311 | FlatbufferFixedAllocatorArray(const FlatbufferFixedAllocatorArray &) = delete; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 312 | void operator=(const NonSizePrefixedFlatbuffer<T> &) = delete; |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 313 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 314 | void CopyFrom(const NonSizePrefixedFlatbuffer<T> &other) { |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 315 | CHECK(!allocator_.is_allocated()) << ": May not overwrite while building"; |
Austin Schuh | 12e7784 | 2020-11-11 20:02:55 -0800 | [diff] [blame] | 316 | CHECK_LE(other.span().size(), Size) |
| 317 | << ": Source flatbuffer is larger than the target."; |
| 318 | memcpy(buffer_.begin(), other.span().data(), other.span().size()); |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 319 | data_ = buffer_.begin(); |
Austin Schuh | 12e7784 | 2020-11-11 20:02:55 -0800 | [diff] [blame] | 320 | size_ = other.span().size(); |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 323 | void Reset() { |
Austin Schuh | 9dac21b | 2020-10-19 10:02:48 -0700 | [diff] [blame] | 324 | CHECK(!allocator_.is_allocated() || data_ != nullptr) |
| 325 | << ": May not reset while building"; |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame] | 326 | fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_); |
| 327 | fbb_.ForceDefaults(true); |
Austin Schuh | 9dac21b | 2020-10-19 10:02:48 -0700 | [diff] [blame] | 328 | data_ = nullptr; |
| 329 | size_ = 0; |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame] | 332 | flatbuffers::FlatBufferBuilder *fbb() { |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 333 | CHECK(!allocator_.allocated()) |
| 334 | << ": Array backed flatbuffer can only be built once"; |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame] | 335 | fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_); |
| 336 | fbb_.ForceDefaults(true); |
| 337 | return &fbb_; |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void Finish(flatbuffers::Offset<T> root) { |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 341 | CHECK(allocator_.allocated()) << ": Cannot finish if not building"; |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame] | 342 | fbb_.Finish(root); |
| 343 | data_ = fbb_.GetBufferPointer(); |
| 344 | size_ = fbb_.GetSize(); |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 345 | DCHECK_LE(size_, Size); |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 348 | absl::Span<uint8_t> span() override { |
| 349 | return absl::Span<uint8_t>(data_, size_); |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 350 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 351 | absl::Span<const uint8_t> span() const override { |
| 352 | return absl::Span<const uint8_t>(data_, size_); |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 353 | } |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 354 | |
| 355 | private: |
| 356 | std::array<uint8_t, Size> buffer_; |
| 357 | PreallocatedAllocator allocator_; |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame] | 358 | flatbuffers::FlatBufferBuilder fbb_; |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 359 | uint8_t *data_ = nullptr; |
| 360 | size_t size_ = 0; |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 361 | }; |
| 362 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 363 | template <typename T> |
| 364 | class SizePrefixedFlatbuffer : public Flatbuffer<T> { |
| 365 | public: |
| 366 | const T &message() const override { |
| 367 | return *flatbuffers::GetSizePrefixedRoot<T>( |
| 368 | reinterpret_cast<const void *>(this->span().data())); |
| 369 | } |
| 370 | |
| 371 | T *mutable_message() override { |
| 372 | return flatbuffers::GetMutableSizePrefixedRoot<T>( |
| 373 | reinterpret_cast<void *>(this->span().data())); |
| 374 | } |
| 375 | |
| 376 | absl::Span<uint8_t> span() override = 0; |
| 377 | absl::Span<const uint8_t> span() const override = 0; |
| 378 | }; |
| 379 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 380 | // This object associates the message type with the memory storing the |
| 381 | // flatbuffer. This only stores root tables. |
| 382 | // |
| 383 | // From a usage point of view, pointers to the data are very different than |
| 384 | // pointers to the tables. |
| 385 | template <typename T> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 386 | class SizePrefixedFlatbufferDetachedBuffer final |
| 387 | : public SizePrefixedFlatbuffer<T> { |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 388 | public: |
| 389 | // Builds a Flatbuffer by taking ownership of the buffer. |
| 390 | SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer) |
| 391 | : buffer_(::std::move(buffer)) { |
| 392 | CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t)); |
| 393 | } |
| 394 | |
| 395 | // Builds a flatbuffer by taking ownership of the buffer from the other |
| 396 | // flatbuffer. |
| 397 | SizePrefixedFlatbufferDetachedBuffer( |
| 398 | SizePrefixedFlatbufferDetachedBuffer &&fb) |
| 399 | : buffer_(::std::move(fb.buffer_)) {} |
| 400 | SizePrefixedFlatbufferDetachedBuffer &operator=( |
| 401 | SizePrefixedFlatbufferDetachedBuffer &&fb) { |
| 402 | ::std::swap(buffer_, fb.buffer_); |
| 403 | return *this; |
| 404 | } |
| 405 | |
| 406 | virtual ~SizePrefixedFlatbufferDetachedBuffer() override {} |
| 407 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 408 | static SizePrefixedFlatbufferDetachedBuffer<T> Empty() { |
| 409 | flatbuffers::FlatBufferBuilder fbb; |
| 410 | fbb.ForceDefaults(true); |
| 411 | const auto end = fbb.EndTable(fbb.StartTable()); |
| 412 | fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end)); |
| 413 | return SizePrefixedFlatbufferDetachedBuffer<T>(fbb.Release()); |
| 414 | } |
| 415 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 416 | // Returns references to the buffer, and the data. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 417 | absl::Span<uint8_t> span() override { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 418 | return absl::Span<uint8_t>(buffer_.data(), buffer_.size()); |
| 419 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 420 | absl::Span<const uint8_t> span() const override { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 421 | return absl::Span<const uint8_t>(buffer_.data(), buffer_.size()); |
| 422 | } |
| 423 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 424 | private: |
| 425 | flatbuffers::DetachedBuffer buffer_; |
| 426 | }; |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 427 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 428 | // ResizeableBuffer backed flatbuffer. |
| 429 | template <typename T> |
| 430 | class SizePrefixedFlatbufferVector : public SizePrefixedFlatbuffer<T> { |
| 431 | public: |
| 432 | // Builds a Flatbuffer around a ResizeableBuffer. |
| 433 | SizePrefixedFlatbufferVector(ResizeableBuffer &&data) |
| 434 | : data_(std::move(data)) {} |
| 435 | |
| 436 | // Builds a Flatbuffer by copying the data from the other flatbuffer. |
| 437 | SizePrefixedFlatbufferVector(const SizePrefixedFlatbuffer<T> &other) { |
| 438 | data_.resize(other.span().size()); |
| 439 | memcpy(data_.data(), other.span().data(), data_.size()); |
| 440 | } |
| 441 | |
| 442 | // Copy constructor. |
| 443 | SizePrefixedFlatbufferVector(const SizePrefixedFlatbufferVector<T> &other) |
| 444 | : data_(other.data_) {} |
| 445 | |
| 446 | // Move constructor. |
| 447 | SizePrefixedFlatbufferVector(SizePrefixedFlatbufferVector<T> &&other) |
| 448 | : data_(std::move(other.data_)) {} |
| 449 | |
| 450 | // Copies the data from the other flatbuffer. |
| 451 | SizePrefixedFlatbufferVector &operator=( |
| 452 | const SizePrefixedFlatbufferVector<T> &other) { |
| 453 | data_ = other.data_; |
| 454 | return *this; |
| 455 | } |
| 456 | SizePrefixedFlatbufferVector &operator=( |
| 457 | SizePrefixedFlatbufferVector<T> &&other) { |
| 458 | data_ = std::move(other.data_); |
| 459 | return *this; |
| 460 | } |
| 461 | |
| 462 | // Constructs an empty flatbuffer of type T. |
| 463 | static SizePrefixedFlatbufferVector<T> Empty() { |
| 464 | return SizePrefixedFlatbufferVector<T>(ResizeableBuffer()); |
| 465 | } |
| 466 | |
| 467 | virtual ~SizePrefixedFlatbufferVector() override {} |
| 468 | |
| 469 | absl::Span<uint8_t> span() override { |
| 470 | return absl::Span<uint8_t>(data_.data(), data_.size()); |
| 471 | } |
| 472 | absl::Span<const uint8_t> span() const override { |
| 473 | return absl::Span<const uint8_t>(data_.data(), data_.size()); |
| 474 | } |
| 475 | |
| 476 | private: |
| 477 | ResizeableBuffer data_; |
| 478 | }; |
| 479 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 480 | // Non-owning Span backed flatbuffer. |
| 481 | template <typename T> |
| 482 | class SizePrefixedFlatbufferSpan : public SizePrefixedFlatbuffer<T> { |
| 483 | public: |
| 484 | // Builds a flatbuffer pointing to the contents of a span. |
| 485 | SizePrefixedFlatbufferSpan(const absl::Span<const uint8_t> data) |
| 486 | : data_(data) {} |
| 487 | // Builds a Flatbuffer pointing to the contents of another flatbuffer. |
| 488 | SizePrefixedFlatbufferSpan(const SizePrefixedFlatbuffer<T> &other) { |
| 489 | data_ = other.span(); |
| 490 | } |
| 491 | |
| 492 | // Points to the data in the other flatbuffer. |
| 493 | SizePrefixedFlatbufferSpan &operator=( |
| 494 | const SizePrefixedFlatbuffer<T> &other) { |
| 495 | data_ = other.span(); |
| 496 | return *this; |
| 497 | } |
| 498 | |
| 499 | ~SizePrefixedFlatbufferSpan() override {} |
| 500 | |
| 501 | absl::Span<uint8_t> span() override { |
| 502 | LOG(FATAL) << "Unimplemented"; |
| 503 | return absl::Span<uint8_t>(nullptr, 0); |
| 504 | } |
| 505 | absl::Span<const uint8_t> span() const override { return data_; } |
| 506 | |
| 507 | private: |
| 508 | absl::Span<const uint8_t> data_; |
| 509 | }; |
| 510 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 511 | inline flatbuffers::DetachedBuffer CopySpanAsDetachedBuffer( |
| 512 | absl::Span<const uint8_t> span) { |
| 513 | // Copy the data from the span. |
| 514 | uint8_t *buf = flatbuffers::DefaultAllocator().allocate(span.size()); |
| 515 | memcpy(buf, span.data(), span.size()); |
| 516 | // Then give it to a DetachedBuffer to manage. |
| 517 | return flatbuffers::DetachedBuffer(nullptr, false, buf, span.size(), buf, |
| 518 | span.size()); |
| 519 | } |
| 520 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 521 | } // namespace aos |
| 522 | |
| 523 | #endif // AOS_FLATBUFFERS_H_ |