20#include "flatbuffers/base.h"
22#if defined(FLATBUFFERS_NAN_DEFAULTS)
26namespace flatbuffers {
30template<
typename T>
inline bool IsTheSameAs(T e, T def) {
return e == def; }
32#if defined(FLATBUFFERS_NAN_DEFAULTS) && \
33 defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
35template<
typename T>
inline bool IsFloatTheSameAs(T e, T def) {
36 return (e == def) || ((def != def) && (e != e));
38template<>
inline bool IsTheSameAs<float>(
float e,
float def) {
39 return IsFloatTheSameAs(e, def);
41template<>
inline bool IsTheSameAs<double>(
double e,
double def) {
42 return IsFloatTheSameAs(e, def);
50inline bool IsOutRange(
const T &v,
const T &low,
const T &high) {
51 return (v < low) || (high < v);
56inline bool IsInRange(
const T &v,
const T &low,
const T &high) {
57 return !IsOutRange(v, low, high);
65 Offset(uoffset_t _o) : o(_o) {}
67 bool IsNull()
const {
return !o; }
70inline void EndianCheck() {
73 FLATBUFFERS_ASSERT(*
reinterpret_cast<char *
>(&endiantest) ==
74 FLATBUFFERS_LITTLEENDIAN);
78template<
typename T> FLATBUFFERS_CONSTEXPR
size_t AlignOf() {
84 return __alignof__(T);
101 typedef T return_type;
102 typedef T mutable_return_type;
103 static const size_t element_stride =
sizeof(T);
104 static return_type Read(
const uint8_t *p, uoffset_t i) {
105 return EndianScalar((
reinterpret_cast<const T *
>(p))[i]);
109 typedef const T *return_type;
110 typedef T *mutable_return_type;
111 static const size_t element_stride =
sizeof(uoffset_t);
112 static return_type Read(
const uint8_t *p, uoffset_t i) {
113 p += i *
sizeof(uoffset_t);
114 return reinterpret_cast<return_type
>(p + ReadScalar<uoffset_t>(p));
118 typedef const T *return_type;
119 typedef T *mutable_return_type;
120 static const size_t element_stride =
sizeof(T);
121 static return_type Read(
const uint8_t *p, uoffset_t i) {
122 return reinterpret_cast<const T *
>(p + i *
sizeof(T));
129 typedef std::random_access_iterator_tag iterator_category;
130 typedef IT value_type;
131 typedef ptrdiff_t difference_type;
133 typedef IT &reference;
146 #if !defined(FLATBUFFERS_CPP98_STL)
155 return data_ == other.data_;
159 return data_ < other.data_;
163 return data_ != other.data_;
217 const uint8_t *data_;
220template<
typename Iterator>
223 : std::reverse_iterator<Iterator>(iter) {}
225 typename Iterator::value_type operator*()
const {
226 return *(std::reverse_iterator<Iterator>::current);
229 typename Iterator::value_type operator->()
const {
230 return *(std::reverse_iterator<Iterator>::current);
247 uoffset_t size()
const {
return EndianScalar(length_); }
250 FLATBUFFERS_ATTRIBUTE(deprecated(
"use size() instead"))
251 uoffset_t Length()
const {
return size(); }
253 typedef typename IndirectHelper<T>::return_type return_type;
254 typedef typename IndirectHelper<T>::mutable_return_type mutable_return_type;
256 return_type Get(uoffset_t i)
const {
257 FLATBUFFERS_ASSERT(i < size());
261 return_type operator[](uoffset_t i)
const {
return Get(i); }
266 template<
typename E> E GetEnum(uoffset_t i)
const {
267 return static_cast<E
>(Get(i));
272 template<
typename U>
const U *GetAs(uoffset_t i)
const {
273 return reinterpret_cast<const U *
>(Get(i));
278 const String *GetAsString(uoffset_t i)
const {
279 return reinterpret_cast<const String *
>(Get(i));
282 const void *GetStructFromOffset(
size_t o)
const {
283 return reinterpret_cast<const void *
>(Data() + o);
312 void Mutate(uoffset_t i,
const T &val) {
313 FLATBUFFERS_ASSERT(i < size());
314 WriteScalar(data() + i, val);
320 void MutateOffset(uoffset_t i,
const uint8_t *val) {
321 FLATBUFFERS_ASSERT(i < size());
322 static_assert(
sizeof(T) ==
sizeof(uoffset_t),
"Unrelated types");
323 WriteScalar(data() + i,
324 static_cast<uoffset_t
>(val - (Data() + i *
sizeof(uoffset_t))));
328 mutable_return_type GetMutableObject(uoffset_t i)
const {
329 FLATBUFFERS_ASSERT(i < size());
334 const uint8_t *Data()
const {
335 return reinterpret_cast<const uint8_t *
>(&length_ + 1);
338 uint8_t *Data() {
return reinterpret_cast<uint8_t *
>(&length_ + 1); }
341 const T *data()
const {
return reinterpret_cast<const T *
>(Data()); }
342 T *data() {
return reinterpret_cast<T *
>(Data()); }
344 template<
typename K> return_type LookupByKey(K key)
const {
345 void *search_result = std::bsearch(
348 if (!search_result) {
352 const uint8_t *element =
reinterpret_cast<const uint8_t *
>(search_result);
370 template<
typename K>
static int KeyCompare(
const void *ap,
const void *bp) {
371 const K *key =
reinterpret_cast<const K *
>(ap);
372 const uint8_t *data =
reinterpret_cast<const uint8_t *
>(bp);
377 return -table->KeyCompareWithValue(*key);
385 uoffset_t size()
const {
return EndianScalar(length_); }
387 const uint8_t *Data()
const {
388 return reinterpret_cast<const uint8_t *
>(&length_ + 1);
390 uint8_t *Data() {
return reinterpret_cast<uint8_t *
>(&length_ + 1); }
402#ifndef FLATBUFFERS_CPP98_STL
403template<
typename T,
typename U>
405 static_assert(std::is_base_of<T, U>::value,
"Unrelated types");
409template<
typename T,
typename U>
410const Vector<Offset<T>> *VectorCast(
const Vector<Offset<U>> *ptr) {
411 static_assert(std::is_base_of<T, U>::value,
"Unrelated types");
412 return reinterpret_cast<const Vector<Offset<T>
> *>(ptr);
418template<
typename T>
static inline size_t VectorLength(
const Vector<T> *v) {
419 return v ? v->size() : 0;
423template<
typename T, u
int16_t length>
class Array {
425 typename flatbuffers::integral_constant<bool,
426 flatbuffers::is_scalar<T>::value>
429 typename flatbuffers::conditional<scalar_tag::value, T, const T *>::type
433 typedef typename IndirectHelper<IndirectHelperType>::return_type return_type;
437 FLATBUFFERS_CONSTEXPR uint16_t size()
const {
return length; }
439 return_type Get(uoffset_t i)
const {
440 FLATBUFFERS_ASSERT(i < size());
444 return_type operator[](uoffset_t i)
const {
return Get(i); }
449 template<
typename E> E GetEnum(uoffset_t i)
const {
450 return static_cast<E
>(Get(i));
472 typename flatbuffers::conditional<scalar_tag::value, void, T *>::type
473 GetMutablePointer(uoffset_t i)
const {
474 FLATBUFFERS_ASSERT(i < size());
475 return const_cast<T *
>(&data()[i]);
479 void Mutate(uoffset_t i,
const T &val) { MutateImpl(scalar_tag(), i, val); }
482 const uint8_t *Data()
const {
return data_; }
484 uint8_t *Data() {
return data_; }
487 const T *data()
const {
return reinterpret_cast<const T *
>(Data()); }
488 T *data() {
return reinterpret_cast<T *
>(Data()); }
491 void MutateImpl(flatbuffers::integral_constant<bool, true>, uoffset_t i,
493 FLATBUFFERS_ASSERT(i < size());
494 WriteScalar(data() + i, val);
497 void MutateImpl(flatbuffers::integral_constant<bool, false>, uoffset_t i,
499 *(GetMutablePointer(i)) = val;
507#if defined(__cpp_constexpr)
513 uint8_t data_[length *
sizeof(T)];
524template<
typename T, u
int16_t length>
class Array<
Offset<T>, length> {
525 static_assert(flatbuffers::is_same<T, void>::value,
"unexpected type T");
528 typedef const void *return_type;
530 const uint8_t *Data()
const {
return data_; }
533 return_type operator[](uoffset_t)
const {
534 FLATBUFFERS_ASSERT(
false);
549static inline bool StringLessThan(
const char *a_data, uoffset_t a_size,
550 const char *b_data, uoffset_t b_size) {
551 const auto cmp = memcmp(a_data, b_data, (std::min)(a_size, b_size));
552 return cmp == 0 ? a_size < b_size : cmp < 0;
556 const char *c_str()
const {
return reinterpret_cast<const char *
>(Data()); }
557 std::string str()
const {
return std::string(c_str(), size()); }
560 #ifdef FLATBUFFERS_HAS_STRING_VIEW
561 flatbuffers::string_view string_view()
const {
562 return flatbuffers::string_view(c_str(), size());
567 bool operator<(
const String &o)
const {
568 return StringLessThan(this->data(), this->size(), o.data(), o.size());
574static inline std::string GetString(
const String *str) {
575 return str ? str->str() :
"";
580static inline const char *GetCstring(
const String *str) {
581 return str ? str->c_str() :
"";
591 virtual uint8_t *allocate(
size_t size) = 0;
594 virtual void deallocate(uint8_t *p,
size_t size) = 0;
601 virtual uint8_t *reallocate_downward(uint8_t *old_p,
size_t old_size,
602 size_t new_size,
size_t in_use_back,
603 size_t in_use_front) {
604 FLATBUFFERS_ASSERT(new_size > old_size);
605 uint8_t *new_p = allocate(new_size);
606 memcpy_downward(old_p, old_size, new_p, new_size, in_use_back,
608 deallocate(old_p, old_size);
617 void memcpy_downward(uint8_t *old_p,
size_t old_size, uint8_t *new_p,
618 size_t new_size,
size_t in_use_back,
619 size_t in_use_front) {
620 memcpy(new_p + new_size - in_use_back, old_p + old_size - in_use_back,
622 memcpy(new_p, old_p, in_use_front);
629 uint8_t *allocate(
size_t size) FLATBUFFERS_OVERRIDE {
630 return new uint8_t[size];
633 void deallocate(uint8_t *p,
size_t) FLATBUFFERS_OVERRIDE {
delete[] p; }
635 static void dealloc(
void *p,
size_t) {
delete[]
static_cast<uint8_t *
>(p); }
642inline uint8_t *Allocate(
Allocator *allocator,
size_t size) {
643 return allocator ? allocator->allocate(size)
647inline void Deallocate(Allocator *allocator, uint8_t *p,
size_t size) {
649 allocator->deallocate(p, size);
651 DefaultAllocator().deallocate(p, size);
654inline uint8_t *ReallocateDownward(Allocator *allocator, uint8_t *old_p,
655 size_t old_size,
size_t new_size,
656 size_t in_use_back,
size_t in_use_front) {
657 return allocator ? allocator->reallocate_downward(old_p, old_size, new_size,
658 in_use_back, in_use_front)
659 : DefaultAllocator().reallocate_downward(
660 old_p, old_size, new_size, in_use_back, in_use_front);
669 : allocator_(
nullptr),
670 own_allocator_(
false),
677 size_t reserved, uint8_t *cur,
size_t sz)
678 : allocator_(allocator),
679 own_allocator_(own_allocator),
686 #if !defined(FLATBUFFERS_CPP98_STL)
689 : allocator_(other.allocator_),
690 own_allocator_(other.own_allocator_),
692 reserved_(other.reserved_),
702 #if !defined(FLATBUFFERS_CPP98_STL)
705 if (
this == &other)
return *
this;
709 allocator_ = other.allocator_;
710 own_allocator_ = other.own_allocator_;
712 reserved_ = other.reserved_;
726 const uint8_t *data()
const {
return cur_; }
728 uint8_t *data() {
return cur_; }
730 size_t size()
const {
return size_; }
735 bool Verify()
const {
736 Verifier verifier(data(), size());
737 return verifier.Verify<T>(
nullptr);
741 const T* GetRoot()
const {
742 return flatbuffers::GetRoot<T>(data());
747 return flatbuffers::GetRoot<T>(data());
753 #if !defined(FLATBUFFERS_CPP98_STL)
757 FLATBUFFERS_DELETE_FUNC(
771 inline void destroy() {
772 if (buf_) Deallocate(allocator_, buf_, reserved_);
773 if (own_allocator_ && allocator_) {
delete allocator_; }
777 inline void reset() {
778 allocator_ =
nullptr;
779 own_allocator_ =
false;
796 bool own_allocator,
size_t buffer_minalign)
797 : allocator_(allocator),
798 own_allocator_(own_allocator),
799 initial_size_(initial_size),
800 buffer_minalign_(buffer_minalign),
807 #if !defined(FLATBUFFERS_CPP98_STL)
813 : allocator_(other.allocator_),
814 own_allocator_(other.own_allocator_),
815 initial_size_(other.initial_size_),
816 buffer_minalign_(other.buffer_minalign_),
817 reserved_(other.reserved_),
820 scratch_(other.scratch_) {
824 other.own_allocator_ =
false;
826 other.buf_ =
nullptr;
827 other.cur_ =
nullptr;
828 other.scratch_ =
nullptr;
832 #if !defined(FLATBUFFERS_CPP98_STL)
856 cur_ = buf_ + reserved_;
864 void clear_scratch() { scratch_ = buf_; }
866 void clear_allocator() {
867 if (own_allocator_ && allocator_) {
delete allocator_; }
868 allocator_ =
nullptr;
869 own_allocator_ =
false;
872 void clear_buffer() {
873 if (buf_) Deallocate(allocator_, buf_, reserved_);
878 uint8_t *release_raw(
size_t &allocated_bytes,
size_t &offset) {
880 allocated_bytes = reserved_;
881 offset =
static_cast<size_t>(cur_ - buf_);
893 DetachedBuffer fb(allocator_, own_allocator_, buf_, reserved_, cur_,
895 if (own_allocator_) {
896 allocator_ =
nullptr;
897 own_allocator_ =
false;
904 size_t ensure_space(
size_t len) {
905 FLATBUFFERS_ASSERT(cur_ >= scratch_ && scratch_ >= buf_);
906 if (len >
static_cast<size_t>(cur_ - scratch_)) { reallocate(len); }
909 FLATBUFFERS_ASSERT(size() < FLATBUFFERS_MAX_BUFFER_SIZE);
913 inline uint8_t *make_space(
size_t len) {
914 size_t space = ensure_space(len);
920 Allocator *get_custom_allocator() {
return allocator_; }
922 uoffset_t size()
const {
923 return static_cast<uoffset_t
>(reserved_ - (cur_ - buf_));
926 uoffset_t scratch_size()
const {
927 return static_cast<uoffset_t
>(scratch_ - buf_);
930 size_t capacity()
const {
return reserved_; }
932 uint8_t *data()
const {
933 FLATBUFFERS_ASSERT(cur_);
937 uint8_t *scratch_data()
const {
938 FLATBUFFERS_ASSERT(buf_);
942 uint8_t *scratch_end()
const {
943 FLATBUFFERS_ASSERT(scratch_);
947 uint8_t *data_at(
size_t offset)
const {
return buf_ + reserved_ - offset; }
949 void push(
const uint8_t *bytes,
size_t num) {
950 if (num > 0) { memcpy(make_space(num), bytes, num); }
954 template<
typename T>
void push_small(
const T &little_endian_t) {
955 make_space(
sizeof(T));
956 *
reinterpret_cast<T *
>(cur_) = little_endian_t;
959 template<
typename T>
void scratch_push_small(
const T &t) {
960 ensure_space(
sizeof(T));
961 *
reinterpret_cast<T *
>(scratch_) = t;
962 scratch_ +=
sizeof(T);
967 void fill(
size_t zero_pad_bytes) {
968 make_space(zero_pad_bytes);
969 for (
size_t i = 0; i < zero_pad_bytes; i++) cur_[i] = 0;
974 void fill_big(
size_t zero_pad_bytes) {
975 memset(make_space(zero_pad_bytes), 0, zero_pad_bytes);
978 void pop(
size_t bytes_to_remove) { cur_ += bytes_to_remove; }
979 void scratch_pop(
size_t bytes_to_remove) { scratch_ -= bytes_to_remove; }
983 swap(allocator_, other.allocator_);
984 swap(own_allocator_, other.own_allocator_);
985 swap(initial_size_, other.initial_size_);
986 swap(buffer_minalign_, other.buffer_minalign_);
987 swap(reserved_, other.reserved_);
988 swap(buf_, other.buf_);
989 swap(cur_, other.cur_);
990 swap(scratch_, other.scratch_);
995 swap(allocator_, other.allocator_);
996 swap(own_allocator_, other.own_allocator_);
1005 bool own_allocator_;
1006 size_t initial_size_;
1007 size_t buffer_minalign_;
1013 void reallocate(
size_t len) {
1014 auto old_reserved = reserved_;
1015 auto old_size = size();
1016 auto old_scratch_size = scratch_size();
1018 (std::max)(len, old_reserved ? old_reserved / 2 : initial_size_);
1019 reserved_ = (reserved_ + buffer_minalign_ - 1) & ~(buffer_minalign_ - 1);
1021 buf_ = ReallocateDownward(allocator_, buf_, old_reserved, reserved_,
1022 old_size, old_scratch_size);
1024 buf_ = Allocate(allocator_, reserved_);
1026 cur_ = buf_ + reserved_ - old_size;
1027 scratch_ = buf_ + old_scratch_size;
1032inline voffset_t FieldIndexToOffset(voffset_t field_id) {
1034 const int fixed_fields = 2;
1035 return static_cast<voffset_t
>((field_id + fixed_fields) *
sizeof(voffset_t));
1038template<
typename T,
typename Alloc>
1039const T *data(
const std::vector<T, Alloc> &v) {
1043 return v.empty() ?
reinterpret_cast<const T *
>(&t) : &v.front();
1045template<
typename T,
typename Alloc> T *data(std::vector<T, Alloc> &v) {
1049 return v.empty() ?
reinterpret_cast<T *
>(&t) : &v.front();
1077 size_t initial_size = 1024,
Allocator *allocator =
nullptr,
1078 bool own_allocator =
false,
1079 size_t buffer_minalign = AlignOf<largest_scalar_t>())
1080 : buf_(initial_size, allocator, own_allocator, buffer_minalign),
1086 force_defaults_(false),
1087 dedup_vtables_(true),
1088 string_pool(nullptr) {
1094 #if !defined(FLATBUFFERS_CPP98_STL)
1099 : buf_(1024,
nullptr,
false, AlignOf<largest_scalar_t>()),
1105 force_defaults_(
false),
1106 dedup_vtables_(
true),
1107 string_pool(
nullptr) {
1116 #if !defined(FLATBUFFERS_CPP98_STL)
1131 buf_.swap(other.buf_);
1132 swap(num_field_loc, other.num_field_loc);
1133 swap(max_voffset_, other.max_voffset_);
1134 swap(nested, other.nested);
1135 swap(finished, other.finished);
1136 swap(minalign_, other.minalign_);
1137 swap(force_defaults_, other.force_defaults_);
1138 swap(dedup_vtables_, other.dedup_vtables_);
1139 swap(string_pool, other.string_pool);
1142 ~FlatBufferBuilder() {
1143 if (string_pool)
delete string_pool;
1159 if (string_pool) string_pool->clear();
1185 return buf_.release();
1192 return buf_.release();
1206 return buf_.release_raw(size, offset);
1220 void Finished()
const {
1226 FLATBUFFERS_ASSERT(finished);
1242 void Pad(
size_t num_bytes) { buf_.fill(num_bytes); }
1244 void TrackMinAlign(
size_t elem_size) {
1245 if (elem_size > minalign_) minalign_ = elem_size;
1248 void Align(
size_t elem_size) {
1249 TrackMinAlign(elem_size);
1250 buf_.fill(PaddingBytes(buf_.size(), elem_size));
1253 void PushFlatBuffer(
const uint8_t *bytes,
size_t size) {
1254 PushBytes(bytes, size);
1258 void PushBytes(
const uint8_t *bytes,
size_t size) { buf_.push(bytes, size); }
1260 void PopBytes(
size_t amount) { buf_.pop(amount); }
1262 template<
typename T>
void AssertScalarT() {
1264 static_assert(flatbuffers::is_scalar<T>::value,
"T must be a scalar type");
1268 template<
typename T> uoffset_t PushElement(T element) {
1270 T litle_endian_element = EndianScalar(element);
1272 buf_.push_small(litle_endian_element);
1276 template<
typename T> uoffset_t PushElement(Offset<T> off) {
1278 return PushElement(ReferTo(off.o));
1283 void TrackField(voffset_t field, uoffset_t off) {
1284 FieldLoc fl = { off, field };
1285 buf_.scratch_push_small(fl);
1287 max_voffset_ = (std::max)(max_voffset_, field);
1291 template<
typename T>
void AddElement(voffset_t field, T e, T def) {
1293 if (IsTheSameAs(e, def) && !force_defaults_)
return;
1294 auto off = PushElement(e);
1295 TrackField(field, off);
1298 template<
typename T>
void AddOffset(voffset_t field, Offset<T> off) {
1299 if (off.IsNull())
return;
1300 AddElement(field, ReferTo(off.o),
static_cast<uoffset_t
>(0));
1303 template<
typename T>
void AddStruct(voffset_t field,
const T *structptr) {
1304 if (!structptr)
return;
1305 Align(AlignOf<T>());
1306 buf_.push_small(*structptr);
1310 void AddStructOffset(voffset_t field, uoffset_t off) {
1311 TrackField(field, off);
1317 uoffset_t ReferTo(uoffset_t off) {
1319 Align(
sizeof(uoffset_t));
1321 FLATBUFFERS_ASSERT(off && off <=
GetSize());
1322 return GetSize() - off +
static_cast<uoffset_t
>(
sizeof(uoffset_t));
1334 FLATBUFFERS_ASSERT(!nested);
1336 FLATBUFFERS_ASSERT(!num_field_loc);
1341 uoffset_t StartTable() {
1350 uoffset_t EndTable(uoffset_t start) {
1352 FLATBUFFERS_ASSERT(nested);
1355 auto vtableoffsetloc = PushElement<soffset_t>(0);
1362 (std::max)(
static_cast<voffset_t
>(max_voffset_ +
sizeof(voffset_t)),
1363 FieldIndexToOffset(0));
1364 buf_.fill_big(max_voffset_);
1365 auto table_object_size = vtableoffsetloc - start;
1367 FLATBUFFERS_ASSERT(table_object_size < 0x10000);
1368 WriteScalar<voffset_t>(buf_.data() +
sizeof(voffset_t),
1369 static_cast<voffset_t
>(table_object_size));
1370 WriteScalar<voffset_t>(buf_.data(), max_voffset_);
1372 for (
auto it = buf_.scratch_end() - num_field_loc *
sizeof(FieldLoc);
1373 it < buf_.scratch_end(); it +=
sizeof(FieldLoc)) {
1374 auto field_location =
reinterpret_cast<FieldLoc *
>(it);
1375 auto pos =
static_cast<voffset_t
>(vtableoffsetloc - field_location->off);
1378 !ReadScalar<voffset_t>(buf_.data() + field_location->id));
1379 WriteScalar<voffset_t>(buf_.data() + field_location->id, pos);
1382 auto vt1 =
reinterpret_cast<voffset_t *
>(buf_.data());
1383 auto vt1_size = ReadScalar<voffset_t>(vt1);
1387 if (dedup_vtables_) {
1388 for (
auto it = buf_.scratch_data(); it < buf_.scratch_end();
1389 it +=
sizeof(uoffset_t)) {
1390 auto vt_offset_ptr =
reinterpret_cast<uoffset_t *
>(it);
1391 auto vt2 =
reinterpret_cast<voffset_t *
>(buf_.data_at(*vt_offset_ptr));
1392 auto vt2_size = ReadScalar<voffset_t>(vt2);
1393 if (vt1_size != vt2_size || 0 != memcmp(vt2, vt1, vt1_size))
continue;
1394 vt_use = *vt_offset_ptr;
1395 buf_.pop(
GetSize() - vtableoffsetloc);
1400 if (vt_use ==
GetSize()) { buf_.scratch_push_small(vt_use); }
1406 WriteScalar(buf_.data_at(vtableoffsetloc),
1407 static_cast<soffset_t
>(vt_use) -
1408 static_cast<soffset_t
>(vtableoffsetloc));
1411 return vtableoffsetloc;
1415 uoffset_t EndTable(uoffset_t start, voffset_t ) {
1416 return EndTable(start);
1421 template<
typename T>
void Required(Offset<T> table, voffset_t field);
1423 uoffset_t StartStruct(
size_t alignment) {
1428 uoffset_t EndStruct() {
return GetSize(); }
1430 void ClearOffsets() {
1431 buf_.scratch_pop(num_field_loc *
sizeof(FieldLoc));
1438 void PreAlign(
size_t len,
size_t alignment) {
1439 TrackMinAlign(alignment);
1440 buf_.fill(PaddingBytes(
GetSize() + len, alignment));
1442 template<
typename T>
void PreAlign(
size_t len) {
1444 PreAlign(len,
sizeof(T));
1454 PreAlign<uoffset_t>(len + 1);
1456 PushBytes(
reinterpret_cast<const uint8_t *
>(str), len);
1457 PushElement(
static_cast<uoffset_t
>(len));
1483 #ifdef FLATBUFFERS_HAS_STRING_VIEW
1497 return str ?
CreateString(str->c_str(), str->size()) : 0;
1517 auto size_before_string = buf_.size();
1521 auto it = string_pool->find(off);
1523 if (it != string_pool->end()) {
1525 buf_.pop(buf_.size() - size_before_string);
1529 string_pool->insert(off);
1561 uoffset_t EndVector(
size_t len) {
1562 FLATBUFFERS_ASSERT(nested);
1564 return PushElement(
static_cast<uoffset_t
>(len));
1567 void StartVector(
size_t len,
size_t elemsize) {
1570 PreAlign<uoffset_t>(len * elemsize);
1571 PreAlign(len * elemsize, elemsize);
1579 void ForceVectorAlignment(
size_t len,
size_t elemsize,
size_t alignment) {
1580 PreAlign(len * elemsize, alignment);
1584 void ForceStringAlignment(
size_t len,
size_t alignment) {
1585 PreAlign((len + 1) *
sizeof(
char), alignment);
1601 StartVector(len,
sizeof(T));
1603 #if FLATBUFFERS_LITTLEENDIAN
1604 PushBytes(
reinterpret_cast<const uint8_t *
>(v), len *
sizeof(T));
1606 if (
sizeof(T) == 1) {
1607 PushBytes(
reinterpret_cast<const uint8_t *
>(v), len);
1609 for (
auto i = len; i > 0; ) {
1610 PushElement(v[--i]);
1618 template<
typename T>
1621 for (
auto i = len; i > 0;) { PushElement(v[--i]); }
1622 return Offset<Vector<Offset<T>>>(EndVector(len));
1639 StartVector(v.size(),
sizeof(uint8_t));
1640 for (
auto i = v.size(); i > 0;) {
1641 PushElement(
static_cast<uint8_t
>(v[--i]));
1643 return Offset<Vector<uint8_t>>(EndVector(v.size()));
1647 #ifndef FLATBUFFERS_CPP98_STL
1656 const std::function<T (
size_t i)> &f) {
1657 std::vector<T> elems(vector_size);
1658 for (
size_t i = 0; i < vector_size; i++) elems[i] = f(i);
1673 template<
typename T,
typename F,
typename S>
1675 std::vector<T> elems(vector_size);
1676 for (
size_t i = 0; i < vector_size; i++) elems[i] = f(i, state);
1687 const std::vector<std::string> &v) {
1688 std::vector<Offset<String>> offsets(v.size());
1689 for (
size_t i = 0; i < v.size(); i++) offsets[i] =
CreateString(v[i]);
1700 template<
typename T>
1702 StartVector(len *
sizeof(T) / AlignOf<T>(), AlignOf<T>());
1703 PushBytes(
reinterpret_cast<const uint8_t *
>(v),
sizeof(T) * len);
1715 template<
typename T,
typename S>
1718 extern T Pack(
const S &);
1719 std::vector<T> vv(len);
1720 std::transform(v, v + len, vv.begin(), Pack);
1721 return CreateVectorOfStructs<T>(data(vv), vv.size());
1725 #ifndef FLATBUFFERS_CPP98_STL
1735 size_t vector_size,
const std::function<
void(
size_t i, T *)> &filler) {
1736 T* structs = StartVectorOfStructs<T>(vector_size);
1737 for (
size_t i = 0; i < vector_size; i++) {
1741 return EndVectorOfStructs<T>(vector_size);
1755 template<
typename T,
typename F,
typename S>
1758 T *structs = StartVectorOfStructs<T>(vector_size);
1759 for (
size_t i = 0; i < vector_size; i++) {
1760 f(i, structs, state);
1763 return EndVectorOfStructs<T>(vector_size);
1772 template<
typename T,
typename Alloc>
1774 const std::vector<T, Alloc> &v) {
1786 template<
typename T,
typename S>
1788 const std::vector<S> &v) {
1789 return CreateVectorOfNativeStructs<T, S>(data(v), v.size());
1793 template<
typename T>
struct StructKeyComparator {
1794 bool operator()(
const T &a,
const T &b)
const {
1795 return a.KeyCompareLessThan(&b);
1799 StructKeyComparator &
operator=(
const StructKeyComparator &);
1810 template<
typename T>
1823 template<
typename T,
typename S>
1825 std::vector<S> *v) {
1826 return CreateVectorOfSortedNativeStructs<T, S>(data(*v), v->size());
1837 template<
typename T>
1839 std::sort(v, v + len, StructKeyComparator<T>());
1852 template<
typename T,
typename S>
1855 extern T Pack(
const S &);
1856 typedef T (*Pack_t)(
const S &);
1857 std::vector<T> vv(len);
1858 std::transform(v, v + len, vv.begin(),
static_cast<Pack_t &
>(Pack));
1859 return CreateVectorOfSortedStructs<T>(vv, len);
1863 template<
typename T>
struct TableKeyComparator {
1865 TableKeyComparator(
const TableKeyComparator &other) : buf_(other.buf_) {}
1866 bool operator()(
const Offset<T> &a,
const Offset<T> &b)
const {
1867 auto table_a =
reinterpret_cast<T *
>(buf_.data_at(a.o));
1868 auto table_b =
reinterpret_cast<T *
>(buf_.data_at(b.o));
1869 return table_a->KeyCompareLessThan(table_b);
1871 vector_downward &buf_;
1874 TableKeyComparator &
operator=(
const TableKeyComparator &other) {
1889 template<
typename T>
1892 std::sort(v, v + len, TableKeyComparator<T>(buf_));
1903 template<
typename T>
1919 StartVector(len, elemsize);
1920 buf_.make_space(len * elemsize);
1922 auto vec_end = EndVector(len);
1923 *buf = buf_.data_at(vec_start);
1935 template<
typename T>
1939 reinterpret_cast<uint8_t **
>(buf));
1942 template<
typename T>
1946 reinterpret_cast<uint8_t **
>(buf));
1952 template<
typename T,
typename U>
1953 Offset<Vector<T>> CreateVectorScalarCast(
const U *v,
size_t len) {
1956 StartVector(len,
sizeof(T));
1957 for (
auto i = len; i > 0;) { PushElement(
static_cast<T
>(v[--i])); }
1958 return Offset<Vector<T>>(EndVector(len));
1964 Align(AlignOf<T>());
1965 buf_.push_small(structobj);
1975 template<
typename T>
1977 Finish(root.o, file_identifier,
false);
1987 template<
typename T>
1989 const char *file_identifier =
nullptr) {
1990 Finish(root.o, file_identifier,
true);
1994 buf_.swap_allocator(other.buf_);
2002 void Finish(uoffset_t root,
const char *file_identifier,
bool size_prefix) {
2004 buf_.clear_scratch();
2006 PreAlign((size_prefix ?
sizeof(uoffset_t) : 0) +
sizeof(uoffset_t) +
2009 if (file_identifier) {
2011 PushBytes(
reinterpret_cast<const uint8_t *
>(file_identifier),
2014 PushElement(ReferTo(root));
2015 if (size_prefix) { PushElement(
GetSize()); }
2028 uoffset_t num_field_loc;
2031 voffset_t max_voffset_;
2041 bool force_defaults_;
2043 bool dedup_vtables_;
2048 auto stra =
reinterpret_cast<const String *
>(buf_->data_at(a.o));
2049 auto strb =
reinterpret_cast<const String *
>(buf_->data_at(b.o));
2050 return StringLessThan(stra->data(), stra->size(), strb->data(),
2058 StringOffsetMap *string_pool;
2063 template<
typename T> T *StartVectorOfStructs(
size_t vector_size) {
2064 StartVector(vector_size *
sizeof(T) / AlignOf<T>(), AlignOf<T>());
2065 return reinterpret_cast<T *
>(buf_.make_space(vector_size *
sizeof(T)));
2070 template<
typename T>
2079template<
typename T> T *GetMutableRoot(
void *buf) {
2081 return reinterpret_cast<T *
>(
2082 reinterpret_cast<uint8_t *
>(buf) +
2083 EndianScalar(*
reinterpret_cast<uoffset_t *
>(buf)));
2086template<
typename T>
const T *GetRoot(
const void *buf) {
2087 return GetMutableRoot<T>(
const_cast<void *
>(buf));
2090template<
typename T>
const T *GetSizePrefixedRoot(
const void *buf) {
2091 return GetRoot<T>(
reinterpret_cast<const uint8_t *
>(buf) +
sizeof(uoffset_t));
2098T *GetMutableTemporaryPointer(FlatBufferBuilder &fbb, Offset<T> offset) {
2099 return reinterpret_cast<T *
>(fbb.GetCurrentBufferPointer() + fbb.GetSize() -
2104const T *GetTemporaryPointer(FlatBufferBuilder &fbb, Offset<T> offset) {
2105 return GetMutableTemporaryPointer<T>(fbb, offset);
2115inline const char *GetBufferIdentifier(
const void *buf,
2116 bool size_prefixed =
false) {
2117 return reinterpret_cast<const char *
>(buf) +
2118 ((size_prefixed) ? 2 *
sizeof(uoffset_t) :
sizeof(uoffset_t));
2122inline bool BufferHasIdentifier(
const void *buf,
const char *identifier,
2123 bool size_prefixed =
false) {
2124 return strncmp(GetBufferIdentifier(buf, size_prefixed), identifier,
2129class Verifier FLATBUFFERS_FINAL_CLASS {
2131 Verifier(
const uint8_t *buf,
size_t buf_len, uoffset_t _max_depth = 64,
2132 uoffset_t _max_tables = 1000000,
bool _check_alignment =
true)
2136 max_depth_(_max_depth),
2138 max_tables_(_max_tables),
2140 check_alignment_(_check_alignment) {
2141 FLATBUFFERS_ASSERT(size_ < FLATBUFFERS_MAX_BUFFER_SIZE);
2145 bool Check(
bool ok)
const {
2147 #ifdef FLATBUFFERS_DEBUG_VERIFICATION_FAILURE
2148 FLATBUFFERS_ASSERT(ok);
2150 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
2159 bool Verify(
size_t elem,
size_t elem_len)
const {
2161 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
2162 auto upper_bound = elem + elem_len;
2163 if (upper_bound_ < upper_bound)
2164 upper_bound_ = upper_bound;
2167 return Check(elem_len < size_ && elem <= size_ - elem_len);
2170 template<
typename T>
bool VerifyAlignment(
size_t elem)
const {
2171 return Check((elem & (
sizeof(T) - 1)) == 0 || !check_alignment_);
2175 template<
typename T>
bool Verify(
size_t elem)
const {
2176 return VerifyAlignment<T>(elem) && Verify(elem,
sizeof(T));
2179 bool VerifyFromPointer(
const uint8_t *p,
size_t len) {
2180 auto o =
static_cast<size_t>(p - buf_);
2181 return Verify(o, len);
2185 bool Verify(
const uint8_t *base, voffset_t elem_off,
size_t elem_len)
const {
2186 return Verify(
static_cast<size_t>(base - buf_) + elem_off, elem_len);
2189 template<
typename T>
2190 bool Verify(
const uint8_t *base, voffset_t elem_off)
const {
2191 return Verify(
static_cast<size_t>(base - buf_) + elem_off,
sizeof(T));
2195 template<
typename T>
bool VerifyTable(
const T *table) {
2196 return !table || table->Verify(*
this);
2200 template<
typename T>
bool VerifyVector(
const Vector<T> *vec)
const {
2201 return !vec || VerifyVectorOrString(
reinterpret_cast<const uint8_t *
>(vec),
2206 template<
typename T>
bool VerifyVector(
const Vector<const T *> *vec)
const {
2207 return VerifyVector(
reinterpret_cast<const Vector<T> *
>(vec));
2211 bool VerifyString(
const String *str)
const {
2213 return !str || (VerifyVectorOrString(
reinterpret_cast<const uint8_t *
>(str),
2216 Check(buf_[end] ==
'\0'));
2220 bool VerifyVectorOrString(
const uint8_t *vec,
size_t elem_size,
2221 size_t *end =
nullptr)
const {
2222 auto veco =
static_cast<size_t>(vec - buf_);
2224 if (!Verify<uoffset_t>(veco))
return false;
2227 auto size = ReadScalar<uoffset_t>(vec);
2228 auto max_elems = FLATBUFFERS_MAX_BUFFER_SIZE / elem_size;
2229 if (!Check(size < max_elems))
2231 auto byte_size =
sizeof(size) + elem_size * size;
2232 if (end) *end = veco + byte_size;
2233 return Verify(veco, byte_size);
2237 bool VerifyVectorOfStrings(
const Vector<Offset<String>> *vec)
const {
2239 for (uoffset_t i = 0; i < vec->size(); i++) {
2240 if (!VerifyString(vec->Get(i)))
return false;
2247 template<
typename T>
bool VerifyVectorOfTables(
const Vector<Offset<T>> *vec) {
2249 for (uoffset_t i = 0; i < vec->size(); i++) {
2250 if (!vec->Get(i)->Verify(*
this))
return false;
2256 __supress_ubsan__(
"unsigned-integer-overflow") bool VerifyTableStart(
2257 const uint8_t *table) {
2259 auto tableo =
static_cast<size_t>(table - buf_);
2260 if (!Verify<soffset_t>(tableo))
return false;
2263 auto vtableo = tableo -
static_cast<size_t>(ReadScalar<soffset_t>(table));
2265 return VerifyComplexity() && Verify<voffset_t>(vtableo) &&
2266 VerifyAlignment<voffset_t>(ReadScalar<voffset_t>(buf_ + vtableo)) &&
2267 Verify(vtableo, ReadScalar<voffset_t>(buf_ + vtableo));
2270 template<
typename T>
2271 bool VerifyBufferFromStart(
const char *identifier,
size_t start) {
2272 if (identifier && (size_ < 2 *
sizeof(flatbuffers::uoffset_t) ||
2273 !BufferHasIdentifier(buf_ + start, identifier))) {
2278 auto o = VerifyOffset(start);
2279 return o &&
reinterpret_cast<const T *
>(buf_ + start + o)->Verify(*
this)
2281 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
2282 && GetComputedSize()
2289 template<
typename T>
bool VerifyBuffer() {
return VerifyBuffer<T>(
nullptr); }
2291 template<
typename T>
bool VerifyBuffer(
const char *identifier) {
2292 return VerifyBufferFromStart<T>(identifier, 0);
2295 template<
typename T>
bool VerifySizePrefixedBuffer(
const char *identifier) {
2296 return Verify<uoffset_t>(0U) &&
2297 ReadScalar<uoffset_t>(buf_) == size_ -
sizeof(uoffset_t) &&
2298 VerifyBufferFromStart<T>(identifier,
sizeof(uoffset_t));
2301 uoffset_t VerifyOffset(
size_t start)
const {
2302 if (!Verify<uoffset_t>(start))
return 0;
2303 auto o = ReadScalar<uoffset_t>(buf_ + start);
2305 if (!Check(o != 0))
return 0;
2307 if (!Check(
static_cast<soffset_t
>(o) >= 0))
return 0;
2310 if (!Verify(start + o, 1))
return 0;
2314 uoffset_t VerifyOffset(
const uint8_t *base, voffset_t start)
const {
2315 return VerifyOffset(
static_cast<size_t>(base - buf_) + start);
2322 bool VerifyComplexity() {
2325 return Check(depth_ <= max_depth_ && num_tables_ <= max_tables_);
2335 size_t GetComputedSize()
const {
2337 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
2338 uintptr_t size = upper_bound_;
2340 size = (size - 1 +
sizeof(uoffset_t)) & ~(
sizeof(uoffset_t) - 1);
2341 return (size > size_) ? 0 : size;
2345 FLATBUFFERS_ASSERT(
false);
2352 const uint8_t *buf_;
2355 uoffset_t max_depth_;
2356 uoffset_t num_tables_;
2357 uoffset_t max_tables_;
2358 mutable size_t upper_bound_;
2359 bool check_alignment_;
2365struct BufferRefBase {};
2366template<
typename T>
struct BufferRef : BufferRefBase {
2367 BufferRef() : buf(nullptr), len(0), must_free(false) {}
2368 BufferRef(uint8_t *_buf, uoffset_t _len)
2369 : buf(_buf), len(_len), must_free(false) {}
2372 if (must_free) free(buf);
2375 const T *GetRoot()
const {
return flatbuffers::GetRoot<T>(buf); }
2378 Verifier verifier(buf, len);
2379 return verifier.VerifyBuffer<T>(
nullptr);
2391class Struct FLATBUFFERS_FINAL_CLASS {
2393 template<
typename T> T GetField(uoffset_t o)
const {
2394 return ReadScalar<T>(&data_[o]);
2397 template<
typename T> T GetStruct(uoffset_t o)
const {
2398 return reinterpret_cast<T
>(&data_[o]);
2401 const uint8_t *GetAddressOf(uoffset_t o)
const {
return &data_[o]; }
2402 uint8_t *GetAddressOf(uoffset_t o) {
return &data_[o]; }
2408 Struct(
const Struct &);
2409 Struct &operator=(
const Struct &);
2418 const uint8_t *GetVTable()
const {
2419 return data_ - ReadScalar<soffset_t>(data_);
2424 voffset_t GetOptionalFieldOffset(voffset_t field)
const {
2426 auto vtable = GetVTable();
2428 auto vtsize = ReadScalar<voffset_t>(vtable);
2431 return field < vtsize ? ReadScalar<voffset_t>(vtable + field) : 0;
2434 template<
typename T> T GetField(voffset_t field, T defaultval)
const {
2435 auto field_offset = GetOptionalFieldOffset(field);
2436 return field_offset ? ReadScalar<T>(data_ + field_offset) : defaultval;
2439 template<
typename P> P GetPointer(voffset_t field) {
2440 auto field_offset = GetOptionalFieldOffset(field);
2441 auto p = data_ + field_offset;
2442 return field_offset ?
reinterpret_cast<P
>(p + ReadScalar<uoffset_t>(p))
2445 template<
typename P> P GetPointer(voffset_t field)
const {
2446 return const_cast<Table *
>(
this)->GetPointer<P>(field);
2449 template<
typename P> P GetStruct(voffset_t field)
const {
2450 auto field_offset = GetOptionalFieldOffset(field);
2451 auto p =
const_cast<uint8_t *
>(data_ + field_offset);
2452 return field_offset ?
reinterpret_cast<P
>(p) :
nullptr;
2455 template<
typename T>
bool SetField(voffset_t field, T val, T def) {
2456 auto field_offset = GetOptionalFieldOffset(field);
2457 if (!field_offset)
return IsTheSameAs(val, def);
2458 WriteScalar(data_ + field_offset, val);
2462 bool SetPointer(voffset_t field,
const uint8_t *val) {
2463 auto field_offset = GetOptionalFieldOffset(field);
2464 if (!field_offset)
return false;
2465 WriteScalar(data_ + field_offset,
2466 static_cast<uoffset_t
>(val - (data_ + field_offset)));
2470 uint8_t *GetAddressOf(voffset_t field) {
2471 auto field_offset = GetOptionalFieldOffset(field);
2472 return field_offset ? data_ + field_offset :
nullptr;
2474 const uint8_t *GetAddressOf(voffset_t field)
const {
2475 return const_cast<Table *
>(
this)->GetAddressOf(field);
2478 bool CheckField(voffset_t field)
const {
2479 return GetOptionalFieldOffset(field) != 0;
2484 bool VerifyTableStart(Verifier &verifier)
const {
2485 return verifier.VerifyTableStart(data_);
2489 template<
typename T>
2490 bool VerifyField(
const Verifier &verifier, voffset_t field)
const {
2493 auto field_offset = GetOptionalFieldOffset(field);
2495 return !field_offset || verifier.Verify<T>(data_, field_offset);
2499 template<
typename T>
2500 bool VerifyFieldRequired(
const Verifier &verifier, voffset_t field)
const {
2501 auto field_offset = GetOptionalFieldOffset(field);
2502 return verifier.Check(field_offset != 0) &&
2503 verifier.Verify<T>(data_, field_offset);
2507 bool VerifyOffset(
const Verifier &verifier, voffset_t field)
const {
2508 auto field_offset = GetOptionalFieldOffset(field);
2509 return !field_offset || verifier.VerifyOffset(data_, field_offset);
2512 bool VerifyOffsetRequired(
const Verifier &verifier, voffset_t field)
const {
2513 auto field_offset = GetOptionalFieldOffset(field);
2514 return verifier.Check(field_offset != 0) &&
2515 verifier.VerifyOffset(data_, field_offset);
2522 Table(
const Table &other);
2523 Table &operator=(
const Table &);
2529void FlatBufferBuilder::Required(Offset<T> table, voffset_t field) {
2530 auto table_ptr =
reinterpret_cast<const Table *
>(buf_.data_at(table.o));
2531 bool ok = table_ptr->GetOptionalFieldOffset(field) != 0;
2533 FLATBUFFERS_ASSERT(ok);
2541inline const uint8_t *GetBufferStartFromRootPointer(
const void *root) {
2542 auto table =
reinterpret_cast<const Table *
>(root);
2543 auto vtable = table->GetVTable();
2545 auto start = (std::min)(vtable,
reinterpret_cast<const uint8_t *
>(root));
2547 start =
reinterpret_cast<const uint8_t *
>(
reinterpret_cast<uintptr_t
>(start) &
2548 ~(
sizeof(uoffset_t) - 1));
2559 "file_identifier is assumed to be the same size as uoffset_t");
2560 for (
auto possible_roots = FLATBUFFERS_MAX_ALIGNMENT /
sizeof(uoffset_t) + 1;
2561 possible_roots; possible_roots--) {
2562 start -=
sizeof(uoffset_t);
2563 if (ReadScalar<uoffset_t>(start) + start ==
2564 reinterpret_cast<const uint8_t *
>(root))
2571 FLATBUFFERS_ASSERT(
false);
2576inline uoffset_t GetPrefixedSize(
const uint8_t *buf) {
2577 return ReadScalar<uoffset_t>(buf);
2583struct NativeTable {};
2593typedef uint64_t hash_value_t;
2595#ifdef FLATBUFFERS_CPP98_STL
2596 typedef void (*resolver_function_t)(
void **pointer_adr, hash_value_t hash);
2597 typedef hash_value_t (*rehasher_function_t)(
void *pointer);
2599 typedef std::function<void (
void **pointer_adr, hash_value_t hash)>
2600 resolver_function_t;
2601 typedef std::function<hash_value_t (
void *pointer)> rehasher_function_t;
2613bool IsFieldPresent(
const T *table,
typename T::FlatBuffersVTableOffset field) {
2615 return reinterpret_cast<const Table *
>(table)->CheckField(
2616 static_cast<voffset_t
>(field));
2622inline int LookupEnum(
const char **names,
const char *name) {
2623 for (
const char **p = names; *p; p++)
2624 if (!strcmp(*p, name))
return static_cast<int>(p - names);
2639#if defined(_MSC_VER)
2640 #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \
2642 struct __declspec(align(alignment))
2643 #define FLATBUFFERS_STRUCT_END(name, size) \
2645 static_assert(sizeof(name) == size, "compiler breaks packing rules")
2646#elif defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
2647 #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \
2648 _Pragma("pack(1)") \
2649 struct __attribute__((aligned(alignment)))
2650 #define FLATBUFFERS_STRUCT_END(name, size) \
2652 static_assert(sizeof(name) == size, "compiler breaks packing rules")
2654 #error Unknown compiler, please define structure alignment macros
2668enum SequenceType { ST_TABLE, ST_STRUCT, ST_UNION, ST_ENUM };
2672#define FLATBUFFERS_GEN_ELEMENTARY_TYPES(ET) \
2688enum ElementaryType {
2689 #define FLATBUFFERS_ET(E) E,
2690 FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET)
2691 #undef FLATBUFFERS_ET
2694inline const char *
const *ElementaryTypeNames() {
2695 static const char *
const names[] = {
2696 #define FLATBUFFERS_ET(E) #E,
2697 FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET)
2698 #undef FLATBUFFERS_ET
2706 uint16_t base_type : 4;
2707 uint16_t is_vector : 1;
2708 int16_t sequence_ref : 11;
2711static_assert(
sizeof(TypeCode) == 2,
"TypeCode");
2716typedef const TypeTable *(*TypeFunction)();
2721 const TypeCode *type_codes;
2722 const TypeFunction *type_refs;
2723 const int64_t *values;
2724 const char *
const *names;
2738#if !defined(_WIN32) && !defined(__CYGWIN__)
2740extern volatile __attribute__((weak))
const char *flatbuffer_version_string;
2741volatile __attribute__((weak))
const char *flatbuffer_version_string =
2743 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR)
"."
2744 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR)
"."
2745 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
2749#define FLATBUFFERS_DEFINE_BITMASK_OPERATORS(E, T)\
2750 inline E operator | (E lhs, E rhs){\
2751 return E(T(lhs) | T(rhs));\
2753 inline E operator & (E lhs, E rhs){\
2754 return E(T(lhs) & T(rhs));\
2756 inline E operator ^ (E lhs, E rhs){\
2757 return E(T(lhs) ^ T(rhs));\
2759 inline E operator ~ (E lhs){\
2762 inline E operator |= (E &lhs, E rhs){\
2766 inline E operator &= (E &lhs, E rhs){\
2770 inline E operator ^= (E &lhs, E rhs){\
2774 inline bool operator !(E rhs) \
2776 return !bool(T(rhs)); \
Definition: flatbuffers.h:586
Definition: flatbuffers.h:423
Definition: flatbuffers.h:627
Definition: flatbuffers.h:666
Helper class to hold data needed in creation of a FlatBuffer.
Definition: flatbuffers.h:1063
Offset< Vector< const T * > > CreateVectorOfNativeStructs(const std::vector< S > &v)
Serialize a std::vector of native structs into a FlatBuffer vector.
Definition: flatbuffers.h:1787
Offset< Vector< T > > CreateVector(const std::vector< T > &v)
Serialize a std::vector into a FlatBuffer vector.
Definition: flatbuffers.h:1631
void Finish(Offset< T > root, const char *file_identifier=nullptr)
Finish serializing a buffer by writing the root offset.
Definition: flatbuffers.h:1976
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:1824
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:1811
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:1235
Offset< String > CreateString(char *str)
Store a string in the buffer, which is null-terminated.
Definition: flatbuffers.h:1471
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:1734
Offset< Vector< T > > CreateUninitializedVector(size_t len, T **buf)
Specialized version of CreateVector for non-copying use cases.
Definition: flatbuffers.h:1936
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:1674
Offset< String > CreateString(const std::string &str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1478
Offset< String > CreateString(const char *str, size_t len)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1452
Offset< String > CreateString(flatbuffers::string_view str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1487
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:1904
DetachedBuffer Release()
Get the released DetachedBuffer.
Definition: flatbuffers.h:1190
FLATBUFFERS_ATTRIBUTE(deprecated("use Release() instead")) DetachedBuffer ReleaseBufferPointer()
Get the released pointer to the serialized buffer.
Definition: flatbuffers.h:1182
Offset< Vector< const T * > > CreateVectorOfStructs(const T *v, size_t len)
Serialize an array of structs into a FlatBuffer vector.
Definition: flatbuffers.h:1701
uoffset_t GetSize() const
The current size of the serialized buffer, counting from the end.
Definition: flatbuffers.h:1164
uint8_t * ReleaseRaw(size_t &size, size_t &offset)
Get the released pointer to the serialized buffer.
Definition: flatbuffers.h:1204
Offset< Vector< const T * > > CreateVectorOfStructs(const std::vector< T, Alloc > &v)
Serialize a std::vector of structs into a FlatBuffer vector.
Definition: flatbuffers.h:1773
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:1853
uint8_t * GetBufferPointer() const
Get the serialized buffer (after you call Finish()).
Definition: flatbuffers.h:1169
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:1988
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:1756
Offset< String > CreateString(const String *str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1496
Offset< String > CreateSharedString(const std::string &str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1547
Offset< String > CreateSharedString(const char *str)
Store a string in the buffer, which null-terminated.
Definition: flatbuffers.h:1538
size_t GetBufferMinAlignment()
get the minimum alignment this buffer needs to be accessed properly.
Definition: flatbuffers.h:1214
FlatBufferBuilder & operator=(FlatBufferBuilder &&other)
Move assignment operator for FlatBufferBuilder.
Definition: flatbuffers.h:1119
void DedupVtables(bool dedup)
By default vtables are deduped in order to save space.
Definition: flatbuffers.h:1239
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:1076
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:1838
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:1655
Offset< Vector< Offset< String > > > CreateVectorOfStrings(const std::vector< std::string > &v)
Serialize a std::vector<std::string> into a FlatBuffer vector.
Definition: flatbuffers.h:1686
Offset< Vector< T > > CreateVector(const T *v, size_t len)
Serialize an array into a FlatBuffer vector.
Definition: flatbuffers.h:1597
Offset< String > CreateString(const T &str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1504
uoffset_t CreateUninitializedVector(size_t len, size_t elemsize, uint8_t **buf)
Specialized version of CreateVector for non-copying use cases.
Definition: flatbuffers.h:1916
uint8_t * GetCurrentBufferPointer() const
Get a pointer to an unfinished buffer.
Definition: flatbuffers.h:1176
Offset< String > CreateSharedString(const String *str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1556
Offset< String > CreateSharedString(const char *str, size_t len)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1514
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:1890
FlatBufferBuilder(FlatBufferBuilder &&other) FlatBufferBuilder(FlatBufferBuilder &other)
Move constructor for FlatBufferBuilder.
Definition: flatbuffers.h:1095
void Clear()
Reset all the state in this FlatBufferBuilder so it can be reused to construct another buffer.
Definition: flatbuffers.h:1153
Offset< Vector< const T * > > CreateVectorOfNativeStructs(const S *v, size_t len)
Serialize an array of native structs into a FlatBuffer vector.
Definition: flatbuffers.h:1716
static const size_t kFileIdentifierLength
The length of a FlatBuffer file header.
Definition: flatbuffers.h:1970
Offset< const T * > CreateStruct(const T &structobj)
Write a struct by itself, typically to be part of a union.
Definition: flatbuffers.h:1962
Offset< String > CreateString(const char *str)
Store a string in the buffer, which is null-terminated.
Definition: flatbuffers.h:1464
Definition: flatbuffers.h:238
Definition: flatbuffers.h:383
Definition: flatbuffers.h:793
Definition: flatbuffers.h:2019
Definition: flatbuffers.h:2045
Definition: flatbuffers.h:100
Definition: flatbuffers.h:62
Definition: flatbuffers.h:555
Definition: flatbuffers.h:128
Definition: flatbuffers.h:221