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 | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 7 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 8 | #include "glog/logging.h" |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 9 | |
| 10 | namespace aos { |
| 11 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 12 | // This class is a base class for all sizes of array backed allocators. |
| 13 | class 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 27 | void Reset() { is_allocated_ = false; } |
| 28 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 29 | 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 Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 35 | class FixedAllocator : public FixedAllocatorBase { |
| 36 | public: |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 37 | FixedAllocator(size_t size) : buffer_(size, 0) {} |
| 38 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 39 | 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 Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 43 | // Releases the data in the buffer. |
| 44 | std::vector<uint8_t> release() { return std::move(buffer_); } |
| 45 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 46 | private: |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 47 | std::vector<uint8_t> buffer_; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 50 | // This class adapts a preallocated memory region to an Allocator. |
| 51 | class 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 Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 65 | // Base class representing an object which holds the memory representing a root |
| 66 | // flatbuffer. |
| 67 | template <typename T> |
| 68 | class Flatbuffer { |
| 69 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 70 | virtual ~Flatbuffer() {} |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 71 | // 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 90 | // String backed flatbuffer. |
| 91 | template <typename T> |
| 92 | class FlatbufferString : public Flatbuffer<T> { |
| 93 | public: |
| 94 | // Builds a flatbuffer using the contents of the string. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 95 | FlatbufferString(const std::string_view data) : data_(data) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 96 | // 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 Kuszmaul | 872efd2 | 2019-12-03 20:59:09 -0800 | [diff] [blame] | 112 | uint8_t *data() override { return reinterpret_cast<uint8_t *>(data_.data()); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 113 | size_t size() const override { return data_.size(); } |
| 114 | |
| 115 | private: |
| 116 | std::string data_; |
| 117 | }; |
| 118 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 119 | // Vector backed flatbuffer. |
| 120 | template <typename T> |
| 121 | class 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 Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame^] | 130 | // Copy constructor. |
| 131 | FlatbufferVector(const FlatbufferVector<T> &other) |
| 132 | : data_(other.data(), other.data() + other.size()) {} |
| 133 | |
Austin Schuh | 03803bd | 2019-12-30 18:08:17 -0800 | [diff] [blame] | 134 | // Move constructor. |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame^] | 135 | FlatbufferVector(FlatbufferVector<T> &&other) |
| 136 | : data_(std::move(other.data_)) {} |
Austin Schuh | 03803bd | 2019-12-30 18:08:17 -0800 | [diff] [blame] | 137 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 138 | // Copies the data from the other flatbuffer. |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame^] | 139 | FlatbufferVector &operator=(const FlatbufferVector<T> &other) { |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 140 | data_ = std::vector<uint8_t>(other.data(), other.data() + other.size()); |
| 141 | return *this; |
| 142 | } |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame^] | 143 | FlatbufferVector &operator=(FlatbufferVector<T> &&other) { |
| 144 | data_ = std::move(other.data_); |
| 145 | return *this; |
| 146 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 147 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 148 | // Constructs an empty flatbuffer of type T. |
| 149 | static FlatbufferVector<T> Empty() { |
| 150 | return FlatbufferVector<T>(std::vector<uint8_t>{}); |
| 151 | } |
| 152 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 153 | virtual ~FlatbufferVector() override {} |
| 154 | |
| 155 | const uint8_t *data() const override { return data_.data(); } |
| 156 | uint8_t *data() override { return data_.data(); } |
| 157 | size_t size() const override { return data_.size(); } |
| 158 | |
| 159 | private: |
| 160 | std::vector<uint8_t> data_; |
| 161 | }; |
| 162 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 163 | // This object associates the message type with the memory storing the |
| 164 | // flatbuffer. This only stores root tables. |
| 165 | // |
| 166 | // From a usage point of view, pointers to the data are very different than |
| 167 | // pointers to the tables. |
| 168 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 169 | class FlatbufferDetachedBuffer final : public Flatbuffer<T> { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 170 | public: |
| 171 | // Builds a Flatbuffer by taking ownership of the buffer. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 172 | FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer) |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 173 | : buffer_(::std::move(buffer)) {} |
| 174 | |
| 175 | // Builds a flatbuffer by taking ownership of the buffer from the other |
| 176 | // flatbuffer. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 177 | FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb) |
| 178 | : buffer_(::std::move(fb.buffer_)) {} |
| 179 | FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 180 | ::std::swap(buffer_, fb.buffer_); |
| 181 | return *this; |
| 182 | } |
| 183 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 184 | virtual ~FlatbufferDetachedBuffer() override {} |
| 185 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 186 | // Constructs an empty flatbuffer of type T. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 187 | static FlatbufferDetachedBuffer<T> Empty() { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 188 | flatbuffers::FlatBufferBuilder fbb; |
| 189 | fbb.ForceDefaults(1); |
| 190 | const auto end = fbb.EndTable(fbb.StartTable()); |
| 191 | fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end)); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 192 | return FlatbufferDetachedBuffer<T>(fbb.Release()); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Returns references to the buffer, and the data. |
| 196 | const flatbuffers::DetachedBuffer &buffer() const { return buffer_; } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 197 | const uint8_t *data() const override { return buffer_.data(); } |
| 198 | uint8_t *data() override { return buffer_.data(); } |
| 199 | size_t size() const override { return buffer_.size(); } |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 200 | |
| 201 | private: |
| 202 | flatbuffers::DetachedBuffer buffer_; |
| 203 | }; |
| 204 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 205 | // This object associates the message type with the memory storing the |
| 206 | // flatbuffer. This only stores root tables. |
| 207 | // |
| 208 | // From a usage point of view, pointers to the data are very different than |
| 209 | // pointers to the tables. |
| 210 | template <typename T> |
| 211 | class SizePrefixedFlatbufferDetachedBuffer final : public Flatbuffer<T> { |
| 212 | public: |
| 213 | // Builds a Flatbuffer by taking ownership of the buffer. |
| 214 | SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer) |
| 215 | : buffer_(::std::move(buffer)) { |
| 216 | CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t)); |
| 217 | } |
| 218 | |
| 219 | // Builds a flatbuffer by taking ownership of the buffer from the other |
| 220 | // flatbuffer. |
| 221 | SizePrefixedFlatbufferDetachedBuffer( |
| 222 | SizePrefixedFlatbufferDetachedBuffer &&fb) |
| 223 | : buffer_(::std::move(fb.buffer_)) {} |
| 224 | SizePrefixedFlatbufferDetachedBuffer &operator=( |
| 225 | SizePrefixedFlatbufferDetachedBuffer &&fb) { |
| 226 | ::std::swap(buffer_, fb.buffer_); |
| 227 | return *this; |
| 228 | } |
| 229 | |
| 230 | virtual ~SizePrefixedFlatbufferDetachedBuffer() override {} |
| 231 | |
| 232 | // Returns references to the buffer, and the data. |
| 233 | const flatbuffers::DetachedBuffer &buffer() const { return buffer_; } |
| 234 | const uint8_t *data() const override { |
| 235 | return buffer_.data() + sizeof(flatbuffers::uoffset_t); |
| 236 | } |
| 237 | uint8_t *data() override { |
| 238 | return buffer_.data() + sizeof(flatbuffers::uoffset_t); |
| 239 | } |
| 240 | size_t size() const override { |
| 241 | return buffer_.size() - sizeof(flatbuffers::uoffset_t); |
| 242 | } |
| 243 | |
| 244 | private: |
| 245 | flatbuffers::DetachedBuffer buffer_; |
| 246 | }; |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 247 | // TODO(austin): Need a way to get our hands on the max size. Can start with |
| 248 | // "large" for now. |
| 249 | |
| 250 | } // namespace aos |
| 251 | |
| 252 | #endif // AOS_FLATBUFFERS_H_ |