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


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 18:04:04