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> |
| 5 | |
| 6 | #include "absl/strings/string_view.h" |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 7 | #include "flatbuffers/flatbuffers.h" |
| 8 | |
| 9 | namespace aos { |
| 10 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 11 | // This class is a base class for all sizes of array backed allocators. |
| 12 | class FixedAllocatorBase : public flatbuffers::Allocator { |
| 13 | public: |
| 14 | // TODO(austin): Read the contract for these. |
| 15 | uint8_t *allocate(size_t) override; |
| 16 | |
| 17 | void deallocate(uint8_t *, size_t) override { is_allocated_ = false; } |
| 18 | |
| 19 | uint8_t *reallocate_downward(uint8_t *, size_t, size_t, size_t, |
| 20 | size_t) override; |
| 21 | |
| 22 | virtual const uint8_t *data() const = 0; |
| 23 | virtual uint8_t *data() = 0; |
| 24 | virtual size_t size() const = 0; |
| 25 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 26 | void Reset() { is_allocated_ = false; } |
| 27 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 28 | private: |
| 29 | bool is_allocated_ = false; |
| 30 | }; |
| 31 | |
| 32 | // This class is a fixed memory allocator which holds the data for a flatbuffer |
| 33 | // in an array. |
| 34 | template <size_t S> |
| 35 | class FixedAllocator : public FixedAllocatorBase { |
| 36 | public: |
| 37 | uint8_t *data() override { return &buffer_[0]; } |
| 38 | const uint8_t *data() const override { return &buffer_[0]; } |
| 39 | size_t size() const override { return buffer_.size(); } |
| 40 | |
| 41 | private: |
| 42 | std::array<uint8_t, S> buffer_; |
| 43 | }; |
| 44 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 45 | // This class adapts a preallocated memory region to an Allocator. |
| 46 | class PreallocatedAllocator : public FixedAllocatorBase { |
| 47 | public: |
| 48 | PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {} |
| 49 | uint8_t *data() override { return reinterpret_cast<uint8_t *>(data_); } |
| 50 | const uint8_t *data() const override { |
| 51 | return reinterpret_cast<const uint8_t *>(data_); |
| 52 | } |
| 53 | size_t size() const override { return size_; } |
| 54 | |
| 55 | private: |
| 56 | void* data_; |
| 57 | size_t size_; |
| 58 | }; |
| 59 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 60 | // Base class representing an object which holds the memory representing a root |
| 61 | // flatbuffer. |
| 62 | template <typename T> |
| 63 | class Flatbuffer { |
| 64 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 65 | virtual ~Flatbuffer() {} |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 66 | // Returns the MiniReflectTypeTable for T. |
| 67 | static const flatbuffers::TypeTable *MiniReflectTypeTable() { |
| 68 | return T::MiniReflectTypeTable(); |
| 69 | } |
| 70 | |
| 71 | // Returns a message from the buffer. |
| 72 | const T &message() const { |
| 73 | return *flatbuffers::GetRoot<T>(reinterpret_cast<const void *>(data())); |
| 74 | } |
| 75 | // Returns a mutable message. It can be mutated via the flatbuffer rules. |
| 76 | T *mutable_message() { |
| 77 | return flatbuffers::GetMutableRoot<T>(reinterpret_cast<void *>(data())); |
| 78 | } |
| 79 | |
| 80 | virtual const uint8_t *data() const = 0; |
| 81 | virtual uint8_t *data() = 0; |
| 82 | virtual size_t size() const = 0; |
| 83 | }; |
| 84 | |
| 85 | // Array backed flatbuffer. |
| 86 | template <typename T> |
| 87 | class FlatbufferArray : public Flatbuffer<T> { |
| 88 | public: |
| 89 | // Builds a Flatbuffer by copying the data from the other flatbuffer. |
| 90 | FlatbufferArray(const Flatbuffer<T> &other) { |
| 91 | CHECK_LE(other.size(), data_.size()); |
| 92 | |
| 93 | memcpy(data_.data(), other.data(), other.size()); |
| 94 | size_ = other.size(); |
| 95 | } |
| 96 | |
| 97 | // Coppies the data from the other flatbuffer. |
| 98 | FlatbufferArray &operator=(const Flatbuffer<T> &other) { |
| 99 | CHECK_LE(other.size(), data_.size()); |
| 100 | |
| 101 | memcpy(data_.data(), other.data(), other.size()); |
| 102 | size_ = other.size(); |
| 103 | return *this; |
| 104 | } |
| 105 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 106 | virtual ~FlatbufferArray() override {} |
| 107 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 108 | // Creates a builder wrapping the underlying data. |
| 109 | flatbuffers::FlatBufferBuilder FlatBufferBuilder() { |
| 110 | data_.deallocate(data_.data(), data_.size()); |
| 111 | flatbuffers::FlatBufferBuilder fbb(data_.size(), &data_); |
| 112 | fbb.ForceDefaults(1); |
| 113 | return fbb; |
| 114 | } |
| 115 | |
| 116 | const uint8_t *data() const override { return data_.data(); } |
| 117 | uint8_t *data() override { return data_.data(); } |
| 118 | size_t size() const override { return size_; } |
| 119 | |
| 120 | private: |
| 121 | FixedAllocator<8 * 1024> data_; |
| 122 | size_t size_ = data_.size(); |
| 123 | }; |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 124 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 125 | // String backed flatbuffer. |
| 126 | template <typename T> |
| 127 | class FlatbufferString : public Flatbuffer<T> { |
| 128 | public: |
| 129 | // Builds a flatbuffer using the contents of the string. |
| 130 | FlatbufferString(const absl::string_view data) : data_(data) {} |
| 131 | // Builds a Flatbuffer by copying the data from the other flatbuffer. |
| 132 | FlatbufferString(const Flatbuffer<T> &other) { |
| 133 | data_ = std::string(other.data(), other.size()); |
| 134 | } |
| 135 | |
| 136 | // Coppies the data from the other flatbuffer. |
| 137 | FlatbufferString &operator=(const Flatbuffer<T> &other) { |
| 138 | data_ = std::string(other.data(), other.size()); |
| 139 | return *this; |
| 140 | } |
| 141 | |
| 142 | virtual ~FlatbufferString() override {} |
| 143 | |
| 144 | const uint8_t *data() const override { |
| 145 | return reinterpret_cast<const uint8_t *>(data_.data()); |
| 146 | } |
| 147 | uint8_t *data() override { |
| 148 | // TODO(james): when we get c++17, can we drop the second cast? |
| 149 | return const_cast<uint8_t *>( |
| 150 | reinterpret_cast<const uint8_t *>(data_.data())); |
| 151 | } |
| 152 | size_t size() const override { return data_.size(); } |
| 153 | |
| 154 | private: |
| 155 | std::string data_; |
| 156 | }; |
| 157 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 158 | // This object associates the message type with the memory storing the |
| 159 | // flatbuffer. This only stores root tables. |
| 160 | // |
| 161 | // From a usage point of view, pointers to the data are very different than |
| 162 | // pointers to the tables. |
| 163 | template <typename T> |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 164 | class FlatbufferDetachedBuffer : public Flatbuffer<T> { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 165 | public: |
| 166 | // Builds a Flatbuffer by taking ownership of the buffer. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 167 | FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer) |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 168 | : buffer_(::std::move(buffer)) {} |
| 169 | |
| 170 | // Builds a flatbuffer by taking ownership of the buffer from the other |
| 171 | // flatbuffer. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 172 | FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb) |
| 173 | : buffer_(::std::move(fb.buffer_)) {} |
| 174 | FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 175 | ::std::swap(buffer_, fb.buffer_); |
| 176 | return *this; |
| 177 | } |
| 178 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 179 | virtual ~FlatbufferDetachedBuffer() override {} |
| 180 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 181 | // Constructs an empty flatbuffer of type T. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 182 | static FlatbufferDetachedBuffer<T> Empty() { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 183 | flatbuffers::FlatBufferBuilder fbb; |
| 184 | fbb.ForceDefaults(1); |
| 185 | const auto end = fbb.EndTable(fbb.StartTable()); |
| 186 | fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end)); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 187 | return FlatbufferDetachedBuffer<T>(fbb.Release()); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // Returns references to the buffer, and the data. |
| 191 | const flatbuffers::DetachedBuffer &buffer() const { return buffer_; } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 192 | const uint8_t *data() const override { return buffer_.data(); } |
| 193 | uint8_t *data() override { return buffer_.data(); } |
| 194 | size_t size() const override { return buffer_.size(); } |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 195 | |
| 196 | private: |
| 197 | flatbuffers::DetachedBuffer buffer_; |
| 198 | }; |
| 199 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 200 | // TODO(austin): Need a way to get our hands on the max size. Can start with |
| 201 | // "large" for now. |
| 202 | |
| 203 | } // namespace aos |
| 204 | |
| 205 | #endif // AOS_FLATBUFFERS_H_ |