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));
46 Offset(uoffset_t _o) : o(_o) {}
48 bool IsNull()
const {
return !o; }
51 inline void EndianCheck() {
54 FLATBUFFERS_ASSERT(*reinterpret_cast<char *>(&endiantest) ==
55 FLATBUFFERS_LITTLEENDIAN);
59 template<
typename T> FLATBUFFERS_CONSTEXPR
size_t AlignOf() {
65 return __alignof__(T);
82 typedef T return_type;
83 typedef T mutable_return_type;
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]);
90 typedef const T *return_type;
91 typedef T *mutable_return_type;
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));
99 typedef const T *return_type;
100 typedef T *mutable_return_type;
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));
110 typedef std::random_access_iterator_tag iterator_category;
111 typedef IT value_type;
112 typedef ptrdiff_t difference_type;
114 typedef IT &reference;
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_;
198 const uint8_t *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_); }
230 FLATBUFFERS_ATTRIBUTE(deprecated(
"use size() instead"))
231 uoffset_t Length()
const {
return size(); }
233 typedef typename IndirectHelper<T>::return_type return_type;
234 typedef typename IndirectHelper<T>::mutable_return_type mutable_return_type;
236 return_type Get(uoffset_t i)
const {
237 FLATBUFFERS_ASSERT(i < size());
241 return_type operator[](uoffset_t i)
const {
return Get(i); }
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));
258 const String *GetAsString(uoffset_t i)
const {
259 return reinterpret_cast<const String *
>(Get(i));
262 const void *GetStructFromOffset(
size_t o)
const {
263 return reinterpret_cast<const void *
>(Data() + o);
266 iterator begin() {
return iterator(Data(), 0); }
267 const_iterator begin()
const {
return const_iterator(Data(), 0); }
269 iterator end() {
return iterator(Data(), size()); }
270 const_iterator end()
const {
return const_iterator(Data(), size()); }
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()); }
278 const_iterator cbegin()
const {
return begin(); }
280 const_iterator cend()
const {
return end(); }
282 const_reverse_iterator crbegin()
const {
return rbegin(); }
284 const_reverse_iterator crend()
const {
return rend(); }
288 void Mutate(uoffset_t i,
const T &val) {
289 FLATBUFFERS_ASSERT(i < size());
290 WriteScalar(data() + i, val);
296 void MutateOffset(uoffset_t i,
const uint8_t *val) {
297 FLATBUFFERS_ASSERT(i < size());
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))));
304 mutable_return_type GetMutableObject(uoffset_t i)
const {
305 FLATBUFFERS_ASSERT(i < size());
310 const uint8_t *Data()
const {
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()); }
320 template<
typename K> return_type LookupByKey(K key)
const {
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_); }
362 const uint8_t *Data()
const {
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");
392 template<
typename T>
static inline size_t VectorLength(
const Vector<T> *v) {
393 return v ? v->size() : 0;
398 static inline bool StringLessThan(
const char *a_data, uoffset_t a_size,
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 416 bool operator<(
const String &o)
const {
417 return StringLessThan(this->data(), this->size(), o.data(), o.size());
423 static inline std::string GetString(
const String * str) {
424 return str ? str->str() :
"";
429 static inline const char * GetCstring(
const String * 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;
450 virtual uint8_t *reallocate_downward(uint8_t *old_p,
size_t old_size,
451 size_t new_size,
size_t in_use_back,
452 size_t in_use_front) {
453 FLATBUFFERS_ASSERT(new_size > old_size);
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);
466 void memcpy_downward(uint8_t *old_p,
size_t 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];
482 void deallocate(uint8_t *p,
size_t) FLATBUFFERS_OVERRIDE {
486 static void dealloc(
void *p,
size_t) {
487 delete[]
static_cast<uint8_t *
>(p);
495 inline uint8_t *Allocate(
Allocator *allocator,
size_t size) {
496 return allocator ? allocator->allocate(size)
500 inline void Deallocate(
Allocator *allocator, uint8_t *p,
size_t size) {
501 if (allocator) allocator->deallocate(p, size);
505 inline uint8_t *ReallocateDownward(
Allocator *allocator, uint8_t *old_p,
506 size_t old_size,
size_t new_size,
507 size_t in_use_back,
size_t in_use_front) {
509 ? allocator->reallocate_downward(old_p, old_size, new_size,
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) 621 inline void destroy() {
622 if (buf_) Deallocate(allocator_, buf_, reserved_);
623 if (own_allocator_ && allocator_) {
delete allocator_; }
627 inline void reset() {
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) 665 : allocator_(other.allocator_),
666 own_allocator_(other.own_allocator_),
667 initial_size_(other.initial_size_),
668 buffer_minalign_(other.buffer_minalign_),
669 reserved_(other.reserved_),
672 scratch_(other.scratch_) {
676 other.own_allocator_ =
false;
678 other.buf_ =
nullptr;
679 other.cur_ =
nullptr;
680 other.scratch_ =
nullptr;
684 #if !defined(FLATBUFFERS_CPP98_STL) 693 #endif // defined(FLATBUFFERS_CPP98_STL) 708 cur_ = buf_ + reserved_;
716 void clear_scratch() {
720 void clear_allocator() {
721 if (own_allocator_ && allocator_) {
delete allocator_; }
722 allocator_ =
nullptr;
723 own_allocator_ =
false;
726 void clear_buffer() {
727 if (buf_) Deallocate(allocator_, buf_, reserved_);
732 uint8_t *release_raw(
size_t &allocated_bytes,
size_t &offset) {
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;
758 size_t ensure_space(
size_t len) {
759 FLATBUFFERS_ASSERT(cur_ >= scratch_ && scratch_ >= buf_);
760 if (len > static_cast<size_t>(cur_ - scratch_)) { reallocate(len); }
763 FLATBUFFERS_ASSERT(size() < FLATBUFFERS_MAX_BUFFER_SIZE);
767 inline uint8_t *make_space(
size_t len) {
768 size_t space = ensure_space(len);
774 Allocator *get_custom_allocator() {
return allocator_; }
776 uoffset_t size()
const {
777 return static_cast<uoffset_t
>(reserved_ - (cur_ - buf_));
780 uoffset_t scratch_size()
const {
781 return static_cast<uoffset_t
>(scratch_ - buf_);
784 size_t capacity()
const {
return reserved_; }
786 uint8_t *data()
const {
787 FLATBUFFERS_ASSERT(cur_);
791 uint8_t *scratch_data()
const {
792 FLATBUFFERS_ASSERT(buf_);
796 uint8_t *scratch_end()
const {
797 FLATBUFFERS_ASSERT(scratch_);
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;
813 template<
typename T>
void scratch_push_small(
const T &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;
827 void fill_big(
size_t zero_pad_bytes) {
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; }
836 swap(allocator_, other.allocator_);
837 swap(own_allocator_, other.own_allocator_);
838 swap(initial_size_, other.initial_size_);
839 swap(buffer_minalign_, other.buffer_minalign_);
840 swap(reserved_, other.reserved_);
841 swap(buf_, other.buf_);
842 swap(cur_, other.cur_);
843 swap(scratch_, other.scratch_);
848 swap(allocator_, other.allocator_);
849 swap(own_allocator_, other.own_allocator_);
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);
874 buf_ = ReallocateDownward(allocator_, buf_, old_reserved, reserved_,
875 old_size, old_scratch_size);
877 buf_ = Allocate(allocator_, reserved_);
879 cur_ = buf_ + reserved_ - old_size;
880 scratch_ = buf_ + old_scratch_size;
885 inline voffset_t FieldIndexToOffset(voffset_t field_id) {
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_);
980 swap(num_field_loc, other.num_field_loc);
981 swap(max_voffset_, other.max_voffset_);
982 swap(nested, other.nested);
983 swap(finished, other.finished);
984 swap(minalign_, other.minalign_);
985 swap(force_defaults_, other.force_defaults_);
986 swap(dedup_vtables_, other.dedup_vtables_);
987 swap(string_pool, other.string_pool);
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 {
1073 FLATBUFFERS_ASSERT(finished);
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() {
1110 static_assert(flatbuffers::is_scalar<T>::value,
"T must be a scalar type");
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));
1167 FLATBUFFERS_ASSERT(off && off <= GetSize());
1168 return GetSize() - off +
static_cast<uoffset_t
>(
sizeof(uoffset_t));
1180 FLATBUFFERS_ASSERT(!nested);
1182 FLATBUFFERS_ASSERT(!num_field_loc);
1187 uoffset_t StartTable() {
1196 uoffset_t EndTable(uoffset_t start) {
1198 FLATBUFFERS_ASSERT(nested);
1201 auto vtableoffsetloc = PushElement<soffset_t>(0);
1208 (std::max)(static_cast<voffset_t>(max_voffset_ +
sizeof(voffset_t)),
1209 FieldIndexToOffset(0));
1210 buf_.fill_big(max_voffset_);
1211 auto table_object_size = vtableoffsetloc - start;
1213 FLATBUFFERS_ASSERT(table_object_size < 0x10000);
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) {
1408 FLATBUFFERS_ASSERT(nested);
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) {
1854 FLATBUFFERS_ASSERT(strlen(file_identifier) == kFileIdentifierLength);
1855 PushBytes(reinterpret_cast<const uint8_t *>(file_identifier),
1856 kFileIdentifierLength);
1858 PushElement(ReferTo(root));
1859 if (size_prefix) { PushElement(GetSize()); }
1872 uoffset_t num_field_loc;
1875 voffset_t max_voffset_;
1885 bool force_defaults_;
1887 bool dedup_vtables_;
1892 auto stra =
reinterpret_cast<const String *
>(buf_->data_at(a.o));
1893 auto strb =
reinterpret_cast<const String *
>(buf_->data_at(b.o));
1894 return StringLessThan(stra->data(), stra->size(),
1895 strb->data(), strb->size());
1902 StringOffsetMap *string_pool;
1907 template<
typename T> T *StartVectorOfStructs(
size_t vector_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 check_alignment_(_check_alignment)
1984 FLATBUFFERS_ASSERT(size_ < FLATBUFFERS_MAX_BUFFER_SIZE);
1988 bool Check(
bool ok)
const {
1990 #ifdef FLATBUFFERS_DEBUG_VERIFICATION_FAILURE 1991 FLATBUFFERS_ASSERT(ok);
1993 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 2002 bool Verify(
size_t elem,
size_t elem_len)
const {
2004 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 2005 auto upper_bound = elem + elem_len;
2006 if (upper_bound_ < upper_bound)
2007 upper_bound_ = upper_bound;
2010 return Check(elem_len < size_ && elem <= size_ - elem_len);
2013 template<
typename T>
bool VerifyAlignment(
size_t elem)
const {
2014 return (elem & (
sizeof(T) - 1)) == 0 || !check_alignment_;
2018 template<
typename T>
bool Verify(
size_t elem)
const {
2019 return VerifyAlignment<T>(elem) && Verify(elem,
sizeof(T));
2023 bool Verify(
const uint8_t *base, voffset_t elem_off,
size_t elem_len)
const {
2024 return Verify(static_cast<size_t>(base - buf_) + elem_off, elem_len);
2027 template<
typename T>
bool Verify(
const uint8_t *base, voffset_t elem_off)
2029 return Verify(static_cast<size_t>(base - buf_) + elem_off,
sizeof(T));
2033 template<
typename T>
bool VerifyTable(
const T *table) {
2034 return !table || table->Verify(*
this);
2038 template<
typename T>
bool VerifyVector(
const Vector<T> *vec)
const {
2039 return !vec || VerifyVectorOrString(reinterpret_cast<const uint8_t *>(vec),
2045 return VerifyVector(
reinterpret_cast<const Vector<T> *
>(vec));
2049 bool VerifyString(
const String *str)
const {
2052 (VerifyVectorOrString(reinterpret_cast<const uint8_t *>(str),
2055 Check(buf_[end] ==
'\0'));
2059 bool VerifyVectorOrString(
const uint8_t *vec,
size_t elem_size,
2060 size_t *end =
nullptr)
const {
2061 auto veco =
static_cast<size_t>(vec - buf_);
2063 if (!Verify<uoffset_t>(veco))
return false;
2066 auto size = ReadScalar<uoffset_t>(vec);
2067 auto max_elems = FLATBUFFERS_MAX_BUFFER_SIZE / elem_size;
2068 if (!Check(size < max_elems))
2070 auto byte_size =
sizeof(size) + elem_size * size;
2071 if (end) *end = veco + byte_size;
2072 return Verify(veco, byte_size);
2078 for (uoffset_t i = 0; i < vec->size(); i++) {
2079 if (!VerifyString(vec->Get(i)))
return false;
2086 template<
typename T>
bool VerifyVectorOfTables(
const Vector<
Offset<T>> *vec) {
2088 for (uoffset_t i = 0; i < vec->size(); i++) {
2089 if (!vec->Get(i)->Verify(*
this))
return false;
2095 bool VerifyTableStart(
const uint8_t *table) {
2097 auto tableo =
static_cast<size_t>(table - buf_);
2098 if (!Verify<soffset_t>(tableo))
return false;
2101 auto vtableo = tableo -
static_cast<size_t>(ReadScalar<soffset_t>(table));
2103 return VerifyComplexity() && Verify<voffset_t>(vtableo) &&
2104 VerifyAlignment<voffset_t>(ReadScalar<voffset_t>(buf_ + vtableo)) &&
2105 Verify(vtableo, ReadScalar<voffset_t>(buf_ + vtableo));
2108 template<
typename T>
2109 bool VerifyBufferFromStart(
const char *identifier,
size_t start) {
2111 (size_ < 2 *
sizeof(flatbuffers::uoffset_t) ||
2112 !BufferHasIdentifier(buf_ + start, identifier))) {
2117 auto o = VerifyOffset(start);
2118 return o &&
reinterpret_cast<const T *
>(buf_ + start + o)->Verify(*
this)
2120 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 2121 && GetComputedSize()
2128 template<
typename T>
bool VerifyBuffer() {
return VerifyBuffer<T>(
nullptr); }
2130 template<
typename T>
bool VerifyBuffer(
const char *identifier) {
2131 return VerifyBufferFromStart<T>(identifier, 0);
2134 template<
typename T>
bool VerifySizePrefixedBuffer(
const char *identifier) {
2135 return Verify<uoffset_t>(0U) &&
2136 ReadScalar<uoffset_t>(buf_) == size_ -
sizeof(uoffset_t) &&
2137 VerifyBufferFromStart<T>(identifier,
sizeof(uoffset_t));
2140 uoffset_t VerifyOffset(
size_t start)
const {
2141 if (!Verify<uoffset_t>(start))
return 0;
2142 auto o = ReadScalar<uoffset_t>(buf_ + start);
2144 if (!Check(o != 0))
return 0;
2146 if (!Check(static_cast<soffset_t>(o) >= 0))
return 0;
2149 if (!Verify(start + o, 1))
return 0;
2153 uoffset_t VerifyOffset(
const uint8_t *base, voffset_t start)
const {
2154 return VerifyOffset(static_cast<size_t>(base - buf_) + start);
2161 bool VerifyComplexity() {
2164 return Check(depth_ <= max_depth_ && num_tables_ <= max_tables_);
2174 size_t GetComputedSize()
const {
2176 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 2177 uintptr_t size = upper_bound_;
2179 size = (size - 1 +
sizeof(uoffset_t)) & ~(
sizeof(uoffset_t) - 1);
2180 return (size > size_) ? 0 : size;
2184 FLATBUFFERS_ASSERT(
false);
2191 const uint8_t *buf_;
2194 uoffset_t max_depth_;
2195 uoffset_t num_tables_;
2196 uoffset_t max_tables_;
2197 mutable size_t upper_bound_;
2198 bool check_alignment_;
2204 struct BufferRefBase {};
2205 template<
typename T>
struct BufferRef : BufferRefBase {
2206 BufferRef() : buf(
nullptr), len(0), must_free(
false) {}
2207 BufferRef(uint8_t *_buf, uoffset_t _len)
2208 : buf(_buf), len(_len), must_free(
false) {}
2211 if (must_free) free(buf);
2214 const T *GetRoot()
const {
return flatbuffers::GetRoot<T>(buf); }
2217 Verifier verifier(buf, len);
2218 return verifier.VerifyBuffer<T>(
nullptr);
2230 class Struct FLATBUFFERS_FINAL_CLASS {
2232 template<
typename T> T GetField(uoffset_t o)
const {
2233 return ReadScalar<T>(&data_[o]);
2236 template<
typename T> T GetStruct(uoffset_t o)
const {
2237 return reinterpret_cast<T
>(&data_[o]);
2240 const uint8_t *GetAddressOf(uoffset_t o)
const {
return &data_[o]; }
2241 uint8_t *GetAddressOf(uoffset_t o) {
return &data_[o]; }
2251 const uint8_t *GetVTable()
const {
2252 return data_ - ReadScalar<soffset_t>(data_);
2257 voffset_t GetOptionalFieldOffset(voffset_t field)
const {
2259 auto vtable = GetVTable();
2261 auto vtsize = ReadScalar<voffset_t>(vtable);
2264 return field < vtsize ? ReadScalar<voffset_t>(vtable + field) : 0;
2267 template<
typename T> T GetField(voffset_t field, T defaultval)
const {
2268 auto field_offset = GetOptionalFieldOffset(field);
2269 return field_offset ? ReadScalar<T>(data_ + field_offset) : defaultval;
2272 template<
typename P> P GetPointer(voffset_t field) {
2273 auto field_offset = GetOptionalFieldOffset(field);
2274 auto p = data_ + field_offset;
2275 return field_offset ?
reinterpret_cast<P
>(p + ReadScalar<uoffset_t>(p))
2278 template<
typename P> P GetPointer(voffset_t field)
const {
2279 return const_cast<Table *
>(
this)->GetPointer<P>(field);
2282 template<
typename P> P GetStruct(voffset_t field)
const {
2283 auto field_offset = GetOptionalFieldOffset(field);
2284 auto p =
const_cast<uint8_t *
>(data_ + field_offset);
2285 return field_offset ?
reinterpret_cast<P
>(p) :
nullptr;
2288 template<
typename T>
bool SetField(voffset_t field, T val, T def) {
2289 auto field_offset = GetOptionalFieldOffset(field);
2290 if (!field_offset)
return IsTheSameAs(val, def);
2291 WriteScalar(data_ + field_offset, val);
2295 bool SetPointer(voffset_t field,
const uint8_t *val) {
2296 auto field_offset = GetOptionalFieldOffset(field);
2297 if (!field_offset)
return false;
2298 WriteScalar(data_ + field_offset,
2299 static_cast<uoffset_t>(val - (data_ + field_offset)));
2303 uint8_t *GetAddressOf(voffset_t field) {
2304 auto field_offset = GetOptionalFieldOffset(field);
2305 return field_offset ? data_ + field_offset :
nullptr;
2307 const uint8_t *GetAddressOf(voffset_t field)
const {
2308 return const_cast<Table *
>(
this)->GetAddressOf(field);
2311 bool CheckField(voffset_t field)
const {
2312 return GetOptionalFieldOffset(field) != 0;
2317 bool VerifyTableStart(Verifier &verifier)
const {
2318 return verifier.VerifyTableStart(data_);
2322 template<
typename T>
2323 bool VerifyField(
const Verifier &verifier, voffset_t field)
const {
2326 auto field_offset = GetOptionalFieldOffset(field);
2328 return !field_offset || verifier.Verify<T>(data_, field_offset);
2332 template<
typename T>
2333 bool VerifyFieldRequired(
const Verifier &verifier, voffset_t field)
const {
2334 auto field_offset = GetOptionalFieldOffset(field);
2335 return verifier.Check(field_offset != 0) &&
2336 verifier.Verify<T>(data_, field_offset);
2340 bool VerifyOffset(
const Verifier &verifier, voffset_t field)
const {
2341 auto field_offset = GetOptionalFieldOffset(field);
2342 return !field_offset || verifier.VerifyOffset(data_, field_offset);
2345 bool VerifyOffsetRequired(
const Verifier &verifier, voffset_t field)
const {
2346 auto field_offset = GetOptionalFieldOffset(field);
2347 return verifier.Check(field_offset != 0) &&
2348 verifier.VerifyOffset(data_, field_offset);
2355 Table(
const Table &other);
2360 template<
typename T>
void FlatBufferBuilder::Required(
Offset<T> table,
2362 auto table_ptr =
reinterpret_cast<const Table *
>(buf_.data_at(table.o));
2363 bool ok = table_ptr->GetOptionalFieldOffset(field) != 0;
2365 FLATBUFFERS_ASSERT(ok);
2373 inline const uint8_t *GetBufferStartFromRootPointer(
const void *root) {
2374 auto table =
reinterpret_cast<const Table *
>(root);
2375 auto vtable = table->GetVTable();
2377 auto start = (std::min)(vtable, reinterpret_cast<const uint8_t *>(root));
2379 start =
reinterpret_cast<const uint8_t *
>(
reinterpret_cast<uintptr_t
>(start) &
2380 ~(
sizeof(uoffset_t) - 1));
2391 "file_identifier is assumed to be the same size as uoffset_t");
2392 for (
auto possible_roots = FLATBUFFERS_MAX_ALIGNMENT /
sizeof(uoffset_t) + 1;
2393 possible_roots; possible_roots--) {
2394 start -=
sizeof(uoffset_t);
2395 if (ReadScalar<uoffset_t>(start) + start ==
2396 reinterpret_cast<const uint8_t *
>(root))
2403 FLATBUFFERS_ASSERT(
false);
2408 inline uoffset_t GetPrefixedSize(
const uint8_t* buf){
return ReadScalar<uoffset_t>(buf); }
2413 struct NativeTable {};
2423 typedef uint64_t hash_value_t;
2425 #ifdef FLATBUFFERS_CPP98_STL 2426 typedef void (*resolver_function_t)(
void **pointer_adr, hash_value_t hash);
2427 typedef hash_value_t (*rehasher_function_t)(
void *pointer);
2429 typedef std::function<void (void **pointer_adr, hash_value_t hash)>
2430 resolver_function_t;
2431 typedef std::function<hash_value_t (void *pointer)> rehasher_function_t;
2442 template<
typename T>
2443 bool IsFieldPresent(
const T *table,
typename T::FlatBuffersVTableOffset field) {
2445 return reinterpret_cast<const Table *
>(table)->CheckField(
2446 static_cast<voffset_t>(field));
2452 inline int LookupEnum(
const char **names,
const char *name) {
2453 for (
const char **p = names; *p; p++)
2454 if (!strcmp(*p, name))
return static_cast<int>(p - names);
2469 #if defined(_MSC_VER) 2470 #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \ 2472 struct __declspec(align(alignment)) 2473 #define FLATBUFFERS_STRUCT_END(name, size) \ 2475 static_assert(sizeof(name) == size, "compiler breaks packing rules") 2476 #elif defined(__GNUC__) || defined(__clang__) 2477 #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \ 2478 _Pragma("pack(1)") \ 2479 struct __attribute__((aligned(alignment))) 2480 #define FLATBUFFERS_STRUCT_END(name, size) \ 2482 static_assert(sizeof(name) == size, "compiler breaks packing rules") 2484 #error Unknown compiler, please define structure alignment macros 2498 enum SequenceType { ST_TABLE, ST_STRUCT, ST_UNION, ST_ENUM };
2502 #define FLATBUFFERS_GEN_ELEMENTARY_TYPES(ET) \ 2516 ET(ET_SEQUENCE) // See SequenceType. 2518 enum ElementaryType {
2519 #define FLATBUFFERS_ET(E) E, 2520 FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET)
2521 #undef FLATBUFFERS_ET 2524 inline const char *
const *ElementaryTypeNames() {
2525 static const char *
const names[] = {
2526 #define FLATBUFFERS_ET(E) #E, 2527 FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET)
2528 #undef FLATBUFFERS_ET 2536 uint16_t base_type : 4;
2537 uint16_t is_vector : 1;
2538 int16_t sequence_ref : 11;
2541 static_assert(
sizeof(TypeCode) == 2,
"TypeCode");
2546 typedef const TypeTable *(*TypeFunction)();
2551 const TypeCode *type_codes;
2552 const TypeFunction *type_refs;
2553 const int64_t *values;
2554 const char *
const *names;
2568 #if !defined(_WIN32) && !defined(__CYGWIN__) 2570 extern volatile __attribute__((weak))
const char *flatbuffer_version_string;
2571 volatile __attribute__((weak))
const char *flatbuffer_version_string =
2573 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR)
"." 2574 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR)
"." 2575 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
2577 #endif // !defined(_WIN32) && !defined(__CYGWIN__) 2579 #define FLATBUFFERS_DEFINE_BITMASK_OPERATORS(E, T)\ 2580 inline E operator | (E lhs, E rhs){\ 2581 return E(T(lhs) | T(rhs));\ 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){\ 2592 inline E operator |= (E &lhs, E rhs){\ 2596 inline E operator &= (E &lhs, E rhs){\ 2600 inline E operator ^= (E &lhs, E rhs){\ 2604 inline bool operator !(E rhs) \ 2606 return !bool(T(rhs)); \ 2613 #endif // FLATBUFFERS_H_ Definition: flatbuffers.h:43
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:1501
uoffset_t CreateUninitializedVector(size_t len, size_t elemsize, uint8_t **buf)
Specialized version of CreateVector for non-copying use cases.
Definition: flatbuffers.h:1759
FLATBUFFERS_ATTRIBUTE(deprecated("use Release() instead")) DetachedBuffer ReleaseBufferPointer()
Get the released pointer to the serialized buffer.
Definition: flatbuffers.h:1030
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:1581
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:1685
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:1671
uoffset_t GetSize() const
The current size of the serialized buffer, counting from the end.
Definition: flatbuffers.h:1012
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:1747
Definition: flatbuffers.h:109
Helper class to hold data needed in creation of a FlatBuffer.
Definition: flatbuffers.h:910
FlatBufferBuilder & operator=(FlatBufferBuilder &&other)
Move assignment operator for FlatBufferBuilder.
Definition: flatbuffers.h:967
Offset< String > CreateString(char *str)
Store a string in the buffer, which is null-terminated.
Definition: flatbuffers.h:1317
void Clear()
Reset all the state in this FlatBufferBuilder so it can be reused to construct another buffer...
Definition: flatbuffers.h:1001
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:1603
Offset< Vector< const T * > > CreateVectorOfNativeStructs(const S *v, size_t len)
Serialize an array of native structs into a FlatBuffer vector.
Definition: flatbuffers.h:1562
Offset< const T * > CreateStruct(const T &structobj)
Write a struct by itself, typically to be part of a union.
Definition: flatbuffers.h:1805
Definition: flatbuffers.h:26
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:1831
Definition: flatbuffers.h:518
Offset< String > CreateSharedString(const char *str)
Store a string in the buffer, which null-terminated.
Definition: flatbuffers.h:1384
Definition: flatbuffers.h:358
Definition: flatbuffers.h:81
Offset< String > CreateString(const char *str, size_t len)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1298
FlatBufferBuilder(FlatBufferBuilder &&other) FlatBufferBuilder(FlatBufferBuilder &other)
Move constructor for FlatBufferBuilder.
Definition: flatbuffers.h:943
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:1081
uint8_t * GetBufferPointer() const
Get the serialized buffer (after you call Finish()).
Definition: flatbuffers.h:1017
Definition: flatbuffers.h:476
void DedupVtables(bool dedup)
By default vtables are deduped in order to save space.
Definition: flatbuffers.h:1085
static const size_t kFileIdentifierLength
The length of a FlatBuffer file header.
Definition: flatbuffers.h:1813
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:923
Offset< String > CreateString(const String *str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1342
Definition: flatbuffers.h:643
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:1700
Offset< String > CreateSharedString(const String *str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1402
uint8_t * GetCurrentBufferPointer() const
Get a pointer to an unfinished buffer.
Definition: flatbuffers.h:1024
Offset< String > CreateString(const T &str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1350
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:1658
Offset< Vector< Offset< String > > > CreateVectorOfStrings(const std::vector< std::string > &v)
Serialize a std::vector<std::string> into a FlatBuffer vector.
Definition: flatbuffers.h:1532
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:1520
size_t GetBufferMinAlignment()
get the minimum alignment this buffer needs to be accessed properly.
Definition: flatbuffers.h:1061
Offset< Vector< const T * > > CreateVectorOfStructs(const std::vector< T, Alloc > &v)
Serialize a std::vector of structs into a FlatBuffer vector.
Definition: flatbuffers.h:1620
Offset< Vector< const T * > > CreateVectorOfStructs(const T *v, size_t len)
Serialize an array of structs into a FlatBuffer vector.
Definition: flatbuffers.h:1547
Offset< Vector< const T * > > CreateVectorOfNativeStructs(const std::vector< S > &v)
Serialize a std::vector of native structs into a FlatBuffer vector.
Definition: flatbuffers.h:1634
Definition: flatbuffers.h:201
Offset< String > CreateString(const char *str)
Store a string in the buffer, which is null-terminated.
Definition: flatbuffers.h:1310
Definition: flatbuffers.h:435
Definition: flatbuffers.h:404
uint8_t * ReleaseRaw(size_t &size, size_t &offset)
Get the released pointer to the serialized buffer.
Definition: flatbuffers.h:1051
Definition: flatbuffers.h:1863
Offset< String > CreateString(const std::string &str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1324
Offset< Vector< T > > CreateVector(const std::vector< T > &v)
Serialize a std::vector into a FlatBuffer vector.
Definition: flatbuffers.h:1477
Offset< Vector< T > > CreateUninitializedVector(size_t len, T **buf)
Specialized version of CreateVector for non-copying use cases.
Definition: flatbuffers.h:1779
Offset< String > CreateString(flatbuffers::string_view str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1333
Offset< Vector< T > > CreateVector(const T *v, size_t len)
Serialize an array into a FlatBuffer vector.
Definition: flatbuffers.h:1443
Offset< String > CreateSharedString(const std::string &str)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1393
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:1733
DetachedBuffer Release()
Get the released DetachedBuffer.
Definition: flatbuffers.h:1038
Offset< String > CreateSharedString(const char *str, size_t len)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1360
Definition: flatbuffers.h:218
void Finish(Offset< T > root, const char *file_identifier=nullptr)
Finish serializing a buffer by writing the root offset.
Definition: flatbuffers.h:1819
Definition: flatbuffers.h:1889