1 /*
2  * Copyright 2014 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef FLATBUFFERS_H_
18 #define FLATBUFFERS_H_
19 
20 /**
21  * @file
22  * A customized version of the FlatBuffers implementation header file targeted
23  * for use within CHRE. This file differs from the mainline FlatBuffers release
24  * via the introduction of the feature flag FLATBUFFERS_CHRE. When defined,
25  * standard library features not used in CHRE are removed. This includes
26  * removing most support for strings and std::vector, removal of std::function,
27  * etc.
28  *
29  * Any user of this header, including CHRE, must make use of a custom Allocator
30  * class when using the FlatBufferBuilder. This ensures that alloc / free
31  * functions specific to the platform are used as new / delete are not
32  * supported, but are used by the DefaultAllocator. Additionally,
33  * FLATBUFFERS_ASSERT must be defined to be the assert function that flatbuffers
34  * should use and FLATBUFFERS_ASSERT_INCLUDE must be the include path to the
35  * file that defines that assert function.
36  */
37 
38 #include "flatbuffers/base.h"
39 
40 #if defined(FLATBUFFERS_NAN_DEFAULTS)
41 #  include <cmath>
42 #endif
43 
44 namespace flatbuffers {
45 // Generic 'operator==' with conditional specialisations.
46 // T e - new value of a scalar field.
47 // T def - default of scalar (is known at compile-time).
IsTheSameAs(T e,T def)48 template<typename T> inline bool IsTheSameAs(T e, T def) { return e == def; }
49 
50 #if defined(FLATBUFFERS_NAN_DEFAULTS) && \
51     defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
52 // Like `operator==(e, def)` with weak NaN if T=(float|double).
IsFloatTheSameAs(T e,T def)53 template<typename T> inline bool IsFloatTheSameAs(T e, T def) {
54   return (e == def) || ((def != def) && (e != e));
55 }
56 template<> inline bool IsTheSameAs<float>(float e, float def) {
57   return IsFloatTheSameAs(e, def);
58 }
59 template<> inline bool IsTheSameAs<double>(double e, double def) {
60   return IsFloatTheSameAs(e, def);
61 }
62 #endif
63 
64 // Check 'v' is out of closed range [low; high].
65 // Workaround for GCC warning [-Werror=type-limits]:
66 // comparison is always true due to limited range of data type.
67 template<typename T>
IsOutRange(const T & v,const T & low,const T & high)68 inline bool IsOutRange(const T &v, const T &low, const T &high) {
69   return (v < low) || (high < v);
70 }
71 
72 // Check 'v' is in closed range [low; high].
73 template<typename T>
IsInRange(const T & v,const T & low,const T & high)74 inline bool IsInRange(const T &v, const T &low, const T &high) {
75   return !IsOutRange(v, low, high);
76 }
77 
78 // Wrapper for uoffset_t to allow safe template specialization.
79 // Value is allowed to be 0 to indicate a null object (see e.g. AddOffset).
80 template<typename T> struct Offset {
81   uoffset_t o;
OffsetOffset82   Offset() : o(0) {}
OffsetOffset83   Offset(uoffset_t _o) : o(_o) {} // NOLINT(google-explicit-constructor)
UnionOffset84   Offset<void> Union() const { return Offset<void>(o); }
IsNullOffset85   bool IsNull() const { return !o; }
86 };
87 
EndianCheck()88 inline void EndianCheck() {
89   int endiantest = 1;
90   // If this fails, see FLATBUFFERS_LITTLEENDIAN above.
91   FLATBUFFERS_ASSERT(*reinterpret_cast<char *>(&endiantest) ==
92                      FLATBUFFERS_LITTLEENDIAN);
93   (void)endiantest;
94 }
95 
AlignOf()96 template<typename T> FLATBUFFERS_CONSTEXPR size_t AlignOf() {
97   // clang-format off
98   #ifdef _MSC_VER
99     return __alignof(T);
100   #else
101     #ifndef alignof
102       return __alignof__(T);
103     #else
104       return alignof(T);
105     #endif
106   #endif
107   // clang-format on
108 }
109 
110 // When we read serialized data from memory, in the case of most scalars,
111 // we want to just read T, but in the case of Offset, we want to actually
112 // perform the indirection and return a pointer.
113 // The template specialization below does just that.
114 // It is wrapped in a struct since function templates can't overload on the
115 // return type like this.
116 // The typedef is for the convenience of callers of this function
117 // (avoiding the need for a trailing return decltype)
118 template<typename T> struct IndirectHelper {
119   typedef T return_type;
120   typedef T mutable_return_type;
121   static const size_t element_stride = sizeof(T);
ReadIndirectHelper122   static return_type Read(const uint8_t *p, uoffset_t i) {
123     return EndianScalar((reinterpret_cast<const T *>(p))[i]);
124   }
125 };
126 template<typename T> struct IndirectHelper<Offset<T>> {
127   typedef const T *return_type;
128   typedef T *mutable_return_type;
129   static const size_t element_stride = sizeof(uoffset_t);
130   static return_type Read(const uint8_t *p, uoffset_t i) {
131     p += i * sizeof(uoffset_t);
132     return reinterpret_cast<return_type>(p + ReadScalar<uoffset_t>(p));
133   }
134 };
135 template<typename T> struct IndirectHelper<const T *> {
136   typedef const T *return_type;
137   typedef T *mutable_return_type;
138   static const size_t element_stride = sizeof(T);
139   static return_type Read(const uint8_t *p, uoffset_t i) {
140     return reinterpret_cast<const T *>(p + i * sizeof(T));
141   }
142 };
143 
144 // An STL compatible iterator implementation for Vector below, effectively
145 // calling Get() for every element.
146 template<typename T, typename IT> struct VectorIterator {
147   typedef std::random_access_iterator_tag iterator_category;
148   typedef IT value_type;
149   typedef ptrdiff_t difference_type;
150   typedef IT *pointer;
151   typedef IT &reference;
152 
153   VectorIterator(const uint8_t *data, uoffset_t i)
154       : data_(data + IndirectHelper<T>::element_stride * i) {}
155   VectorIterator(const VectorIterator &other) : data_(other.data_) {}
156   VectorIterator() : data_(nullptr) {}
157 
158   VectorIterator &operator=(const VectorIterator &other) {
159     data_ = other.data_;
160     return *this;
161   }
162 
163   // clang-format off
164   #if !defined(FLATBUFFERS_CPP98_STL)
165   VectorIterator &operator=(VectorIterator &&other) {
166     data_ = other.data_;
167     return *this;
168   }
169   #endif  // !defined(FLATBUFFERS_CPP98_STL)
170   // clang-format on
171 
172   bool operator==(const VectorIterator &other) const {
173     return data_ == other.data_;
174   }
175 
176   bool operator<(const VectorIterator &other) const {
177     return data_ < other.data_;
178   }
179 
180   bool operator!=(const VectorIterator &other) const {
181     return data_ != other.data_;
182   }
183 
184   difference_type operator-(const VectorIterator &other) const {
185     return (data_ - other.data_) / IndirectHelper<T>::element_stride;
186   }
187 
188   IT operator*() const { return IndirectHelper<T>::Read(data_, 0); }
189 
190   IT operator->() const { return IndirectHelper<T>::Read(data_, 0); }
191 
192   VectorIterator &operator++() {
193     data_ += IndirectHelper<T>::element_stride;
194     return *this;
195   }
196 
197   VectorIterator operator++(int) {
198     VectorIterator temp(data_, 0);
199     data_ += IndirectHelper<T>::element_stride;
200     return temp;
201   }
202 
203   VectorIterator operator+(const uoffset_t &offset) const {
204     return VectorIterator(data_ + offset * IndirectHelper<T>::element_stride,
205                           0);
206   }
207 
208   VectorIterator &operator+=(const uoffset_t &offset) {
209     data_ += offset * IndirectHelper<T>::element_stride;
210     return *this;
211   }
212 
213   VectorIterator &operator--() {
214     data_ -= IndirectHelper<T>::element_stride;
215     return *this;
216   }
217 
218   VectorIterator operator--(int) {
219     VectorIterator temp(data_, 0);
220     data_ -= IndirectHelper<T>::element_stride;
221     return temp;
222   }
223 
224   VectorIterator operator-(const uoffset_t &offset) const {
225     return VectorIterator(data_ - offset * IndirectHelper<T>::element_stride,
226                           0);
227   }
228 
229   VectorIterator &operator-=(const uoffset_t &offset) {
230     data_ -= offset * IndirectHelper<T>::element_stride;
231     return *this;
232   }
233 
234  private:
235   const uint8_t *data_;
236 };
237 
238 template<typename Iterator>
239 struct VectorReverseIterator : public std::reverse_iterator<Iterator> {
240   explicit VectorReverseIterator(Iterator iter)
241       : std::reverse_iterator<Iterator>(iter) {}
242 
243   typename Iterator::value_type operator*() const {
244     return *(std::reverse_iterator<Iterator>::current);
245   }
246 
247   typename Iterator::value_type operator->() const {
248     return *(std::reverse_iterator<Iterator>::current);
249   }
250 };
251 
252 struct String;
253 
254 // This is used as a helper type for accessing vectors.
255 // Vector::data() assumes the vector elements start after the length field.
256 template<typename T> class Vector {
257  public:
258   typedef VectorIterator<T, typename IndirectHelper<T>::mutable_return_type>
259       iterator;
260   typedef VectorIterator<T, typename IndirectHelper<T>::return_type>
261       const_iterator;
262   typedef VectorReverseIterator<iterator> reverse_iterator;
263   typedef VectorReverseIterator<const_iterator> const_reverse_iterator;
264 
265   uoffset_t size() const { return EndianScalar(length_); }
266 
267   // Deprecated: use size(). Here for backwards compatibility.
268   FLATBUFFERS_ATTRIBUTE(deprecated("use size() instead"))
269   uoffset_t Length() const { return size(); }
270 
271   typedef typename IndirectHelper<T>::return_type return_type;
272   typedef typename IndirectHelper<T>::mutable_return_type mutable_return_type;
273 
274   return_type Get(uoffset_t i) const {
275     FLATBUFFERS_ASSERT(i < size());
276     return IndirectHelper<T>::Read(Data(), i);
277   }
278 
279   return_type operator[](uoffset_t i) const { return Get(i); }
280 
281   // If this is a Vector of enums, T will be its storage type, not the enum
282   // type. This function makes it convenient to retrieve value with enum
283   // type E.
284   template<typename E> E GetEnum(uoffset_t i) const {
285     return static_cast<E>(Get(i));
286   }
287 
288   // If this a vector of unions, this does the cast for you. There's no check
289   // to make sure this is the right type!
290   template<typename U> const U *GetAs(uoffset_t i) const {
291     return reinterpret_cast<const U *>(Get(i));
292   }
293 
294   #ifndef FLATBUFFERS_CHRE
295   // If this a vector of unions, this does the cast for you. There's no check
296   // to make sure this is actually a string!
297   const String *GetAsString(uoffset_t i) const {
298     return reinterpret_cast<const String *>(Get(i));
299   }
300   #endif
301 
302   const void *GetStructFromOffset(size_t o) const {
303     return reinterpret_cast<const void *>(Data() + o);
304   }
305 
306   iterator begin() { return iterator(Data(), 0); }
307   const_iterator begin() const { return const_iterator(Data(), 0); }
308 
309   iterator end() { return iterator(Data(), size()); }
310   const_iterator end() const { return const_iterator(Data(), size()); }
311 
312   reverse_iterator rbegin() { return reverse_iterator(end() - 1); }
313   const_reverse_iterator rbegin() const {
314     return const_reverse_iterator(end() - 1);
315   }
316 
317   reverse_iterator rend() { return reverse_iterator(begin() - 1); }
318   const_reverse_iterator rend() const {
319     return const_reverse_iterator(begin() - 1);
320   }
321 
322   const_iterator cbegin() const { return begin(); }
323 
324   const_iterator cend() const { return end(); }
325 
326   const_reverse_iterator crbegin() const { return rbegin(); }
327 
328   const_reverse_iterator crend() const { return rend(); }
329 
330   // Change elements if you have a non-const pointer to this object.
331   // Scalars only. See reflection.h, and the documentation.
332   void Mutate(uoffset_t i, const T &val) {
333     FLATBUFFERS_ASSERT(i < size());
334     WriteScalar(data() + i, val);
335   }
336 
337   // Change an element of a vector of tables (or strings).
338   // "val" points to the new table/string, as you can obtain from
339   // e.g. reflection::AddFlatBuffer().
340   void MutateOffset(uoffset_t i, const uint8_t *val) {
341     FLATBUFFERS_ASSERT(i < size());
342     static_assert(sizeof(T) == sizeof(uoffset_t), "Unrelated types");
343     WriteScalar(data() + i,
344                 static_cast<uoffset_t>(val - (Data() + i * sizeof(uoffset_t))));
345   }
346 
347   // Get a mutable pointer to tables/strings inside this vector.
348   mutable_return_type GetMutableObject(uoffset_t i) const {
349     FLATBUFFERS_ASSERT(i < size());
350     return const_cast<mutable_return_type>(IndirectHelper<T>::Read(Data(), i));
351   }
352 
353   // The raw data in little endian format. Use with care.
354   const uint8_t *Data() const {
355     return reinterpret_cast<const uint8_t *>(&length_ + 1);
356   }
357 
358   uint8_t *Data() { return reinterpret_cast<uint8_t *>(&length_ + 1); }
359 
360   // Similarly, but typed, much like std::vector::data
361   const T *data() const { return reinterpret_cast<const T *>(Data()); }
362   T *data() { return reinterpret_cast<T *>(Data()); }
363 
364   template<typename K> return_type LookupByKey(K key) const {
365     void *search_result = std::bsearch(
366         &key, Data(), size(), IndirectHelper<T>::element_stride, KeyCompare<K>);
367 
368     if (!search_result) {
369       return nullptr;  // Key not found.
370     }
371 
372     const uint8_t *element = reinterpret_cast<const uint8_t *>(search_result);
373 
374     return IndirectHelper<T>::Read(element, 0);
375   }
376 
377  protected:
378   // This class is only used to access pre-existing data. Don't ever
379   // try to construct these manually.
380   Vector();
381 
382   uoffset_t length_;
383 
384  private:
385   // This class is a pointer. Copying will therefore create an invalid object.
386   // Private and unimplemented copy constructor.
387   Vector(const Vector &);
388   Vector &operator=(const Vector &);
389 
390   template<typename K> static int KeyCompare(const void *ap, const void *bp) {
391     const K *key = reinterpret_cast<const K *>(ap);
392     const uint8_t *data = reinterpret_cast<const uint8_t *>(bp);
393     auto table = IndirectHelper<T>::Read(data, 0);
394 
395     // std::bsearch compares with the operands transposed, so we negate the
396     // result here.
397     return -table->KeyCompareWithValue(*key);
398   }
399 };
400 
401 // Represent a vector much like the template above, but in this case we
402 // don't know what the element types are (used with reflection.h).
403 class VectorOfAny {
404  public:
405   uoffset_t size() const { return EndianScalar(length_); }
406 
407   const uint8_t *Data() const {
408     return reinterpret_cast<const uint8_t *>(&length_ + 1);
409   }
410   uint8_t *Data() { return reinterpret_cast<uint8_t *>(&length_ + 1); }
411 
412  protected:
413   VectorOfAny();
414 
415   uoffset_t length_;
416 
417  private:
418   VectorOfAny(const VectorOfAny &);
419   VectorOfAny &operator=(const VectorOfAny &);
420 };
421 
422 #ifndef FLATBUFFERS_CPP98_STL
423 template<typename T, typename U>
424 Vector<Offset<T>> *VectorCast(Vector<Offset<U>> *ptr) {
425   static_assert(std::is_base_of<T, U>::value, "Unrelated types");
426   return reinterpret_cast<Vector<Offset<T>> *>(ptr);
427 }
428 
429 template<typename T, typename U>
430 const Vector<Offset<T>> *VectorCast(const Vector<Offset<U>> *ptr) {
431   static_assert(std::is_base_of<T, U>::value, "Unrelated types");
432   return reinterpret_cast<const Vector<Offset<T>> *>(ptr);
433 }
434 #endif
435 
436 // Convenient helper function to get the length of any vector, regardless
437 // of whether it is null or not (the field is not set).
438 template<typename T> static inline size_t VectorLength(const Vector<T> *v) {
439   return v ? v->size() : 0;
440 }
441 
442 // This is used as a helper type for accessing arrays.
443 template<typename T, uint16_t length> class Array {
444   typedef
445       typename flatbuffers::integral_constant<bool,
446                                               flatbuffers::is_scalar<T>::value>
447           scalar_tag;
448   typedef
449       typename flatbuffers::conditional<scalar_tag::value, T, const T *>::type
450           IndirectHelperType;
451 
452  public:
453   typedef typename IndirectHelper<IndirectHelperType>::return_type return_type;
454   typedef VectorIterator<T, return_type> const_iterator;
455   typedef VectorReverseIterator<const_iterator> const_reverse_iterator;
456 
457   FLATBUFFERS_CONSTEXPR uint16_t size() const { return length; }
458 
459   return_type Get(uoffset_t i) const {
460     FLATBUFFERS_ASSERT(i < size());
461     return IndirectHelper<IndirectHelperType>::Read(Data(), i);
462   }
463 
464   return_type operator[](uoffset_t i) const { return Get(i); }
465 
466   // If this is a Vector of enums, T will be its storage type, not the enum
467   // type. This function makes it convenient to retrieve value with enum
468   // type E.
469   template<typename E> E GetEnum(uoffset_t i) const {
470     return static_cast<E>(Get(i));
471   }
472 
473   const_iterator begin() const { return const_iterator(Data(), 0); }
474   const_iterator end() const { return const_iterator(Data(), size()); }
475 
476   const_reverse_iterator rbegin() const {
477     return const_reverse_iterator(end());
478   }
479   const_reverse_iterator rend() const { return const_reverse_iterator(end()); }
480 
481   const_iterator cbegin() const { return begin(); }
482   const_iterator cend() const { return end(); }
483 
484   const_reverse_iterator crbegin() const { return rbegin(); }
485   const_reverse_iterator crend() const { return rend(); }
486 
487   // Get a mutable pointer to elements inside this array.
488   // This method used to mutate arrays of structs followed by a @p Mutate
489   // operation. For primitive types use @p Mutate directly.
490   // @warning Assignments and reads to/from the dereferenced pointer are not
491   //  automatically converted to the correct endianness.
492   typename flatbuffers::conditional<scalar_tag::value, void, T *>::type
493   GetMutablePointer(uoffset_t i) const {
494     FLATBUFFERS_ASSERT(i < size());
495     return const_cast<T *>(&data()[i]);
496   }
497 
498   // Change elements if you have a non-const pointer to this object.
499   void Mutate(uoffset_t i, const T &val) { MutateImpl(scalar_tag(), i, val); }
500 
501   // The raw data in little endian format. Use with care.
502   const uint8_t *Data() const { return data_; }
503 
504   uint8_t *Data() { return data_; }
505 
506   // Similarly, but typed, much like std::vector::data
507   const T *data() const { return reinterpret_cast<const T *>(Data()); }
508   T *data() { return reinterpret_cast<T *>(Data()); }
509 
510  protected:
511   void MutateImpl(flatbuffers::integral_constant<bool, true>, uoffset_t i,
512                   const T &val) {
513     FLATBUFFERS_ASSERT(i < size());
514     WriteScalar(data() + i, val);
515   }
516 
517   void MutateImpl(flatbuffers::integral_constant<bool, false>, uoffset_t i,
518                   const T &val) {
519     *(GetMutablePointer(i)) = val;
520   }
521 
522   // This class is only used to access pre-existing data. Don't ever
523   // try to construct these manually.
524   // 'constexpr' allows us to use 'size()' at compile time.
525   // @note Must not use 'FLATBUFFERS_CONSTEXPR' here, as const is not allowed on
526   //  a constructor.
527 #if defined(__cpp_constexpr)
528   constexpr Array();
529 #else
530   Array();
531 #endif
532 
533   uint8_t data_[length * sizeof(T)];
534 
535  private:
536   // This class is a pointer. Copying will therefore create an invalid object.
537   // Private and unimplemented copy constructor.
538   Array(const Array &);
539   Array &operator=(const Array &);
540 };
541 
542 // Specialization for Array[struct] with access using Offset<void> pointer.
543 // This specialization used by idl_gen_text.cpp.
544 template<typename T, uint16_t length> class Array<Offset<T>, length> {
545   static_assert(flatbuffers::is_same<T, void>::value, "unexpected type T");
546 
547  public:
548   typedef const void *return_type;
549 
550   const uint8_t *Data() const { return data_; }
551 
552   // Make idl_gen_text.cpp::PrintContainer happy.
553   return_type operator[](uoffset_t) const {
554     FLATBUFFERS_ASSERT(false);
555     return nullptr;
556   }
557 
558  private:
559   // This class is only used to access pre-existing data.
560   Array();
561   Array(const Array &);
562   Array &operator=(const Array &);
563 
564   uint8_t data_[1];
565 };
566 
567 // Lexicographically compare two strings (possibly containing nulls), and
568 // return true if the first is less than the second.
569 static inline bool StringLessThan(const char *a_data, uoffset_t a_size,
570                                   const char *b_data, uoffset_t b_size) {
571   const auto cmp = memcmp(a_data, b_data, (std::min)(a_size, b_size));
572   return cmp == 0 ? a_size < b_size : cmp < 0;
573 }
574 
575 struct String : public Vector<char> {
576   const char *c_str() const { return reinterpret_cast<const char *>(Data()); }
577   #ifndef FLATBUFFERS_CHRE
578   std::string str() const { return std::string(c_str(), size()); }
579   #endif  // !FLATBUFFERS_CHRE
580 
581   // clang-format off
582   #ifdef FLATBUFFERS_HAS_STRING_VIEW
583   flatbuffers::string_view string_view() const {
584     return flatbuffers::string_view(c_str(), size());
585   }
586   #endif // FLATBUFFERS_HAS_STRING_VIEW
587   // clang-format on
588 
589   bool operator<(const String &o) const {
590     return StringLessThan(this->data(), this->size(), o.data(), o.size());
591   }
592 };
593 
594 #ifndef FLATBUFFERS_CHRE
595 // Convenience function to get std::string from a String returning an empty
596 // string on null pointer.
597 static inline std::string GetString(const String *str) {
598   return str ? str->str() : "";
599 }
600 #endif  // !FLATBUFFERS_CHRE
601 
602 // Convenience function to get char* from a String returning an empty string on
603 // null pointer.
604 static inline const char *GetCstring(const String *str) {
605   return str ? str->c_str() : "";
606 }
607 
608 // Allocator interface. This is flatbuffers-specific and meant only for
609 // `vector_downward` usage.
610 class Allocator {
611  public:
612   virtual ~Allocator() {}
613 
614   // Allocate `size` bytes of memory.
615   virtual uint8_t *allocate(size_t size) = 0;
616 
617   // Deallocate `size` bytes of memory at `p` allocated by this allocator.
618   virtual void deallocate(uint8_t *p, size_t size) = 0;
619 
620   // Reallocate `new_size` bytes of memory, replacing the old region of size
621   // `old_size` at `p`. In contrast to a normal realloc, this grows downwards,
622   // and is intended specifcally for `vector_downward` use.
623   // `in_use_back` and `in_use_front` indicate how much of `old_size` is
624   // actually in use at each end, and needs to be copied.
625   virtual uint8_t *reallocate_downward(uint8_t *old_p, size_t old_size,
626                                        size_t new_size, size_t in_use_back,
627                                        size_t in_use_front) {
628     FLATBUFFERS_ASSERT(new_size > old_size);  // vector_downward only grows
629     uint8_t *new_p = allocate(new_size);
630     memcpy_downward(old_p, old_size, new_p, new_size, in_use_back,
631                     in_use_front);
632     deallocate(old_p, old_size);
633     return new_p;
634   }
635 
636  protected:
637   // Called by `reallocate_downward` to copy memory from `old_p` of `old_size`
638   // to `new_p` of `new_size`. Only memory of size `in_use_front` and
639   // `in_use_back` will be copied from the front and back of the old memory
640   // allocation.
641   void memcpy_downward(uint8_t *old_p, size_t old_size, uint8_t *new_p,
642                        size_t new_size, size_t in_use_back,
643                        size_t in_use_front) {
644     memcpy(new_p + new_size - in_use_back, old_p + old_size - in_use_back,
645            in_use_back);
646     memcpy(new_p, old_p, in_use_front);
647   }
648 };
649 
650 #ifndef FLATBUFFERS_CHRE
651 // DefaultAllocator uses new/delete to allocate memory regions
652 class DefaultAllocator : public Allocator {
653  public:
654   uint8_t *allocate(size_t size) FLATBUFFERS_OVERRIDE {
655     return new uint8_t[size];
656   }
657 
658   void deallocate(uint8_t *p, size_t) FLATBUFFERS_OVERRIDE {
659     delete[] p;
660   }
661 
662   static void dealloc(void *p, size_t) {
663     delete[] static_cast<uint8_t *>(p);
664   }
665 };
666 #endif
667 
668 // These functions allow for a null allocator to mean use the default allocator,
669 // as used by DetachedBuffer and vector_downward below.
670 // This is to avoid having a statically or dynamically allocated default
671 // allocator, or having to move it between the classes that may own it.
672 inline uint8_t *Allocate(Allocator *allocator, size_t size) {
673   #ifndef FLATBUFFERS_CHRE
674     return allocator ? allocator->allocate(size)
675                      : DefaultAllocator().allocate(size);
676   #else
677     if (allocator)
678       return allocator->allocate(size);
679     else {
680       FLATBUFFERS_ASSERT(false);
681       return nullptr;
682     }
683   #endif // FLATBUFFERS_CHRE
684 }
685 
686 inline void Deallocate(Allocator *allocator, uint8_t *p, size_t size) {
687   #ifndef FLATBUFFERS_CHRE
688     if (allocator)
689       allocator->deallocate(p, size);
690     else
691       DefaultAllocator().deallocate(p, size);
692   #else
693     if (allocator)
694       allocator->deallocate(p, size);
695     else
696       FLATBUFFERS_ASSERT(false);
697   #endif // FLATBUFFERS_CHRE
698 }
699 
700 inline uint8_t *ReallocateDownward(Allocator *allocator, uint8_t *old_p,
701                                    size_t old_size, size_t new_size,
702                                    size_t in_use_back, size_t in_use_front) {
703   #ifndef FLATBUFFERS_CHRE
704     return allocator ? allocator->reallocate_downward(old_p, old_size, new_size,
705                                                       in_use_back, in_use_front)
706                      : DefaultAllocator().reallocate_downward(
707                         old_p, old_size, new_size, in_use_back, in_use_front);
708   #else
709     if (allocator)
710       return allocator->reallocate_downward(old_p, old_size, new_size,
711                                             in_use_back, in_use_front);
712     else {
713       FLATBUFFERS_ASSERT(false);
714       return nullptr;
715     }
716   #endif // FLATBUFFERS_CHRE
717 }
718 
719 // DetachedBuffer is a finished flatbuffer memory region, detached from its
720 // builder. The original memory region and allocator are also stored so that
721 // the DetachedBuffer can manage the memory lifetime.
722 class DetachedBuffer {
723  public:
724   DetachedBuffer()
725       : allocator_(nullptr),
726         own_allocator_(false),
727         buf_(nullptr),
728         reserved_(0),
729         cur_(nullptr),
730         size_(0) {}
731 
732   DetachedBuffer(Allocator *allocator, bool own_allocator, uint8_t *buf,
733                  size_t reserved, uint8_t *cur, size_t sz)
734       : allocator_(allocator),
735         own_allocator_(own_allocator),
736         buf_(buf),
737         reserved_(reserved),
738         cur_(cur),
739         size_(sz) {}
740 
741   // clang-format off
742   #if !defined(FLATBUFFERS_CPP98_STL)
743   // clang-format on
744   DetachedBuffer(DetachedBuffer &&other)
745       : allocator_(other.allocator_),
746         own_allocator_(other.own_allocator_),
747         buf_(other.buf_),
748         reserved_(other.reserved_),
749         cur_(other.cur_),
750         size_(other.size_) {
751     other.reset();
752   }
753   // clang-format off
754   #endif  // !defined(FLATBUFFERS_CPP98_STL)
755   // clang-format on
756 
757   // clang-format off
758   #if !defined(FLATBUFFERS_CPP98_STL)
759   // clang-format on
760   DetachedBuffer &operator=(DetachedBuffer &&other) {
761     if (this == &other) return *this;
762 
763     destroy();
764 
765     allocator_ = other.allocator_;
766     own_allocator_ = other.own_allocator_;
767     buf_ = other.buf_;
768     reserved_ = other.reserved_;
769     cur_ = other.cur_;
770     size_ = other.size_;
771 
772     other.reset();
773 
774     return *this;
775   }
776   // clang-format off
777   #endif  // !defined(FLATBUFFERS_CPP98_STL)
778   // clang-format on
779 
780   ~DetachedBuffer() { destroy(); }
781 
782   const uint8_t *data() const { return cur_; }
783 
784   uint8_t *data() { return cur_; }
785 
786   size_t size() const { return size_; }
787 
788   // clang-format off
789   #if 0  // disabled for now due to the ordering of classes in this header
790   template <class T>
791   bool Verify() const {
792     Verifier verifier(data(), size());
793     return verifier.Verify<T>(nullptr);
794   }
795 
796   template <class T>
797   const T* GetRoot() const {
798     return flatbuffers::GetRoot<T>(data());
799   }
800 
801   template <class T>
802   T* GetRoot() {
803     return flatbuffers::GetRoot<T>(data());
804   }
805   #endif
806   // clang-format on
807 
808   // clang-format off
809   #if !defined(FLATBUFFERS_CPP98_STL)
810   // clang-format on
811   // These may change access mode, leave these at end of public section
812   FLATBUFFERS_DELETE_FUNC(DetachedBuffer(const DetachedBuffer &other))
813   FLATBUFFERS_DELETE_FUNC(
814       DetachedBuffer &operator=(const DetachedBuffer &other))
815   // clang-format off
816   #endif  // !defined(FLATBUFFERS_CPP98_STL)
817   // clang-format on
818 
819  protected:
820   Allocator *allocator_;
821   bool own_allocator_;
822   uint8_t *buf_;
823   size_t reserved_;
824   uint8_t *cur_;
825   size_t size_;
826 
827   inline void destroy() {
828     if (buf_) Deallocate(allocator_, buf_, reserved_);
829     if (own_allocator_ && allocator_) { delete allocator_; }
830     reset();
831   }
832 
833   inline void reset() {
834     allocator_ = nullptr;
835     own_allocator_ = false;
836     buf_ = nullptr;
837     reserved_ = 0;
838     cur_ = nullptr;
839     size_ = 0;
840   }
841 };
842 
843 // This is a minimal replication of std::vector<uint8_t> functionality,
844 // except growing from higher to lower addresses. i.e push_back() inserts data
845 // in the lowest address in the vector.
846 // Since this vector leaves the lower part unused, we support a "scratch-pad"
847 // that can be stored there for temporary data, to share the allocated space.
848 // Essentially, this supports 2 std::vectors in a single buffer.
849 class vector_downward {
850  public:
851   explicit vector_downward(size_t initial_size, Allocator *allocator,
852                            bool own_allocator, size_t buffer_minalign)
853       : allocator_(allocator),
854         own_allocator_(own_allocator),
855         initial_size_(initial_size),
856         buffer_minalign_(buffer_minalign),
857         reserved_(0),
858         buf_(nullptr),
859         cur_(nullptr),
860         scratch_(nullptr) {}
861 
862   // clang-format off
863   #if !defined(FLATBUFFERS_CPP98_STL)
864   vector_downward(vector_downward &&other)
865   #else
866   vector_downward(vector_downward &other)
867   #endif  // defined(FLATBUFFERS_CPP98_STL)
868       // clang-format on
869       : allocator_(other.allocator_),
870         own_allocator_(other.own_allocator_),
871         initial_size_(other.initial_size_),
872         buffer_minalign_(other.buffer_minalign_),
873         reserved_(other.reserved_),
874         buf_(other.buf_),
875         cur_(other.cur_),
876         scratch_(other.scratch_) {
877     // No change in other.allocator_
878     // No change in other.initial_size_
879     // No change in other.buffer_minalign_
880     other.own_allocator_ = false;
881     other.reserved_ = 0;
882     other.buf_ = nullptr;
883     other.cur_ = nullptr;
884     other.scratch_ = nullptr;
885   }
886 
887   // clang-format off
888   #if !defined(FLATBUFFERS_CPP98_STL)
889   // clang-format on
890   vector_downward &operator=(vector_downward &&other) {
891     // Move construct a temporary and swap idiom
892     vector_downward temp(std::move(other));
893     swap(temp);
894     return *this;
895   }
896   // clang-format off
897   #endif  // defined(FLATBUFFERS_CPP98_STL)
898   // clang-format on
899 
900   ~vector_downward() {
901     clear_buffer();
902     clear_allocator();
903   }
904 
905   void reset() {
906     clear_buffer();
907     clear();
908   }
909 
910   void clear() {
911     if (buf_) {
912       cur_ = buf_ + reserved_;
913     } else {
914       reserved_ = 0;
915       cur_ = nullptr;
916     }
917     clear_scratch();
918   }
919 
920   void clear_scratch() { scratch_ = buf_; }
921 
922   void clear_allocator() {
923     if (own_allocator_ && allocator_) { delete allocator_; }
924     allocator_ = nullptr;
925     own_allocator_ = false;
926   }
927 
928   void clear_buffer() {
929     if (buf_) Deallocate(allocator_, buf_, reserved_);
930     buf_ = nullptr;
931   }
932 
933   // Relinquish the pointer to the caller.
934   uint8_t *release_raw(size_t &allocated_bytes, size_t &offset) {
935     auto *buf = buf_;
936     allocated_bytes = reserved_;
937     offset = static_cast<size_t>(cur_ - buf_);
938 
939     // release_raw only relinquishes the buffer ownership.
940     // Does not deallocate or reset the allocator. Destructor will do that.
941     buf_ = nullptr;
942     clear();
943     return buf;
944   }
945 
946   // Relinquish the pointer to the caller.
947   DetachedBuffer release() {
948     // allocator ownership (if any) is transferred to DetachedBuffer.
949     DetachedBuffer fb(allocator_, own_allocator_, buf_, reserved_, cur_,
950                       size());
951     if (own_allocator_) {
952       allocator_ = nullptr;
953       own_allocator_ = false;
954     }
955     buf_ = nullptr;
956     clear();
957     return fb;
958   }
959 
960   size_t ensure_space(size_t len) {
961     FLATBUFFERS_ASSERT(cur_ >= scratch_ && scratch_ >= buf_);
962     if (len > static_cast<size_t>(cur_ - scratch_)) { reallocate(len); }
963     // Beyond this, signed offsets may not have enough range:
964     // (FlatBuffers > 2GB not supported).
965     FLATBUFFERS_ASSERT(size() < FLATBUFFERS_MAX_BUFFER_SIZE);
966     return len;
967   }
968 
969   inline uint8_t *make_space(size_t len) {
970     size_t space = ensure_space(len);
971     cur_ -= space;
972     return cur_;
973   }
974 
975   // Returns nullptr if using the DefaultAllocator.
976   Allocator *get_custom_allocator() { return allocator_; }
977 
978   uoffset_t size() const {
979     return static_cast<uoffset_t>(reserved_ - static_cast<size_t>(cur_ - buf_));
980   }
981 
982   uoffset_t scratch_size() const {
983     return static_cast<uoffset_t>(scratch_ - buf_);
984   }
985 
986   size_t capacity() const { return reserved_; }
987 
988   uint8_t *data() const {
989     FLATBUFFERS_ASSERT(cur_);
990     return cur_;
991   }
992 
993   uint8_t *scratch_data() const {
994     FLATBUFFERS_ASSERT(buf_);
995     return buf_;
996   }
997 
998   uint8_t *scratch_end() const {
999     FLATBUFFERS_ASSERT(scratch_);
1000     return scratch_;
1001   }
1002 
1003   uint8_t *data_at(size_t offset) const { return buf_ + reserved_ - offset; }
1004 
1005   void push(const uint8_t *bytes, size_t num) {
1006     if (num > 0) { memcpy(make_space(num), bytes, num); }
1007   }
1008 
1009   // Specialized version of push() that avoids memcpy call for small data.
1010   template<typename T> void push_small(const T &little_endian_t) {
1011     make_space(sizeof(T));
1012     *reinterpret_cast<T *>(cur_) = little_endian_t;
1013   }
1014 
1015   template<typename T> void scratch_push_small(const T &t) {
1016     ensure_space(sizeof(T));
1017     *reinterpret_cast<T *>(scratch_) = t;
1018     scratch_ += sizeof(T);
1019   }
1020 
1021   // fill() is most frequently called with small byte counts (<= 4),
1022   // which is why we're using loops rather than calling memset.
1023   void fill(size_t zero_pad_bytes) {
1024     make_space(zero_pad_bytes);
1025     for (size_t i = 0; i < zero_pad_bytes; i++) cur_[i] = 0;
1026   }
1027 
1028   // Version for when we know the size is larger.
1029   // Precondition: zero_pad_bytes > 0
1030   void fill_big(size_t zero_pad_bytes) {
1031     memset(make_space(zero_pad_bytes), 0, zero_pad_bytes);
1032   }
1033 
1034   void pop(size_t bytes_to_remove) { cur_ += bytes_to_remove; }
1035   void scratch_pop(size_t bytes_to_remove) { scratch_ -= bytes_to_remove; }
1036 
1037   void swap(vector_downward &other) {
1038     using std::swap;
1039     swap(allocator_, other.allocator_);
1040     swap(own_allocator_, other.own_allocator_);
1041     swap(initial_size_, other.initial_size_);
1042     swap(buffer_minalign_, other.buffer_minalign_);
1043     swap(reserved_, other.reserved_);
1044     swap(buf_, other.buf_);
1045     swap(cur_, other.cur_);
1046     swap(scratch_, other.scratch_);
1047   }
1048 
1049   void swap_allocator(vector_downward &other) {
1050     using std::swap;
1051     swap(allocator_, other.allocator_);
1052     swap(own_allocator_, other.own_allocator_);
1053   }
1054 
1055  private:
1056   // You shouldn't really be copying instances of this class.
1057   FLATBUFFERS_DELETE_FUNC(vector_downward(const vector_downward &))
1058   FLATBUFFERS_DELETE_FUNC(vector_downward &operator=(const vector_downward &))
1059 
1060   Allocator *allocator_;
1061   bool own_allocator_;
1062   size_t initial_size_;
1063   size_t buffer_minalign_;
1064   size_t reserved_;
1065   uint8_t *buf_;
1066   uint8_t *cur_;  // Points at location between empty (below) and used (above).
1067   uint8_t *scratch_;  // Points to the end of the scratchpad in use.
1068 
1069   void reallocate(size_t len) {
1070     auto old_reserved = reserved_;
1071     auto old_size = size();
1072     auto old_scratch_size = scratch_size();
1073     reserved_ +=
1074         (std::max)(len, old_reserved ? old_reserved / 2 : initial_size_);
1075     reserved_ = (reserved_ + buffer_minalign_ - 1) & ~(buffer_minalign_ - 1);
1076     if (buf_) {
1077       buf_ = ReallocateDownward(allocator_, buf_, old_reserved, reserved_,
1078                                 old_size, old_scratch_size);
1079     } else {
1080       buf_ = Allocate(allocator_, reserved_);
1081     }
1082     cur_ = buf_ + reserved_ - old_size;
1083     scratch_ = buf_ + old_scratch_size;
1084   }
1085 };
1086 
1087 // Converts a Field ID to a virtual table offset.
1088 inline voffset_t FieldIndexToOffset(voffset_t field_id) {
1089   // Should correspond to what EndTable() below builds up.
1090   const int fixed_fields = 2;  // Vtable size and Object Size.
1091   return static_cast<voffset_t>((field_id + fixed_fields) * sizeof(voffset_t));
1092 }
1093 
1094 #ifndef FLATBUFFERS_CHRE
1095 template<typename T, typename Alloc>
1096 const T *data(const std::vector<T, Alloc> &v) {
1097   // Eventually the returned pointer gets passed down to memcpy, so
1098   // we need it to be non-null to avoid undefined behavior.
1099   static uint8_t t;
1100   return v.empty() ? reinterpret_cast<const T *>(&t) : &v.front();
1101 }
1102 template<typename T, typename Alloc> T *data(std::vector<T, Alloc> &v) {
1103   // Eventually the returned pointer gets passed down to memcpy, so
1104   // we need it to be non-null to avoid undefined behavior.
1105   static uint8_t t;
1106   return v.empty() ? reinterpret_cast<T *>(&t) : &v.front();
1107 }
1108 #endif
1109 
1110 /// @endcond
1111 
1112 /// @addtogroup flatbuffers_cpp_api
1113 /// @{
1114 /// @class FlatBufferBuilder
1115 /// @brief Helper class to hold data needed in creation of a FlatBuffer.
1116 /// To serialize data, you typically call one of the `Create*()` functions in
1117 /// the generated code, which in turn call a sequence of `StartTable`/
1118 /// `PushElement`/`AddElement`/`EndTable`, or the builtin `CreateString`/
1119 /// `CreateVector` functions. Do this is depth-first order to build up a tree to
1120 /// the root. `Finish()` wraps up the buffer ready for transport.
1121 class FlatBufferBuilder {
1122  public:
1123   /// @brief Default constructor for FlatBufferBuilder.
1124   /// @param[in] initial_size The initial size of the buffer, in bytes. Defaults
1125   /// to `1024`.
1126   /// @param[in] allocator An `Allocator` to use. If null will use
1127   /// `DefaultAllocator`.
1128   /// @param[in] own_allocator Whether the builder/vector should own the
1129   /// allocator. Defaults to / `false`.
1130   /// @param[in] buffer_minalign Force the buffer to be aligned to the given
1131   /// minimum alignment upon reallocation. Only needed if you intend to store
1132   /// types with custom alignment AND you wish to read the buffer in-place
1133   /// directly after creation.
1134   explicit FlatBufferBuilder(
1135       size_t initial_size = 1024, Allocator *allocator = nullptr,
1136       bool own_allocator = false,
1137       size_t buffer_minalign = AlignOf<largest_scalar_t>())
1138       : buf_(initial_size, allocator, own_allocator, buffer_minalign),
1139         num_field_loc(0),
1140         max_voffset_(0),
1141         nested(false),
1142         finished(false),
1143         minalign_(1),
1144         force_defaults_(false),
1145         dedup_vtables_(true) {
1146     #ifndef FLATBUFFERS_CHRE
1147     string_pool = nullptr;
1148     #endif
1149 
1150     EndianCheck();
1151   }
1152 
1153   // clang-format off
1154   /// @brief Move constructor for FlatBufferBuilder.
1155   #if !defined(FLATBUFFERS_CPP98_STL)
1156   FlatBufferBuilder(FlatBufferBuilder &&other)
1157   #else
1158   FlatBufferBuilder(FlatBufferBuilder &other)
1159   #endif  // #if !defined(FLATBUFFERS_CPP98_STL)
1160     : buf_(1024, nullptr, false, AlignOf<largest_scalar_t>()),
1161       num_field_loc(0),
1162       max_voffset_(0),
1163       nested(false),
1164       finished(false),
1165       minalign_(1),
1166       force_defaults_(false),
1167       dedup_vtables_(true) {
1168     #ifndef FLATBUFFERS_CHRE
1169     string_pool = nullptr;
1170     #endif
1171 
1172     EndianCheck();
1173     // Default construct and swap idiom.
1174     // Lack of delegating constructors in vs2010 makes it more verbose than needed.
1175     Swap(other);
1176   }
1177   // clang-format on
1178 
1179   // clang-format off
1180   #if !defined(FLATBUFFERS_CPP98_STL)
1181   // clang-format on
1182   /// @brief Move assignment operator for FlatBufferBuilder.
1183   FlatBufferBuilder &operator=(FlatBufferBuilder &&other) {
1184     // Move construct a temporary and swap idiom
1185     FlatBufferBuilder temp(std::move(other));
1186     Swap(temp);
1187     return *this;
1188   }
1189   // clang-format off
1190   #endif  // defined(FLATBUFFERS_CPP98_STL)
1191   // clang-format on
1192 
1193   void Swap(FlatBufferBuilder &other) {
1194     using std::swap;
1195     buf_.swap(other.buf_);
1196     swap(num_field_loc, other.num_field_loc);
1197     swap(max_voffset_, other.max_voffset_);
1198     swap(nested, other.nested);
1199     swap(finished, other.finished);
1200     swap(minalign_, other.minalign_);
1201     swap(force_defaults_, other.force_defaults_);
1202     swap(dedup_vtables_, other.dedup_vtables_);
1203     #ifndef FLATBUFFERS_CHRE
1204     swap(string_pool, other.string_pool);
1205     #endif
1206   }
1207 
1208   ~FlatBufferBuilder() {
1209     #ifndef FLATBUFFERS_CHRE
1210     if (string_pool) delete string_pool;
1211     #endif
1212   }
1213 
1214   void Reset() {
1215     Clear();       // clear builder state
1216     buf_.reset();  // deallocate buffer
1217   }
1218 
1219   /// @brief Reset all the state in this FlatBufferBuilder so it can be reused
1220   /// to construct another buffer.
1221   void Clear() {
1222     ClearOffsets();
1223     buf_.clear();
1224     nested = false;
1225     finished = false;
1226     minalign_ = 1;
1227     #ifndef FLATBUFFERS_CHRE
1228     if (string_pool) string_pool->clear();
1229     #endif
1230   }
1231 
1232   /// @brief The current size of the serialized buffer, counting from the end.
1233   /// @return Returns an `uoffset_t` with the current size of the buffer.
1234   uoffset_t GetSize() const { return buf_.size(); }
1235 
1236   /// @brief Get the serialized buffer (after you call `Finish()`).
1237   /// @return Returns an `uint8_t` pointer to the FlatBuffer data inside the
1238   /// buffer.
1239   uint8_t *GetBufferPointer() const {
1240     Finished();
1241     return buf_.data();
1242   }
1243 
1244   /// @brief Get a pointer to an unfinished buffer.
1245   /// @return Returns a `uint8_t` pointer to the unfinished buffer.
1246   uint8_t *GetCurrentBufferPointer() const { return buf_.data(); }
1247 
1248   /// @brief Get the released pointer to the serialized buffer.
1249   /// @warning Do NOT attempt to use this FlatBufferBuilder afterwards!
1250   /// @return A `FlatBuffer` that owns the buffer and its allocator and
1251   /// behaves similar to a `unique_ptr` with a deleter.
1252   FLATBUFFERS_ATTRIBUTE(deprecated("use Release() instead"))
1253   DetachedBuffer ReleaseBufferPointer() {
1254     Finished();
1255     return buf_.release();
1256   }
1257 
1258   /// @brief Get the released DetachedBuffer.
1259   /// @return A `DetachedBuffer` that owns the buffer and its allocator.
1260   DetachedBuffer Release() {
1261     Finished();
1262     return buf_.release();
1263   }
1264 
1265   /// @brief Get the released pointer to the serialized buffer.
1266   /// @param size The size of the memory block containing
1267   /// the serialized `FlatBuffer`.
1268   /// @param offset The offset from the released pointer where the finished
1269   /// `FlatBuffer` starts.
1270   /// @return A raw pointer to the start of the memory block containing
1271   /// the serialized `FlatBuffer`.
1272   /// @remark If the allocator is owned, it gets deleted when the destructor is
1273   /// called..
1274   uint8_t *ReleaseRaw(size_t &size, size_t &offset) {
1275     Finished();
1276     return buf_.release_raw(size, offset);
1277   }
1278 
1279   /// @brief get the minimum alignment this buffer needs to be accessed
1280   /// properly. This is only known once all elements have been written (after
1281   /// you call Finish()). You can use this information if you need to embed
1282   /// a FlatBuffer in some other buffer, such that you can later read it
1283   /// without first having to copy it into its own buffer.
1284   size_t GetBufferMinAlignment() {
1285     Finished();
1286     return minalign_;
1287   }
1288 
1289   /// @cond FLATBUFFERS_INTERNAL
1290   void Finished() const {
1291     // If you get this assert, you're attempting to get access a buffer
1292     // which hasn't been finished yet. Be sure to call
1293     // FlatBufferBuilder::Finish with your root table.
1294     // If you really need to access an unfinished buffer, call
1295     // GetCurrentBufferPointer instead.
1296     FLATBUFFERS_ASSERT(finished);
1297   }
1298   /// @endcond
1299 
1300   /// @brief In order to save space, fields that are set to their default value
1301   /// don't get serialized into the buffer.
1302   /// @param[in] fd When set to `true`, always serializes default values that
1303   /// are set. Optional fields which are not set explicitly, will still not be
1304   /// serialized.
1305   void ForceDefaults(bool fd) { force_defaults_ = fd; }
1306 
1307   /// @brief By default vtables are deduped in order to save space.
1308   /// @param[in] dedup When set to `true`, dedup vtables.
1309   void DedupVtables(bool dedup) { dedup_vtables_ = dedup; }
1310 
1311   /// @cond FLATBUFFERS_INTERNAL
1312   void Pad(size_t num_bytes) { buf_.fill(num_bytes); }
1313 
1314   void TrackMinAlign(size_t elem_size) {
1315     if (elem_size > minalign_) minalign_ = elem_size;
1316   }
1317 
1318   void Align(size_t elem_size) {
1319     TrackMinAlign(elem_size);
1320     buf_.fill(PaddingBytes(buf_.size(), elem_size));
1321   }
1322 
1323   void PushFlatBuffer(const uint8_t *bytes, size_t size) {
1324     PushBytes(bytes, size);
1325     finished = true;
1326   }
1327 
1328   void PushBytes(const uint8_t *bytes, size_t size) { buf_.push(bytes, size); }
1329 
1330   void PopBytes(size_t amount) { buf_.pop(amount); }
1331 
1332   template<typename T> void AssertScalarT() {
1333     // The code assumes power of 2 sizes and endian-swap-ability.
1334     static_assert(flatbuffers::is_scalar<T>::value, "T must be a scalar type");
1335   }
1336 
1337   // Write a single aligned scalar to the buffer
1338   template<typename T> uoffset_t PushElement(T element) {
1339     AssertScalarT<T>();
1340     T litle_endian_element = EndianScalar(element);
1341     Align(sizeof(T));
1342     buf_.push_small(litle_endian_element);
1343     return GetSize();
1344   }
1345 
1346   template<typename T> uoffset_t PushElement(Offset<T> off) {
1347     // Special case for offsets: see ReferTo below.
1348     return PushElement(ReferTo(off.o));
1349   }
1350 
1351   // When writing fields, we track where they are, so we can create correct
1352   // vtables later.
1353   void TrackField(voffset_t field, uoffset_t off) {
1354     FieldLoc fl = { off, field };
1355     buf_.scratch_push_small(fl);
1356     num_field_loc++;
1357     max_voffset_ = (std::max)(max_voffset_, field);
1358   }
1359 
1360   // Like PushElement, but additionally tracks the field this represents.
1361   template<typename T> void AddElement(voffset_t field, T e, T def) {
1362     // We don't serialize values equal to the default.
1363     if (IsTheSameAs(e, def) && !force_defaults_) return;
1364     auto off = PushElement(e);
1365     TrackField(field, off);
1366   }
1367 
1368   template<typename T> void AddOffset(voffset_t field, Offset<T> off) {
1369     if (off.IsNull()) return;  // Don't store.
1370     AddElement(field, ReferTo(off.o), static_cast<uoffset_t>(0));
1371   }
1372 
1373   template<typename T> void AddStruct(voffset_t field, const T *structptr) {
1374     if (!structptr) return;  // Default, don't store.
1375     Align(AlignOf<T>());
1376     buf_.push_small(*structptr);
1377     TrackField(field, GetSize());
1378   }
1379 
1380   void AddStructOffset(voffset_t field, uoffset_t off) {
1381     TrackField(field, off);
1382   }
1383 
1384   // Offsets initially are relative to the end of the buffer (downwards).
1385   // This function converts them to be relative to the current location
1386   // in the buffer (when stored here), pointing upwards.
1387   uoffset_t ReferTo(uoffset_t off) {
1388     // Align to ensure GetSize() below is correct.
1389     Align(sizeof(uoffset_t));
1390     // Offset must refer to something already in buffer.
1391     FLATBUFFERS_ASSERT(off && off <= GetSize());
1392     return GetSize() - off + static_cast<uoffset_t>(sizeof(uoffset_t));
1393   }
1394 
1395   void NotNested() {
1396     // If you hit this, you're trying to construct a Table/Vector/String
1397     // during the construction of its parent table (between the MyTableBuilder
1398     // and table.Finish().
1399     // Move the creation of these sub-objects to above the MyTableBuilder to
1400     // not get this assert.
1401     // Ignoring this assert may appear to work in simple cases, but the reason
1402     // it is here is that storing objects in-line may cause vtable offsets
1403     // to not fit anymore. It also leads to vtable duplication.
1404     FLATBUFFERS_ASSERT(!nested);
1405     // If you hit this, fields were added outside the scope of a table.
1406     FLATBUFFERS_ASSERT(!num_field_loc);
1407   }
1408 
1409   // From generated code (or from the parser), we call StartTable/EndTable
1410   // with a sequence of AddElement calls in between.
1411   uoffset_t StartTable() {
1412     NotNested();
1413     nested = true;
1414     return GetSize();
1415   }
1416 
1417   // This finishes one serialized object by generating the vtable if it's a
1418   // table, comparing it against existing vtables, and writing the
1419   // resulting vtable offset.
1420   uoffset_t EndTable(uoffset_t start) {
1421     // If you get this assert, a corresponding StartTable wasn't called.
1422     FLATBUFFERS_ASSERT(nested);
1423     // Write the vtable offset, which is the start of any Table.
1424     // We fill it's value later.
1425     auto vtableoffsetloc = PushElement<soffset_t>(0);
1426     // Write a vtable, which consists entirely of voffset_t elements.
1427     // It starts with the number of offsets, followed by a type id, followed
1428     // by the offsets themselves. In reverse:
1429     // Include space for the last offset and ensure empty tables have a
1430     // minimum size.
1431     max_voffset_ =
1432         (std::max)(static_cast<voffset_t>(max_voffset_ + sizeof(voffset_t)),
1433                    FieldIndexToOffset(0));
1434     buf_.fill_big(max_voffset_);
1435     auto table_object_size = vtableoffsetloc - start;
1436     // Vtable use 16bit offsets.
1437     FLATBUFFERS_ASSERT(table_object_size < 0x10000);
1438     WriteScalar<voffset_t>(buf_.data() + sizeof(voffset_t),
1439                            static_cast<voffset_t>(table_object_size));
1440     WriteScalar<voffset_t>(buf_.data(), max_voffset_);
1441     // Write the offsets into the table
1442     for (auto it = buf_.scratch_end() - num_field_loc * sizeof(FieldLoc);
1443          it < buf_.scratch_end(); it += sizeof(FieldLoc)) {
1444       auto field_location = reinterpret_cast<FieldLoc *>(it);
1445       auto pos = static_cast<voffset_t>(vtableoffsetloc - field_location->off);
1446       // If this asserts, it means you've set a field twice.
1447       FLATBUFFERS_ASSERT(
1448           !ReadScalar<voffset_t>(buf_.data() + field_location->id));
1449       WriteScalar<voffset_t>(buf_.data() + field_location->id, pos);
1450     }
1451     ClearOffsets();
1452     auto vt1 = reinterpret_cast<voffset_t *>(buf_.data());
1453     auto vt1_size = ReadScalar<voffset_t>(vt1);
1454     auto vt_use = GetSize();
1455     // See if we already have generated a vtable with this exact same
1456     // layout before. If so, make it point to the old one, remove this one.
1457     if (dedup_vtables_) {
1458       for (auto it = buf_.scratch_data(); it < buf_.scratch_end();
1459            it += sizeof(uoffset_t)) {
1460         auto vt_offset_ptr = reinterpret_cast<uoffset_t *>(it);
1461         auto vt2 = reinterpret_cast<voffset_t *>(buf_.data_at(*vt_offset_ptr));
1462         auto vt2_size = ReadScalar<voffset_t>(vt2);
1463         if (vt1_size != vt2_size || 0 != memcmp(vt2, vt1, vt1_size)) continue;
1464         vt_use = *vt_offset_ptr;
1465         buf_.pop(GetSize() - vtableoffsetloc);
1466         break;
1467       }
1468     }
1469     // If this is a new vtable, remember it.
1470     if (vt_use == GetSize()) { buf_.scratch_push_small(vt_use); }
1471     // Fill the vtable offset we created above.
1472     // The offset points from the beginning of the object to where the
1473     // vtable is stored.
1474     // Offsets default direction is downward in memory for future format
1475     // flexibility (storing all vtables at the start of the file).
1476     WriteScalar(buf_.data_at(vtableoffsetloc),
1477                 static_cast<soffset_t>(vt_use) -
1478                     static_cast<soffset_t>(vtableoffsetloc));
1479 
1480     nested = false;
1481     return vtableoffsetloc;
1482   }
1483 
1484   FLATBUFFERS_ATTRIBUTE(deprecated("call the version above instead"))
1485   uoffset_t EndTable(uoffset_t start, voffset_t /*numfields*/) {
1486     return EndTable(start);
1487   }
1488 
1489   // This checks a required field has been set in a given table that has
1490   // just been constructed.
1491   template<typename T> void Required(Offset<T> table, voffset_t field);
1492 
1493   uoffset_t StartStruct(size_t alignment) {
1494     Align(alignment);
1495     return GetSize();
1496   }
1497 
1498   uoffset_t EndStruct() { return GetSize(); }
1499 
1500   void ClearOffsets() {
1501     buf_.scratch_pop(num_field_loc * sizeof(FieldLoc));
1502     num_field_loc = 0;
1503     max_voffset_ = 0;
1504   }
1505 
1506   // Aligns such that when "len" bytes are written, an object can be written
1507   // after it with "alignment" without padding.
1508   void PreAlign(size_t len, size_t alignment) {
1509     TrackMinAlign(alignment);
1510     buf_.fill(PaddingBytes(GetSize() + len, alignment));
1511   }
1512   template<typename T> void PreAlign(size_t len) {
1513     AssertScalarT<T>();
1514     PreAlign(len, sizeof(T));
1515   }
1516   /// @endcond
1517 
1518   /// @brief Store a string in the buffer, which can contain any binary data.
1519   /// @param[in] str A const char pointer to the data to be stored as a string.
1520   /// @param[in] len The number of bytes that should be stored from `str`.
1521   /// @return Returns the offset in the buffer where the string starts.
1522   Offset<String> CreateString(const char *str, size_t len) {
1523     NotNested();
1524     PreAlign<uoffset_t>(len + 1);  // Always 0-terminated.
1525     buf_.fill(1);
1526     PushBytes(reinterpret_cast<const uint8_t *>(str), len);
1527     PushElement(static_cast<uoffset_t>(len));
1528     return Offset<String>(GetSize());
1529   }
1530 
1531   /// @brief Store a string in the buffer, which is null-terminated.
1532   /// @param[in] str A const char pointer to a C-string to add to the buffer.
1533   /// @return Returns the offset in the buffer where the string starts.
1534   Offset<String> CreateString(const char *str) {
1535     return CreateString(str, strlen(str));
1536   }
1537 
1538   /// @brief Store a string in the buffer, which is null-terminated.
1539   /// @param[in] str A char pointer to a C-string to add to the buffer.
1540   /// @return Returns the offset in the buffer where the string starts.
1541   Offset<String> CreateString(char *str) {
1542     return CreateString(str, strlen(str));
1543   }
1544 
1545   #ifndef FLATBUFFERS_CHRE
1546   /// @brief Store a string in the buffer, which can contain any binary data.
1547   /// @param[in] str A const reference to a std::string to store in the buffer.
1548   /// @return Returns the offset in the buffer where the string starts.
1549   Offset<String> CreateString(const std::string &str) {
1550     return CreateString(str.c_str(), str.length());
1551   }
1552 
1553   // clang-format off
1554   #ifdef FLATBUFFERS_HAS_STRING_VIEW
1555   /// @brief Store a string in the buffer, which can contain any binary data.
1556   /// @param[in] str A const string_view to copy in to the buffer.
1557   /// @return Returns the offset in the buffer where the string starts.
1558   Offset<String> CreateString(flatbuffers::string_view str) {
1559     return CreateString(str.data(), str.size());
1560   }
1561   #endif // FLATBUFFERS_HAS_STRING_VIEW
1562   // clang-format on
1563 
1564   /// @brief Store a string in the buffer, which can contain any binary data.
1565   /// @param[in] str A const pointer to a `String` struct to add to the buffer.
1566   /// @return Returns the offset in the buffer where the string starts
1567   Offset<String> CreateString(const String *str) {
1568     return str ? CreateString(str->c_str(), str->size()) : 0;
1569   }
1570 
1571   /// @brief Store a string in the buffer, which can contain any binary data.
1572   /// @param[in] str A const reference to a std::string like type with support
1573   /// of T::c_str() and T::length() to store in the buffer.
1574   /// @return Returns the offset in the buffer where the string starts.
1575   template<typename T> Offset<String> CreateString(const T &str) {
1576     return CreateString(str.c_str(), str.length());
1577   }
1578 
1579   /// @brief Store a string in the buffer, which can contain any binary data.
1580   /// If a string with this exact contents has already been serialized before,
1581   /// instead simply returns the offset of the existing string.
1582   /// @param[in] str A const char pointer to the data to be stored as a string.
1583   /// @param[in] len The number of bytes that should be stored from `str`.
1584   /// @return Returns the offset in the buffer where the string starts.
1585   Offset<String> CreateSharedString(const char *str, size_t len) {
1586     if (!string_pool)
1587       string_pool = new StringOffsetMap(StringOffsetCompare(buf_));
1588     auto size_before_string = buf_.size();
1589     // Must first serialize the string, since the set is all offsets into
1590     // buffer.
1591     auto off = CreateString(str, len);
1592     auto it = string_pool->find(off);
1593     // If it exists we reuse existing serialized data!
1594     if (it != string_pool->end()) {
1595       // We can remove the string we serialized.
1596       buf_.pop(buf_.size() - size_before_string);
1597       return *it;
1598     }
1599     // Record this string for future use.
1600     string_pool->insert(off);
1601     return off;
1602   }
1603 
1604   /// @brief Store a string in the buffer, which null-terminated.
1605   /// If a string with this exact contents has already been serialized before,
1606   /// instead simply returns the offset of the existing string.
1607   /// @param[in] str A const char pointer to a C-string to add to the buffer.
1608   /// @return Returns the offset in the buffer where the string starts.
1609   Offset<String> CreateSharedString(const char *str) {
1610     return CreateSharedString(str, strlen(str));
1611   }
1612 
1613   /// @brief Store a string in the buffer, which can contain any binary data.
1614   /// If a string with this exact contents has already been serialized before,
1615   /// instead simply returns the offset of the existing string.
1616   /// @param[in] str A const reference to a std::string to store in the buffer.
1617   /// @return Returns the offset in the buffer where the string starts.
1618   Offset<String> CreateSharedString(const std::string &str) {
1619     return CreateSharedString(str.c_str(), str.length());
1620   }
1621 
1622   /// @brief Store a string in the buffer, which can contain any binary data.
1623   /// If a string with this exact contents has already been serialized before,
1624   /// instead simply returns the offset of the existing string.
1625   /// @param[in] str A const pointer to a `String` struct to add to the buffer.
1626   /// @return Returns the offset in the buffer where the string starts
1627   Offset<String> CreateSharedString(const String *str) {
1628     return CreateSharedString(str->c_str(), str->size());
1629   }
1630   #endif  // !FLATBUFFERS_CHRE
1631 
1632   /// @cond FLATBUFFERS_INTERNAL
1633   uoffset_t EndVector(size_t len) {
1634     FLATBUFFERS_ASSERT(nested);  // Hit if no corresponding StartVector.
1635     nested = false;
1636     return PushElement(static_cast<uoffset_t>(len));
1637   }
1638 
1639   void StartVector(size_t len, size_t elemsize) {
1640     NotNested();
1641     nested = true;
1642     PreAlign<uoffset_t>(len * elemsize);
1643     PreAlign(len * elemsize, elemsize);  // Just in case elemsize > uoffset_t.
1644   }
1645 
1646   // Call this right before StartVector/CreateVector if you want to force the
1647   // alignment to be something different than what the element size would
1648   // normally dictate.
1649   // This is useful when storing a nested_flatbuffer in a vector of bytes,
1650   // or when storing SIMD floats, etc.
1651   void ForceVectorAlignment(size_t len, size_t elemsize, size_t alignment) {
1652     PreAlign(len * elemsize, alignment);
1653   }
1654 
1655   // Similar to ForceVectorAlignment but for String fields.
1656   void ForceStringAlignment(size_t len, size_t alignment) {
1657     PreAlign((len + 1) * sizeof(char), alignment);
1658   }
1659 
1660   /// @endcond
1661 
1662   /// @brief Serialize an array into a FlatBuffer `vector`.
1663   /// @tparam T The data type of the array elements.
1664   /// @param[in] v A pointer to the array of type `T` to serialize into the
1665   /// buffer as a `vector`.
1666   /// @param[in] len The number of elements to serialize.
1667   /// @return Returns a typed `Offset` into the serialized data indicating
1668   /// where the vector is stored.
1669   template<typename T> Offset<Vector<T>> CreateVector(const T *v, size_t len) {
1670     // If this assert hits, you're specifying a template argument that is
1671     // causing the wrong overload to be selected, remove it.
1672     AssertScalarT<T>();
1673     StartVector(len, sizeof(T));
1674     // clang-format off
1675     #if FLATBUFFERS_LITTLEENDIAN
1676       PushBytes(reinterpret_cast<const uint8_t *>(v), len * sizeof(T));
1677     #else
1678       if (sizeof(T) == 1) {
1679         PushBytes(reinterpret_cast<const uint8_t *>(v), len);
1680       } else {
1681         for (auto i = len; i > 0; ) {
1682           PushElement(v[--i]);
1683         }
1684       }
1685     #endif
1686     // clang-format on
1687     return Offset<Vector<T>>(EndVector(len));
1688   }
1689 
1690   template<typename T>
1691   Offset<Vector<Offset<T>>> CreateVector(const Offset<T> *v, size_t len) {
1692     StartVector(len, sizeof(Offset<T>));
1693     for (auto i = len; i > 0;) { PushElement(v[--i]); }
1694     return Offset<Vector<Offset<T>>>(EndVector(len));
1695   }
1696 
1697   #ifndef FLATBUFFERS_CHRE
1698   /// @brief Serialize a `std::vector` into a FlatBuffer `vector`.
1699   /// @tparam T The data type of the `std::vector` elements.
1700   /// @param v A const reference to the `std::vector` to serialize into the
1701   /// buffer as a `vector`.
1702   /// @return Returns a typed `Offset` into the serialized data indicating
1703   /// where the vector is stored.
1704   template<typename T> Offset<Vector<T>> CreateVector(const std::vector<T> &v) {
1705     return CreateVector(data(v), v.size());
1706   }
1707 
1708   // vector<bool> may be implemented using a bit-set, so we can't access it as
1709   // an array. Instead, read elements manually.
1710   // Background: https://isocpp.org/blog/2012/11/on-vectorbool
1711   Offset<Vector<uint8_t>> CreateVector(const std::vector<bool> &v) {
1712     StartVector(v.size(), sizeof(uint8_t));
1713     for (auto i = v.size(); i > 0;) {
1714       PushElement(static_cast<uint8_t>(v[--i]));
1715     }
1716     return Offset<Vector<uint8_t>>(EndVector(v.size()));
1717   }
1718   #else  // else if defined(FLATBUFFERS_CHRE)
1719   // We need to define this function as it's optionally used in the
1720   // Create<Type>Direct() helper functions generated by the FlatBuffer compiler,
1721   // however its use at runtime is not supported.
1722   template<typename T> Offset<Vector<T>> CreateVector(
1723       const std::vector<T>& /* v */) {
1724     // std::vector use by FlatBuffers is not supported in CHRE.
1725     FLATBUFFERS_ASSERT(false);
1726     return 0;
1727   }
1728   #endif  // FLATBUFFERS_CHRE
1729 
1730   // clang-format off
1731   #if !defined(FLATBUFFERS_CPP98_STL) && !defined(FLATBUFFERS_CHRE)
1732   /// @brief Serialize values returned by a function into a FlatBuffer `vector`.
1733   /// This is a convenience function that takes care of iteration for you.
1734   /// @tparam T The data type of the `std::vector` elements.
1735   /// @param f A function that takes the current iteration 0..vector_size-1 and
1736   /// returns any type that you can construct a FlatBuffers vector out of.
1737   /// @return Returns a typed `Offset` into the serialized data indicating
1738   /// where the vector is stored.
1739   template<typename T> Offset<Vector<T>> CreateVector(size_t vector_size,
1740       const std::function<T (size_t i)> &f) {
1741     std::vector<T> elems(vector_size);
1742     for (size_t i = 0; i < vector_size; i++) elems[i] = f(i);
1743     return CreateVector(elems);
1744   }
1745   #endif
1746   // clang-format on
1747 
1748   #ifndef FLATBUFFERS_CHRE
1749   /// @brief Serialize values returned by a function into a FlatBuffer `vector`.
1750   /// This is a convenience function that takes care of iteration for you.
1751   /// @tparam T The data type of the `std::vector` elements.
1752   /// @param f A function that takes the current iteration 0..vector_size-1,
1753   /// and the state parameter returning any type that you can construct a
1754   /// FlatBuffers vector out of.
1755   /// @param state State passed to f.
1756   /// @return Returns a typed `Offset` into the serialized data indicating
1757   /// where the vector is stored.
1758   template<typename T, typename F, typename S>
1759   Offset<Vector<T>> CreateVector(size_t vector_size, F f, S *state) {
1760     std::vector<T> elems(vector_size);
1761     for (size_t i = 0; i < vector_size; i++) elems[i] = f(i, state);
1762     return CreateVector(elems);
1763   }
1764 
1765   /// @brief Serialize a `std::vector<std::string>` into a FlatBuffer `vector`.
1766   /// This is a convenience function for a common case.
1767   /// @param v A const reference to the `std::vector` to serialize into the
1768   /// buffer as a `vector`.
1769   /// @return Returns a typed `Offset` into the serialized data indicating
1770   /// where the vector is stored.
1771   Offset<Vector<Offset<String>>> CreateVectorOfStrings(
1772       const std::vector<std::string> &v) {
1773     std::vector<Offset<String>> offsets(v.size());
1774     for (size_t i = 0; i < v.size(); i++) offsets[i] = CreateString(v[i]);
1775     return CreateVector(offsets);
1776   }
1777   #endif  // !FLATBUFFERS_CHRE
1778 
1779   /// @brief Serialize an array of structs into a FlatBuffer `vector`.
1780   /// @tparam T The data type of the struct array elements.
1781   /// @param[in] v A pointer to the array of type `T` to serialize into the
1782   /// buffer as a `vector`.
1783   /// @param[in] len The number of elements to serialize.
1784   /// @return Returns a typed `Offset` into the serialized data indicating
1785   /// where the vector is stored.
1786   template<typename T>
1787   Offset<Vector<const T *>> CreateVectorOfStructs(const T *v, size_t len) {
1788     StartVector(len * sizeof(T) / AlignOf<T>(), AlignOf<T>());
1789     PushBytes(reinterpret_cast<const uint8_t *>(v), sizeof(T) * len);
1790     return Offset<Vector<const T *>>(EndVector(len));
1791   }
1792 
1793   #ifndef FLATBUFFERS_CHRE
1794   /// @brief Serialize an array of native structs into a FlatBuffer `vector`.
1795   /// @tparam T The data type of the struct array elements.
1796   /// @tparam S The data type of the native struct array elements.
1797   /// @param[in] v A pointer to the array of type `S` to serialize into the
1798   /// buffer as a `vector`.
1799   /// @param[in] len The number of elements to serialize.
1800   /// @return Returns a typed `Offset` into the serialized data indicating
1801   /// where the vector is stored.
1802   template<typename T, typename S>
1803   Offset<Vector<const T *>> CreateVectorOfNativeStructs(const S *v,
1804                                                         size_t len) {
1805     extern T Pack(const S &);
1806     std::vector<T> vv(len);
1807     std::transform(v, v + len, vv.begin(), Pack);
1808     return CreateVectorOfStructs<T>(data(vv), vv.size());
1809   }
1810 
1811   // clang-format off
1812   #ifndef FLATBUFFERS_CPP98_STL
1813   /// @brief Serialize an array of structs into a FlatBuffer `vector`.
1814   /// @tparam T The data type of the struct array elements.
1815   /// @param[in] filler A function that takes the current iteration 0..vector_size-1
1816   /// and a pointer to the struct that must be filled.
1817   /// @return Returns a typed `Offset` into the serialized data indicating
1818   /// where the vector is stored.
1819   /// This is mostly useful when flatbuffers are generated with mutation
1820   /// accessors.
1821   template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
1822       size_t vector_size, const std::function<void(size_t i, T *)> &filler) {
1823     T* structs = StartVectorOfStructs<T>(vector_size);
1824     for (size_t i = 0; i < vector_size; i++) {
1825       filler(i, structs);
1826       structs++;
1827     }
1828     return EndVectorOfStructs<T>(vector_size);
1829   }
1830   #endif
1831   #endif  // !FLATBUFFERS_CHRE
1832   // clang-format on
1833 
1834   /// @brief Serialize an array of structs into a FlatBuffer `vector`.
1835   /// @tparam T The data type of the struct array elements.
1836   /// @param[in] f A function that takes the current iteration 0..vector_size-1,
1837   /// a pointer to the struct that must be filled and the state argument.
1838   /// @param[in] state Arbitrary state to pass to f.
1839   /// @return Returns a typed `Offset` into the serialized data indicating
1840   /// where the vector is stored.
1841   /// This is mostly useful when flatbuffers are generated with mutation
1842   /// accessors.
1843   template<typename T, typename F, typename S>
1844   Offset<Vector<const T *>> CreateVectorOfStructs(size_t vector_size, F f,
1845                                                   S *state) {
1846     T *structs = StartVectorOfStructs<T>(vector_size);
1847     for (size_t i = 0; i < vector_size; i++) {
1848       f(i, structs, state);
1849       structs++;
1850     }
1851     return EndVectorOfStructs<T>(vector_size);
1852   }
1853 
1854   #ifndef FLATBUFFERS_CHRE
1855   /// @brief Serialize a `std::vector` of structs into a FlatBuffer `vector`.
1856   /// @tparam T The data type of the `std::vector` struct elements.
1857   /// @param[in] v A const reference to the `std::vector` of structs to
1858   /// serialize into the buffer as a `vector`.
1859   /// @return Returns a typed `Offset` into the serialized data indicating
1860   /// where the vector is stored.
1861   template<typename T, typename Alloc>
1862   Offset<Vector<const T *>> CreateVectorOfStructs(
1863       const std::vector<T, Alloc> &v) {
1864     return CreateVectorOfStructs(data(v), v.size());
1865   }
1866 
1867   /// @brief Serialize a `std::vector` of native structs into a FlatBuffer
1868   /// `vector`.
1869   /// @tparam T The data type of the `std::vector` struct elements.
1870   /// @tparam S The data type of the `std::vector` native struct elements.
1871   /// @param[in] v A const reference to the `std::vector` of structs to
1872   /// serialize into the buffer as a `vector`.
1873   /// @return Returns a typed `Offset` into the serialized data indicating
1874   /// where the vector is stored.
1875   template<typename T, typename S>
1876   Offset<Vector<const T *>> CreateVectorOfNativeStructs(
1877       const std::vector<S> &v) {
1878     return CreateVectorOfNativeStructs<T, S>(data(v), v.size());
1879   }
1880 
1881   /// @cond FLATBUFFERS_INTERNAL
1882   template<typename T> struct StructKeyComparator {
1883     bool operator()(const T &a, const T &b) const {
1884       return a.KeyCompareLessThan(&b);
1885     }
1886 
1887    private:
1888     StructKeyComparator &operator=(const StructKeyComparator &);
1889   };
1890   /// @endcond
1891 
1892   /// @brief Serialize a `std::vector` of structs into a FlatBuffer `vector`
1893   /// in sorted order.
1894   /// @tparam T The data type of the `std::vector` struct elements.
1895   /// @param[in] v A const reference to the `std::vector` of structs to
1896   /// serialize into the buffer as a `vector`.
1897   /// @return Returns a typed `Offset` into the serialized data indicating
1898   /// where the vector is stored.
1899   template<typename T>
1900   Offset<Vector<const T *>> CreateVectorOfSortedStructs(std::vector<T> *v) {
1901     return CreateVectorOfSortedStructs(data(*v), v->size());
1902   }
1903 
1904   /// @brief Serialize a `std::vector` of native structs into a FlatBuffer
1905   /// `vector` in sorted order.
1906   /// @tparam T The data type of the `std::vector` struct elements.
1907   /// @tparam S The data type of the `std::vector` native struct elements.
1908   /// @param[in] v A const reference to the `std::vector` of structs to
1909   /// serialize into the buffer as a `vector`.
1910   /// @return Returns a typed `Offset` into the serialized data indicating
1911   /// where the vector is stored.
1912   template<typename T, typename S>
1913   Offset<Vector<const T *>> CreateVectorOfSortedNativeStructs(
1914       std::vector<S> *v) {
1915     return CreateVectorOfSortedNativeStructs<T, S>(data(*v), v->size());
1916   }
1917 
1918   /// @brief Serialize an array of structs into a FlatBuffer `vector` in sorted
1919   /// order.
1920   /// @tparam T The data type of the struct array elements.
1921   /// @param[in] v A pointer to the array of type `T` to serialize into the
1922   /// buffer as a `vector`.
1923   /// @param[in] len The number of elements to serialize.
1924   /// @return Returns a typed `Offset` into the serialized data indicating
1925   /// where the vector is stored.
1926   template<typename T>
1927   Offset<Vector<const T *>> CreateVectorOfSortedStructs(T *v, size_t len) {
1928     std::sort(v, v + len, StructKeyComparator<T>());
1929     return CreateVectorOfStructs(v, len);
1930   }
1931 
1932   /// @brief Serialize an array of native structs into a FlatBuffer `vector` in
1933   /// sorted order.
1934   /// @tparam T The data type of the struct array elements.
1935   /// @tparam S The data type of the native struct array elements.
1936   /// @param[in] v A pointer to the array of type `S` to serialize into the
1937   /// buffer as a `vector`.
1938   /// @param[in] len The number of elements to serialize.
1939   /// @return Returns a typed `Offset` into the serialized data indicating
1940   /// where the vector is stored.
1941   template<typename T, typename S>
1942   Offset<Vector<const T *>> CreateVectorOfSortedNativeStructs(S *v,
1943                                                               size_t len) {
1944     extern T Pack(const S &);
1945     typedef T (*Pack_t)(const S &);
1946     std::vector<T> vv(len);
1947     std::transform(v, v + len, vv.begin(), static_cast<Pack_t &>(Pack));
1948     return CreateVectorOfSortedStructs<T>(vv, len);
1949   }
1950 
1951   /// @cond FLATBUFFERS_INTERNAL
1952   template<typename T> struct TableKeyComparator {
1953     TableKeyComparator(vector_downward &buf) : buf_(buf) {}
1954     TableKeyComparator(const TableKeyComparator &other) : buf_(other.buf_) {}
1955     bool operator()(const Offset<T> &a, const Offset<T> &b) const {
1956       auto table_a = reinterpret_cast<T *>(buf_.data_at(a.o));
1957       auto table_b = reinterpret_cast<T *>(buf_.data_at(b.o));
1958       return table_a->KeyCompareLessThan(table_b);
1959     }
1960     vector_downward &buf_;
1961 
1962    private:
1963     TableKeyComparator &operator=(const TableKeyComparator &other) {
1964       buf_ = other.buf_;
1965       return *this;
1966     }
1967   };
1968   /// @endcond
1969 
1970   /// @brief Serialize an array of `table` offsets as a `vector` in the buffer
1971   /// in sorted order.
1972   /// @tparam T The data type that the offset refers to.
1973   /// @param[in] v An array of type `Offset<T>` that contains the `table`
1974   /// offsets to store in the buffer in sorted order.
1975   /// @param[in] len The number of elements to store in the `vector`.
1976   /// @return Returns a typed `Offset` into the serialized data indicating
1977   /// where the vector is stored.
1978   template<typename T>
1979   Offset<Vector<Offset<T>>> CreateVectorOfSortedTables(Offset<T> *v,
1980                                                        size_t len) {
1981     std::sort(v, v + len, TableKeyComparator<T>(buf_));
1982     return CreateVector(v, len);
1983   }
1984 
1985   /// @brief Serialize an array of `table` offsets as a `vector` in the buffer
1986   /// in sorted order.
1987   /// @tparam T The data type that the offset refers to.
1988   /// @param[in] v An array of type `Offset<T>` that contains the `table`
1989   /// offsets to store in the buffer in sorted order.
1990   /// @return Returns a typed `Offset` into the serialized data indicating
1991   /// where the vector is stored.
1992   template<typename T>
1993   Offset<Vector<Offset<T>>> CreateVectorOfSortedTables(
1994       std::vector<Offset<T>> *v) {
1995     return CreateVectorOfSortedTables(data(*v), v->size());
1996   }
1997   #endif  // !FLATBUFFERS_CHRE
1998 
1999   /// @brief Specialized version of `CreateVector` for non-copying use cases.
2000   /// Write the data any time later to the returned buffer pointer `buf`.
2001   /// @param[in] len The number of elements to store in the `vector`.
2002   /// @param[in] elemsize The size of each element in the `vector`.
2003   /// @param[out] buf A pointer to a `uint8_t` pointer that can be
2004   /// written to at a later time to serialize the data into a `vector`
2005   /// in the buffer.
2006   uoffset_t CreateUninitializedVector(size_t len, size_t elemsize,
2007                                       uint8_t **buf) {
2008     NotNested();
2009     StartVector(len, elemsize);
2010     buf_.make_space(len * elemsize);
2011     auto vec_start = GetSize();
2012     auto vec_end = EndVector(len);
2013     *buf = buf_.data_at(vec_start);
2014     return vec_end;
2015   }
2016 
2017   /// @brief Specialized version of `CreateVector` for non-copying use cases.
2018   /// Write the data any time later to the returned buffer pointer `buf`.
2019   /// @tparam T The data type of the data that will be stored in the buffer
2020   /// as a `vector`.
2021   /// @param[in] len The number of elements to store in the `vector`.
2022   /// @param[out] buf A pointer to a pointer of type `T` that can be
2023   /// written to at a later time to serialize the data into a `vector`
2024   /// in the buffer.
2025   template<typename T>
2026   Offset<Vector<T>> CreateUninitializedVector(size_t len, T **buf) {
2027     AssertScalarT<T>();
2028     return CreateUninitializedVector(len, sizeof(T),
2029                                      reinterpret_cast<uint8_t **>(buf));
2030   }
2031 
2032   template<typename T>
2033   Offset<Vector<const T *>> CreateUninitializedVectorOfStructs(size_t len,
2034                                                                T **buf) {
2035     return CreateUninitializedVector(len, sizeof(T),
2036                                      reinterpret_cast<uint8_t **>(buf));
2037   }
2038 
2039   // @brief Create a vector of scalar type T given as input a vector of scalar
2040   // type U, useful with e.g. pre "enum class" enums, or any existing scalar
2041   // data of the wrong type.
2042   template<typename T, typename U>
2043   Offset<Vector<T>> CreateVectorScalarCast(const U *v, size_t len) {
2044     AssertScalarT<T>();
2045     AssertScalarT<U>();
2046     StartVector(len, sizeof(T));
2047     for (auto i = len; i > 0;) { PushElement(static_cast<T>(v[--i])); }
2048     return Offset<Vector<T>>(EndVector(len));
2049   }
2050 
2051   /// @brief Write a struct by itself, typically to be part of a union.
2052   template<typename T> Offset<const T *> CreateStruct(const T &structobj) {
2053     NotNested();
2054     Align(AlignOf<T>());
2055     buf_.push_small(structobj);
2056     return Offset<const T *>(GetSize());
2057   }
2058 
2059   /// @brief The length of a FlatBuffer file header.
2060   static const size_t kFileIdentifierLength = 4;
2061 
2062   /// @brief Finish serializing a buffer by writing the root offset.
2063   /// @param[in] file_identifier If a `file_identifier` is given, the buffer
2064   /// will be prefixed with a standard FlatBuffers file header.
2065   template<typename T>
2066   void Finish(Offset<T> root, const char *file_identifier = nullptr) {
2067     Finish(root.o, file_identifier, false);
2068   }
2069 
2070   /// @brief Finish a buffer with a 32 bit size field pre-fixed (size of the
2071   /// buffer following the size field). These buffers are NOT compatible
2072   /// with standard buffers created by Finish, i.e. you can't call GetRoot
2073   /// on them, you have to use GetSizePrefixedRoot instead.
2074   /// All >32 bit quantities in this buffer will be aligned when the whole
2075   /// size pre-fixed buffer is aligned.
2076   /// These kinds of buffers are useful for creating a stream of FlatBuffers.
2077   template<typename T>
2078   void FinishSizePrefixed(Offset<T> root,
2079                           const char *file_identifier = nullptr) {
2080     Finish(root.o, file_identifier, true);
2081   }
2082 
2083   void SwapBufAllocator(FlatBufferBuilder &other) {
2084     buf_.swap_allocator(other.buf_);
2085   }
2086 
2087  protected:
2088   // You shouldn't really be copying instances of this class.
2089   FlatBufferBuilder(const FlatBufferBuilder &);
2090   FlatBufferBuilder &operator=(const FlatBufferBuilder &);
2091 
2092   void Finish(uoffset_t root, const char *file_identifier, bool size_prefix) {
2093     NotNested();
2094     buf_.clear_scratch();
2095     // This will cause the whole buffer to be aligned.
2096     PreAlign((size_prefix ? sizeof(uoffset_t) : 0) + sizeof(uoffset_t) +
2097                  (file_identifier ? kFileIdentifierLength : 0),
2098              minalign_);
2099     if (file_identifier) {
2100       FLATBUFFERS_ASSERT(strlen(file_identifier) == kFileIdentifierLength);
2101       PushBytes(reinterpret_cast<const uint8_t *>(file_identifier),
2102                 kFileIdentifierLength);
2103     }
2104     PushElement(ReferTo(root));  // Location of root.
2105     if (size_prefix) { PushElement(GetSize()); }
2106     finished = true;
2107   }
2108 
2109   struct FieldLoc {
2110     uoffset_t off;
2111     voffset_t id;
2112   };
2113 
2114   vector_downward buf_;
2115 
2116   // Accumulating offsets of table members while it is being built.
2117   // We store these in the scratch pad of buf_, after the vtable offsets.
2118   uoffset_t num_field_loc;
2119   // Track how much of the vtable is in use, so we can output the most compact
2120   // possible vtable.
2121   voffset_t max_voffset_;
2122 
2123   // Ensure objects are not nested.
2124   bool nested;
2125 
2126   // Ensure the buffer is finished before it is being accessed.
2127   bool finished;
2128 
2129   size_t minalign_;
2130 
2131   bool force_defaults_;  // Serialize values equal to their defaults anyway.
2132 
2133   bool dedup_vtables_;
2134 
2135   #ifndef FLATBUFFERS_CHRE
2136   struct StringOffsetCompare {
2137     StringOffsetCompare(const vector_downward &buf) : buf_(&buf) {}
2138     bool operator()(const Offset<String> &a, const Offset<String> &b) const {
2139       auto stra = reinterpret_cast<const String *>(buf_->data_at(a.o));
2140       auto strb = reinterpret_cast<const String *>(buf_->data_at(b.o));
2141       return StringLessThan(stra->data(), stra->size(), strb->data(),
2142                             strb->size());
2143     }
2144     const vector_downward *buf_;
2145   };
2146 
2147   // For use with CreateSharedString. Instantiated on first use only.
2148   typedef std::set<Offset<String>, StringOffsetCompare> StringOffsetMap;
2149   StringOffsetMap *string_pool;
2150   #endif  // !FLATBUFFERS_CHRE
2151 
2152  private:
2153   // Allocates space for a vector of structures.
2154   // Must be completed with EndVectorOfStructs().
2155   template<typename T> T *StartVectorOfStructs(size_t vector_size) {
2156     StartVector(vector_size * sizeof(T) / AlignOf<T>(), AlignOf<T>());
2157     return reinterpret_cast<T *>(buf_.make_space(vector_size * sizeof(T)));
2158   }
2159 
2160   // End the vector of structues in the flatbuffers.
2161   // Vector should have previously be started with StartVectorOfStructs().
2162   template<typename T>
2163   Offset<Vector<const T *>> EndVectorOfStructs(size_t vector_size) {
2164     return Offset<Vector<const T *>>(EndVector(vector_size));
2165   }
2166 };
2167 /// @}
2168 
2169 /// @cond FLATBUFFERS_INTERNAL
2170 // Helpers to get a typed pointer to the root object contained in the buffer.
2171 template<typename T> T *GetMutableRoot(void *buf) {
2172   EndianCheck();
2173   return reinterpret_cast<T *>(
2174       reinterpret_cast<uint8_t *>(buf) +
2175       EndianScalar(*reinterpret_cast<uoffset_t *>(buf)));
2176 }
2177 
2178 template<typename T> const T *GetRoot(const void *buf) {
2179   return GetMutableRoot<T>(const_cast<void *>(buf));
2180 }
2181 
2182 template<typename T> const T *GetSizePrefixedRoot(const void *buf) {
2183   return GetRoot<T>(reinterpret_cast<const uint8_t *>(buf) + sizeof(uoffset_t));
2184 }
2185 
2186 /// Helpers to get a typed pointer to objects that are currently being built.
2187 /// @warning Creating new objects will lead to reallocations and invalidates
2188 /// the pointer!
2189 template<typename T>
2190 T *GetMutableTemporaryPointer(FlatBufferBuilder &fbb, Offset<T> offset) {
2191   return reinterpret_cast<T *>(fbb.GetCurrentBufferPointer() + fbb.GetSize() -
2192                                offset.o);
2193 }
2194 
2195 template<typename T>
2196 const T *GetTemporaryPointer(FlatBufferBuilder &fbb, Offset<T> offset) {
2197   return GetMutableTemporaryPointer<T>(fbb, offset);
2198 }
2199 
2200 /// @brief Get a pointer to the the file_identifier section of the buffer.
2201 /// @return Returns a const char pointer to the start of the file_identifier
2202 /// characters in the buffer.  The returned char * has length
2203 /// 'flatbuffers::FlatBufferBuilder::kFileIdentifierLength'.
2204 /// This function is UNDEFINED for FlatBuffers whose schema does not include
2205 /// a file_identifier (likely points at padding or the start of a the root
2206 /// vtable).
2207 inline const char *GetBufferIdentifier(const void *buf,
2208                                        bool size_prefixed = false) {
2209   return reinterpret_cast<const char *>(buf) +
2210          ((size_prefixed) ? 2 * sizeof(uoffset_t) : sizeof(uoffset_t));
2211 }
2212 
2213 // Helper to see if the identifier in a buffer has the expected value.
2214 inline bool BufferHasIdentifier(const void *buf, const char *identifier,
2215                                 bool size_prefixed = false) {
2216   return strncmp(GetBufferIdentifier(buf, size_prefixed), identifier,
2217                  FlatBufferBuilder::kFileIdentifierLength) == 0;
2218 }
2219 
2220 // Helper class to verify the integrity of a FlatBuffer
2221 class Verifier FLATBUFFERS_FINAL_CLASS {
2222  public:
2223   Verifier(const uint8_t *buf, size_t buf_len, uoffset_t _max_depth = 64,
2224            uoffset_t _max_tables = 1000000, bool _check_alignment = true)
2225       : buf_(buf),
2226         size_(buf_len),
2227         depth_(0),
2228         max_depth_(_max_depth),
2229         num_tables_(0),
2230         max_tables_(_max_tables),
2231         upper_bound_(0),
2232         check_alignment_(_check_alignment) {
2233     FLATBUFFERS_ASSERT(size_ < FLATBUFFERS_MAX_BUFFER_SIZE);
2234   }
2235 
2236   // Central location where any verification failures register.
2237   bool Check(bool ok) const {
2238     // clang-format off
2239     #ifdef FLATBUFFERS_DEBUG_VERIFICATION_FAILURE
2240       FLATBUFFERS_ASSERT(ok);
2241     #endif
2242     #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
2243       if (!ok)
2244         upper_bound_ = 0;
2245     #endif
2246     // clang-format on
2247     return ok;
2248   }
2249 
2250   // Verify any range within the buffer.
2251   bool Verify(size_t elem, size_t elem_len) const {
2252     // clang-format off
2253     #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
2254       auto upper_bound = elem + elem_len;
2255       if (upper_bound_ < upper_bound)
2256         upper_bound_ =  upper_bound;
2257     #endif
2258     // clang-format on
2259     return Check(elem_len < size_ && elem <= size_ - elem_len);
2260   }
2261 
2262   template<typename T> bool VerifyAlignment(size_t elem) const {
2263     return Check((elem & (sizeof(T) - 1)) == 0 || !check_alignment_);
2264   }
2265 
2266   // Verify a range indicated by sizeof(T).
2267   template<typename T> bool Verify(size_t elem) const {
2268     return VerifyAlignment<T>(elem) && Verify(elem, sizeof(T));
2269   }
2270 
2271   bool VerifyFromPointer(const uint8_t *p, size_t len) {
2272     auto o = static_cast<size_t>(p - buf_);
2273     return Verify(o, len);
2274   }
2275 
2276   // Verify relative to a known-good base pointer.
2277   bool Verify(const uint8_t *base, voffset_t elem_off, size_t elem_len) const {
2278     return Verify(static_cast<size_t>(base - buf_) + elem_off, elem_len);
2279   }
2280 
2281   template<typename T>
2282   bool Verify(const uint8_t *base, voffset_t elem_off) const {
2283     return Verify(static_cast<size_t>(base - buf_) + elem_off, sizeof(T));
2284   }
2285 
2286   // Verify a pointer (may be NULL) of a table type.
2287   template<typename T> bool VerifyTable(const T *table) {
2288     return !table || table->Verify(*this);
2289   }
2290 
2291   // Verify a pointer (may be NULL) of any vector type.
2292   template<typename T> bool VerifyVector(const Vector<T> *vec) const {
2293     return !vec || VerifyVectorOrString(reinterpret_cast<const uint8_t *>(vec),
2294                                         sizeof(T));
2295   }
2296 
2297   // Verify a pointer (may be NULL) of a vector to struct.
2298   template<typename T> bool VerifyVector(const Vector<const T *> *vec) const {
2299     return VerifyVector(reinterpret_cast<const Vector<T> *>(vec));
2300   }
2301 
2302   // Verify a pointer (may be NULL) to string.
2303   bool VerifyString(const String *str) const {
2304     size_t end;
2305     return !str || (VerifyVectorOrString(reinterpret_cast<const uint8_t *>(str),
2306                                          1, &end) &&
2307                     Verify(end, 1) &&           // Must have terminator
2308                     Check(buf_[end] == '\0'));  // Terminating byte must be 0.
2309   }
2310 
2311   // Common code between vectors and strings.
2312   bool VerifyVectorOrString(const uint8_t *vec, size_t elem_size,
2313                             size_t *end = nullptr) const {
2314     auto veco = static_cast<size_t>(vec - buf_);
2315     // Check we can read the size field.
2316     if (!Verify<uoffset_t>(veco)) return false;
2317     // Check the whole array. If this is a string, the byte past the array
2318     // must be 0.
2319     auto size = ReadScalar<uoffset_t>(vec);
2320     auto max_elems = FLATBUFFERS_MAX_BUFFER_SIZE / elem_size;
2321     if (!Check(size < max_elems))
2322       return false;  // Protect against byte_size overflowing.
2323     auto byte_size = sizeof(size) + elem_size * size;
2324     if (end) *end = veco + byte_size;
2325     return Verify(veco, byte_size);
2326   }
2327 
2328   #ifndef FLATBUFFERS_CHRE
2329   // Special case for string contents, after the above has been called.
2330   bool VerifyVectorOfStrings(const Vector<Offset<String>> *vec) const {
2331     if (vec) {
2332       for (uoffset_t i = 0; i < vec->size(); i++) {
2333         if (!VerifyString(vec->Get(i))) return false;
2334       }
2335     }
2336     return true;
2337   }
2338   #endif
2339 
2340   // Special case for table contents, after the above has been called.
2341   template<typename T> bool VerifyVectorOfTables(const Vector<Offset<T>> *vec) {
2342     if (vec) {
2343       for (uoffset_t i = 0; i < vec->size(); i++) {
2344         if (!vec->Get(i)->Verify(*this)) return false;
2345       }
2346     }
2347     return true;
2348   }
2349 
2350   __supress_ubsan__("unsigned-integer-overflow") bool VerifyTableStart(
2351       const uint8_t *table) {
2352     // Check the vtable offset.
2353     auto tableo = static_cast<size_t>(table - buf_);
2354     if (!Verify<soffset_t>(tableo)) return false;
2355     // This offset may be signed, but doing the subtraction unsigned always
2356     // gives the result we want.
2357     auto vtableo = tableo - static_cast<size_t>(ReadScalar<soffset_t>(table));
2358     // Check the vtable size field, then check vtable fits in its entirety.
2359     return VerifyComplexity() && Verify<voffset_t>(vtableo) &&
2360            VerifyAlignment<voffset_t>(ReadScalar<voffset_t>(buf_ + vtableo)) &&
2361            Verify(vtableo, ReadScalar<voffset_t>(buf_ + vtableo));
2362   }
2363 
2364   template<typename T>
2365   bool VerifyBufferFromStart(const char *identifier, size_t start) {
2366     if (identifier && (size_ < 2 * sizeof(flatbuffers::uoffset_t) ||
2367                        !BufferHasIdentifier(buf_ + start, identifier))) {
2368       return false;
2369     }
2370 
2371     // Call T::Verify, which must be in the generated code for this type.
2372     auto o = VerifyOffset(start);
2373     return o && reinterpret_cast<const T *>(buf_ + start + o)->Verify(*this)
2374     // clang-format off
2375     #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
2376            && GetComputedSize()
2377     #endif
2378         ;
2379     // clang-format on
2380   }
2381 
2382   // Verify this whole buffer, starting with root type T.
2383   template<typename T> bool VerifyBuffer() { return VerifyBuffer<T>(nullptr); }
2384 
2385   template<typename T> bool VerifyBuffer(const char *identifier) {
2386     return VerifyBufferFromStart<T>(identifier, 0);
2387   }
2388 
2389   template<typename T> bool VerifySizePrefixedBuffer(const char *identifier) {
2390     return Verify<uoffset_t>(0U) &&
2391            ReadScalar<uoffset_t>(buf_) == size_ - sizeof(uoffset_t) &&
2392            VerifyBufferFromStart<T>(identifier, sizeof(uoffset_t));
2393   }
2394 
2395   uoffset_t VerifyOffset(size_t start) const {
2396     if (!Verify<uoffset_t>(start)) return 0;
2397     auto o = ReadScalar<uoffset_t>(buf_ + start);
2398     // May not point to itself.
2399     if (!Check(o != 0)) return 0;
2400     // Can't wrap around / buffers are max 2GB.
2401     if (!Check(static_cast<soffset_t>(o) >= 0)) return 0;
2402     // Must be inside the buffer to create a pointer from it (pointer outside
2403     // buffer is UB).
2404     if (!Verify(start + o, 1)) return 0;
2405     return o;
2406   }
2407 
2408   uoffset_t VerifyOffset(const uint8_t *base, voffset_t start) const {
2409     return VerifyOffset(static_cast<size_t>(base - buf_) + start);
2410   }
2411 
2412   // Called at the start of a table to increase counters measuring data
2413   // structure depth and amount, and possibly bails out with false if
2414   // limits set by the constructor have been hit. Needs to be balanced
2415   // with EndTable().
2416   bool VerifyComplexity() {
2417     depth_++;
2418     num_tables_++;
2419     return Check(depth_ <= max_depth_ && num_tables_ <= max_tables_);
2420   }
2421 
2422   // Called at the end of a table to pop the depth count.
2423   bool EndTable() {
2424     depth_--;
2425     return true;
2426   }
2427 
2428   // Returns the message size in bytes
2429   size_t GetComputedSize() const {
2430     // clang-format off
2431     #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
2432       uintptr_t size = upper_bound_;
2433       // Align the size to uoffset_t
2434       size = (size - 1 + sizeof(uoffset_t)) & ~(sizeof(uoffset_t) - 1);
2435       return (size > size_) ?  0 : size;
2436     #else
2437       // Must turn on FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE for this to work.
2438       (void)upper_bound_;
2439       FLATBUFFERS_ASSERT(false);
2440       return 0;
2441     #endif
2442     // clang-format on
2443   }
2444 
2445  private:
2446   const uint8_t *buf_;
2447   size_t size_;
2448   uoffset_t depth_;
2449   uoffset_t max_depth_;
2450   uoffset_t num_tables_;
2451   uoffset_t max_tables_;
2452   mutable size_t upper_bound_;
2453   bool check_alignment_;
2454 };
2455 
2456 // Convenient way to bundle a buffer and its length, to pass it around
2457 // typed by its root.
2458 // A BufferRef does not own its buffer.
2459 struct BufferRefBase {};  // for std::is_base_of
2460 template<typename T> struct BufferRef : BufferRefBase {
2461   BufferRef() : buf(nullptr), len(0), must_free(false) {}
2462   BufferRef(uint8_t *_buf, uoffset_t _len)
2463       : buf(_buf), len(_len), must_free(false) {}
2464 
2465   ~BufferRef() {
2466     if (must_free) free(buf);
2467   }
2468 
2469   const T *GetRoot() const { return flatbuffers::GetRoot<T>(buf); }
2470 
2471   bool Verify() {
2472     Verifier verifier(buf, len);
2473     return verifier.VerifyBuffer<T>(nullptr);
2474   }
2475 
2476   uint8_t *buf;
2477   uoffset_t len;
2478   bool must_free;
2479 };
2480 
2481 // "structs" are flat structures that do not have an offset table, thus
2482 // always have all members present and do not support forwards/backwards
2483 // compatible extensions.
2484 
2485 class Struct FLATBUFFERS_FINAL_CLASS {
2486  public:
2487   template<typename T> T GetField(uoffset_t o) const {
2488     return ReadScalar<T>(&data_[o]);
2489   }
2490 
2491   template<typename T> T GetStruct(uoffset_t o) const {
2492     return reinterpret_cast<T>(&data_[o]);
2493   }
2494 
2495   const uint8_t *GetAddressOf(uoffset_t o) const { return &data_[o]; }
2496   uint8_t *GetAddressOf(uoffset_t o) { return &data_[o]; }
2497 
2498  private:
2499   // private constructor & copy constructor: you obtain instances of this
2500   // class by pointing to existing data only
2501   Struct();
2502   Struct(const Struct &);
2503   Struct &operator=(const Struct &);
2504 
2505   uint8_t data_[1];
2506 };
2507 
2508 // "tables" use an offset table (possibly shared) that allows fields to be
2509 // omitted and added at will, but uses an extra indirection to read.
2510 class Table {
2511  public:
2512   const uint8_t *GetVTable() const {
2513     return data_ - ReadScalar<soffset_t>(data_);
2514   }
2515 
2516   // This gets the field offset for any of the functions below it, or 0
2517   // if the field was not present.
2518   voffset_t GetOptionalFieldOffset(voffset_t field) const {
2519     // The vtable offset is always at the start.
2520     auto vtable = GetVTable();
2521     // The first element is the size of the vtable (fields + type id + itself).
2522     auto vtsize = ReadScalar<voffset_t>(vtable);
2523     // If the field we're accessing is outside the vtable, we're reading older
2524     // data, so it's the same as if the offset was 0 (not present).
2525     return field < vtsize ? ReadScalar<voffset_t>(vtable + field) : 0;
2526   }
2527 
2528   template<typename T> T GetField(voffset_t field, T defaultval) const {
2529     auto field_offset = GetOptionalFieldOffset(field);
2530     return field_offset ? ReadScalar<T>(data_ + field_offset) : defaultval;
2531   }
2532 
2533   template<typename P> P GetPointer(voffset_t field) {
2534     auto field_offset = GetOptionalFieldOffset(field);
2535     auto p = data_ + field_offset;
2536     return field_offset ? reinterpret_cast<P>(p + ReadScalar<uoffset_t>(p))
2537                         : nullptr;
2538   }
2539   template<typename P> P GetPointer(voffset_t field) const {
2540     return const_cast<Table *>(this)->GetPointer<P>(field);
2541   }
2542 
2543   template<typename P> P GetStruct(voffset_t field) const {
2544     auto field_offset = GetOptionalFieldOffset(field);
2545     auto p = const_cast<uint8_t *>(data_ + field_offset);
2546     return field_offset ? reinterpret_cast<P>(p) : nullptr;
2547   }
2548 
2549   template<typename T> bool SetField(voffset_t field, T val, T def) {
2550     auto field_offset = GetOptionalFieldOffset(field);
2551     if (!field_offset) return IsTheSameAs(val, def);
2552     WriteScalar(data_ + field_offset, val);
2553     return true;
2554   }
2555 
2556   bool SetPointer(voffset_t field, const uint8_t *val) {
2557     auto field_offset = GetOptionalFieldOffset(field);
2558     if (!field_offset) return false;
2559     WriteScalar(data_ + field_offset,
2560                 static_cast<uoffset_t>(val - (data_ + field_offset)));
2561     return true;
2562   }
2563 
2564   uint8_t *GetAddressOf(voffset_t field) {
2565     auto field_offset = GetOptionalFieldOffset(field);
2566     return field_offset ? data_ + field_offset : nullptr;
2567   }
2568   const uint8_t *GetAddressOf(voffset_t field) const {
2569     return const_cast<Table *>(this)->GetAddressOf(field);
2570   }
2571 
2572   bool CheckField(voffset_t field) const {
2573     return GetOptionalFieldOffset(field) != 0;
2574   }
2575 
2576   // Verify the vtable of this table.
2577   // Call this once per table, followed by VerifyField once per field.
2578   bool VerifyTableStart(Verifier &verifier) const {
2579     return verifier.VerifyTableStart(data_);
2580   }
2581 
2582   // Verify a particular field.
2583   template<typename T>
2584   bool VerifyField(const Verifier &verifier, voffset_t field) const {
2585     // Calling GetOptionalFieldOffset should be safe now thanks to
2586     // VerifyTable().
2587     auto field_offset = GetOptionalFieldOffset(field);
2588     // Check the actual field.
2589     return !field_offset || verifier.Verify<T>(data_, field_offset);
2590   }
2591 
2592   // VerifyField for required fields.
2593   template<typename T>
2594   bool VerifyFieldRequired(const Verifier &verifier, voffset_t field) const {
2595     auto field_offset = GetOptionalFieldOffset(field);
2596     return verifier.Check(field_offset != 0) &&
2597            verifier.Verify<T>(data_, field_offset);
2598   }
2599 
2600   // Versions for offsets.
2601   bool VerifyOffset(const Verifier &verifier, voffset_t field) const {
2602     auto field_offset = GetOptionalFieldOffset(field);
2603     return !field_offset || verifier.VerifyOffset(data_, field_offset);
2604   }
2605 
2606   bool VerifyOffsetRequired(const Verifier &verifier, voffset_t field) const {
2607     auto field_offset = GetOptionalFieldOffset(field);
2608     return verifier.Check(field_offset != 0) &&
2609            verifier.VerifyOffset(data_, field_offset);
2610   }
2611 
2612  private:
2613   // private constructor & copy constructor: you obtain instances of this
2614   // class by pointing to existing data only
2615   Table();
2616   Table(const Table &other);
2617   Table &operator=(const Table &);
2618 
2619   uint8_t data_[1];
2620 };
2621 
2622 template<typename T>
2623 void FlatBufferBuilder::Required(Offset<T> table, voffset_t field) {
2624   auto table_ptr = reinterpret_cast<const Table *>(buf_.data_at(table.o));
2625   bool ok = table_ptr->GetOptionalFieldOffset(field) != 0;
2626   // If this fails, the caller will show what field needs to be set.
2627   FLATBUFFERS_ASSERT(ok);
2628   (void)ok;
2629 }
2630 
2631 /// @brief This can compute the start of a FlatBuffer from a root pointer, i.e.
2632 /// it is the opposite transformation of GetRoot().
2633 /// This may be useful if you want to pass on a root and have the recipient
2634 /// delete the buffer afterwards.
2635 inline const uint8_t *GetBufferStartFromRootPointer(const void *root) {
2636   auto table = reinterpret_cast<const Table *>(root);
2637   auto vtable = table->GetVTable();
2638   // Either the vtable is before the root or after the root.
2639   auto start = (std::min)(vtable, reinterpret_cast<const uint8_t *>(root));
2640   // Align to at least sizeof(uoffset_t).
2641   start = reinterpret_cast<const uint8_t *>(reinterpret_cast<uintptr_t>(start) &
2642                                             ~(sizeof(uoffset_t) - 1));
2643   // Additionally, there may be a file_identifier in the buffer, and the root
2644   // offset. The buffer may have been aligned to any size between
2645   // sizeof(uoffset_t) and FLATBUFFERS_MAX_ALIGNMENT (see "force_align").
2646   // Sadly, the exact alignment is only known when constructing the buffer,
2647   // since it depends on the presence of values with said alignment properties.
2648   // So instead, we simply look at the next uoffset_t values (root,
2649   // file_identifier, and alignment padding) to see which points to the root.
2650   // None of the other values can "impersonate" the root since they will either
2651   // be 0 or four ASCII characters.
2652   static_assert(FlatBufferBuilder::kFileIdentifierLength == sizeof(uoffset_t),
2653                 "file_identifier is assumed to be the same size as uoffset_t");
2654   for (auto possible_roots = FLATBUFFERS_MAX_ALIGNMENT / sizeof(uoffset_t) + 1;
2655        possible_roots; possible_roots--) {
2656     start -= sizeof(uoffset_t);
2657     if (ReadScalar<uoffset_t>(start) + start ==
2658         reinterpret_cast<const uint8_t *>(root))
2659       return start;
2660   }
2661   // We didn't find the root, either the "root" passed isn't really a root,
2662   // or the buffer is corrupt.
2663   // Assert, because calling this function with bad data may cause reads
2664   // outside of buffer boundaries.
2665   FLATBUFFERS_ASSERT(false);
2666   return nullptr;
2667 }
2668 
2669 /// @brief This return the prefixed size of a FlatBuffer.
2670 inline uoffset_t GetPrefixedSize(const uint8_t *buf) {
2671   return ReadScalar<uoffset_t>(buf);
2672 }
2673 
2674 // Base class for native objects (FlatBuffer data de-serialized into native
2675 // C++ data structures).
2676 // Contains no functionality, purely documentative.
2677 struct NativeTable {};
2678 
2679 /// @brief Function types to be used with resolving hashes into objects and
2680 /// back again. The resolver gets a pointer to a field inside an object API
2681 /// object that is of the type specified in the schema using the attribute
2682 /// `cpp_type` (it is thus important whatever you write to this address
2683 /// matches that type). The value of this field is initially null, so you
2684 /// may choose to implement a delayed binding lookup using this function
2685 /// if you wish. The resolver does the opposite lookup, for when the object
2686 /// is being serialized again.
2687 typedef uint64_t hash_value_t;
2688 // clang-format off
2689 #if defined(FLATBUFFERS_CPP98_STL) || defined(FLATBUFFERS_CHRE)
2690   typedef void (*resolver_function_t)(void **pointer_adr, hash_value_t hash);
2691   typedef hash_value_t (*rehasher_function_t)(void *pointer);
2692 #else
2693   typedef std::function<void (void **pointer_adr, hash_value_t hash)>
2694           resolver_function_t;
2695   typedef std::function<hash_value_t (void *pointer)> rehasher_function_t;
2696 #endif
2697 // clang-format on
2698 
2699 // Helper function to test if a field is present, using any of the field
2700 // enums in the generated code.
2701 // `table` must be a generated table type. Since this is a template parameter,
2702 // this is not typechecked to be a subclass of Table, so beware!
2703 // Note: this function will return false for fields equal to the default
2704 // value, since they're not stored in the buffer (unless force_defaults was
2705 // used).
2706 template<typename T>
2707 bool IsFieldPresent(const T *table, typename T::FlatBuffersVTableOffset field) {
2708   // Cast, since Table is a private baseclass of any table types.
2709   return reinterpret_cast<const Table *>(table)->CheckField(
2710       static_cast<voffset_t>(field));
2711 }
2712 
2713 // Utility function for reverse lookups on the EnumNames*() functions
2714 // (in the generated C++ code)
2715 // names must be NULL terminated.
2716 inline int LookupEnum(const char **names, const char *name) {
2717   for (const char **p = names; *p; p++)
2718     if (!strcmp(*p, name)) return static_cast<int>(p - names);
2719   return -1;
2720 }
2721 
2722 // These macros allow us to layout a struct with a guarantee that they'll end
2723 // up looking the same on different compilers and platforms.
2724 // It does this by disallowing the compiler to do any padding, and then
2725 // does padding itself by inserting extra padding fields that make every
2726 // element aligned to its own size.
2727 // Additionally, it manually sets the alignment of the struct as a whole,
2728 // which is typically its largest element, or a custom size set in the schema
2729 // by the force_align attribute.
2730 // These are used in the generated code only.
2731 
2732 // clang-format off
2733 #if defined(_MSC_VER)
2734   #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \
2735     __pragma(pack(1)) \
2736     struct __declspec(align(alignment))
2737   #define FLATBUFFERS_STRUCT_END(name, size) \
2738     __pragma(pack()) \
2739     static_assert(sizeof(name) == size, "compiler breaks packing rules")
2740 #elif defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
2741   #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \
2742     _Pragma("pack(1)") \
2743     struct __attribute__((aligned(alignment)))
2744   #define FLATBUFFERS_STRUCT_END(name, size) \
2745     _Pragma("pack()") \
2746     static_assert(sizeof(name) == size, "compiler breaks packing rules")
2747 #else
2748   #error Unknown compiler, please define structure alignment macros
2749 #endif
2750 // clang-format on
2751 
2752 // Minimal reflection via code generation.
2753 // Besides full-fat reflection (see reflection.h) and parsing/printing by
2754 // loading schemas (see idl.h), we can also have code generation for mimimal
2755 // reflection data which allows pretty-printing and other uses without needing
2756 // a schema or a parser.
2757 // Generate code with --reflect-types (types only) or --reflect-names (names
2758 // also) to enable.
2759 // See minireflect.h for utilities using this functionality.
2760 
2761 // These types are organized slightly differently as the ones in idl.h.
2762 enum SequenceType { ST_TABLE, ST_STRUCT, ST_UNION, ST_ENUM };
2763 
2764 // Scalars have the same order as in idl.h
2765 // clang-format off
2766 #define FLATBUFFERS_GEN_ELEMENTARY_TYPES(ET) \
2767   ET(ET_UTYPE) \
2768   ET(ET_BOOL) \
2769   ET(ET_CHAR) \
2770   ET(ET_UCHAR) \
2771   ET(ET_SHORT) \
2772   ET(ET_USHORT) \
2773   ET(ET_INT) \
2774   ET(ET_UINT) \
2775   ET(ET_LONG) \
2776   ET(ET_ULONG) \
2777   ET(ET_FLOAT) \
2778   ET(ET_DOUBLE) \
2779   ET(ET_STRING) \
2780   ET(ET_SEQUENCE)  // See SequenceType.
2781 
2782 enum ElementaryType {
2783   #define FLATBUFFERS_ET(E) E,
2784     FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET)
2785   #undef FLATBUFFERS_ET
2786 };
2787 
2788 inline const char * const *ElementaryTypeNames() {
2789   static const char * const names[] = {
2790     #define FLATBUFFERS_ET(E) #E,
2791       FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET)
2792     #undef FLATBUFFERS_ET
2793   };
2794   return names;
2795 }
2796 // clang-format on
2797 
2798 // Basic type info cost just 16bits per field!
2799 struct TypeCode {
2800   uint16_t base_type : 4;  // ElementaryType
2801   uint16_t is_vector : 1;
2802   int16_t sequence_ref : 11;  // Index into type_refs below, or -1 for none.
2803 };
2804 
2805 static_assert(sizeof(TypeCode) == 2, "TypeCode");
2806 
2807 struct TypeTable;
2808 
2809 // Signature of the static method present in each type.
2810 typedef const TypeTable *(*TypeFunction)();
2811 
2812 struct TypeTable {
2813   SequenceType st;
2814   size_t num_elems;  // of type_codes, values, names (but not type_refs).
2815   const TypeCode *type_codes;     // num_elems count
2816   const TypeFunction *type_refs;  // less than num_elems entries (see TypeCode).
2817   const int64_t *values;  // Only set for non-consecutive enum/union or structs.
2818   const char *const *names;  // Only set if compiled with --reflect-names.
2819 };
2820 
2821 // String which identifies the current version of FlatBuffers.
2822 // flatbuffer_version_string is used by Google developers to identify which
2823 // applications uploaded to Google Play are using this library.  This allows
2824 // the development team at Google to determine the popularity of the library.
2825 // How it works: Applications that are uploaded to the Google Play Store are
2826 // scanned for this version string.  We track which applications are using it
2827 // to measure popularity.  You are free to remove it (of course) but we would
2828 // appreciate if you left it in.
2829 
2830 // Weak linkage is culled by VS & doesn't work on cygwin.
2831 // clang-format off
2832 #if !defined(_WIN32) && !defined(__CYGWIN__)
2833 
2834 extern volatile __attribute__((weak)) const char *flatbuffer_version_string;
2835 volatile __attribute__((weak)) const char *flatbuffer_version_string =
2836   "FlatBuffers "
2837   FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
2838   FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
2839   FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
2840 
2841 #endif  // !defined(_WIN32) && !defined(__CYGWIN__)
2842 
2843 #define FLATBUFFERS_DEFINE_BITMASK_OPERATORS(E, T)\
2844     inline E operator | (E lhs, E rhs){\
2845         return E(T(lhs) | T(rhs));\
2846     }\
2847     inline E operator & (E lhs, E rhs){\
2848         return E(T(lhs) & T(rhs));\
2849     }\
2850     inline E operator ^ (E lhs, E rhs){\
2851         return E(T(lhs) ^ T(rhs));\
2852     }\
2853     inline E operator ~ (E lhs){\
2854         return E(~T(lhs));\
2855     }\
2856     inline E operator |= (E &lhs, E rhs){\
2857         lhs = lhs | rhs;\
2858         return lhs;\
2859     }\
2860     inline E operator &= (E &lhs, E rhs){\
2861         lhs = lhs & rhs;\
2862         return lhs;\
2863     }\
2864     inline E operator ^= (E &lhs, E rhs){\
2865         lhs = lhs ^ rhs;\
2866         return lhs;\
2867     }\
2868     inline bool operator !(E rhs) \
2869     {\
2870         return !bool(T(rhs)); \
2871     }
2872 /// @endcond
2873 }  // namespace flatbuffers
2874 
2875 // clang-format on
2876 
2877 #endif  // FLATBUFFERS_H_
2878