17 #ifndef FLATBUFFERS_H_ 18 #define FLATBUFFERS_H_ 20 #include "flatbuffers/base.h" 28 Offset(uoffset_t _o) : o(_o) {}
30 bool IsNull()
const {
return !o; }
33 inline void EndianCheck() {
36 FLATBUFFERS_ASSERT(*reinterpret_cast<char *>(&endiantest) ==
37 FLATBUFFERS_LITTLEENDIAN);
41 template<
typename T> FLATBUFFERS_CONSTEXPR
size_t AlignOf() {
47 return __alignof__(T);
64 typedef T return_type;
65 typedef T mutable_return_type;
66 static const size_t element_stride =
sizeof(T);
67 static return_type Read(
const uint8_t *p, uoffset_t i) {
68 return EndianScalar((reinterpret_cast<const T *>(p))[i]);
72 typedef const T *return_type;
73 typedef T *mutable_return_type;
74 static const size_t element_stride =
sizeof(uoffset_t);
75 static return_type Read(
const uint8_t *p, uoffset_t i) {
76 p += i *
sizeof(uoffset_t);
77 return reinterpret_cast<return_type
>(p + ReadScalar<uoffset_t>(p));
81 typedef const T *return_type;
82 typedef T *mutable_return_type;
83 static const size_t element_stride =
sizeof(T);
84 static return_type Read(
const uint8_t *p, uoffset_t i) {
85 return reinterpret_cast<const T *
>(p + i *
sizeof(T));
92 typedef std::random_access_iterator_tag iterator_category;
93 typedef IT value_type;
94 typedef ptrdiff_t difference_type;
96 typedef IT &reference;
113 return data_ == other.data_;
117 return data_ < other.data_;
121 return data_ != other.data_;
175 const uint8_t *data_;
189 uoffset_t size()
const {
return EndianScalar(length_); }
192 uoffset_t Length()
const {
return size(); }
194 typedef typename IndirectHelper<T>::return_type return_type;
195 typedef typename IndirectHelper<T>::mutable_return_type mutable_return_type;
197 return_type Get(uoffset_t i)
const {
198 FLATBUFFERS_ASSERT(i < size());
202 return_type operator[](uoffset_t i)
const {
return Get(i); }
207 template<
typename E> E GetEnum(uoffset_t i)
const {
208 return static_cast<E
>(Get(i));
213 template<
typename U>
const U *GetAs(uoffset_t i)
const {
214 return reinterpret_cast<const U *
>(Get(i));
219 const String *GetAsString(uoffset_t i)
const {
220 return reinterpret_cast<const String *
>(Get(i));
223 const void *GetStructFromOffset(
size_t o)
const {
224 return reinterpret_cast<const void *
>(Data() + o);
227 iterator begin() {
return iterator(Data(), 0); }
228 const_iterator begin()
const {
return const_iterator(Data(), 0); }
230 iterator end() {
return iterator(Data(), size()); }
231 const_iterator end()
const {
return const_iterator(Data(), size()); }
235 void Mutate(uoffset_t i,
const T &val) {
236 FLATBUFFERS_ASSERT(i < size());
237 WriteScalar(data() + i, val);
243 void MutateOffset(uoffset_t i,
const uint8_t *val) {
244 FLATBUFFERS_ASSERT(i < size());
245 static_assert(
sizeof(T) ==
sizeof(uoffset_t),
"Unrelated types");
246 WriteScalar(data() + i,
247 static_cast<uoffset_t>(val - (Data() + i *
sizeof(uoffset_t))));
251 mutable_return_type GetMutableObject(uoffset_t i)
const {
252 FLATBUFFERS_ASSERT(i < size());
257 const uint8_t *Data()
const {
258 return reinterpret_cast<const uint8_t *
>(&length_ + 1);
261 uint8_t *Data() {
return reinterpret_cast<uint8_t *
>(&length_ + 1); }
264 const T *data()
const {
return reinterpret_cast<const T *
>(Data()); }
265 T *data() {
return reinterpret_cast<T *
>(Data()); }
267 template<
typename K> return_type LookupByKey(K key)
const {
268 void *search_result = std::bsearch(
271 if (!search_result) {
275 const uint8_t *element =
reinterpret_cast<const uint8_t *
>(search_result);
292 template<
typename K>
static int KeyCompare(
const void *ap,
const void *bp) {
293 const K *key =
reinterpret_cast<const K *
>(ap);
294 const uint8_t *data =
reinterpret_cast<const uint8_t *
>(bp);
299 return -table->KeyCompareWithValue(*key);
307 uoffset_t size()
const {
return EndianScalar(length_); }
309 const uint8_t *Data()
const {
310 return reinterpret_cast<const uint8_t *
>(&length_ + 1);
312 uint8_t *Data() {
return reinterpret_cast<uint8_t *
>(&length_ + 1); }
323 #ifndef FLATBUFFERS_CPP98_STL 324 template<
typename T,
typename U>
326 static_assert(std::is_base_of<T, U>::value,
"Unrelated types");
330 template<
typename T,
typename U>
332 static_assert(std::is_base_of<T, U>::value,
"Unrelated types");
339 template<
typename T>
static inline size_t VectorLength(
const Vector<T> *v) {
340 return v ? v->Length() : 0;
344 const char *c_str()
const {
return reinterpret_cast<const char *
>(Data()); }
345 std::string str()
const {
return std::string(c_str(), Length()); }
348 #ifdef FLATBUFFERS_HAS_STRING_VIEW 349 flatbuffers::string_view string_view()
const {
350 return flatbuffers::string_view(c_str(), Length());
352 #endif // FLATBUFFERS_HAS_STRING_VIEW 355 bool operator<(
const String &o)
const {
356 return strcmp(c_str(), o.c_str()) < 0;
362 static inline std::string GetString(
const String * str) {
363 return str ? str->str() :
"";
368 static inline const char * GetCstring(
const String * str) {
369 return str ? str->c_str() :
"";
379 virtual uint8_t *allocate(
size_t size) = 0;
382 virtual void deallocate(uint8_t *p,
size_t size) = 0;
389 virtual uint8_t *reallocate_downward(uint8_t *old_p,
size_t old_size,
390 size_t new_size,
size_t in_use_back,
391 size_t in_use_front) {
392 FLATBUFFERS_ASSERT(new_size > old_size);
393 uint8_t *new_p = allocate(new_size);
394 memcpy_downward(old_p, old_size, new_p, new_size, in_use_back,
396 deallocate(old_p, old_size);
405 void memcpy_downward(uint8_t *old_p,
size_t old_size,
406 uint8_t *new_p,
size_t new_size,
407 size_t in_use_back,
size_t in_use_front) {
408 memcpy(new_p + new_size - in_use_back, old_p + old_size - in_use_back,
410 memcpy(new_p, old_p, in_use_front);
417 uint8_t *allocate(
size_t size) FLATBUFFERS_OVERRIDE {
418 return new uint8_t[size];
421 void deallocate(uint8_t *p,
size_t) FLATBUFFERS_OVERRIDE {
430 inline uint8_t *Allocate(
Allocator *allocator,
size_t size) {
431 return allocator ? allocator->allocate(size)
435 inline void Deallocate(
Allocator *allocator, uint8_t *p,
size_t size) {
436 if (allocator) allocator->deallocate(p, size);
440 inline uint8_t *ReallocateDownward(
Allocator *allocator, uint8_t *old_p,
441 size_t old_size,
size_t new_size,
442 size_t in_use_back,
size_t in_use_front) {
444 ? allocator->reallocate_downward(old_p, old_size, new_size,
445 in_use_back, in_use_front)
447 in_use_back, in_use_front);
456 : allocator_(
nullptr),
457 own_allocator_(
false),
464 size_t reserved, uint8_t *cur,
size_t sz)
465 : allocator_(allocator),
466 own_allocator_(own_allocator),
473 : allocator_(other.allocator_),
474 own_allocator_(other.own_allocator_),
476 reserved_(other.reserved_),
485 allocator_ = other.allocator_;
486 own_allocator_ = other.own_allocator_;
488 reserved_ = other.reserved_;
499 const uint8_t *data()
const {
return cur_; }
501 uint8_t *data() {
return cur_; }
503 size_t size()
const {
return size_; }
506 #if 0 // disabled for now due to the ordering of classes in this header 508 bool Verify()
const {
509 Verifier verifier(data(), size());
510 return verifier.Verify<T>(
nullptr);
514 const T* GetRoot()
const {
515 return flatbuffers::GetRoot<T>(data());
520 return flatbuffers::GetRoot<T>(data());
527 FLATBUFFERS_DELETE_FUNC(
538 inline void destroy() {
539 if (buf_) Deallocate(allocator_, buf_, reserved_);
540 if (own_allocator_ && allocator_) {
delete allocator_; }
544 inline void reset() {
545 allocator_ =
nullptr;
546 own_allocator_ =
false;
565 size_t buffer_minalign)
566 : allocator_(allocator),
567 own_allocator_(own_allocator),
568 initial_size_(initial_size),
569 buffer_minalign_(buffer_minalign),
576 : allocator_(other.allocator_),
577 own_allocator_(other.own_allocator_),
578 initial_size_(other.initial_size_),
579 buffer_minalign_(other.buffer_minalign_),
580 reserved_(other.reserved_),
583 scratch_(other.scratch_) {
587 other.own_allocator_ =
false;
589 other.buf_ =
nullptr;
590 other.cur_ =
nullptr;
591 other.scratch_ =
nullptr;
613 cur_ = buf_ + reserved_;
621 void clear_scratch() {
625 void clear_allocator() {
626 if (own_allocator_ && allocator_) {
delete allocator_; }
627 allocator_ =
nullptr;
628 own_allocator_ =
false;
631 void clear_buffer() {
632 if (buf_) Deallocate(allocator_, buf_, reserved_);
637 uint8_t *release_raw(
size_t &allocated_bytes,
size_t &offset) {
639 allocated_bytes = reserved_;
640 offset =
static_cast<size_t>(cur_ - buf_);
652 DetachedBuffer fb(allocator_, own_allocator_, buf_, reserved_, cur_,
654 if (own_allocator_) {
655 allocator_ =
nullptr;
656 own_allocator_ =
false;
663 size_t ensure_space(
size_t len) {
664 FLATBUFFERS_ASSERT(cur_ >= scratch_ && scratch_ >= buf_);
665 if (len > static_cast<size_t>(cur_ - scratch_)) { reallocate(len); }
668 FLATBUFFERS_ASSERT(size() < FLATBUFFERS_MAX_BUFFER_SIZE);
672 inline uint8_t *make_space(
size_t len) {
673 size_t space = ensure_space(len);
679 Allocator *get_custom_allocator() {
return allocator_; }
681 uoffset_t size()
const {
682 return static_cast<uoffset_t
>(reserved_ - (cur_ - buf_));
685 uoffset_t scratch_size()
const {
686 return static_cast<uoffset_t
>(scratch_ - buf_);
689 size_t capacity()
const {
return reserved_; }
691 uint8_t *data()
const {
692 FLATBUFFERS_ASSERT(cur_);
696 uint8_t *scratch_data()
const {
697 FLATBUFFERS_ASSERT(buf_);
701 uint8_t *scratch_end()
const {
702 FLATBUFFERS_ASSERT(scratch_);
706 uint8_t *data_at(
size_t offset)
const {
return buf_ + reserved_ - offset; }
708 void push(
const uint8_t *bytes,
size_t num) {
709 memcpy(make_space(num), bytes, num);
713 template<
typename T>
void push_small(
const T &little_endian_t) {
714 make_space(
sizeof(T));
715 *
reinterpret_cast<T *
>(cur_) = little_endian_t;
718 template<
typename T>
void scratch_push_small(
const T &t) {
719 ensure_space(
sizeof(T));
720 *
reinterpret_cast<T *
>(scratch_) = t;
721 scratch_ +=
sizeof(T);
726 void fill(
size_t zero_pad_bytes) {
727 make_space(zero_pad_bytes);
728 for (
size_t i = 0; i < zero_pad_bytes; i++) cur_[i] = 0;
732 void fill_big(
size_t zero_pad_bytes) {
733 memset(make_space(zero_pad_bytes), 0, zero_pad_bytes);
736 void pop(
size_t bytes_to_remove) { cur_ += bytes_to_remove; }
737 void scratch_pop(
size_t bytes_to_remove) { scratch_ -= bytes_to_remove; }
741 swap(allocator_, other.allocator_);
742 swap(own_allocator_, other.own_allocator_);
743 swap(initial_size_, other.initial_size_);
744 swap(buffer_minalign_, other.buffer_minalign_);
745 swap(reserved_, other.reserved_);
746 swap(buf_, other.buf_);
747 swap(cur_, other.cur_);
748 swap(scratch_, other.scratch_);
753 swap(allocator_, other.allocator_);
754 swap(own_allocator_, other.own_allocator_);
764 size_t initial_size_;
765 size_t buffer_minalign_;
771 void reallocate(
size_t len) {
772 auto old_reserved = reserved_;
773 auto old_size = size();
774 auto old_scratch_size = scratch_size();
775 reserved_ += (std::max)(len,
776 old_reserved ? old_reserved / 2 : initial_size_);
777 reserved_ = (reserved_ + buffer_minalign_ - 1) & ~(buffer_minalign_ - 1);
779 buf_ = ReallocateDownward(allocator_, buf_, old_reserved, reserved_,
780 old_size, old_scratch_size);
782 buf_ = Allocate(allocator_, reserved_);
784 cur_ = buf_ + reserved_ - old_size;
785 scratch_ = buf_ + old_scratch_size;
790 inline voffset_t FieldIndexToOffset(voffset_t field_id) {
792 const int fixed_fields = 2;
793 return static_cast<voffset_t
>((field_id + fixed_fields) *
sizeof(voffset_t));
796 template<
typename T,
typename Alloc>
797 const T *data(
const std::vector<T, Alloc> &v) {
798 return v.empty() ? nullptr : &v.front();
800 template<
typename T,
typename Alloc> T *data(std::vector<T, Alloc> &v) {
801 return v.empty() ? nullptr : &v.front();
830 bool own_allocator =
false,
831 size_t buffer_minalign =
832 AlignOf<largest_scalar_t>())
833 : buf_(initial_size, allocator, own_allocator, buffer_minalign),
839 force_defaults_(false),
840 dedup_vtables_(true),
841 string_pool(nullptr) {
847 : buf_(1024, nullptr, false, AlignOf<largest_scalar_t>()),
853 force_defaults_(false),
854 dedup_vtables_(true),
855 string_pool(nullptr) {
872 buf_.swap(other.buf_);
873 swap(num_field_loc, other.num_field_loc);
874 swap(max_voffset_, other.max_voffset_);
875 swap(nested, other.nested);
876 swap(finished, other.finished);
877 swap(minalign_, other.minalign_);
878 swap(force_defaults_, other.force_defaults_);
879 swap(dedup_vtables_, other.dedup_vtables_);
880 swap(string_pool, other.string_pool);
884 if (string_pool)
delete string_pool;
900 if (string_pool) string_pool->clear();
905 uoffset_t
GetSize()
const {
return buf_.size(); }
926 return buf_.release();
933 return buf_.release();
946 return buf_.release_raw(size, offset);
960 void Finished()
const {
966 FLATBUFFERS_ASSERT(finished);
981 void Pad(
size_t num_bytes) { buf_.fill(num_bytes); }
983 void TrackMinAlign(
size_t elem_size) {
984 if (elem_size > minalign_) minalign_ = elem_size;
987 void Align(
size_t elem_size) {
988 TrackMinAlign(elem_size);
989 buf_.fill(PaddingBytes(buf_.size(), elem_size));
992 void PushFlatBuffer(
const uint8_t *bytes,
size_t size) {
993 PushBytes(bytes, size);
997 void PushBytes(
const uint8_t *bytes,
size_t size) { buf_.push(bytes, size); }
999 void PopBytes(
size_t amount) { buf_.pop(amount); }
1001 template<
typename T>
void AssertScalarT() {
1003 static_assert(flatbuffers::is_scalar<T>::value,
"T must be a scalar type");
1007 template<
typename T> uoffset_t PushElement(T element) {
1009 T litle_endian_element = EndianScalar(element);
1011 buf_.push_small(litle_endian_element);
1015 template<
typename T> uoffset_t PushElement(
Offset<T> off) {
1017 return PushElement(ReferTo(off.o));
1022 void TrackField(voffset_t field, uoffset_t off) {
1024 buf_.scratch_push_small(fl);
1026 max_voffset_ = (std::max)(max_voffset_, field);
1030 template<
typename T>
void AddElement(voffset_t field, T e, T def) {
1032 if (e == def && !force_defaults_)
return;
1033 auto off = PushElement(e);
1034 TrackField(field, off);
1037 template<
typename T>
void AddOffset(voffset_t field,
Offset<T> off) {
1038 if (off.IsNull())
return;
1039 AddElement(field, ReferTo(off.o), static_cast<uoffset_t>(0));
1042 template<
typename T>
void AddStruct(voffset_t field,
const T *structptr) {
1043 if (!structptr)
return;
1044 Align(AlignOf<T>());
1045 buf_.push_small(*structptr);
1046 TrackField(field, GetSize());
1049 void AddStructOffset(voffset_t field, uoffset_t off) {
1050 TrackField(field, off);
1056 uoffset_t ReferTo(uoffset_t off) {
1058 Align(
sizeof(uoffset_t));
1060 FLATBUFFERS_ASSERT(off && off <= GetSize());
1061 return GetSize() - off +
static_cast<uoffset_t
>(
sizeof(uoffset_t));
1073 FLATBUFFERS_ASSERT(!nested);
1075 FLATBUFFERS_ASSERT(!num_field_loc);
1080 uoffset_t StartTable() {
1089 uoffset_t EndTable(uoffset_t start) {
1091 FLATBUFFERS_ASSERT(nested);
1094 auto vtableoffsetloc = PushElement<soffset_t>(0);
1101 (std::max)(static_cast<voffset_t>(max_voffset_ +
sizeof(voffset_t)),
1102 FieldIndexToOffset(0));
1103 buf_.fill_big(max_voffset_);
1104 auto table_object_size = vtableoffsetloc - start;
1106 FLATBUFFERS_ASSERT(table_object_size < 0x10000);
1107 WriteScalar<voffset_t>(buf_.data() +
sizeof(voffset_t),
1108 static_cast<voffset_t>(table_object_size));
1109 WriteScalar<voffset_t>(buf_.data(), max_voffset_);
1111 for (
auto it = buf_.scratch_end() - num_field_loc *
sizeof(
FieldLoc);
1112 it < buf_.scratch_end(); it +=
sizeof(
FieldLoc)) {
1113 auto field_location =
reinterpret_cast<FieldLoc *
>(it);
1114 auto pos =
static_cast<voffset_t
>(vtableoffsetloc - field_location->off);
1117 !ReadScalar<voffset_t>(buf_.data() + field_location->id));
1118 WriteScalar<voffset_t>(buf_.data() + field_location->id, pos);
1121 auto vt1 =
reinterpret_cast<voffset_t *
>(buf_.data());
1122 auto vt1_size = ReadScalar<voffset_t>(vt1);
1123 auto vt_use = GetSize();
1126 if (dedup_vtables_) {
1127 for (
auto it = buf_.scratch_data(); it < buf_.scratch_end();
1128 it +=
sizeof(uoffset_t)) {
1129 auto vt_offset_ptr =
reinterpret_cast<uoffset_t *
>(it);
1130 auto vt2 =
reinterpret_cast<voffset_t *
>(buf_.data_at(*vt_offset_ptr));
1131 auto vt2_size = *vt2;
1132 if (vt1_size != vt2_size || memcmp(vt2, vt1, vt1_size))
continue;
1133 vt_use = *vt_offset_ptr;
1134 buf_.pop(GetSize() - vtableoffsetloc);
1139 if (vt_use == GetSize()) { buf_.scratch_push_small(vt_use); }
1145 WriteScalar(buf_.data_at(vtableoffsetloc),
1146 static_cast<soffset_t
>(vt_use) -
1147 static_cast<soffset_t>(vtableoffsetloc));
1150 return vtableoffsetloc;
1154 uoffset_t EndTable(uoffset_t start, voffset_t ) {
1155 return EndTable(start);
1160 template<
typename T>
void Required(
Offset<T> table, voffset_t field);
1162 uoffset_t StartStruct(
size_t alignment) {
1167 uoffset_t EndStruct() {
return GetSize(); }
1169 void ClearOffsets() {
1170 buf_.scratch_pop(num_field_loc *
sizeof(
FieldLoc));
1177 void PreAlign(
size_t len,
size_t alignment) {
1178 TrackMinAlign(alignment);
1179 buf_.fill(PaddingBytes(GetSize() + len, alignment));
1181 template<
typename T>
void PreAlign(
size_t len) {
1183 PreAlign(len,
sizeof(T));
1193 PreAlign<uoffset_t>(len + 1);
1195 PushBytes(reinterpret_cast<const uint8_t *>(str), len);
1196 PushElement(static_cast<uoffset_t>(len));
1204 return CreateString(str, strlen(str));
1211 return CreateString(str, strlen(str));
1218 return CreateString(str.c_str(), str.length());
1222 #ifdef FLATBUFFERS_HAS_STRING_VIEW 1227 return CreateString(str.data(), str.size());
1229 #endif // FLATBUFFERS_HAS_STRING_VIEW 1236 return str ? CreateString(str->c_str(), str->Length()) : 0;
1244 return CreateString(str.c_str(), str.length());
1256 auto size_before_string = buf_.size();
1259 auto off = CreateString(str, len);
1260 auto it = string_pool->find(off);
1262 if (it != string_pool->end()) {
1264 buf_.pop(buf_.size() - size_before_string);
1268 string_pool->insert(off);
1278 return CreateSharedString(str, strlen(str));
1287 return CreateSharedString(str.c_str(), str.length());
1296 return CreateSharedString(str->c_str(), str->Length());
1300 uoffset_t EndVector(
size_t len) {
1301 FLATBUFFERS_ASSERT(nested);
1303 return PushElement(static_cast<uoffset_t>(len));
1306 void StartVector(
size_t len,
size_t elemsize) {
1309 PreAlign<uoffset_t>(len * elemsize);
1310 PreAlign(len * elemsize, elemsize);
1318 void ForceVectorAlignment(
size_t len,
size_t elemsize,
size_t alignment) {
1319 PreAlign(len * elemsize, alignment);
1323 void ForceStringAlignment(
size_t len,
size_t alignment) {
1324 PreAlign((len + 1) *
sizeof(
char), alignment);
1340 StartVector(len,
sizeof(T));
1342 #if FLATBUFFERS_LITTLEENDIAN 1343 PushBytes(reinterpret_cast<const uint8_t *>(v), len *
sizeof(T));
1345 if (
sizeof(T) == 1) {
1346 PushBytes(reinterpret_cast<const uint8_t *>(v), len);
1348 for (
auto i = len; i > 0; ) {
1349 PushElement(v[--i]);
1357 template<
typename T>
1360 for (
auto i = len; i > 0;) { PushElement(v[--i]); }
1371 return CreateVector(data(v), v.size());
1378 StartVector(v.size(),
sizeof(uint8_t));
1379 for (
auto i = v.size(); i > 0;) {
1380 PushElement(static_cast<uint8_t>(v[--i]));
1386 #ifndef FLATBUFFERS_CPP98_STL 1395 const std::function<T (
size_t i)> &f) {
1396 std::vector<T> elems(vector_size);
1397 for (
size_t i = 0; i < vector_size; i++) elems[i] = f(i);
1398 return CreateVector(elems);
1412 template<
typename T,
typename F,
typename S>
1414 std::vector<T> elems(vector_size);
1415 for (
size_t i = 0; i < vector_size; i++) elems[i] = f(i, state);
1416 return CreateVector(elems);
1426 const std::vector<std::string> &v) {
1427 std::vector<Offset<String>> offsets(v.size());
1428 for (
size_t i = 0; i < v.size(); i++) offsets[i] = CreateString(v[i]);
1429 return CreateVector(offsets);
1439 template<
typename T>
1441 StartVector(len *
sizeof(T) / AlignOf<T>(), AlignOf<T>());
1442 PushBytes(reinterpret_cast<const uint8_t *>(v),
sizeof(T) * len);
1454 template<
typename T,
typename S>
1457 extern T Pack(
const S &);
1458 typedef T (*Pack_t)(
const S &);
1459 std::vector<T> vv(len);
1460 std::transform(v, v + len, vv.begin(), *(Pack_t)&Pack);
1461 return CreateVectorOfStructs<T>(vv.data(), vv.size());
1465 #ifndef FLATBUFFERS_CPP98_STL 1475 size_t vector_size,
const std::function<
void(
size_t i, T *)> &filler) {
1476 T* structs = StartVectorOfStructs<T>(vector_size);
1477 for (
size_t i = 0; i < vector_size; i++) {
1481 return EndVectorOfStructs<T>(vector_size);
1495 template<
typename T,
typename F,
typename S>
1498 T *structs = StartVectorOfStructs<T>(vector_size);
1499 for (
size_t i = 0; i < vector_size; i++) {
1500 f(i, structs, state);
1503 return EndVectorOfStructs<T>(vector_size);
1512 template<
typename T,
typename Alloc>
1514 const std::vector<T, Alloc> &v) {
1515 return CreateVectorOfStructs(data(v), v.size());
1526 template<
typename T,
typename S>
1528 const std::vector<S> &v) {
1529 return CreateVectorOfNativeStructs<T, S>(data(v), v.size());
1533 template<
typename T>
struct StructKeyComparator {
1534 bool operator()(
const T &a,
const T &b)
const {
1535 return a.KeyCompareLessThan(&b);
1539 StructKeyComparator &operator=(
const StructKeyComparator &);
1550 template<
typename T>
1552 return CreateVectorOfSortedStructs(data(*v), v->size());
1563 template<
typename T,
typename S>
1565 std::vector<S> *v) {
1566 return CreateVectorOfSortedNativeStructs<T, S>(data(*v), v->size());
1577 template<
typename T>
1579 std::sort(v, v + len, StructKeyComparator<T>());
1580 return CreateVectorOfStructs(v, len);
1592 template<
typename T,
typename S>
1595 extern T Pack(
const S &);
1596 typedef T (*Pack_t)(
const S &);
1597 std::vector<T> vv(len);
1598 std::transform(v, v + len, vv.begin(), *(Pack_t)&Pack);
1599 return CreateVectorOfSortedStructs<T>(vv, len);
1603 template<
typename T>
struct TableKeyComparator {
1606 auto table_a =
reinterpret_cast<T *
>(buf_.data_at(a.o));
1607 auto table_b =
reinterpret_cast<T *
>(buf_.data_at(b.o));
1608 return table_a->KeyCompareLessThan(table_b);
1613 TableKeyComparator &operator=(
const TableKeyComparator &);
1625 template<
typename T>
1628 std::sort(v, v + len, TableKeyComparator<T>(buf_));
1629 return CreateVector(v, len);
1639 template<
typename T>
1642 return CreateVectorOfSortedTables(data(*v), v->size());
1655 StartVector(len, elemsize);
1656 buf_.make_space(len * elemsize);
1657 auto vec_start = GetSize();
1658 auto vec_end = EndVector(len);
1659 *buf = buf_.data_at(vec_start);
1671 template<
typename T>
1674 return CreateUninitializedVector(len,
sizeof(T),
1675 reinterpret_cast<uint8_t **>(buf));
1678 template<
typename T>
1680 return CreateUninitializedVector(len,
sizeof(T),
1681 reinterpret_cast<uint8_t **>(buf));
1687 Align(AlignOf<T>());
1688 buf_.push_small(structobj);
1693 static const size_t kFileIdentifierLength = 4;
1698 template<
typename T>
1700 Finish(root.o, file_identifier,
false);
1710 template<
typename T>
1712 const char *file_identifier =
nullptr) {
1713 Finish(root.o, file_identifier,
true);
1721 void Finish(uoffset_t root,
const char *file_identifier,
bool size_prefix) {
1723 buf_.clear_scratch();
1725 PreAlign((size_prefix ?
sizeof(uoffset_t) : 0) +
sizeof(uoffset_t) +
1726 (file_identifier ? kFileIdentifierLength : 0),
1728 if (file_identifier) {
1729 FLATBUFFERS_ASSERT(strlen(file_identifier) == kFileIdentifierLength);
1730 PushBytes(reinterpret_cast<const uint8_t *>(file_identifier),
1731 kFileIdentifierLength);
1733 PushElement(ReferTo(root));
1734 if (size_prefix) { PushElement(GetSize()); }
1747 uoffset_t num_field_loc;
1750 voffset_t max_voffset_;
1760 bool force_defaults_;
1762 bool dedup_vtables_;
1767 auto stra =
reinterpret_cast<const String *
>(buf_->data_at(a.o));
1768 auto strb =
reinterpret_cast<const String *
>(buf_->data_at(b.o));
1769 return strncmp(stra->c_str(), strb->c_str(),
1770 (std::min)(stra->size(), strb->size()) + 1) < 0;
1777 StringOffsetMap *string_pool;
1782 template<
typename T> T *StartVectorOfStructs(
size_t vector_size) {
1783 StartVector(vector_size *
sizeof(T) / AlignOf<T>(), AlignOf<T>());
1784 return reinterpret_cast<T *
>(buf_.make_space(vector_size *
sizeof(T)));
1789 template<
typename T>
1798 template<
typename T> T *GetMutableRoot(
void *buf) {
1800 return reinterpret_cast<T *
>(
1801 reinterpret_cast<uint8_t *
>(buf) +
1802 EndianScalar(*reinterpret_cast<uoffset_t *>(buf)));
1805 template<
typename T>
const T *GetRoot(
const void *buf) {
1806 return GetMutableRoot<T>(
const_cast<void *
>(buf));
1809 template<
typename T>
const T *GetSizePrefixedRoot(
const void *buf) {
1810 return GetRoot<T>(
reinterpret_cast<const uint8_t *
>(buf) +
sizeof(uoffset_t));
1816 template<
typename T>
1822 template<
typename T>
1824 return GetMutableTemporaryPointer<T>(fbb, offset);
1834 inline const char *GetBufferIdentifier(
const void *buf,
bool size_prefixed =
false) {
1835 return reinterpret_cast<const char *
>(buf) +
1836 ((size_prefixed) ? 2 *
sizeof(uoffset_t) :
sizeof(uoffset_t));
1840 inline bool BufferHasIdentifier(
const void *buf,
const char *identifier,
bool size_prefixed =
false) {
1841 return strncmp(GetBufferIdentifier(buf, size_prefixed), identifier,
1846 class Verifier FLATBUFFERS_FINAL_CLASS {
1848 Verifier(
const uint8_t *buf,
size_t buf_len, uoffset_t _max_depth = 64,
1849 uoffset_t _max_tables = 1000000)
1853 max_depth_(_max_depth),
1855 max_tables_(_max_tables)
1857 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 1862 assert(size_ < FLATBUFFERS_MAX_BUFFER_SIZE);
1866 bool Check(
bool ok)
const {
1868 #ifdef FLATBUFFERS_DEBUG_VERIFICATION_FAILURE 1869 FLATBUFFERS_ASSERT(ok);
1871 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 1880 bool Verify(
size_t elem,
size_t elem_len)
const {
1882 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 1883 auto upper_bound = elem + elem_len;
1884 if (upper_bound_ < upper_bound)
1885 upper_bound_ = upper_bound;
1888 return Check(elem_len < size_ && elem <= size_ - elem_len);
1891 template<
typename T>
bool VerifyAlignment(
size_t elem)
const {
1892 return (elem & (
sizeof(T) - 1)) == 0;
1896 template<
typename T>
bool Verify(
size_t elem)
const {
1897 return VerifyAlignment<T>(elem) && Verify(elem,
sizeof(T));
1901 bool Verify(
const uint8_t *base, voffset_t elem_off,
size_t elem_len)
const {
1902 return Verify(static_cast<size_t>(base - buf_) + elem_off, elem_len);
1905 template<
typename T>
bool Verify(
const uint8_t *base, voffset_t elem_off)
1907 return Verify(static_cast<size_t>(base - buf_) + elem_off,
sizeof(T));
1911 template<
typename T>
bool VerifyTable(
const T *table) {
1912 return !table || table->Verify(*
this);
1916 template<
typename T>
bool VerifyVector(
const Vector<T> *vec)
const {
1917 return !vec || VerifyVectorOrString(reinterpret_cast<const uint8_t *>(vec),
1923 return VerifyVector(
reinterpret_cast<const Vector<T> *
>(vec));
1927 bool VerifyString(
const String *str)
const {
1930 (VerifyVectorOrString(reinterpret_cast<const uint8_t *>(str),
1933 Check(buf_[end] ==
'\0'));
1937 bool VerifyVectorOrString(
const uint8_t *vec,
size_t elem_size,
1938 size_t *end =
nullptr)
const {
1939 auto veco =
static_cast<size_t>(vec - buf_);
1941 if (!Verify<uoffset_t>(veco))
return false;
1944 auto size = ReadScalar<uoffset_t>(vec);
1945 auto max_elems = FLATBUFFERS_MAX_BUFFER_SIZE / elem_size;
1946 if (!Check(size < max_elems))
1948 auto byte_size =
sizeof(size) + elem_size * size;
1949 if (end) *end = veco + byte_size;
1950 return Verify(veco, byte_size);
1956 for (uoffset_t i = 0; i < vec->size(); i++) {
1957 if (!VerifyString(vec->Get(i)))
return false;
1964 template<
typename T>
bool VerifyVectorOfTables(
const Vector<
Offset<T>> *vec) {
1966 for (uoffset_t i = 0; i < vec->size(); i++) {
1967 if (!vec->Get(i)->Verify(*
this))
return false;
1973 bool VerifyTableStart(
const uint8_t *table) {
1975 auto tableo =
static_cast<size_t>(table - buf_);
1976 if (!Verify<soffset_t>(tableo))
return false;
1979 auto vtableo = tableo -
static_cast<size_t>(ReadScalar<soffset_t>(table));
1981 return VerifyComplexity() && Verify<voffset_t>(vtableo) &&
1982 VerifyAlignment<voffset_t>(ReadScalar<voffset_t>(buf_ + vtableo)) &&
1983 Verify(vtableo, ReadScalar<voffset_t>(buf_ + vtableo));
1986 template<
typename T>
1987 bool VerifyBufferFromStart(
const char *identifier,
size_t start) {
1989 (size_ < 2 *
sizeof(flatbuffers::uoffset_t) ||
1990 !BufferHasIdentifier(buf_ + start, identifier))) {
1995 auto o = VerifyOffset(start);
1996 return o &&
reinterpret_cast<const T *
>(buf_ + start + o)->Verify(*
this)
1998 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 1999 && GetComputedSize()
2006 template<
typename T>
bool VerifyBuffer() {
return VerifyBuffer<T>(
nullptr); }
2008 template<
typename T>
bool VerifyBuffer(
const char *identifier) {
2009 return VerifyBufferFromStart<T>(identifier, 0);
2012 template<
typename T>
bool VerifySizePrefixedBuffer(
const char *identifier) {
2013 return Verify<uoffset_t>(0U) &&
2014 ReadScalar<uoffset_t>(buf_) == size_ -
sizeof(uoffset_t) &&
2015 VerifyBufferFromStart<T>(identifier,
sizeof(uoffset_t));
2018 uoffset_t VerifyOffset(
size_t start)
const {
2019 if (!Verify<uoffset_t>(start))
return 0;
2020 auto o = ReadScalar<uoffset_t>(buf_ + start);
2024 if (!Check(static_cast<soffset_t>(o) >= 0))
return 0;
2027 if (!Verify(start + o, 1))
return 0;
2031 uoffset_t VerifyOffset(
const uint8_t *base, voffset_t start)
const {
2032 return VerifyOffset(static_cast<size_t>(base - buf_) + start);
2039 bool VerifyComplexity() {
2042 return Check(depth_ <= max_depth_ && num_tables_ <= max_tables_);
2052 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 2054 size_t GetComputedSize()
const {
2055 uintptr_t size = upper_bound_;
2057 size = (size - 1 +
sizeof(uoffset_t)) & ~(
sizeof(uoffset_t) - 1);
2058 return (size > size_) ? 0 : size;
2064 const uint8_t *buf_;
2067 uoffset_t max_depth_;
2068 uoffset_t num_tables_;
2069 uoffset_t max_tables_;
2071 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 2072 mutable size_t upper_bound_;
2080 struct BufferRefBase {};
2081 template<
typename T>
struct BufferRef : BufferRefBase {
2082 BufferRef() : buf(
nullptr), len(0), must_free(
false) {}
2083 BufferRef(uint8_t *_buf, uoffset_t _len)
2084 : buf(_buf), len(_len), must_free(
false) {}
2087 if (must_free) free(buf);
2090 const T *GetRoot()
const {
return flatbuffers::GetRoot<T>(buf); }
2093 Verifier verifier(buf, len);
2094 return verifier.VerifyBuffer<T>(
nullptr);
2106 class Struct FLATBUFFERS_FINAL_CLASS {
2108 template<
typename T> T GetField(uoffset_t o)
const {
2109 return ReadScalar<T>(&data_[o]);
2112 template<
typename T> T GetStruct(uoffset_t o)
const {
2113 return reinterpret_cast<T
>(&data_[o]);
2116 const uint8_t *GetAddressOf(uoffset_t o)
const {
return &data_[o]; }
2117 uint8_t *GetAddressOf(uoffset_t o) {
return &data_[o]; }
2127 const uint8_t *GetVTable()
const {
2128 return data_ - ReadScalar<soffset_t>(data_);
2133 voffset_t GetOptionalFieldOffset(voffset_t field)
const {
2135 auto vtable = GetVTable();
2137 auto vtsize = ReadScalar<voffset_t>(vtable);
2140 return field < vtsize ? ReadScalar<voffset_t>(vtable + field) : 0;
2143 template<
typename T> T GetField(voffset_t field, T defaultval)
const {
2144 auto field_offset = GetOptionalFieldOffset(field);
2145 return field_offset ? ReadScalar<T>(data_ + field_offset) : defaultval;
2148 template<
typename P> P GetPointer(voffset_t field) {
2149 auto field_offset = GetOptionalFieldOffset(field);
2150 auto p = data_ + field_offset;
2151 return field_offset ?
reinterpret_cast<P
>(p + ReadScalar<uoffset_t>(p))
2154 template<
typename P> P GetPointer(voffset_t field)
const {
2155 return const_cast<Table *
>(
this)->GetPointer<P>(field);
2158 template<
typename P> P GetStruct(voffset_t field)
const {
2159 auto field_offset = GetOptionalFieldOffset(field);
2160 auto p =
const_cast<uint8_t *
>(data_ + field_offset);
2161 return field_offset ?
reinterpret_cast<P
>(p) :
nullptr;
2164 template<
typename T>
bool SetField(voffset_t field, T val, T def) {
2165 auto field_offset = GetOptionalFieldOffset(field);
2166 if (!field_offset)
return val == def;
2167 WriteScalar(data_ + field_offset, val);
2171 bool SetPointer(voffset_t field,
const uint8_t *val) {
2172 auto field_offset = GetOptionalFieldOffset(field);
2173 if (!field_offset)
return false;
2174 WriteScalar(data_ + field_offset,
2175 static_cast<uoffset_t>(val - (data_ + field_offset)));
2179 uint8_t *GetAddressOf(voffset_t field) {
2180 auto field_offset = GetOptionalFieldOffset(field);
2181 return field_offset ? data_ + field_offset :
nullptr;
2183 const uint8_t *GetAddressOf(voffset_t field)
const {
2184 return const_cast<Table *
>(
this)->GetAddressOf(field);
2187 bool CheckField(voffset_t field)
const {
2188 return GetOptionalFieldOffset(field) != 0;
2193 bool VerifyTableStart(Verifier &verifier)
const {
2194 return verifier.VerifyTableStart(data_);
2198 template<
typename T>
2199 bool VerifyField(
const Verifier &verifier, voffset_t field)
const {
2202 auto field_offset = GetOptionalFieldOffset(field);
2204 return !field_offset || verifier.Verify<T>(data_, field_offset);
2208 template<
typename T>
2209 bool VerifyFieldRequired(
const Verifier &verifier, voffset_t field)
const {
2210 auto field_offset = GetOptionalFieldOffset(field);
2211 return verifier.Check(field_offset != 0) &&
2212 verifier.Verify<T>(data_, field_offset);
2216 bool VerifyOffset(
const Verifier &verifier, voffset_t field)
const {
2217 auto field_offset = GetOptionalFieldOffset(field);
2218 return !field_offset || verifier.VerifyOffset(data_, field_offset);
2221 bool VerifyOffsetRequired(
const Verifier &verifier, voffset_t field)
const {
2222 auto field_offset = GetOptionalFieldOffset(field);
2223 return verifier.Check(field_offset != 0) &&
2224 verifier.VerifyOffset(data_, field_offset);
2231 Table(
const Table &other);
2236 template<
typename T>
void FlatBufferBuilder::Required(
Offset<T> table,
2238 auto table_ptr =
reinterpret_cast<const Table *
>(buf_.data_at(table.o));
2239 bool ok = table_ptr->GetOptionalFieldOffset(field) != 0;
2241 FLATBUFFERS_ASSERT(ok);
2249 inline const uint8_t *GetBufferStartFromRootPointer(
const void *root) {
2250 auto table =
reinterpret_cast<const Table *
>(root);
2251 auto vtable = table->GetVTable();
2253 auto start = (std::min)(vtable, reinterpret_cast<const uint8_t *>(root));
2255 start =
reinterpret_cast<const uint8_t *
>(
reinterpret_cast<uintptr_t
>(start) &
2256 ~(
sizeof(uoffset_t) - 1));
2267 "file_identifier is assumed to be the same size as uoffset_t");
2268 for (
auto possible_roots = FLATBUFFERS_MAX_ALIGNMENT /
sizeof(uoffset_t) + 1;
2269 possible_roots; possible_roots--) {
2270 start -=
sizeof(uoffset_t);
2271 if (ReadScalar<uoffset_t>(start) + start ==
2272 reinterpret_cast<const uint8_t *
>(root))
2279 FLATBUFFERS_ASSERT(
false);
2284 inline uoffset_t GetPrefixedSize(
const uint8_t* buf){
return ReadScalar<uoffset_t>(buf); }
2289 struct NativeTable {};
2299 typedef uint64_t hash_value_t;
2301 #ifdef FLATBUFFERS_CPP98_STL 2302 typedef void (*resolver_function_t)(
void **pointer_adr, hash_value_t hash);
2303 typedef hash_value_t (*rehasher_function_t)(
void *pointer);
2305 typedef std::function<void (void **pointer_adr, hash_value_t hash)>
2306 resolver_function_t;
2307 typedef std::function<hash_value_t (void *pointer)> rehasher_function_t;
2318 template<
typename T>
bool IsFieldPresent(
const T *table, voffset_t field) {
2320 return reinterpret_cast<const Table *
>(table)->CheckField(field);
2326 inline int LookupEnum(
const char **names,
const char *name) {
2327 for (
const char **p = names; *p; p++)
2328 if (!strcmp(*p, name))
return static_cast<int>(p - names);
2343 #if defined(_MSC_VER) 2344 #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \ 2345 __pragma(pack(1)); \ 2346 struct __declspec(align(alignment)) 2347 #define FLATBUFFERS_STRUCT_END(name, size) \ 2349 static_assert(sizeof(name) == size, "compiler breaks packing rules") 2350 #elif defined(__GNUC__) || defined(__clang__) 2351 #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \ 2352 _Pragma("pack(1)") \ 2353 struct __attribute__((aligned(alignment))) 2354 #define FLATBUFFERS_STRUCT_END(name, size) \ 2356 static_assert(sizeof(name) == size, "compiler breaks packing rules") 2358 #error Unknown compiler, please define structure alignment macros 2372 enum SequenceType { ST_TABLE, ST_STRUCT, ST_UNION, ST_ENUM };
2376 #define FLATBUFFERS_GEN_ELEMENTARY_TYPES(ET) \ 2390 ET(ET_SEQUENCE) // See SequenceType. 2392 enum ElementaryType {
2393 #define FLATBUFFERS_ET(E) E, 2394 FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET)
2395 #undef FLATBUFFERS_ET 2398 inline const char *
const *ElementaryTypeNames() {
2399 static const char *
const names[] = {
2400 #define FLATBUFFERS_ET(E) #E, 2401 FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET)
2402 #undef FLATBUFFERS_ET 2410 uint16_t base_type : 4;
2411 uint16_t is_vector : 1;
2412 int16_t sequence_ref : 11;
2415 static_assert(
sizeof(TypeCode) == 2,
"TypeCode");
2420 typedef const TypeTable *(*TypeFunction)();
2425 const TypeCode *type_codes;
2426 const TypeFunction *type_refs;
2427 const int32_t *values;
2428 const char *
const *names;
2442 #if !defined(_WIN32) && !defined(__CYGWIN__) 2444 extern volatile __attribute__((weak))
const char *flatbuffer_version_string;
2445 volatile __attribute__((weak))
const char *flatbuffer_version_string =
2447 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR)
"." 2448 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR)
"." 2449 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
2451 #endif // !defined(_WIN32) && !defined(__CYGWIN__) 2453 #define FLATBUFFERS_DEFINE_BITMASK_OPERATORS(E, T)\ 2454 inline E operator | (E lhs, E rhs){\ 2455 return E(T(lhs) | T(rhs));\ 2457 inline E operator & (E lhs, E rhs){\ 2458 return E(T(lhs) & T(rhs));\ 2460 inline E operator ^ (E lhs, E rhs){\ 2461 return E(T(lhs) ^ T(rhs));\ 2463 inline E operator ~ (E lhs){\ 2466 inline E operator |= (E &lhs, E rhs){\ 2470 inline E operator &= (E &lhs, E rhs){\ 2474 inline E operator ^= (E &lhs, E rhs){\ 2478 inline bool operator !(E rhs) \ 2480 return !bool(T(rhs)); \ 2487 #endif // FLATBUFFERS_H_ Definition: flatbuffers.h:25
Offset< Vector< T > > CreateVector(size_t vector_size, const std::function< T(size_t i)> &f)
Serialize values returned by a function into a FlatBuffer vector.
Definition: flatbuffers.h:1394
uoffset_t CreateUninitializedVector(size_t len, size_t elemsize, uint8_t **buf)
Specialized version of CreateVector for non-copying use cases.
Definition: flatbuffers.h:1652
Offset< Vector< const T * > > CreateVectorOfStructs(size_t vector_size, const std::function< void(size_t i, T *)> &filler)
Serialize an array of structs into a FlatBuffer vector.
Definition: flatbuffers.h:1474
Offset< Vector< const T * > > CreateVectorOfSortedStructs(T *v, size_t len)
Serialize an array of structs into a FlatBuffer vector in sorted order.
Definition: flatbuffers.h:1578
Offset< Vector< const T * > > CreateVectorOfSortedNativeStructs(std::vector< S > *v)
Serialize a std::vector of native structs into a FlatBuffer vector in sorted order.
Definition: flatbuffers.h:1564
uoffset_t GetSize() const
The current size of the serialized buffer, counting from the end.
Definition: flatbuffers.h:905
Offset< Vector< Offset< T > > > CreateVectorOfSortedTables(std::vector< Offset< T >> *v)
Serialize an array of table offsets as a vector in the buffer in sorted order.
Definition: flatbuffers.h:1640
Definition: flatbuffers.h:91
Helper class to hold data needed in creation of a FlatBuffer.
Definition: flatbuffers.h:815
FlatBufferBuilder & operator=(FlatBufferBuilder &&other)
Move assignment operator for FlatBufferBuilder.
Definition: flatbuffers.h:863
Offset< String > CreateString(char *str)
Store a string in the buffer, which is null-terminated.
Definition: flatbuffers.h:1210
void Clear()
Reset all the state in this FlatBufferBuilder so it can be reused to construct another buffer...
Definition: flatbuffers.h:894
Offset< Vector< const T * > > CreateVectorOfStructs(size_t vector_size, F f, S *state)
Serialize an array of structs into a FlatBuffer vector.
Definition: flatbuffers.h:1496
Offset< Vector< const T * > > CreateVectorOfNativeStructs(const S *v, size_t len)
Serialize an array of native structs into a FlatBuffer vector.
Definition: flatbuffers.h:1455
Offset< const T * > CreateStruct(const T &structobj)
Write a struct by itself, typically to be part of a union.
Definition: flatbuffers.h:1685
Definition: flatbuffers.h:22
void FinishSizePrefixed(Offset< T > root, const char *file_identifier=nullptr)
Finish a buffer with a 32 bit size field pre-fixed (size of the buffer following the size field)...
Definition: flatbuffers.h:1711
Definition: flatbuffers.h:453
Offset< String > CreateSharedString(const char *str)
Store a string in the buffer, which null-terminated.
Definition: flatbuffers.h:1277
Definition: flatbuffers.h:305
FlatBufferBuilder(FlatBufferBuilder &&other)
Move constructor for FlatBufferBuilder.
Definition: flatbuffers.h:846
Definition: flatbuffers.h:63
Offset< String > CreateString(const char *str, size_t len)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1191
void ForceDefaults(bool fd)
In order to save space, fields that are set to their default value don't get serialized into the buff...
Definition: flatbuffers.h:974
uint8_t * GetBufferPointer() const
Get the serialized buffer (after you call Finish()).
Definition: flatbuffers.h:910
Definition: flatbuffers.h:415
void DedupVtables(bool dedup)
By default vtables are deduped in order to save space.
Definition: flatbuffers.h:978
static const size_t kFileIdentifierLength
The length of a FlatBuffer file header.
Definition: flatbuffers.h:1693
FlatBufferBuilder(size_t initial_size=1024, Allocator *allocator=nullptr, bool own_allocator=false, size_t buffer_minalign=AlignOf< largest_scalar_t >())
Default constructor for FlatBufferBuilder.
Definition: flatbuffers.h:828
Offset< String > CreateString(const String *str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1235
Definition: flatbuffers.h:560
Offset< Vector< const T * > > CreateVectorOfSortedNativeStructs(S *v, size_t len)
Serialize an array of native structs into a FlatBuffer vector in sorted order.
Definition: flatbuffers.h:1593
Offset< String > CreateSharedString(const String *str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1295
uint8_t * GetCurrentBufferPointer() const
Get a pointer to an unfinished buffer.
Definition: flatbuffers.h:917
Offset< String > CreateString(const T &str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1243
Offset< Vector< const T * > > CreateVectorOfSortedStructs(std::vector< T > *v)
Serialize a std::vector of structs into a FlatBuffer vector in sorted order.
Definition: flatbuffers.h:1551
Offset< Vector< Offset< String > > > CreateVectorOfStrings(const std::vector< std::string > &v)
Serialize a std::vector<std::string> into a FlatBuffer vector.
Definition: flatbuffers.h:1425
Offset< Vector< T > > CreateVector(size_t vector_size, F f, S *state)
Serialize values returned by a function into a FlatBuffer vector.
Definition: flatbuffers.h:1413
size_t GetBufferMinAlignment()
get the minimum alignment this buffer needs to be accessed properly.
Definition: flatbuffers.h:954
Offset< Vector< const T * > > CreateVectorOfStructs(const std::vector< T, Alloc > &v)
Serialize a std::vector of structs into a FlatBuffer vector.
Definition: flatbuffers.h:1513
Offset< Vector< const T * > > CreateVectorOfStructs(const T *v, size_t len)
Serialize an array of structs into a FlatBuffer vector.
Definition: flatbuffers.h:1440
Offset< Vector< const T * > > CreateVectorOfNativeStructs(const std::vector< S > &v)
Serialize a std::vector of native structs into a FlatBuffer vector.
Definition: flatbuffers.h:1527
Offset< String > CreateString(const char *str)
Store a string in the buffer, which is null-terminated.
Definition: flatbuffers.h:1203
Definition: flatbuffers.h:374
Definition: flatbuffers.h:343
uint8_t * ReleaseRaw(size_t &size, size_t &offset)
Get the released pointer to the serialized buffer.
Definition: flatbuffers.h:944
Definition: flatbuffers.h:1738
Offset< String > CreateString(const std::string &str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1217
Offset< Vector< T > > CreateVector(const std::vector< T > &v)
Serialize a std::vector into a FlatBuffer vector.
Definition: flatbuffers.h:1370
Offset< Vector< T > > CreateUninitializedVector(size_t len, T **buf)
Specialized version of CreateVector for non-copying use cases.
Definition: flatbuffers.h:1672
Offset< String > CreateString(flatbuffers::string_view str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1226
Offset< Vector< T > > CreateVector(const T *v, size_t len)
Serialize an array into a FlatBuffer vector.
Definition: flatbuffers.h:1336
Offset< String > CreateSharedString(const std::string &str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1286
Offset< Vector< Offset< T > > > CreateVectorOfSortedTables(Offset< T > *v, size_t len)
Serialize an array of table offsets as a vector in the buffer in sorted order.
Definition: flatbuffers.h:1626
DetachedBuffer Release()
Get the released DetachedBuffer.
Definition: flatbuffers.h:931
Offset< String > CreateSharedString(const char *str, size_t len)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1253
Definition: flatbuffers.h:182
DetachedBuffer ReleaseBufferPointer()
Get the released pointer to the serialized buffer.
Definition: flatbuffers.h:924
void Finish(Offset< T > root, const char *file_identifier=nullptr)
Finish serializing a buffer by writing the root offset.
Definition: flatbuffers.h:1699
Definition: flatbuffers.h:1764