17 #ifndef FLATBUFFERS_H_ 18 #define FLATBUFFERS_H_ 20 #include "flatbuffers/base.h" 22 #if defined(FLATBUFFERS_NAN_DEFAULTS) 28 template<
typename T>
inline bool IsTheSameAs(T e, T def) {
return e == def; }
30 #if defined(FLATBUFFERS_NAN_DEFAULTS) && \ 31 (!defined(_MSC_VER) || _MSC_VER >= 1800) 33 template<>
inline bool IsTheSameAs<float>(
float e,
float def) {
34 return (e == def) || (std::isnan(def) && std::isnan(e));
36 template<>
inline bool IsTheSameAs<double>(
double e,
double def) {
37 return (e == def) || (std::isnan(def) && std::isnan(e));
55 FLATBUFFERS_LITTLEENDIAN);
59 template<
typename T> FLATBUFFERS_CONSTEXPR
size_t AlignOf() {
65 return __alignof__(T);
84 static const size_t element_stride =
sizeof(T);
85 static return_type
Read(
const uint8_t *p, uoffset_t i) {
86 return EndianScalar((reinterpret_cast<const T *>(p))[i]);
92 static const size_t element_stride =
sizeof(uoffset_t);
93 static return_type
Read(
const uint8_t *p, uoffset_t i) {
94 p += i *
sizeof(uoffset_t);
95 return reinterpret_cast<return_type
>(p + ReadScalar<uoffset_t>(p));
101 static const size_t element_stride =
sizeof(T);
102 static return_type
Read(
const uint8_t *p, uoffset_t i) {
103 return reinterpret_cast<const T *
>(p + i *
sizeof(T));
127 #if !defined(FLATBUFFERS_CPP98_STL) 132 #endif // !defined(FLATBUFFERS_CPP98_STL) 136 return data_ == other.
data_;
140 return data_ < other.
data_;
144 return data_ != other.
data_;
202 public std::reverse_iterator<Iterator> {
206 typename Iterator::value_type
operator*()
const {
return *(iter_ - 1); }
208 typename Iterator::value_type
operator->()
const {
return *(iter_ - 1); }
227 uoffset_t
size()
const {
return EndianScalar(length_); }
231 uoffset_t Length()
const {
return size(); }
236 return_type
Get(uoffset_t i)
const {
246 template<
typename E> E
GetEnum(uoffset_t i)
const {
247 return static_cast<E
>(Get(i));
252 template<
typename U>
const U *
GetAs(uoffset_t i)
const {
253 return reinterpret_cast<const U *
>(Get(i));
259 return reinterpret_cast<const String *
>(Get(i));
263 return reinterpret_cast<const void *
>(Data() +
o);
272 reverse_iterator
rbegin() {
return reverse_iterator(end()); }
273 const_reverse_iterator
rbegin()
const {
return const_reverse_iterator(end()); }
275 reverse_iterator
rend() {
return reverse_iterator(end()); }
276 const_reverse_iterator
rend()
const {
return const_reverse_iterator(end()); }
282 const_reverse_iterator
crbegin()
const {
return rbegin(); }
284 const_reverse_iterator
crend()
const {
return rend(); }
290 WriteScalar(
data() + i, val);
298 static_assert(
sizeof(T) ==
sizeof(uoffset_t),
"Unrelated types");
299 WriteScalar(
data() + i,
300 static_cast<uoffset_t>(val - (Data() + i *
sizeof(uoffset_t))));
311 return reinterpret_cast<const uint8_t *
>(&length_ + 1);
314 uint8_t *
Data() {
return reinterpret_cast<uint8_t *
>(&length_ + 1); }
317 const T *
data()
const {
return reinterpret_cast<const T *
>(Data()); }
318 T *
data() {
return reinterpret_cast<T *
>(Data()); }
321 void *search_result = std::bsearch(
324 if (!search_result) {
328 const uint8_t *element =
reinterpret_cast<const uint8_t *
>(search_result);
345 template<
typename K>
static int KeyCompare(
const void *ap,
const void *bp) {
346 const K *key =
reinterpret_cast<const K *
>(ap);
347 const uint8_t *
data =
reinterpret_cast<const uint8_t *
>(bp);
352 return -table->KeyCompareWithValue(*key);
360 uoffset_t
size()
const {
return EndianScalar(length_); }
363 return reinterpret_cast<const uint8_t *
>(&length_ + 1);
365 uint8_t *
Data() {
return reinterpret_cast<uint8_t *
>(&length_ + 1); }
376 #ifndef FLATBUFFERS_CPP98_STL 377 template<
typename T,
typename U>
379 static_assert(std::is_base_of<T, U>::value,
"Unrelated types");
383 template<
typename T,
typename U>
385 static_assert(std::is_base_of<T, U>::value,
"Unrelated types");
393 return v ? v->
size() : 0;
399 const char *b_data, uoffset_t b_size) {
400 const auto cmp = memcmp(a_data, b_data, (std::min)(a_size, b_size));
401 return cmp == 0 ? a_size < b_size : cmp < 0;
405 const char *
c_str()
const {
return reinterpret_cast<const char *
>(Data()); }
406 std::string
str()
const {
return std::string(c_str(), size()); }
409 #ifdef FLATBUFFERS_HAS_STRING_VIEW 410 flatbuffers::string_view string_view()
const {
411 return flatbuffers::string_view(c_str(), size());
413 #endif // FLATBUFFERS_HAS_STRING_VIEW 424 return str ? str->
str() :
"";
430 return str ? str->
c_str() :
"";
440 virtual uint8_t *allocate(
size_t size) = 0;
443 virtual void deallocate(uint8_t *p,
size_t size) = 0;
451 size_t new_size,
size_t in_use_back,
452 size_t in_use_front) {
454 uint8_t *new_p = allocate(new_size);
455 memcpy_downward(old_p, old_size, new_p, new_size, in_use_back,
457 deallocate(old_p, old_size);
467 uint8_t *new_p,
size_t new_size,
468 size_t in_use_back,
size_t in_use_front) {
469 memcpy(new_p + new_size - in_use_back, old_p + old_size - in_use_back,
471 memcpy(new_p, old_p, in_use_front);
478 uint8_t *
allocate(
size_t size) FLATBUFFERS_OVERRIDE {
479 return new uint8_t[size];
487 delete[]
static_cast<uint8_t *
>(p);
496 return allocator ? allocator->
allocate(size)
501 if (allocator) allocator->
deallocate(p, size);
506 size_t old_size,
size_t new_size,
507 size_t in_use_back,
size_t in_use_front) {
510 in_use_back, in_use_front)
512 in_use_back, in_use_front);
521 : allocator_(nullptr),
522 own_allocator_(false),
529 size_t reserved, uint8_t *cur,
size_t sz)
530 : allocator_(allocator),
531 own_allocator_(own_allocator),
538 #if !defined(FLATBUFFERS_CPP98_STL) 541 : allocator_(other.allocator_),
542 own_allocator_(other.own_allocator_),
544 reserved_(other.reserved_),
550 #endif // !defined(FLATBUFFERS_CPP98_STL) 554 #if !defined(FLATBUFFERS_CPP98_STL) 559 allocator_ = other.allocator_;
560 own_allocator_ = other.own_allocator_;
562 reserved_ = other.reserved_;
571 #endif // !defined(FLATBUFFERS_CPP98_STL) 576 const uint8_t *
data()
const {
return cur_; }
578 uint8_t *
data() {
return cur_; }
580 size_t size()
const {
return size_; }
583 #if 0 // disabled for now due to the ordering of classes in this header 585 bool Verify()
const {
586 Verifier verifier(
data(), size());
587 return verifier.Verify<T>(
nullptr);
591 const T* GetRoot()
const {
592 return flatbuffers::GetRoot<T>(
data());
597 return flatbuffers::GetRoot<T>(
data());
603 #if !defined(FLATBUFFERS_CPP98_STL) 607 FLATBUFFERS_DELETE_FUNC(
610 #endif // !defined(FLATBUFFERS_CPP98_STL) 622 if (buf_)
Deallocate(allocator_, buf_, reserved_);
623 if (own_allocator_ && allocator_) {
delete allocator_; }
628 allocator_ =
nullptr;
629 own_allocator_ =
false;
648 size_t buffer_minalign)
649 : allocator_(allocator),
650 own_allocator_(own_allocator),
651 initial_size_(initial_size),
652 buffer_minalign_(buffer_minalign),
659 #if !defined(FLATBUFFERS_CPP98_STL) 663 #endif // defined(FLATBUFFERS_CPP98_STL) 678 other.
buf_ =
nullptr;
679 other.
cur_ =
nullptr;
684 #if !defined(FLATBUFFERS_CPP98_STL) 693 #endif // defined(FLATBUFFERS_CPP98_STL) 708 cur_ = buf_ + reserved_;
721 if (own_allocator_ && allocator_) {
delete allocator_; }
722 allocator_ =
nullptr;
723 own_allocator_ =
false;
727 if (buf_)
Deallocate(allocator_, buf_, reserved_);
734 allocated_bytes = reserved_;
735 offset =
static_cast<size_t>(cur_ - buf_);
747 DetachedBuffer fb(allocator_, own_allocator_, buf_, reserved_, cur_,
749 if (own_allocator_) {
750 allocator_ =
nullptr;
751 own_allocator_ =
false;
760 if (len > static_cast<size_t>(cur_ - scratch_)) { reallocate(len); }
768 size_t space = ensure_space(len);
777 return static_cast<uoffset_t
>(reserved_ - (cur_ - buf_));
781 return static_cast<uoffset_t
>(scratch_ - buf_);
801 uint8_t *
data_at(
size_t offset)
const {
return buf_ + reserved_ - offset; }
803 void push(
const uint8_t *bytes,
size_t num) {
804 memcpy(make_space(num), bytes, num);
808 template<
typename T>
void push_small(
const T &little_endian_t) {
809 make_space(
sizeof(T));
810 *
reinterpret_cast<T *
>(cur_) = little_endian_t;
814 ensure_space(
sizeof(T));
815 *
reinterpret_cast<T *
>(scratch_) = t;
816 scratch_ +=
sizeof(T);
821 void fill(
size_t zero_pad_bytes) {
822 make_space(zero_pad_bytes);
823 for (
size_t i = 0; i < zero_pad_bytes; i++) cur_[i] = 0;
828 memset(make_space(zero_pad_bytes), 0, zero_pad_bytes);
831 void pop(
size_t bytes_to_remove) { cur_ += bytes_to_remove; }
832 void scratch_pop(
size_t bytes_to_remove) { scratch_ -= bytes_to_remove; }
859 size_t initial_size_;
860 size_t buffer_minalign_;
866 void reallocate(
size_t len) {
867 auto old_reserved = reserved_;
868 auto old_size = size();
869 auto old_scratch_size = scratch_size();
870 reserved_ += (std::max)(len,
871 old_reserved ? old_reserved / 2 : initial_size_);
872 reserved_ = (reserved_ + buffer_minalign_ - 1) & ~(buffer_minalign_ - 1);
875 old_size, old_scratch_size);
877 buf_ =
Allocate(allocator_, reserved_);
879 cur_ = buf_ + reserved_ - old_size;
880 scratch_ = buf_ + old_scratch_size;
887 const int fixed_fields = 2;
888 return static_cast<voffset_t
>((field_id + fixed_fields) *
sizeof(voffset_t));
891 template<
typename T,
typename Alloc>
892 const T *
data(
const std::vector<T, Alloc> &v) {
893 return v.empty() ?
nullptr : &v.front();
895 template<
typename T,
typename Alloc> T *
data(std::vector<T, Alloc> &v) {
896 return v.empty() ?
nullptr : &v.front();
925 bool own_allocator =
false,
926 size_t buffer_minalign =
927 AlignOf<largest_scalar_t>())
928 : buf_(initial_size, allocator, own_allocator, buffer_minalign),
934 force_defaults_(false),
935 dedup_vtables_(true),
936 string_pool(nullptr) {
942 #if !defined(FLATBUFFERS_CPP98_STL) 946 #endif // #if !defined(FLATBUFFERS_CPP98_STL) 947 : buf_(1024,
nullptr,
false, AlignOf<largest_scalar_t>()),
953 force_defaults_(
false),
954 dedup_vtables_(
true),
955 string_pool(
nullptr) {
964 #if !defined(FLATBUFFERS_CPP98_STL) 974 #endif // defined(FLATBUFFERS_CPP98_STL) 979 buf_.swap(other.
buf_);
991 if (string_pool)
delete string_pool;
1007 if (string_pool) string_pool->clear();
1031 ReleaseBufferPointer() {
1033 return buf_.release();
1040 return buf_.release();
1053 return buf_.release_raw(size, offset);
1067 void Finished()
const {
1088 void Pad(
size_t num_bytes) { buf_.fill(num_bytes); }
1090 void TrackMinAlign(
size_t elem_size) {
1091 if (elem_size > minalign_) minalign_ = elem_size;
1094 void Align(
size_t elem_size) {
1095 TrackMinAlign(elem_size);
1096 buf_.fill(PaddingBytes(buf_.size(), elem_size));
1099 void PushFlatBuffer(
const uint8_t *bytes,
size_t size) {
1100 PushBytes(bytes, size);
1104 void PushBytes(
const uint8_t *bytes,
size_t size) { buf_.push(bytes, size); }
1106 void PopBytes(
size_t amount) { buf_.pop(amount); }
1108 template<
typename T>
void AssertScalarT() {
1114 template<
typename T> uoffset_t PushElement(T element) {
1116 T litle_endian_element = EndianScalar(element);
1118 buf_.push_small(litle_endian_element);
1122 template<
typename T> uoffset_t PushElement(
Offset<T> off) {
1124 return PushElement(ReferTo(off.
o));
1129 void TrackField(voffset_t field, uoffset_t off) {
1131 buf_.scratch_push_small(fl);
1133 max_voffset_ = (std::max)(max_voffset_, field);
1137 template<
typename T>
void AddElement(voffset_t field, T e, T def) {
1139 if (
IsTheSameAs(e, def) && !force_defaults_)
return;
1140 auto off = PushElement(e);
1141 TrackField(field, off);
1144 template<
typename T>
void AddOffset(voffset_t field,
Offset<T> off) {
1145 if (off.
IsNull())
return;
1146 AddElement(field, ReferTo(off.
o), static_cast<uoffset_t>(0));
1149 template<
typename T>
void AddStruct(voffset_t field,
const T *structptr) {
1150 if (!structptr)
return;
1151 Align(AlignOf<T>());
1152 buf_.push_small(*structptr);
1153 TrackField(field, GetSize());
1156 void AddStructOffset(voffset_t field, uoffset_t off) {
1157 TrackField(field, off);
1163 uoffset_t ReferTo(uoffset_t off) {
1165 Align(
sizeof(uoffset_t));
1168 return GetSize() - off +
static_cast<uoffset_t
>(
sizeof(uoffset_t));
1187 uoffset_t StartTable() {
1196 uoffset_t EndTable(uoffset_t start) {
1201 auto vtableoffsetloc = PushElement<soffset_t>(0);
1208 (std::max)(static_cast<voffset_t>(max_voffset_ +
sizeof(voffset_t)),
1210 buf_.fill_big(max_voffset_);
1211 auto table_object_size = vtableoffsetloc - start;
1214 WriteScalar<voffset_t>(buf_.data() +
sizeof(voffset_t),
1215 static_cast<voffset_t>(table_object_size));
1216 WriteScalar<voffset_t>(buf_.data(), max_voffset_);
1218 for (
auto it = buf_.scratch_end() - num_field_loc *
sizeof(
FieldLoc);
1219 it < buf_.scratch_end(); it +=
sizeof(
FieldLoc)) {
1220 auto field_location =
reinterpret_cast<FieldLoc *
>(it);
1221 auto pos =
static_cast<voffset_t
>(vtableoffsetloc - field_location->off);
1224 !ReadScalar<voffset_t>(buf_.data() + field_location->id));
1225 WriteScalar<voffset_t>(buf_.data() + field_location->id, pos);
1228 auto vt1 =
reinterpret_cast<voffset_t *
>(buf_.data());
1229 auto vt1_size = ReadScalar<voffset_t>(vt1);
1230 auto vt_use = GetSize();
1233 if (dedup_vtables_) {
1234 for (
auto it = buf_.scratch_data(); it < buf_.scratch_end();
1235 it +=
sizeof(uoffset_t)) {
1236 auto vt_offset_ptr =
reinterpret_cast<uoffset_t *
>(it);
1237 auto vt2 =
reinterpret_cast<voffset_t *
>(buf_.data_at(*vt_offset_ptr));
1238 auto vt2_size = *vt2;
1239 if (vt1_size != vt2_size || 0 != memcmp(vt2, vt1, vt1_size))
continue;
1240 vt_use = *vt_offset_ptr;
1241 buf_.pop(GetSize() - vtableoffsetloc);
1246 if (vt_use == GetSize()) { buf_.scratch_push_small(vt_use); }
1252 WriteScalar(buf_.data_at(vtableoffsetloc),
1253 static_cast<soffset_t
>(vt_use) -
1254 static_cast<soffset_t>(vtableoffsetloc));
1257 return vtableoffsetloc;
1260 FLATBUFFERS_ATTRIBUTE(deprecated(
"call the version above instead"))
1261 uoffset_t EndTable(uoffset_t start, voffset_t ) {
1262 return EndTable(start);
1267 template<
typename T>
void Required(
Offset<T> table, voffset_t field);
1269 uoffset_t StartStruct(
size_t alignment) {
1274 uoffset_t EndStruct() {
return GetSize(); }
1276 void ClearOffsets() {
1277 buf_.scratch_pop(num_field_loc *
sizeof(
FieldLoc));
1284 void PreAlign(
size_t len,
size_t alignment) {
1285 TrackMinAlign(alignment);
1286 buf_.fill(PaddingBytes(GetSize() + len, alignment));
1288 template<
typename T>
void PreAlign(
size_t len) {
1290 PreAlign(len,
sizeof(T));
1300 PreAlign<uoffset_t>(len + 1);
1302 PushBytes(reinterpret_cast<const uint8_t *>(str), len);
1303 PushElement(static_cast<uoffset_t>(len));
1311 return CreateString(str, strlen(str));
1318 return CreateString(str, strlen(str));
1325 return CreateString(str.c_str(), str.length());
1329 #ifdef FLATBUFFERS_HAS_STRING_VIEW 1334 return CreateString(str.data(), str.size());
1336 #endif // FLATBUFFERS_HAS_STRING_VIEW 1343 return str ? CreateString(str->
c_str(), str->
size()) : 0;
1351 return CreateString(str.c_str(), str.length());
1363 auto size_before_string = buf_.size();
1366 auto off = CreateString(str, len);
1367 auto it = string_pool->find(off);
1369 if (it != string_pool->end()) {
1371 buf_.pop(buf_.size() - size_before_string);
1375 string_pool->insert(off);
1385 return CreateSharedString(str, strlen(str));
1394 return CreateSharedString(str.c_str(), str.length());
1403 return CreateSharedString(str->
c_str(), str->
size());
1407 uoffset_t EndVector(
size_t len) {
1410 return PushElement(static_cast<uoffset_t>(len));
1413 void StartVector(
size_t len,
size_t elemsize) {
1416 PreAlign<uoffset_t>(len * elemsize);
1417 PreAlign(len * elemsize, elemsize);
1425 void ForceVectorAlignment(
size_t len,
size_t elemsize,
size_t alignment) {
1426 PreAlign(len * elemsize, alignment);
1430 void ForceStringAlignment(
size_t len,
size_t alignment) {
1431 PreAlign((len + 1) *
sizeof(
char), alignment);
1447 StartVector(len,
sizeof(T));
1449 #if FLATBUFFERS_LITTLEENDIAN 1450 PushBytes(reinterpret_cast<const uint8_t *>(v), len *
sizeof(T));
1452 if (
sizeof(T) == 1) {
1453 PushBytes(reinterpret_cast<const uint8_t *>(v), len);
1455 for (
auto i = len; i > 0; ) {
1456 PushElement(v[--i]);
1464 template<
typename T>
1467 for (
auto i = len; i > 0;) { PushElement(v[--i]); }
1478 return CreateVector(
data(v), v.size());
1485 StartVector(v.size(),
sizeof(uint8_t));
1486 for (
auto i = v.size(); i > 0;) {
1487 PushElement(static_cast<uint8_t>(v[--i]));
1493 #ifndef FLATBUFFERS_CPP98_STL 1502 const std::function<T (
size_t i)> &f) {
1503 std::vector<T> elems(vector_size);
1504 for (
size_t i = 0; i < vector_size; i++) elems[i] = f(i);
1505 return CreateVector(elems);
1519 template<
typename T,
typename F,
typename S>
1521 std::vector<T> elems(vector_size);
1522 for (
size_t i = 0; i < vector_size; i++) elems[i] = f(i, state);
1523 return CreateVector(elems);
1533 const std::vector<std::string> &v) {
1534 std::vector<Offset<String>> offsets(v.size());
1535 for (
size_t i = 0; i < v.size(); i++) offsets[i] = CreateString(v[i]);
1536 return CreateVector(offsets);
1546 template<
typename T>
1548 StartVector(len *
sizeof(T) / AlignOf<T>(), AlignOf<T>());
1549 PushBytes(reinterpret_cast<const uint8_t *>(v),
sizeof(T) * len);
1561 template<
typename T,
typename S>
1564 extern T Pack(
const S &);
1565 typedef T (*Pack_t)(
const S &);
1566 std::vector<T> vv(len);
1567 std::transform(v, v + len, vv.begin(),
static_cast<Pack_t&
>(Pack));
1568 return CreateVectorOfStructs<T>(vv.data(), vv.size());
1572 #ifndef FLATBUFFERS_CPP98_STL 1582 size_t vector_size,
const std::function<
void(
size_t i, T *)> &filler) {
1583 T* structs = StartVectorOfStructs<T>(vector_size);
1584 for (
size_t i = 0; i < vector_size; i++) {
1588 return EndVectorOfStructs<T>(vector_size);
1602 template<
typename T,
typename F,
typename S>
1605 T *structs = StartVectorOfStructs<T>(vector_size);
1606 for (
size_t i = 0; i < vector_size; i++) {
1607 f(i, structs, state);
1610 return EndVectorOfStructs<T>(vector_size);
1619 template<
typename T,
typename Alloc>
1621 const std::vector<T, Alloc> &v) {
1622 return CreateVectorOfStructs(
data(v), v.size());
1633 template<
typename T,
typename S>
1635 const std::vector<S> &v) {
1636 return CreateVectorOfNativeStructs<T, S>(
data(v), v.size());
1640 template<
typename T>
struct StructKeyComparator {
1641 bool operator()(
const T &a,
const T &b)
const {
1642 return a.KeyCompareLessThan(&b);
1646 StructKeyComparator &operator=(
const StructKeyComparator &);
1657 template<
typename T>
1659 return CreateVectorOfSortedStructs(
data(*v), v->size());
1670 template<
typename T,
typename S>
1672 std::vector<S> *v) {
1673 return CreateVectorOfSortedNativeStructs<T, S>(
data(*v), v->size());
1684 template<
typename T>
1686 std::sort(v, v + len, StructKeyComparator<T>());
1687 return CreateVectorOfStructs(v, len);
1699 template<
typename T,
typename S>
1702 extern T Pack(
const S &);
1703 typedef T (*Pack_t)(
const S &);
1704 std::vector<T> vv(len);
1705 std::transform(v, v + len, vv.begin(),
static_cast<Pack_t&
>(Pack));
1706 return CreateVectorOfSortedStructs<T>(vv, len);
1710 template<
typename T>
struct TableKeyComparator {
1713 auto table_a =
reinterpret_cast<T *
>(buf_.data_at(a.
o));
1714 auto table_b =
reinterpret_cast<T *
>(buf_.data_at(b.
o));
1715 return table_a->KeyCompareLessThan(table_b);
1720 TableKeyComparator &operator=(
const TableKeyComparator &);
1732 template<
typename T>
1735 std::sort(v, v + len, TableKeyComparator<T>(buf_));
1736 return CreateVector(v, len);
1746 template<
typename T>
1749 return CreateVectorOfSortedTables(
data(*v), v->size());
1762 StartVector(len, elemsize);
1763 buf_.make_space(len * elemsize);
1764 auto vec_start = GetSize();
1765 auto vec_end = EndVector(len);
1766 *buf = buf_.data_at(vec_start);
1778 template<
typename T>
1781 return CreateUninitializedVector(len,
sizeof(T),
1782 reinterpret_cast<uint8_t **>(buf));
1785 template<
typename T>
1787 return CreateUninitializedVector(len,
sizeof(T),
1788 reinterpret_cast<uint8_t **>(buf));
1795 template<
typename T,
typename U>
1799 StartVector(len,
sizeof(T));
1800 for (
auto i = len; i > 0;) { PushElement(static_cast<T>(v[--i])); }
1807 Align(AlignOf<T>());
1808 buf_.push_small(structobj);
1813 static const size_t kFileIdentifierLength = 4;
1818 template<
typename T>
1820 Finish(root.
o, file_identifier,
false);
1830 template<
typename T>
1832 const char *file_identifier =
nullptr) {
1833 Finish(root.
o, file_identifier,
true);
1837 buf_.swap_allocator(other.
buf_);
1846 void Finish(uoffset_t root,
const char *file_identifier,
bool size_prefix) {
1848 buf_.clear_scratch();
1850 PreAlign((size_prefix ?
sizeof(uoffset_t) : 0) +
sizeof(uoffset_t) +
1851 (file_identifier ? kFileIdentifierLength : 0),
1853 if (file_identifier) {
1855 PushBytes(reinterpret_cast<const uint8_t *>(file_identifier),
1856 kFileIdentifierLength);
1858 PushElement(ReferTo(root));
1859 if (size_prefix) { PushElement(GetSize()); }
1892 auto stra =
reinterpret_cast<const String *
>(buf_->
data_at(a.
o));
1893 auto strb =
reinterpret_cast<const String *
>(buf_->
data_at(b.
o));
1895 strb->data(), strb->size());
1908 StartVector(vector_size *
sizeof(T) / AlignOf<T>(), AlignOf<T>());
1909 return reinterpret_cast<T *
>(buf_.
make_space(vector_size *
sizeof(T)));
1914 template<
typename T>
1923 template<
typename T> T *GetMutableRoot(
void *buf) {
1925 return reinterpret_cast<T *
>(
1926 reinterpret_cast<uint8_t *
>(buf) +
1927 EndianScalar(*reinterpret_cast<uoffset_t *>(buf)));
1930 template<
typename T>
const T *GetRoot(
const void *buf) {
1931 return GetMutableRoot<T>(
const_cast<void *
>(buf));
1934 template<
typename T>
const T *GetSizePrefixedRoot(
const void *buf) {
1935 return GetRoot<T>(
reinterpret_cast<const uint8_t *
>(buf) +
sizeof(uoffset_t));
1941 template<
typename T>
1947 template<
typename T>
1949 return GetMutableTemporaryPointer<T>(fbb, offset);
1959 inline const char *GetBufferIdentifier(
const void *buf,
bool size_prefixed =
false) {
1960 return reinterpret_cast<const char *
>(buf) +
1961 ((size_prefixed) ? 2 *
sizeof(uoffset_t) :
sizeof(uoffset_t));
1965 inline bool BufferHasIdentifier(
const void *buf,
const char *identifier,
bool size_prefixed =
false) {
1966 return strncmp(GetBufferIdentifier(buf, size_prefixed), identifier,
1971 class Verifier FLATBUFFERS_FINAL_CLASS {
1973 Verifier(
const uint8_t *buf,
size_t buf_len, uoffset_t _max_depth = 64,
1974 uoffset_t _max_tables = 1000000,
bool _check_alignment =
true)
1978 max_depth_(_max_depth),
1980 max_tables_(_max_tables)
1982 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 1985 , check_alignment_(_check_alignment)
1992 bool Check(
bool ok)
const {
1994 #ifdef FLATBUFFERS_DEBUG_VERIFICATION_FAILURE 1997 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 2006 bool Verify(
size_t elem,
size_t elem_len)
const {
2008 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 2009 auto upper_bound = elem + elem_len;
2010 if (upper_bound_ < upper_bound)
2011 upper_bound_ = upper_bound;
2014 return Check(elem_len < size_ && elem <= size_ - elem_len);
2017 template<
typename T>
bool VerifyAlignment(
size_t elem)
const {
2018 return (elem & (
sizeof(T) - 1)) == 0 || !check_alignment_;
2022 template<
typename T>
bool Verify(
size_t elem)
const {
2023 return VerifyAlignment<T>(elem) && Verify(elem,
sizeof(T));
2027 bool Verify(
const uint8_t *base, voffset_t elem_off,
size_t elem_len)
const {
2028 return Verify(static_cast<size_t>(base - buf_) + elem_off, elem_len);
2031 template<
typename T>
bool Verify(
const uint8_t *base, voffset_t elem_off)
2033 return Verify(static_cast<size_t>(base - buf_) + elem_off,
sizeof(T));
2037 template<
typename T>
bool VerifyTable(
const T *table) {
2038 return !table || table->Verify(*
this);
2042 template<
typename T>
bool VerifyVector(
const Vector<T> *vec)
const {
2043 return !vec || VerifyVectorOrString(reinterpret_cast<const uint8_t *>(vec),
2049 return VerifyVector(
reinterpret_cast<const Vector<T> *
>(vec));
2053 bool VerifyString(
const String *
str)
const {
2056 (VerifyVectorOrString(reinterpret_cast<const uint8_t *>(str),
2059 Check(buf_[end] ==
'\0'));
2063 bool VerifyVectorOrString(
const uint8_t *vec,
size_t elem_size,
2064 size_t *end =
nullptr)
const {
2065 auto veco =
static_cast<size_t>(vec - buf_);
2067 if (!Verify<uoffset_t>(veco))
return false;
2070 auto size = ReadScalar<uoffset_t>(vec);
2071 auto max_elems = FLATBUFFERS_MAX_BUFFER_SIZE / elem_size;
2072 if (!Check(size < max_elems))
2074 auto byte_size =
sizeof(size) + elem_size * size;
2075 if (end) *end = veco + byte_size;
2076 return Verify(veco, byte_size);
2082 for (uoffset_t i = 0; i < vec->size(); i++) {
2083 if (!VerifyString(vec->Get(i)))
return false;
2090 template<
typename T>
bool VerifyVectorOfTables(
const Vector<
Offset<T>> *vec) {
2092 for (uoffset_t i = 0; i < vec->size(); i++) {
2093 if (!vec->Get(i)->Verify(*
this))
return false;
2099 bool VerifyTableStart(
const uint8_t *table) {
2101 auto tableo =
static_cast<size_t>(table - buf_);
2102 if (!Verify<soffset_t>(tableo))
return false;
2105 auto vtableo = tableo -
static_cast<size_t>(ReadScalar<soffset_t>(table));
2107 return VerifyComplexity() && Verify<voffset_t>(vtableo) &&
2108 VerifyAlignment<voffset_t>(ReadScalar<voffset_t>(buf_ + vtableo)) &&
2109 Verify(vtableo, ReadScalar<voffset_t>(buf_ + vtableo));
2112 template<
typename T>
2113 bool VerifyBufferFromStart(
const char *identifier,
size_t start) {
2115 (size_ < 2 *
sizeof(flatbuffers::uoffset_t) ||
2116 !BufferHasIdentifier(buf_ + start, identifier))) {
2121 auto o = VerifyOffset(start);
2122 return o &&
reinterpret_cast<const T *
>(buf_ + start +
o)->Verify(*
this)
2124 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 2125 && GetComputedSize()
2132 template<
typename T>
bool VerifyBuffer() {
return VerifyBuffer<T>(
nullptr); }
2134 template<
typename T>
bool VerifyBuffer(
const char *identifier) {
2135 return VerifyBufferFromStart<T>(identifier, 0);
2138 template<
typename T>
bool VerifySizePrefixedBuffer(
const char *identifier) {
2139 return Verify<uoffset_t>(0U) &&
2140 ReadScalar<uoffset_t>(buf_) == size_ -
sizeof(uoffset_t) &&
2141 VerifyBufferFromStart<T>(identifier,
sizeof(uoffset_t));
2144 uoffset_t VerifyOffset(
size_t start)
const {
2145 if (!Verify<uoffset_t>(start))
return 0;
2146 auto o = ReadScalar<uoffset_t>(buf_ + start);
2148 if (!Check(
o != 0))
return 0;
2150 if (!Check(static_cast<soffset_t>(
o) >= 0))
return 0;
2153 if (!Verify(start +
o, 1))
return 0;
2157 uoffset_t VerifyOffset(
const uint8_t *base, voffset_t start)
const {
2158 return VerifyOffset(static_cast<size_t>(base - buf_) + start);
2165 bool VerifyComplexity() {
2168 return Check(depth_ <= max_depth_ && num_tables_ <= max_tables_);
2178 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 2180 size_t GetComputedSize()
const {
2181 uintptr_t size = upper_bound_;
2183 size = (size - 1 +
sizeof(uoffset_t)) & ~(
sizeof(uoffset_t) - 1);
2184 return (size > size_) ? 0 : size;
2190 const uint8_t *buf_;
2193 uoffset_t max_depth_;
2194 uoffset_t num_tables_;
2195 uoffset_t max_tables_;
2197 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 2198 mutable size_t upper_bound_;
2201 bool check_alignment_;
2207 struct BufferRefBase {};
2208 template<
typename T>
struct BufferRef : BufferRefBase {
2209 BufferRef() : buf(
nullptr), len(0), must_free(
false) {}
2210 BufferRef(uint8_t *_buf, uoffset_t _len)
2211 : buf(_buf), len(_len), must_free(
false) {}
2214 if (must_free) free(buf);
2217 const T *GetRoot()
const {
return flatbuffers::GetRoot<T>(buf); }
2220 Verifier verifier(buf, len);
2221 return verifier.VerifyBuffer<T>(
nullptr);
2233 class Struct FLATBUFFERS_FINAL_CLASS {
2235 template<
typename T> T GetField(uoffset_t
o)
const {
2236 return ReadScalar<T>(&data_[
o]);
2239 template<
typename T> T GetStruct(uoffset_t o)
const {
2240 return reinterpret_cast<T
>(&data_[
o]);
2243 const uint8_t *GetAddressOf(uoffset_t o)
const {
return &data_[
o]; }
2244 uint8_t *GetAddressOf(uoffset_t o) {
return &data_[
o]; }
2254 const uint8_t *GetVTable()
const {
2255 return data_ - ReadScalar<soffset_t>(data_);
2260 voffset_t GetOptionalFieldOffset(voffset_t field)
const {
2262 auto vtable = GetVTable();
2264 auto vtsize = ReadScalar<voffset_t>(vtable);
2267 return field < vtsize ? ReadScalar<voffset_t>(vtable + field) : 0;
2270 template<
typename T> T GetField(voffset_t field, T defaultval)
const {
2271 auto field_offset = GetOptionalFieldOffset(field);
2272 return field_offset ? ReadScalar<T>(data_ + field_offset) : defaultval;
2275 template<
typename P> P GetPointer(voffset_t field) {
2276 auto field_offset = GetOptionalFieldOffset(field);
2277 auto p = data_ + field_offset;
2278 return field_offset ?
reinterpret_cast<P
>(p + ReadScalar<uoffset_t>(p))
2281 template<
typename P> P GetPointer(voffset_t field)
const {
2282 return const_cast<Table *
>(
this)->GetPointer<P>(field);
2285 template<
typename P> P GetStruct(voffset_t field)
const {
2286 auto field_offset = GetOptionalFieldOffset(field);
2287 auto p =
const_cast<uint8_t *
>(data_ + field_offset);
2288 return field_offset ?
reinterpret_cast<P
>(p) :
nullptr;
2291 template<
typename T>
bool SetField(voffset_t field, T val, T def) {
2292 auto field_offset = GetOptionalFieldOffset(field);
2294 WriteScalar(data_ + field_offset, val);
2298 bool SetPointer(voffset_t field,
const uint8_t *val) {
2299 auto field_offset = GetOptionalFieldOffset(field);
2300 if (!field_offset)
return false;
2301 WriteScalar(data_ + field_offset,
2302 static_cast<uoffset_t>(val - (data_ + field_offset)));
2306 uint8_t *GetAddressOf(voffset_t field) {
2307 auto field_offset = GetOptionalFieldOffset(field);
2308 return field_offset ? data_ + field_offset :
nullptr;
2310 const uint8_t *GetAddressOf(voffset_t field)
const {
2311 return const_cast<Table *
>(
this)->GetAddressOf(field);
2314 bool CheckField(voffset_t field)
const {
2315 return GetOptionalFieldOffset(field) != 0;
2320 bool VerifyTableStart(Verifier &verifier)
const {
2321 return verifier.VerifyTableStart(data_);
2325 template<
typename T>
2326 bool VerifyField(
const Verifier &verifier, voffset_t field)
const {
2329 auto field_offset = GetOptionalFieldOffset(field);
2331 return !field_offset || verifier.Verify<T>(data_, field_offset);
2335 template<
typename T>
2336 bool VerifyFieldRequired(
const Verifier &verifier, voffset_t field)
const {
2337 auto field_offset = GetOptionalFieldOffset(field);
2338 return verifier.Check(field_offset != 0) &&
2339 verifier.Verify<T>(data_, field_offset);
2343 bool VerifyOffset(
const Verifier &verifier, voffset_t field)
const {
2344 auto field_offset = GetOptionalFieldOffset(field);
2345 return !field_offset || verifier.VerifyOffset(data_, field_offset);
2348 bool VerifyOffsetRequired(
const Verifier &verifier, voffset_t field)
const {
2349 auto field_offset = GetOptionalFieldOffset(field);
2350 return verifier.Check(field_offset != 0) &&
2351 verifier.VerifyOffset(data_, field_offset);
2358 Table(
const Table &other);
2363 template<
typename T>
void FlatBufferBuilder::Required(
Offset<T> table,
2365 auto table_ptr =
reinterpret_cast<const Table *
>(buf_.data_at(table.
o));
2366 bool ok = table_ptr->GetOptionalFieldOffset(field) != 0;
2376 inline const uint8_t *GetBufferStartFromRootPointer(
const void *root) {
2377 auto table =
reinterpret_cast<const Table *
>(root);
2378 auto vtable = table->GetVTable();
2380 auto start = (std::min)(vtable, reinterpret_cast<const uint8_t *>(root));
2382 start =
reinterpret_cast<const uint8_t *
>(
reinterpret_cast<uintptr_t
>(start) &
2383 ~(
sizeof(uoffset_t) - 1));
2394 "file_identifier is assumed to be the same size as uoffset_t");
2395 for (
auto possible_roots = FLATBUFFERS_MAX_ALIGNMENT /
sizeof(uoffset_t) + 1;
2396 possible_roots; possible_roots--) {
2397 start -=
sizeof(uoffset_t);
2398 if (ReadScalar<uoffset_t>(start) + start ==
2399 reinterpret_cast<const uint8_t *
>(root))
2411 inline uoffset_t GetPrefixedSize(
const uint8_t* buf){
return ReadScalar<uoffset_t>(buf); }
2416 struct NativeTable {};
2426 typedef uint64_t hash_value_t;
2428 #ifdef FLATBUFFERS_CPP98_STL 2429 typedef void (*resolver_function_t)(
void **pointer_adr, hash_value_t hash);
2430 typedef hash_value_t (*rehasher_function_t)(
void *pointer);
2432 typedef std::function<void (void **pointer_adr, hash_value_t hash)>
2433 resolver_function_t;
2434 typedef std::function<hash_value_t (void *pointer)> rehasher_function_t;
2445 template<
typename T>
2446 bool IsFieldPresent(
const T *table,
typename T::FlatBuffersVTableOffset field) {
2448 return reinterpret_cast<const Table *
>(table)->CheckField(
2449 static_cast<voffset_t>(field));
2455 inline int LookupEnum(
const char **names,
const char *name) {
2456 for (
const char **p = names; *p; p++)
2457 if (!strcmp(*p, name))
return static_cast<int>(p - names);
2472 #if defined(_MSC_VER) 2473 #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \ 2474 __pragma(pack(1)); \ 2475 struct __declspec(align(alignment)) 2476 #define FLATBUFFERS_STRUCT_END(name, size) \ 2478 static_assert(sizeof(name) == size, "compiler breaks packing rules") 2479 #elif defined(__GNUC__) || defined(__clang__) 2480 #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \ 2481 _Pragma("pack(1)") \ 2482 struct __attribute__((aligned(alignment))) 2483 #define FLATBUFFERS_STRUCT_END(name, size) \ 2485 static_assert(sizeof(name) == size, "compiler breaks packing rules") 2487 #error Unknown compiler, please define structure alignment macros 2501 enum SequenceType { ST_TABLE, ST_STRUCT, ST_UNION, ST_ENUM };
2505 #define FLATBUFFERS_GEN_ELEMENTARY_TYPES(ET) \ 2519 ET(ET_SEQUENCE) // See SequenceType. 2521 enum ElementaryType {
2522 #define FLATBUFFERS_ET(E) E, 2523 FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET)
2524 #undef FLATBUFFERS_ET 2527 inline const char *
const *ElementaryTypeNames() {
2528 static const char *
const names[] = {
2529 #define FLATBUFFERS_ET(E) #E, 2530 FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET)
2531 #undef FLATBUFFERS_ET 2539 uint16_t base_type : 4;
2540 uint16_t is_vector : 1;
2541 int16_t sequence_ref : 11;
2544 static_assert(
sizeof(TypeCode) == 2,
"TypeCode");
2549 typedef const TypeTable *(*TypeFunction)();
2554 const TypeCode *type_codes;
2555 const TypeFunction *type_refs;
2556 const int64_t *values;
2557 const char *
const *names;
2571 #if !defined(_WIN32) && !defined(__CYGWIN__) 2573 extern volatile __attribute__((weak))
const char *flatbuffer_version_string;
2574 volatile __attribute__((weak))
const char *flatbuffer_version_string =
2576 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR)
"." 2577 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR)
"." 2578 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
2580 #endif // !defined(_WIN32) && !defined(__CYGWIN__) 2582 #define FLATBUFFERS_DEFINE_BITMASK_OPERATORS(E, T)\ 2583 inline E operator | (E lhs, E rhs){\ 2584 return E(T(lhs) | T(rhs));\ 2586 inline E operator & (E lhs, E rhs){\ 2587 return E(T(lhs) & T(rhs));\ 2589 inline E operator ^ (E lhs, E rhs){\ 2590 return E(T(lhs) ^ T(rhs));\ 2592 inline E operator ~ (E lhs){\ 2595 inline E operator |= (E &lhs, E rhs){\ 2599 inline E operator &= (E &lhs, E rhs){\ 2603 inline E operator ^= (E &lhs, E rhs){\ 2607 inline bool operator !(E rhs) \ 2609 return !bool(T(rhs)); \ 2616 #endif // FLATBUFFERS_H_ FLATBUFFERS_CONSTEXPR size_t AlignOf()
virtual uint8_t * reallocate_downward(uint8_t *old_p, size_t old_size, size_t new_size, size_t in_use_back, size_t in_use_front)
Offset< Vector< const T * > > CreateVectorOfSortedStructs(T *v, size_t len)
Serialize an array of structs into a FlatBuffer vector in sorted order.
const_reverse_iterator rbegin() const
VectorIterator & operator+=(const uoffset_t &offset)
StringOffsetMap * string_pool
void Mutate(uoffset_t i, const T &val)
const U * GetAs(uoffset_t i) const
uint8_t * allocate(size_t size) FLATBUFFERS_OVERRIDE
Allocator * get_custom_allocator()
void memcpy_downward(uint8_t *old_p, size_t old_size, uint8_t *new_p, size_t new_size, size_t in_use_back, size_t in_use_front)
IndirectHelper< T >::return_type return_type
VectorIterator operator++(int)
VectorIterator operator-(const uoffset_t &offset) const
VectorIterator< T, typename IndirectHelper< T >::mutable_return_type > iterator
Offset< Vector< const T * > > CreateVectorOfSortedStructs(std::vector< T > *v)
Serialize a std::vector of structs into a FlatBuffer vector in sorted order.
void fill(size_t zero_pad_bytes)
void ForceDefaults(bool fd)
In order to save space, fields that are set to their default value don't get serialized into the buff...
Offset< const T * > CreateStruct(const T &structobj)
Write a struct by itself, typically to be part of a union.
FLATBUFFERS_ATTRIBUTE(deprecated("use Release() instead")) DetachedBuffer ReleaseBufferPointer()
Get the released pointer to the serialized buffer.
const_reverse_iterator crend() const
bool IsTheSameAs(T e, T def)
vector_downward(vector_downward &&other)
static int KeyCompare(const void *ap, const void *bp)
Offset< Vector< const T * > > CreateVectorOfStructs(size_t vector_size, F f, S *state)
Serialize an array of structs into a FlatBuffer vector.
const_iterator cbegin() const
#define FLATBUFFERS_ASSERT
Offset< Vector< T > > CreateUninitializedVector(size_t len, T **buf)
Specialized version of CreateVector for non-copying use cases. Write the data any time later to the r...
voffset_t FieldIndexToOffset(voffset_t field_id)
VectorIterator & operator=(const VectorIterator &other)
void MutateOffset(uoffset_t i, const uint8_t *val)
void Finish(Offset< T > root, const char *file_identifier=nullptr)
Finish serializing a buffer by writing the root offset.
Offset< Vector< T > > CreateVector(const std::vector< T > &v)
Serialize a std::vector into a FlatBuffer vector.
const uint8_t * Data() const
difference_type operator-(const VectorIterator &other) const
VectorIterator operator+(const uoffset_t &offset) const
static void dealloc(void *p, size_t)
void swap(linb::any &lhs, linb::any &rhs) noexcept
const_iterator begin() const
const char * c_str() const
const uint8_t * Data() const
void destroy(routine_t id)
VectorIterator(const uint8_t *data, uoffset_t i)
Offset< String > CreateString(const char *str, size_t len)
Store a string in the buffer, which can contain any binary data.
return_type LookupByKey(K key) const
vector_downward(size_t initial_size, Allocator *allocator, bool own_allocator, size_t buffer_minalign)
uint8_t * Allocate(Allocator *allocator, size_t size)
uint8_t * scratch_data() const
bool operator<(const VectorIterator &other) const
Offset< String > CreateString(const char *str)
Store a string in the buffer, which is null-terminated.
const uint8_t * data() const
FLATBUFFERS_DELETE_FUNC(DetachedBuffer &operator=(const DetachedBuffer &other)) protected bool own_allocator_
IndirectHelper< T >::mutable_return_type mutable_return_type
return_type operator[](uoffset_t i) const
Offset< Vector< Offset< String > > > CreateVectorOfStrings(const std::vector< std::string > &v)
Serialize a std::vector<std::string> into a FlatBuffer vector. This is a convenience function for a c...
Offset< Vector< const T * > > CreateVectorOfSortedNativeStructs(std::vector< S > *v)
Serialize a std::vector of native structs into a FlatBuffer vector in sorted order.
uoffset_t scratch_size() const
void SwapBufAllocator(FlatBufferBuilder &other)
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.
void swap(vector_downward &other)
static bool StringLessThan(const char *a_data, uoffset_t a_size, const char *b_data, uoffset_t b_size)
T * StartVectorOfStructs(size_t vector_size)
ptrdiff_t difference_type
Vector< Offset< T > > * VectorCast(Vector< Offset< U >> *ptr)
Offset< String > CreateString(const T &str)
Store a string in the buffer, which can contain any binary data.
Helper class to hold data needed in creation of a FlatBuffer. To serialize data, you typically call o...
VectorIterator< T, typename IndirectHelper< T >::return_type > const_iterator
static return_type Read(const uint8_t *p, uoffset_t i)
FLATBUFFERS_ATTRIBUTE(deprecated("use size() instead")) uoffset_t Length() const
uint8_t * GetCurrentBufferPointer() const
Get a pointer to an unfinished buffer.
const_reverse_iterator crbegin() const
uint8_t * make_space(size_t len)
DetachedBuffer(DetachedBuffer &&other)
bool operator==(const VectorIterator &other) const
void Deallocate(Allocator *allocator, uint8_t *p, size_t size)
void push(const uint8_t *bytes, size_t num)
void pop(size_t bytes_to_remove)
VectorIterator & operator--()
void DedupVtables(bool dedup)
By default vtables are deduped in order to save space.
Iterator::value_type operator*() const
E GetEnum(uoffset_t i) const
void push_small(const T &little_endian_t)
vector_downward & operator=(vector_downward &&other)
virtual uint8_t * allocate(size_t size)=0
return_type Get(uoffset_t i) const
bool operator()(const Offset< String > &a, const Offset< String > &b) const
Offset< Vector< const T * > > CreateVectorOfNativeStructs(const S *v, size_t len)
Serialize an array of native structs into a FlatBuffer vector.
static std::string GetString(const String *str)
DetachedBuffer Release()
Get the released DetachedBuffer.
Offset< String > CreateString(char *str)
Store a string in the buffer, which is null-terminated.
FlatBufferBuilder & operator=(FlatBufferBuilder &&other)
Move assignment operator for FlatBufferBuilder.
const_reverse_iterator rend() const
VectorIterator operator--(int)
void deallocate(uint8_t *p, size_t) FLATBUFFERS_OVERRIDE
Offset< Vector< const T * > > CreateVectorOfNativeStructs(const std::vector< S > &v)
Serialize a std::vector of native structs into a FlatBuffer vector.
void Finish(uoffset_t root, const char *file_identifier, bool size_prefix)
const T * data(const std::vector< T, Alloc > &v)
Offset< Vector< uint8_t > > CreateVector(const std::vector< bool > &v)
bool operator<(const String &o) const
void scratch_pop(size_t bytes_to_remove)
void fill_big(size_t zero_pad_bytes)
void scratch_push_small(const T &t)
uint8_t * scratch_end() const
Offset< Vector< T > > CreateVector(size_t vector_size, F f, S *state)
Serialize values returned by a function into a FlatBuffer vector. This is a convenience function that...
Offset< Vector< const T * > > CreateVectorOfSortedNativeStructs(S *v, size_t len)
Serialize an array of native structs into a FlatBuffer vector in sorted order.
Offset< Vector< const T * > > CreateVectorOfStructs(const T *v, size_t len)
Serialize an array of structs into a FlatBuffer vector.
VectorReverseIterator< const_iterator > const_reverse_iterator
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.
Offset< Vector< const T * > > CreateVectorOfStructs(const std::vector< T, Alloc > &v)
Serialize a std::vector of structs into a FlatBuffer vector.
uoffset_t GetSize() const
The current size of the serialized buffer, counting from the end.
uoffset_t CreateUninitializedVector(size_t len, size_t elemsize, uint8_t **buf)
Specialized version of CreateVector for non-copying use cases. Write the data any time later to the r...
Iterator::value_type operator->() const
VectorReverseIterator< iterator > reverse_iterator
Offset< Vector< T > > CreateVector(const T *v, size_t len)
Serialize an array into a FlatBuffer vector.
std::random_access_iterator_tag iterator_category
static size_t VectorLength(const Vector< T > *v)
std::set< Offset< String >, StringOffsetCompare > StringOffsetMap
uint8_t * data_at(size_t offset) const
StringOffsetCompare(const vector_downward &buf)
reverse_iterator rbegin()
Offset< String > CreateSharedString(const char *str, size_t len)
Store a string in the buffer, which can contain any binary data. If a string with this exact contents...
mutable_return_type GetMutableObject(uoffset_t i) const
Offset< Vector< const T * > > EndVectorOfStructs(size_t vector_size)
VectorReverseIterator(Iterator iter)
size_t ensure_space(size_t len)
DetachedBuffer(Allocator *allocator, bool own_allocator, uint8_t *buf, size_t reserved, uint8_t *cur, size_t sz)
Offset< String > CreateSharedString(const char *str)
Store a string in the buffer, which null-terminated. If a string with this exact contents has already...
DetachedBuffer & operator=(DetachedBuffer &&other)
const_iterator end() const
size_t GetBufferMinAlignment()
get the minimum alignment this buffer needs to be accessed properly. This is only known once all elem...
VectorIterator(const VectorIterator &other)
VectorIterator & operator=(VectorIterator &&other)
VectorIterator & operator++()
Offset< String > CreateSharedString(const std::string &str)
Store a string in the buffer, which can contain any binary data. If a string with this exact contents...
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)...
Offset< String > CreateString(const String *str)
Store a string in the buffer, which can contain any binary data.
uint8_t * ReallocateDownward(Allocator *allocator, uint8_t *old_p, size_t old_size, size_t new_size, size_t in_use_back, size_t in_use_front)
uint8_t * ReleaseRaw(size_t &size, size_t &offset)
Get the released pointer to the serialized buffer.
void Clear()
Reset all the state in this FlatBufferBuilder so it can be reused to construct another buffer...
static return_type Read(const uint8_t *p, uoffset_t i)
Offset< Vector< Offset< T > > > CreateVector(const Offset< T > *v, size_t len)
static const size_t kFileIdentifierLength
The length of a FlatBuffer file header.
Offset< String > CreateString(const std::string &str)
Store a string in the buffer, which can contain any binary data.
static const char * GetCstring(const String *str)
static return_type Read(const uint8_t *p, uoffset_t i)
VectorIterator & operator-=(const uoffset_t &offset)
const void * GetStructFromOffset(size_t o) const
Offset< Vector< const T * > > CreateUninitializedVectorOfStructs(size_t len, T **buf)
uint8_t * release_raw(size_t &allocated_bytes, size_t &offset)
void Swap(FlatBufferBuilder &other)
const String * GetAsString(uoffset_t i) const
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.
FlatBufferBuilder(FlatBufferBuilder &&other)
Move constructor for FlatBufferBuilder.
bool operator!=(const VectorIterator &other) const
virtual void deallocate(uint8_t *p, size_t size)=0
Offset< void > Union() const
void swap_allocator(vector_downward &other)
Offset< String > CreateSharedString(const String *str)
Store a string in the buffer, which can contain any binary data. If a string with this exact contents...
const_iterator cend() const
uint8_t * GetBufferPointer() const
Get the serialized buffer (after you call Finish()).
const vector_downward * buf_
Offset< Vector< T > > CreateVectorScalarCast(const U *v, size_t len)