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_) {
584 other.allocator_ =
nullptr;
585 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_);
650 DetachedBuffer fb(allocator_, own_allocator_, buf_, reserved_, cur_,
652 allocator_ =
nullptr;
653 own_allocator_ =
false;
659 size_t ensure_space(
size_t len) {
660 FLATBUFFERS_ASSERT(cur_ >= scratch_ && scratch_ >= buf_);
661 if (len > static_cast<size_t>(cur_ - scratch_)) { reallocate(len); }
664 FLATBUFFERS_ASSERT(size() < FLATBUFFERS_MAX_BUFFER_SIZE);
668 inline uint8_t *make_space(
size_t len) {
669 size_t space = ensure_space(len);
675 Allocator *get_custom_allocator() {
return allocator_; }
677 uoffset_t size()
const {
678 return static_cast<uoffset_t
>(reserved_ - (cur_ - buf_));
681 uoffset_t scratch_size()
const {
682 return static_cast<uoffset_t
>(scratch_ - buf_);
685 size_t capacity()
const {
return reserved_; }
687 uint8_t *data()
const {
688 FLATBUFFERS_ASSERT(cur_);
692 uint8_t *scratch_data()
const {
693 FLATBUFFERS_ASSERT(buf_);
697 uint8_t *scratch_end()
const {
698 FLATBUFFERS_ASSERT(scratch_);
702 uint8_t *data_at(
size_t offset)
const {
return buf_ + reserved_ - offset; }
704 void push(
const uint8_t *bytes,
size_t num) {
705 memcpy(make_space(num), bytes, num);
709 template<
typename T>
void push_small(
const T &little_endian_t) {
710 make_space(
sizeof(T));
711 *
reinterpret_cast<T *
>(cur_) = little_endian_t;
714 template<
typename T>
void scratch_push_small(
const T &t) {
715 ensure_space(
sizeof(T));
716 *
reinterpret_cast<T *
>(scratch_) = t;
717 scratch_ +=
sizeof(T);
722 void fill(
size_t zero_pad_bytes) {
723 make_space(zero_pad_bytes);
724 for (
size_t i = 0; i < zero_pad_bytes; i++) cur_[i] = 0;
728 void fill_big(
size_t zero_pad_bytes) {
729 memset(make_space(zero_pad_bytes), 0, zero_pad_bytes);
732 void pop(
size_t bytes_to_remove) { cur_ += bytes_to_remove; }
733 void scratch_pop(
size_t bytes_to_remove) { scratch_ -= bytes_to_remove; }
737 swap(allocator_, other.allocator_);
738 swap(own_allocator_, other.own_allocator_);
739 swap(initial_size_, other.initial_size_);
740 swap(buffer_minalign_, other.buffer_minalign_);
741 swap(reserved_, other.reserved_);
742 swap(buf_, other.buf_);
743 swap(cur_, other.cur_);
744 swap(scratch_, other.scratch_);
749 swap(allocator_, other.allocator_);
750 swap(own_allocator_, other.own_allocator_);
760 size_t initial_size_;
761 size_t buffer_minalign_;
767 void reallocate(
size_t len) {
768 auto old_reserved = reserved_;
769 auto old_size = size();
770 auto old_scratch_size = scratch_size();
771 reserved_ += (std::max)(len,
772 old_reserved ? old_reserved / 2 : initial_size_);
773 reserved_ = (reserved_ + buffer_minalign_ - 1) & ~(buffer_minalign_ - 1);
775 buf_ = ReallocateDownward(allocator_, buf_, old_reserved, reserved_,
776 old_size, old_scratch_size);
778 buf_ = Allocate(allocator_, reserved_);
780 cur_ = buf_ + reserved_ - old_size;
781 scratch_ = buf_ + old_scratch_size;
786 inline voffset_t FieldIndexToOffset(voffset_t field_id) {
788 const int fixed_fields = 2;
789 return static_cast<voffset_t
>((field_id + fixed_fields) *
sizeof(voffset_t));
792 template<
typename T,
typename Alloc>
793 const T *data(
const std::vector<T, Alloc> &v) {
794 return v.empty() ? nullptr : &v.front();
796 template<
typename T,
typename Alloc> T *data(std::vector<T, Alloc> &v) {
797 return v.empty() ? nullptr : &v.front();
826 bool own_allocator =
false,
827 size_t buffer_minalign =
828 AlignOf<largest_scalar_t>())
829 : buf_(initial_size, allocator, own_allocator, buffer_minalign),
835 force_defaults_(false),
836 dedup_vtables_(true),
837 string_pool(nullptr) {
843 : buf_(1024, nullptr, false, AlignOf<largest_scalar_t>()),
849 force_defaults_(false),
850 dedup_vtables_(true),
851 string_pool(nullptr) {
868 buf_.swap(other.buf_);
869 swap(num_field_loc, other.num_field_loc);
870 swap(max_voffset_, other.max_voffset_);
871 swap(nested, other.nested);
872 swap(finished, other.finished);
873 swap(minalign_, other.minalign_);
874 swap(force_defaults_, other.force_defaults_);
875 swap(dedup_vtables_, other.dedup_vtables_);
876 swap(string_pool, other.string_pool);
880 if (string_pool)
delete string_pool;
896 if (string_pool) string_pool->clear();
901 uoffset_t
GetSize()
const {
return buf_.size(); }
922 return buf_.release();
929 return buf_.release();
942 return buf_.release_raw(size, offset);
956 void Finished()
const {
962 FLATBUFFERS_ASSERT(finished);
977 void Pad(
size_t num_bytes) { buf_.fill(num_bytes); }
979 void TrackMinAlign(
size_t elem_size) {
980 if (elem_size > minalign_) minalign_ = elem_size;
983 void Align(
size_t elem_size) {
984 TrackMinAlign(elem_size);
985 buf_.fill(PaddingBytes(buf_.size(), elem_size));
988 void PushFlatBuffer(
const uint8_t *bytes,
size_t size) {
989 PushBytes(bytes, size);
993 void PushBytes(
const uint8_t *bytes,
size_t size) { buf_.push(bytes, size); }
995 void PopBytes(
size_t amount) { buf_.pop(amount); }
997 template<
typename T>
void AssertScalarT() {
999 static_assert(flatbuffers::is_scalar<T>::value,
"T must be a scalar type");
1003 template<
typename T> uoffset_t PushElement(T element) {
1005 T litle_endian_element = EndianScalar(element);
1007 buf_.push_small(litle_endian_element);
1011 template<
typename T> uoffset_t PushElement(
Offset<T> off) {
1013 return PushElement(ReferTo(off.o));
1018 void TrackField(voffset_t field, uoffset_t off) {
1020 buf_.scratch_push_small(fl);
1022 max_voffset_ = (std::max)(max_voffset_, field);
1026 template<
typename T>
void AddElement(voffset_t field, T e, T def) {
1028 if (e == def && !force_defaults_)
return;
1029 auto off = PushElement(e);
1030 TrackField(field, off);
1033 template<
typename T>
void AddOffset(voffset_t field,
Offset<T> off) {
1034 if (off.IsNull())
return;
1035 AddElement(field, ReferTo(off.o), static_cast<uoffset_t>(0));
1038 template<
typename T>
void AddStruct(voffset_t field,
const T *structptr) {
1039 if (!structptr)
return;
1040 Align(AlignOf<T>());
1041 buf_.push_small(*structptr);
1042 TrackField(field, GetSize());
1045 void AddStructOffset(voffset_t field, uoffset_t off) {
1046 TrackField(field, off);
1052 uoffset_t ReferTo(uoffset_t off) {
1054 Align(
sizeof(uoffset_t));
1056 FLATBUFFERS_ASSERT(off && off <= GetSize());
1057 return GetSize() - off +
static_cast<uoffset_t
>(
sizeof(uoffset_t));
1069 FLATBUFFERS_ASSERT(!nested);
1071 FLATBUFFERS_ASSERT(!num_field_loc);
1076 uoffset_t StartTable() {
1085 uoffset_t EndTable(uoffset_t start) {
1087 FLATBUFFERS_ASSERT(nested);
1090 auto vtableoffsetloc = PushElement<soffset_t>(0);
1097 (std::max)(static_cast<voffset_t>(max_voffset_ +
sizeof(voffset_t)),
1098 FieldIndexToOffset(0));
1099 buf_.fill_big(max_voffset_);
1100 auto table_object_size = vtableoffsetloc - start;
1102 FLATBUFFERS_ASSERT(table_object_size < 0x10000);
1103 WriteScalar<voffset_t>(buf_.data() +
sizeof(voffset_t),
1104 static_cast<voffset_t>(table_object_size));
1105 WriteScalar<voffset_t>(buf_.data(), max_voffset_);
1107 for (
auto it = buf_.scratch_end() - num_field_loc *
sizeof(
FieldLoc);
1108 it < buf_.scratch_end(); it +=
sizeof(
FieldLoc)) {
1109 auto field_location =
reinterpret_cast<FieldLoc *
>(it);
1110 auto pos =
static_cast<voffset_t
>(vtableoffsetloc - field_location->off);
1113 !ReadScalar<voffset_t>(buf_.data() + field_location->id));
1114 WriteScalar<voffset_t>(buf_.data() + field_location->id, pos);
1117 auto vt1 =
reinterpret_cast<voffset_t *
>(buf_.data());
1118 auto vt1_size = ReadScalar<voffset_t>(vt1);
1119 auto vt_use = GetSize();
1122 if (dedup_vtables_) {
1123 for (
auto it = buf_.scratch_data(); it < buf_.scratch_end();
1124 it +=
sizeof(uoffset_t)) {
1125 auto vt_offset_ptr =
reinterpret_cast<uoffset_t *
>(it);
1126 auto vt2 =
reinterpret_cast<voffset_t *
>(buf_.data_at(*vt_offset_ptr));
1127 auto vt2_size = *vt2;
1128 if (vt1_size != vt2_size || memcmp(vt2, vt1, vt1_size))
continue;
1129 vt_use = *vt_offset_ptr;
1130 buf_.pop(GetSize() - vtableoffsetloc);
1135 if (vt_use == GetSize()) { buf_.scratch_push_small(vt_use); }
1141 WriteScalar(buf_.data_at(vtableoffsetloc),
1142 static_cast<soffset_t
>(vt_use) -
1143 static_cast<soffset_t>(vtableoffsetloc));
1146 return vtableoffsetloc;
1150 uoffset_t EndTable(uoffset_t start, voffset_t ) {
1151 return EndTable(start);
1156 template<
typename T>
void Required(
Offset<T> table, voffset_t field);
1158 uoffset_t StartStruct(
size_t alignment) {
1163 uoffset_t EndStruct() {
return GetSize(); }
1165 void ClearOffsets() {
1166 buf_.scratch_pop(num_field_loc *
sizeof(
FieldLoc));
1173 void PreAlign(
size_t len,
size_t alignment) {
1174 TrackMinAlign(alignment);
1175 buf_.fill(PaddingBytes(GetSize() + len, alignment));
1177 template<
typename T>
void PreAlign(
size_t len) {
1179 PreAlign(len,
sizeof(T));
1189 PreAlign<uoffset_t>(len + 1);
1191 PushBytes(reinterpret_cast<const uint8_t *>(str), len);
1192 PushElement(static_cast<uoffset_t>(len));
1200 return CreateString(str, strlen(str));
1207 return CreateString(str, strlen(str));
1214 return CreateString(str.c_str(), str.length());
1218 #ifdef FLATBUFFERS_HAS_STRING_VIEW 1223 return CreateString(str.data(), str.size());
1225 #endif // FLATBUFFERS_HAS_STRING_VIEW 1232 return str ? CreateString(str->c_str(), str->Length()) : 0;
1240 return CreateString(str.c_str(), str.length());
1252 auto size_before_string = buf_.size();
1255 auto off = CreateString(str, len);
1256 auto it = string_pool->find(off);
1258 if (it != string_pool->end()) {
1260 buf_.pop(buf_.size() - size_before_string);
1264 string_pool->insert(off);
1274 return CreateSharedString(str, strlen(str));
1283 return CreateSharedString(str.c_str(), str.length());
1292 return CreateSharedString(str->c_str(), str->Length());
1296 uoffset_t EndVector(
size_t len) {
1297 FLATBUFFERS_ASSERT(nested);
1299 return PushElement(static_cast<uoffset_t>(len));
1302 void StartVector(
size_t len,
size_t elemsize) {
1305 PreAlign<uoffset_t>(len * elemsize);
1306 PreAlign(len * elemsize, elemsize);
1314 void ForceVectorAlignment(
size_t len,
size_t elemsize,
size_t alignment) {
1315 PreAlign(len * elemsize, alignment);
1319 void ForceStringAlignment(
size_t len,
size_t alignment) {
1320 PreAlign((len + 1) *
sizeof(
char), alignment);
1336 StartVector(len,
sizeof(T));
1338 #if FLATBUFFERS_LITTLEENDIAN 1339 PushBytes(reinterpret_cast<const uint8_t *>(v), len *
sizeof(T));
1341 if (
sizeof(T) == 1) {
1342 PushBytes(reinterpret_cast<const uint8_t *>(v), len);
1344 for (
auto i = len; i > 0; ) {
1345 PushElement(v[--i]);
1353 template<
typename T>
1356 for (
auto i = len; i > 0;) { PushElement(v[--i]); }
1367 return CreateVector(data(v), v.size());
1374 StartVector(v.size(),
sizeof(uint8_t));
1375 for (
auto i = v.size(); i > 0;) {
1376 PushElement(static_cast<uint8_t>(v[--i]));
1382 #ifndef FLATBUFFERS_CPP98_STL 1391 const std::function<T (
size_t i)> &f) {
1392 std::vector<T> elems(vector_size);
1393 for (
size_t i = 0; i < vector_size; i++) elems[i] = f(i);
1394 return CreateVector(elems);
1408 template<
typename T,
typename F,
typename S>
1410 std::vector<T> elems(vector_size);
1411 for (
size_t i = 0; i < vector_size; i++) elems[i] = f(i, state);
1412 return CreateVector(elems);
1422 const std::vector<std::string> &v) {
1423 std::vector<Offset<String>> offsets(v.size());
1424 for (
size_t i = 0; i < v.size(); i++) offsets[i] = CreateString(v[i]);
1425 return CreateVector(offsets);
1435 template<
typename T>
1437 StartVector(len *
sizeof(T) / AlignOf<T>(), AlignOf<T>());
1438 PushBytes(reinterpret_cast<const uint8_t *>(v),
sizeof(T) * len);
1450 template<
typename T,
typename S>
1453 extern T Pack(
const S &);
1454 typedef T (*Pack_t)(
const S &);
1455 std::vector<T> vv(len);
1456 std::transform(v, v + len, vv.begin(), *(Pack_t)&Pack);
1457 return CreateVectorOfStructs<T>(vv.data(), vv.size());
1461 #ifndef FLATBUFFERS_CPP98_STL 1471 size_t vector_size,
const std::function<
void(
size_t i, T *)> &filler) {
1472 T* structs = StartVectorOfStructs<T>(vector_size);
1473 for (
size_t i = 0; i < vector_size; i++) {
1477 return EndVectorOfStructs<T>(vector_size);
1491 template<
typename T,
typename F,
typename S>
1494 T *structs = StartVectorOfStructs<T>(vector_size);
1495 for (
size_t i = 0; i < vector_size; i++) {
1496 f(i, structs, state);
1499 return EndVectorOfStructs<T>(vector_size);
1508 template<
typename T,
typename Alloc>
1510 const std::vector<T, Alloc> &v) {
1511 return CreateVectorOfStructs(data(v), v.size());
1522 template<
typename T,
typename S>
1524 const std::vector<S> &v) {
1525 return CreateVectorOfNativeStructs<T, S>(data(v), v.size());
1529 template<
typename T>
struct StructKeyComparator {
1530 bool operator()(
const T &a,
const T &b)
const {
1531 return a.KeyCompareLessThan(&b);
1535 StructKeyComparator &operator=(
const StructKeyComparator &);
1546 template<
typename T>
1548 return CreateVectorOfSortedStructs(data(*v), v->size());
1559 template<
typename T,
typename S>
1561 std::vector<S> *v) {
1562 return CreateVectorOfSortedNativeStructs<T, S>(data(*v), v->size());
1573 template<
typename T>
1575 std::sort(v, v + len, StructKeyComparator<T>());
1576 return CreateVectorOfStructs(v, len);
1588 template<
typename T,
typename S>
1591 extern T Pack(
const S &);
1592 typedef T (*Pack_t)(
const S &);
1593 std::vector<T> vv(len);
1594 std::transform(v, v + len, vv.begin(), *(Pack_t)&Pack);
1595 return CreateVectorOfSortedStructs<T>(vv, len);
1599 template<
typename T>
struct TableKeyComparator {
1602 auto table_a =
reinterpret_cast<T *
>(buf_.data_at(a.o));
1603 auto table_b =
reinterpret_cast<T *
>(buf_.data_at(b.o));
1604 return table_a->KeyCompareLessThan(table_b);
1609 TableKeyComparator &operator=(
const TableKeyComparator &);
1621 template<
typename T>
1624 std::sort(v, v + len, TableKeyComparator<T>(buf_));
1625 return CreateVector(v, len);
1635 template<
typename T>
1638 return CreateVectorOfSortedTables(data(*v), v->size());
1651 StartVector(len, elemsize);
1652 buf_.make_space(len * elemsize);
1653 auto vec_start = GetSize();
1654 auto vec_end = EndVector(len);
1655 *buf = buf_.data_at(vec_start);
1667 template<
typename T>
1670 return CreateUninitializedVector(len,
sizeof(T),
1671 reinterpret_cast<uint8_t **>(buf));
1674 template<
typename T>
1676 return CreateUninitializedVector(len,
sizeof(T),
1677 reinterpret_cast<uint8_t **>(buf));
1683 Align(AlignOf<T>());
1684 buf_.push_small(structobj);
1689 static const size_t kFileIdentifierLength = 4;
1694 template<
typename T>
1696 Finish(root.o, file_identifier,
false);
1706 template<
typename T>
1708 const char *file_identifier =
nullptr) {
1709 Finish(root.o, file_identifier,
true);
1717 void Finish(uoffset_t root,
const char *file_identifier,
bool size_prefix) {
1719 buf_.clear_scratch();
1721 PreAlign((size_prefix ?
sizeof(uoffset_t) : 0) +
sizeof(uoffset_t) +
1722 (file_identifier ? kFileIdentifierLength : 0),
1724 if (file_identifier) {
1725 FLATBUFFERS_ASSERT(strlen(file_identifier) == kFileIdentifierLength);
1726 PushBytes(reinterpret_cast<const uint8_t *>(file_identifier),
1727 kFileIdentifierLength);
1729 PushElement(ReferTo(root));
1730 if (size_prefix) { PushElement(GetSize()); }
1743 uoffset_t num_field_loc;
1746 voffset_t max_voffset_;
1756 bool force_defaults_;
1758 bool dedup_vtables_;
1763 auto stra =
reinterpret_cast<const String *
>(buf_->data_at(a.o));
1764 auto strb =
reinterpret_cast<const String *
>(buf_->data_at(b.o));
1765 return strncmp(stra->c_str(), strb->c_str(),
1766 (std::min)(stra->size(), strb->size()) + 1) < 0;
1773 StringOffsetMap *string_pool;
1778 template<
typename T> T *StartVectorOfStructs(
size_t vector_size) {
1779 StartVector(vector_size *
sizeof(T) / AlignOf<T>(), AlignOf<T>());
1780 return reinterpret_cast<T *
>(buf_.make_space(vector_size *
sizeof(T)));
1785 template<
typename T>
1794 template<
typename T> T *GetMutableRoot(
void *buf) {
1796 return reinterpret_cast<T *
>(
1797 reinterpret_cast<uint8_t *
>(buf) +
1798 EndianScalar(*reinterpret_cast<uoffset_t *>(buf)));
1801 template<
typename T>
const T *GetRoot(
const void *buf) {
1802 return GetMutableRoot<T>(
const_cast<void *
>(buf));
1805 template<
typename T>
const T *GetSizePrefixedRoot(
const void *buf) {
1806 return GetRoot<T>(
reinterpret_cast<const uint8_t *
>(buf) +
sizeof(uoffset_t));
1812 template<
typename T>
1818 template<
typename T>
1820 return GetMutableTemporaryPointer<T>(fbb, offset);
1830 inline const char *GetBufferIdentifier(
const void *buf,
bool size_prefixed =
false) {
1831 return reinterpret_cast<const char *
>(buf) +
1832 ((size_prefixed) ? 2 *
sizeof(uoffset_t) :
sizeof(uoffset_t));
1836 inline bool BufferHasIdentifier(
const void *buf,
const char *identifier,
bool size_prefixed =
false) {
1837 return strncmp(GetBufferIdentifier(buf, size_prefixed), identifier,
1842 class Verifier FLATBUFFERS_FINAL_CLASS {
1844 Verifier(
const uint8_t *buf,
size_t buf_len, uoffset_t _max_depth = 64,
1845 uoffset_t _max_tables = 1000000)
1849 max_depth_(_max_depth),
1851 max_tables_(_max_tables)
1853 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 1858 assert(size_ < FLATBUFFERS_MAX_BUFFER_SIZE);
1862 bool Check(
bool ok)
const {
1864 #ifdef FLATBUFFERS_DEBUG_VERIFICATION_FAILURE 1865 FLATBUFFERS_ASSERT(ok);
1867 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 1876 bool Verify(
size_t elem,
size_t elem_len)
const {
1878 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 1879 auto upper_bound = elem + elem_len;
1880 if (upper_bound_ < upper_bound)
1881 upper_bound_ = upper_bound;
1884 return Check(elem_len < size_ && elem <= size_ - elem_len);
1887 template<
typename T>
bool VerifyAlignment(
size_t elem)
const {
1888 return (elem & (
sizeof(T) - 1)) == 0;
1892 template<
typename T>
bool Verify(
size_t elem)
const {
1893 return VerifyAlignment<T>(elem) && Verify(elem,
sizeof(T));
1897 bool Verify(
const uint8_t *base, voffset_t elem_off,
size_t elem_len)
const {
1898 return Verify(static_cast<size_t>(base - buf_) + elem_off, elem_len);
1901 template<
typename T>
bool Verify(
const uint8_t *base, voffset_t elem_off)
1903 return Verify(static_cast<size_t>(base - buf_) + elem_off,
sizeof(T));
1907 template<
typename T>
bool VerifyTable(
const T *table) {
1908 return !table || table->Verify(*
this);
1912 template<
typename T>
bool VerifyVector(
const Vector<T> *vec)
const {
1913 return !vec || VerifyVectorOrString(reinterpret_cast<const uint8_t *>(vec),
1919 return VerifyVector(
reinterpret_cast<const Vector<T> *
>(vec));
1923 bool VerifyString(
const String *str)
const {
1926 (VerifyVectorOrString(reinterpret_cast<const uint8_t *>(str),
1929 Check(buf_[end] ==
'\0'));
1933 bool VerifyVectorOrString(
const uint8_t *vec,
size_t elem_size,
1934 size_t *end =
nullptr)
const {
1935 auto veco =
static_cast<size_t>(vec - buf_);
1937 if (!Verify<uoffset_t>(veco))
return false;
1940 auto size = ReadScalar<uoffset_t>(vec);
1941 auto max_elems = FLATBUFFERS_MAX_BUFFER_SIZE / elem_size;
1942 if (!Check(size < max_elems))
1944 auto byte_size =
sizeof(size) + elem_size * size;
1945 if (end) *end = veco + byte_size;
1946 return Verify(veco, byte_size);
1952 for (uoffset_t i = 0; i < vec->size(); i++) {
1953 if (!VerifyString(vec->Get(i)))
return false;
1960 template<
typename T>
bool VerifyVectorOfTables(
const Vector<
Offset<T>> *vec) {
1962 for (uoffset_t i = 0; i < vec->size(); i++) {
1963 if (!vec->Get(i)->Verify(*
this))
return false;
1969 bool VerifyTableStart(
const uint8_t *table) {
1971 auto tableo =
static_cast<size_t>(table - buf_);
1972 if (!Verify<soffset_t>(tableo))
return false;
1975 auto vtableo = tableo -
static_cast<size_t>(ReadScalar<soffset_t>(table));
1977 return VerifyComplexity() && Verify<voffset_t>(vtableo) &&
1978 VerifyAlignment<voffset_t>(ReadScalar<voffset_t>(buf_ + vtableo)) &&
1979 Verify(vtableo, ReadScalar<voffset_t>(buf_ + vtableo));
1982 template<
typename T>
1983 bool VerifyBufferFromStart(
const char *identifier,
size_t start) {
1985 (size_ < 2 *
sizeof(flatbuffers::uoffset_t) ||
1986 !BufferHasIdentifier(buf_ + start, identifier))) {
1991 auto o = VerifyOffset(start);
1992 return o &&
reinterpret_cast<const T *
>(buf_ + start + o)->Verify(*
this)
1994 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 1995 && GetComputedSize()
2002 template<
typename T>
bool VerifyBuffer() {
return VerifyBuffer<T>(
nullptr); }
2004 template<
typename T>
bool VerifyBuffer(
const char *identifier) {
2005 return VerifyBufferFromStart<T>(identifier, 0);
2008 template<
typename T>
bool VerifySizePrefixedBuffer(
const char *identifier) {
2009 return Verify<uoffset_t>(0U) &&
2010 ReadScalar<uoffset_t>(buf_) == size_ -
sizeof(uoffset_t) &&
2011 VerifyBufferFromStart<T>(identifier,
sizeof(uoffset_t));
2014 uoffset_t VerifyOffset(
size_t start)
const {
2015 if (!Verify<uoffset_t>(start))
return 0;
2016 auto o = ReadScalar<uoffset_t>(buf_ + start);
2020 if (!Check(static_cast<soffset_t>(o) >= 0))
return 0;
2023 if (!Verify(start + o, 1))
return 0;
2027 uoffset_t VerifyOffset(
const uint8_t *base, voffset_t start)
const {
2028 return VerifyOffset(static_cast<size_t>(base - buf_) + start);
2035 bool VerifyComplexity() {
2038 return Check(depth_ <= max_depth_ && num_tables_ <= max_tables_);
2048 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 2050 size_t GetComputedSize()
const {
2051 uintptr_t size = upper_bound_;
2053 size = (size - 1 +
sizeof(uoffset_t)) & ~(
sizeof(uoffset_t) - 1);
2054 return (size > size_) ? 0 : size;
2060 const uint8_t *buf_;
2063 uoffset_t max_depth_;
2064 uoffset_t num_tables_;
2065 uoffset_t max_tables_;
2067 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 2068 mutable size_t upper_bound_;
2076 struct BufferRefBase {};
2077 template<
typename T>
struct BufferRef : BufferRefBase {
2078 BufferRef() : buf(
nullptr), len(0), must_free(
false) {}
2079 BufferRef(uint8_t *_buf, uoffset_t _len)
2080 : buf(_buf), len(_len), must_free(
false) {}
2083 if (must_free) free(buf);
2086 const T *GetRoot()
const {
return flatbuffers::GetRoot<T>(buf); }
2089 Verifier verifier(buf, len);
2090 return verifier.VerifyBuffer<T>(
nullptr);
2102 class Struct FLATBUFFERS_FINAL_CLASS {
2104 template<
typename T> T GetField(uoffset_t o)
const {
2105 return ReadScalar<T>(&data_[o]);
2108 template<
typename T> T GetStruct(uoffset_t o)
const {
2109 return reinterpret_cast<T
>(&data_[o]);
2112 const uint8_t *GetAddressOf(uoffset_t o)
const {
return &data_[o]; }
2113 uint8_t *GetAddressOf(uoffset_t o) {
return &data_[o]; }
2123 const uint8_t *GetVTable()
const {
2124 return data_ - ReadScalar<soffset_t>(data_);
2129 voffset_t GetOptionalFieldOffset(voffset_t field)
const {
2131 auto vtable = GetVTable();
2133 auto vtsize = ReadScalar<voffset_t>(vtable);
2136 return field < vtsize ? ReadScalar<voffset_t>(vtable + field) : 0;
2139 template<
typename T> T GetField(voffset_t field, T defaultval)
const {
2140 auto field_offset = GetOptionalFieldOffset(field);
2141 return field_offset ? ReadScalar<T>(data_ + field_offset) : defaultval;
2144 template<
typename P> P GetPointer(voffset_t field) {
2145 auto field_offset = GetOptionalFieldOffset(field);
2146 auto p = data_ + field_offset;
2147 return field_offset ?
reinterpret_cast<P
>(p + ReadScalar<uoffset_t>(p))
2150 template<
typename P> P GetPointer(voffset_t field)
const {
2151 return const_cast<Table *
>(
this)->GetPointer<P>(field);
2154 template<
typename P> P GetStruct(voffset_t field)
const {
2155 auto field_offset = GetOptionalFieldOffset(field);
2156 auto p =
const_cast<uint8_t *
>(data_ + field_offset);
2157 return field_offset ?
reinterpret_cast<P
>(p) :
nullptr;
2160 template<
typename T>
bool SetField(voffset_t field, T val, T def) {
2161 auto field_offset = GetOptionalFieldOffset(field);
2162 if (!field_offset)
return val == def;
2163 WriteScalar(data_ + field_offset, val);
2167 bool SetPointer(voffset_t field,
const uint8_t *val) {
2168 auto field_offset = GetOptionalFieldOffset(field);
2169 if (!field_offset)
return false;
2170 WriteScalar(data_ + field_offset,
2171 static_cast<uoffset_t>(val - (data_ + field_offset)));
2175 uint8_t *GetAddressOf(voffset_t field) {
2176 auto field_offset = GetOptionalFieldOffset(field);
2177 return field_offset ? data_ + field_offset :
nullptr;
2179 const uint8_t *GetAddressOf(voffset_t field)
const {
2180 return const_cast<Table *
>(
this)->GetAddressOf(field);
2183 bool CheckField(voffset_t field)
const {
2184 return GetOptionalFieldOffset(field) != 0;
2189 bool VerifyTableStart(Verifier &verifier)
const {
2190 return verifier.VerifyTableStart(data_);
2194 template<
typename T>
2195 bool VerifyField(
const Verifier &verifier, voffset_t field)
const {
2198 auto field_offset = GetOptionalFieldOffset(field);
2200 return !field_offset || verifier.Verify<T>(data_, field_offset);
2204 template<
typename T>
2205 bool VerifyFieldRequired(
const Verifier &verifier, voffset_t field)
const {
2206 auto field_offset = GetOptionalFieldOffset(field);
2207 return verifier.Check(field_offset != 0) &&
2208 verifier.Verify<T>(data_, field_offset);
2212 bool VerifyOffset(
const Verifier &verifier, voffset_t field)
const {
2213 auto field_offset = GetOptionalFieldOffset(field);
2214 return !field_offset || verifier.VerifyOffset(data_, field_offset);
2217 bool VerifyOffsetRequired(
const Verifier &verifier, voffset_t field)
const {
2218 auto field_offset = GetOptionalFieldOffset(field);
2219 return verifier.Check(field_offset != 0) &&
2220 verifier.VerifyOffset(data_, field_offset);
2227 Table(
const Table &other);
2232 template<
typename T>
void FlatBufferBuilder::Required(
Offset<T> table,
2234 auto table_ptr =
reinterpret_cast<const Table *
>(buf_.data_at(table.o));
2235 bool ok = table_ptr->GetOptionalFieldOffset(field) != 0;
2237 FLATBUFFERS_ASSERT(ok);
2245 inline const uint8_t *GetBufferStartFromRootPointer(
const void *root) {
2246 auto table =
reinterpret_cast<const Table *
>(root);
2247 auto vtable = table->GetVTable();
2249 auto start = (std::min)(vtable, reinterpret_cast<const uint8_t *>(root));
2251 start =
reinterpret_cast<const uint8_t *
>(
reinterpret_cast<uintptr_t
>(start) &
2252 ~(
sizeof(uoffset_t) - 1));
2263 "file_identifier is assumed to be the same size as uoffset_t");
2264 for (
auto possible_roots = FLATBUFFERS_MAX_ALIGNMENT /
sizeof(uoffset_t) + 1;
2265 possible_roots; possible_roots--) {
2266 start -=
sizeof(uoffset_t);
2267 if (ReadScalar<uoffset_t>(start) + start ==
2268 reinterpret_cast<const uint8_t *
>(root))
2275 FLATBUFFERS_ASSERT(
false);
2280 inline uoffset_t GetPrefixedSize(
const uint8_t* buf){
return ReadScalar<uoffset_t>(buf); }
2285 struct NativeTable {};
2295 typedef uint64_t hash_value_t;
2297 #ifdef FLATBUFFERS_CPP98_STL 2298 typedef void (*resolver_function_t)(
void **pointer_adr, hash_value_t hash);
2299 typedef hash_value_t (*rehasher_function_t)(
void *pointer);
2301 typedef std::function<void (void **pointer_adr, hash_value_t hash)>
2302 resolver_function_t;
2303 typedef std::function<hash_value_t (void *pointer)> rehasher_function_t;
2314 template<
typename T>
bool IsFieldPresent(
const T *table, voffset_t field) {
2316 return reinterpret_cast<const Table *
>(table)->CheckField(field);
2322 inline int LookupEnum(
const char **names,
const char *name) {
2323 for (
const char **p = names; *p; p++)
2324 if (!strcmp(*p, name))
return static_cast<int>(p - names);
2339 #if defined(_MSC_VER) 2340 #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \ 2341 __pragma(pack(1)); \ 2342 struct __declspec(align(alignment)) 2343 #define FLATBUFFERS_STRUCT_END(name, size) \ 2345 static_assert(sizeof(name) == size, "compiler breaks packing rules") 2346 #elif defined(__GNUC__) || defined(__clang__) 2347 #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \ 2348 _Pragma("pack(1)") \ 2349 struct __attribute__((aligned(alignment))) 2350 #define FLATBUFFERS_STRUCT_END(name, size) \ 2352 static_assert(sizeof(name) == size, "compiler breaks packing rules") 2354 #error Unknown compiler, please define structure alignment macros 2368 enum SequenceType { ST_TABLE, ST_STRUCT, ST_UNION, ST_ENUM };
2372 #define FLATBUFFERS_GEN_ELEMENTARY_TYPES(ET) \ 2386 ET(ET_SEQUENCE) // See SequenceType. 2388 enum ElementaryType {
2389 #define FLATBUFFERS_ET(E) E, 2390 FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET)
2391 #undef FLATBUFFERS_ET 2394 inline const char *
const *ElementaryTypeNames() {
2395 static const char *
const names[] = {
2396 #define FLATBUFFERS_ET(E) #E, 2397 FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET)
2398 #undef FLATBUFFERS_ET 2406 uint16_t base_type : 4;
2407 uint16_t is_vector : 1;
2408 int16_t sequence_ref : 11;
2411 static_assert(
sizeof(TypeCode) == 2,
"TypeCode");
2416 typedef const TypeTable *(*TypeFunction)();
2421 const TypeCode *type_codes;
2422 const TypeFunction *type_refs;
2423 const int32_t *values;
2424 const char *
const *names;
2438 #if !defined(_WIN32) && !defined(__CYGWIN__) 2440 extern volatile __attribute__((weak))
const char *flatbuffer_version_string;
2441 volatile __attribute__((weak))
const char *flatbuffer_version_string =
2443 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR)
"." 2444 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR)
"." 2445 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
2447 #endif // !defined(_WIN32) && !defined(__CYGWIN__) 2449 #define FLATBUFFERS_DEFINE_BITMASK_OPERATORS(E, T)\ 2450 inline E operator | (E lhs, E rhs){\ 2451 return E(T(lhs) | T(rhs));\ 2453 inline E operator & (E lhs, E rhs){\ 2454 return E(T(lhs) & T(rhs));\ 2456 inline E operator ^ (E lhs, E rhs){\ 2457 return E(T(lhs) ^ T(rhs));\ 2459 inline E operator ~ (E lhs){\ 2462 inline E operator |= (E &lhs, E rhs){\ 2466 inline E operator &= (E &lhs, E rhs){\ 2470 inline E operator ^= (E &lhs, E rhs){\ 2474 inline bool operator !(E rhs) \ 2476 return !bool(T(rhs)); \ 2481 #if defined(_MSC_VER) 2482 #pragma warning(pop) 2486 #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:1390
uoffset_t CreateUninitializedVector(size_t len, size_t elemsize, uint8_t **buf)
Specialized version of CreateVector for non-copying use cases.
Definition: flatbuffers.h:1648
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:1470
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:1574
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:1560
uoffset_t GetSize() const
The current size of the serialized buffer, counting from the end.
Definition: flatbuffers.h:901
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:1636
Definition: flatbuffers.h:91
Helper class to hold data needed in creation of a FlatBuffer.
Definition: flatbuffers.h:811
FlatBufferBuilder & operator=(FlatBufferBuilder &&other)
Move assignment operator for FlatBufferBuilder.
Definition: flatbuffers.h:859
Offset< String > CreateString(char *str)
Store a string in the buffer, which is null-terminated.
Definition: flatbuffers.h:1206
void Clear()
Reset all the state in this FlatBufferBuilder so it can be reused to construct another buffer...
Definition: flatbuffers.h:890
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:1492
Offset< Vector< const T * > > CreateVectorOfNativeStructs(const S *v, size_t len)
Serialize an array of native structs into a FlatBuffer vector.
Definition: flatbuffers.h:1451
Offset< const T * > CreateStruct(const T &structobj)
Write a struct by itself, typically to be part of a union.
Definition: flatbuffers.h:1681
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:1707
Definition: flatbuffers.h:453
Offset< String > CreateSharedString(const char *str)
Store a string in the buffer, which null-terminated.
Definition: flatbuffers.h:1273
Definition: flatbuffers.h:305
FlatBufferBuilder(FlatBufferBuilder &&other)
Move constructor for FlatBufferBuilder.
Definition: flatbuffers.h:842
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:1187
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:970
uint8_t * GetBufferPointer() const
Get the serialized buffer (after you call Finish()).
Definition: flatbuffers.h:906
Definition: flatbuffers.h:415
void DedupVtables(bool dedup)
By default vtables are deduped in order to save space.
Definition: flatbuffers.h:974
static const size_t kFileIdentifierLength
The length of a FlatBuffer file header.
Definition: flatbuffers.h:1689
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:824
Offset< String > CreateString(const String *str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1231
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:1589
Offset< String > CreateSharedString(const String *str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1291
uint8_t * GetCurrentBufferPointer() const
Get a pointer to an unfinished buffer.
Definition: flatbuffers.h:913
Offset< String > CreateString(const T &str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1239
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:1547
Offset< Vector< Offset< String > > > CreateVectorOfStrings(const std::vector< std::string > &v)
Serialize a std::vector<std::string> into a FlatBuffer vector.
Definition: flatbuffers.h:1421
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:1409
size_t GetBufferMinAlignment()
get the minimum alignment this buffer needs to be accessed properly.
Definition: flatbuffers.h:950
Offset< Vector< const T * > > CreateVectorOfStructs(const std::vector< T, Alloc > &v)
Serialize a std::vector of structs into a FlatBuffer vector.
Definition: flatbuffers.h:1509
Offset< Vector< const T * > > CreateVectorOfStructs(const T *v, size_t len)
Serialize an array of structs into a FlatBuffer vector.
Definition: flatbuffers.h:1436
Offset< Vector< const T * > > CreateVectorOfNativeStructs(const std::vector< S > &v)
Serialize a std::vector of native structs into a FlatBuffer vector.
Definition: flatbuffers.h:1523
Offset< String > CreateString(const char *str)
Store a string in the buffer, which is null-terminated.
Definition: flatbuffers.h:1199
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:940
Definition: flatbuffers.h:1734
Offset< String > CreateString(const std::string &str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1213
Offset< Vector< T > > CreateVector(const std::vector< T > &v)
Serialize a std::vector into a FlatBuffer vector.
Definition: flatbuffers.h:1366
Offset< Vector< T > > CreateUninitializedVector(size_t len, T **buf)
Specialized version of CreateVector for non-copying use cases.
Definition: flatbuffers.h:1668
Offset< String > CreateString(flatbuffers::string_view str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1222
Offset< Vector< T > > CreateVector(const T *v, size_t len)
Serialize an array into a FlatBuffer vector.
Definition: flatbuffers.h:1332
Offset< String > CreateSharedString(const std::string &str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1282
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:1622
DetachedBuffer Release()
Get the released DetachedBuffer.
Definition: flatbuffers.h:927
Offset< String > CreateSharedString(const char *str, size_t len)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1249
Definition: flatbuffers.h:182
DetachedBuffer ReleaseBufferPointer()
Get the released pointer to the serialized buffer.
Definition: flatbuffers.h:920
void Finish(Offset< T > root, const char *file_identifier=nullptr)
Finish serializing a buffer by writing the root offset.
Definition: flatbuffers.h:1695
Definition: flatbuffers.h:1760