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" |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 8 | #include "aos/macros.h" |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 9 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 10 | #include "glog/logging.h" |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 11 | |
| 12 | namespace aos { |
| 13 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 14 | // This class is a base class for all sizes of array backed allocators. |
| 15 | class FixedAllocatorBase : public flatbuffers::Allocator { |
| 16 | public: |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 17 | ~FixedAllocatorBase() override { CHECK(!is_allocated_); } |
| 18 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 19 | // TODO(austin): Read the contract for these. |
| 20 | uint8_t *allocate(size_t) override; |
| 21 | |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 22 | void deallocate(uint8_t *allocated_data, size_t allocated_size) override { |
| 23 | DCHECK_LE(allocated_size, size()); |
| 24 | DCHECK_EQ(allocated_data, data()); |
| 25 | CHECK(is_allocated_); |
| 26 | is_allocated_ = false; |
| 27 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 28 | |
| 29 | uint8_t *reallocate_downward(uint8_t *, size_t, size_t, size_t, |
| 30 | size_t) override; |
| 31 | |
| 32 | virtual const uint8_t *data() const = 0; |
| 33 | virtual uint8_t *data() = 0; |
| 34 | virtual size_t size() const = 0; |
| 35 | |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 36 | void Reset() { |
| 37 | CHECK(!is_allocated_); |
| 38 | is_allocated_ = false; |
| 39 | } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 40 | bool is_allocated() const { return is_allocated_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 41 | |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 42 | bool allocated() { return is_allocated_; } |
| 43 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 44 | private: |
| 45 | bool is_allocated_ = false; |
| 46 | }; |
| 47 | |
| 48 | // 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] | 49 | // in a vector. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 50 | class FixedAllocator : public FixedAllocatorBase { |
| 51 | public: |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 52 | FixedAllocator(size_t size) : buffer_(size, 0) {} |
| 53 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 54 | uint8_t *data() override { return &buffer_[0]; } |
| 55 | const uint8_t *data() const override { return &buffer_[0]; } |
| 56 | size_t size() const override { return buffer_.size(); } |
| 57 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 58 | // Releases the data in the buffer. |
| 59 | std::vector<uint8_t> release() { return std::move(buffer_); } |
| 60 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 61 | private: |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 62 | std::vector<uint8_t> buffer_; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 65 | // This class adapts a preallocated memory region to an Allocator. |
| 66 | class PreallocatedAllocator : public FixedAllocatorBase { |
| 67 | public: |
| 68 | PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {} |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 69 | PreallocatedAllocator(const PreallocatedAllocator &) = delete; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 70 | PreallocatedAllocator(PreallocatedAllocator &&other) |
| 71 | : data_(other.data_), size_(other.size_) { |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 72 | CHECK(!is_allocated()) << ": May not overwrite in-use allocator"; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 73 | CHECK(!other.is_allocated()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 74 | } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 75 | |
| 76 | PreallocatedAllocator &operator=(const PreallocatedAllocator &) = delete; |
| 77 | PreallocatedAllocator &operator=(PreallocatedAllocator &&other) { |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 78 | CHECK(!is_allocated()) << ": May not overwrite in-use allocator"; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 79 | CHECK(!other.is_allocated()); |
| 80 | data_ = other.data_; |
| 81 | size_ = other.size_; |
| 82 | return *this; |
| 83 | } |
| 84 | |
| 85 | uint8_t *data() final { |
| 86 | return reinterpret_cast<uint8_t *>(CHECK_NOTNULL(data_)); |
| 87 | } |
| 88 | const uint8_t *data() const final { |
| 89 | return reinterpret_cast<const uint8_t *>(CHECK_NOTNULL(data_)); |
| 90 | } |
| 91 | size_t size() const final { return size_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 92 | |
| 93 | private: |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 94 | void *data_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 95 | size_t size_; |
| 96 | }; |
| 97 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 98 | // Base class representing an object which holds the memory representing a root |
| 99 | // flatbuffer. |
| 100 | template <typename T> |
| 101 | class Flatbuffer { |
| 102 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 103 | virtual ~Flatbuffer() {} |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 104 | // Returns the MiniReflectTypeTable for T. |
| 105 | static const flatbuffers::TypeTable *MiniReflectTypeTable() { |
| 106 | return T::MiniReflectTypeTable(); |
| 107 | } |
| 108 | |
| 109 | // Returns a message from the buffer. |
| 110 | const T &message() const { |
| 111 | return *flatbuffers::GetRoot<T>(reinterpret_cast<const void *>(data())); |
| 112 | } |
| 113 | // Returns a mutable message. It can be mutated via the flatbuffer rules. |
| 114 | T *mutable_message() { |
| 115 | return flatbuffers::GetMutableRoot<T>(reinterpret_cast<void *>(data())); |
| 116 | } |
| 117 | |
| 118 | virtual const uint8_t *data() const = 0; |
| 119 | virtual uint8_t *data() = 0; |
| 120 | virtual size_t size() const = 0; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 121 | |
| 122 | absl::Span<uint8_t> span() { return absl::Span<uint8_t>(data(), size()); } |
| 123 | absl::Span<const uint8_t> span() const { |
| 124 | return absl::Span<const uint8_t>(data(), size()); |
| 125 | } |
Austin Schuh | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 126 | |
| 127 | // Wipes out the data buffer. This is handy to mark an instance as freed, and |
| 128 | // make attempts to use it fail more obviously. |
| 129 | void Wipe() { memset(data(), 0, size()); } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 132 | // String backed flatbuffer. |
| 133 | template <typename T> |
| 134 | class FlatbufferString : public Flatbuffer<T> { |
| 135 | public: |
| 136 | // Builds a flatbuffer using the contents of the string. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 137 | FlatbufferString(const std::string_view data) : data_(data) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 138 | // Builds a Flatbuffer by copying the data from the other flatbuffer. |
| 139 | FlatbufferString(const Flatbuffer<T> &other) { |
Brian Silverman | c8a5b55 | 2020-04-28 16:43:59 -0700 | [diff] [blame] | 140 | data_ = |
| 141 | std::string(reinterpret_cast<const char *>(other.data()), other.size()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // Coppies the data from the other flatbuffer. |
| 145 | FlatbufferString &operator=(const Flatbuffer<T> &other) { |
| 146 | data_ = std::string(other.data(), other.size()); |
| 147 | return *this; |
| 148 | } |
| 149 | |
| 150 | virtual ~FlatbufferString() override {} |
| 151 | |
| 152 | const uint8_t *data() const override { |
| 153 | return reinterpret_cast<const uint8_t *>(data_.data()); |
| 154 | } |
James Kuszmaul | 872efd2 | 2019-12-03 20:59:09 -0800 | [diff] [blame] | 155 | uint8_t *data() override { return reinterpret_cast<uint8_t *>(data_.data()); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 156 | size_t size() const override { return data_.size(); } |
| 157 | |
| 158 | private: |
| 159 | std::string data_; |
| 160 | }; |
| 161 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 162 | // Vector backed flatbuffer. |
| 163 | template <typename T> |
| 164 | class FlatbufferVector : public Flatbuffer<T> { |
| 165 | public: |
| 166 | // Builds a Flatbuffer around a vector. |
| 167 | FlatbufferVector(std::vector<uint8_t> &&data) : data_(std::move(data)) {} |
| 168 | |
| 169 | // Builds a Flatbuffer by copying the data from the other flatbuffer. |
| 170 | FlatbufferVector(const Flatbuffer<T> &other) |
| 171 | : data_(other.data(), other.data() + other.size()) {} |
| 172 | |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 173 | // Copy constructor. |
| 174 | FlatbufferVector(const FlatbufferVector<T> &other) |
| 175 | : data_(other.data(), other.data() + other.size()) {} |
| 176 | |
Austin Schuh | 03803bd | 2019-12-30 18:08:17 -0800 | [diff] [blame] | 177 | // Move constructor. |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 178 | FlatbufferVector(FlatbufferVector<T> &&other) |
| 179 | : data_(std::move(other.data_)) {} |
Austin Schuh | 03803bd | 2019-12-30 18:08:17 -0800 | [diff] [blame] | 180 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 181 | // Copies the data from the other flatbuffer. |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 182 | FlatbufferVector &operator=(const FlatbufferVector<T> &other) { |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 183 | data_ = std::vector<uint8_t>(other.data(), other.data() + other.size()); |
| 184 | return *this; |
| 185 | } |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 186 | FlatbufferVector &operator=(FlatbufferVector<T> &&other) { |
| 187 | data_ = std::move(other.data_); |
| 188 | return *this; |
| 189 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 190 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 191 | // Constructs an empty flatbuffer of type T. |
| 192 | static FlatbufferVector<T> Empty() { |
| 193 | return FlatbufferVector<T>(std::vector<uint8_t>{}); |
| 194 | } |
| 195 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 196 | virtual ~FlatbufferVector() override {} |
| 197 | |
| 198 | const uint8_t *data() const override { return data_.data(); } |
| 199 | uint8_t *data() override { return data_.data(); } |
| 200 | size_t size() const override { return data_.size(); } |
| 201 | |
| 202 | private: |
| 203 | std::vector<uint8_t> data_; |
| 204 | }; |
| 205 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 206 | // This object associates the message type with the memory storing the |
| 207 | // flatbuffer. This only stores root tables. |
| 208 | // |
| 209 | // From a usage point of view, pointers to the data are very different than |
| 210 | // pointers to the tables. |
| 211 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 212 | class FlatbufferDetachedBuffer final : public Flatbuffer<T> { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 213 | public: |
| 214 | // Builds a Flatbuffer by taking ownership of the buffer. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 215 | FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer) |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 216 | : buffer_(::std::move(buffer)) {} |
| 217 | |
| 218 | // Builds a flatbuffer by taking ownership of the buffer from the other |
| 219 | // flatbuffer. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 220 | FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb) |
| 221 | : buffer_(::std::move(fb.buffer_)) {} |
| 222 | FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 223 | ::std::swap(buffer_, fb.buffer_); |
| 224 | return *this; |
| 225 | } |
| 226 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 227 | virtual ~FlatbufferDetachedBuffer() override {} |
| 228 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 229 | // Constructs an empty flatbuffer of type T. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 230 | static FlatbufferDetachedBuffer<T> Empty() { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 231 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 232 | fbb.ForceDefaults(true); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 233 | const auto end = fbb.EndTable(fbb.StartTable()); |
| 234 | fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end)); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 235 | return FlatbufferDetachedBuffer<T>(fbb.Release()); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // Returns references to the buffer, and the data. |
| 239 | const flatbuffers::DetachedBuffer &buffer() const { return buffer_; } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 240 | const uint8_t *data() const override { return buffer_.data(); } |
| 241 | uint8_t *data() override { return buffer_.data(); } |
| 242 | size_t size() const override { return buffer_.size(); } |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 243 | |
| 244 | private: |
| 245 | flatbuffers::DetachedBuffer buffer_; |
| 246 | }; |
| 247 | |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 248 | // Array backed flatbuffer which manages building of the flatbuffer. |
| 249 | template <typename T, size_t Size> |
| 250 | class FlatbufferFixedAllocatorArray final : public Flatbuffer<T> { |
| 251 | public: |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 252 | FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {} |
| 253 | |
| 254 | FlatbufferFixedAllocatorArray(const FlatbufferFixedAllocatorArray &) = delete; |
| 255 | void operator=(const Flatbuffer<T> &) = delete; |
| 256 | |
| 257 | void CopyFrom(const Flatbuffer<T> &other) { |
| 258 | CHECK(!allocator_.is_allocated()) << ": May not overwrite while building"; |
| 259 | memcpy(buffer_.begin(), other.data(), other.size()); |
| 260 | data_ = buffer_.begin(); |
| 261 | size_ = other.size(); |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 264 | void Reset() { |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 265 | CHECK(!allocator_.is_allocated()) << ": May not reset while building"; |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame^] | 266 | fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_); |
| 267 | fbb_.ForceDefaults(true); |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame^] | 270 | flatbuffers::FlatBufferBuilder *fbb() { |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 271 | CHECK(!allocator_.allocated()) |
| 272 | << ": Array backed flatbuffer can only be built once"; |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame^] | 273 | fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_); |
| 274 | fbb_.ForceDefaults(true); |
| 275 | return &fbb_; |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | void Finish(flatbuffers::Offset<T> root) { |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 279 | CHECK(allocator_.allocated()) << ": Cannot finish if not building"; |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame^] | 280 | fbb_.Finish(root); |
| 281 | data_ = fbb_.GetBufferPointer(); |
| 282 | size_ = fbb_.GetSize(); |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 283 | DCHECK_LE(size_, Size); |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | const uint8_t *data() const override { |
| 287 | CHECK_NOTNULL(data_); |
| 288 | return data_; |
| 289 | } |
| 290 | uint8_t *data() override { |
| 291 | CHECK_NOTNULL(data_); |
| 292 | return data_; |
| 293 | } |
| 294 | size_t size() const override { return size_; } |
| 295 | |
| 296 | private: |
| 297 | std::array<uint8_t, Size> buffer_; |
| 298 | PreallocatedAllocator allocator_; |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame^] | 299 | flatbuffers::FlatBufferBuilder fbb_; |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 300 | uint8_t *data_ = nullptr; |
| 301 | size_t size_ = 0; |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 302 | }; |
| 303 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 304 | // This object associates the message type with the memory storing the |
| 305 | // flatbuffer. This only stores root tables. |
| 306 | // |
| 307 | // From a usage point of view, pointers to the data are very different than |
| 308 | // pointers to the tables. |
| 309 | template <typename T> |
| 310 | class SizePrefixedFlatbufferDetachedBuffer final : public Flatbuffer<T> { |
| 311 | public: |
| 312 | // Builds a Flatbuffer by taking ownership of the buffer. |
| 313 | SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer) |
| 314 | : buffer_(::std::move(buffer)) { |
| 315 | CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t)); |
| 316 | } |
| 317 | |
| 318 | // Builds a flatbuffer by taking ownership of the buffer from the other |
| 319 | // flatbuffer. |
| 320 | SizePrefixedFlatbufferDetachedBuffer( |
| 321 | SizePrefixedFlatbufferDetachedBuffer &&fb) |
| 322 | : buffer_(::std::move(fb.buffer_)) {} |
| 323 | SizePrefixedFlatbufferDetachedBuffer &operator=( |
| 324 | SizePrefixedFlatbufferDetachedBuffer &&fb) { |
| 325 | ::std::swap(buffer_, fb.buffer_); |
| 326 | return *this; |
| 327 | } |
| 328 | |
| 329 | virtual ~SizePrefixedFlatbufferDetachedBuffer() override {} |
| 330 | |
| 331 | // Returns references to the buffer, and the data. |
| 332 | const flatbuffers::DetachedBuffer &buffer() const { return buffer_; } |
| 333 | const uint8_t *data() const override { |
| 334 | return buffer_.data() + sizeof(flatbuffers::uoffset_t); |
| 335 | } |
| 336 | uint8_t *data() override { |
| 337 | return buffer_.data() + sizeof(flatbuffers::uoffset_t); |
| 338 | } |
| 339 | size_t size() const override { |
| 340 | return buffer_.size() - sizeof(flatbuffers::uoffset_t); |
| 341 | } |
| 342 | |
| 343 | private: |
| 344 | flatbuffers::DetachedBuffer buffer_; |
| 345 | }; |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 346 | |
| 347 | } // namespace aos |
| 348 | |
| 349 | #endif // AOS_FLATBUFFERS_H_ |