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