583c90bba1a9bddfc6d8d18cdde924ff2e6dbea3
[platform/upstream/flatbuffers.git] / include / flatbuffers / flexbuffers.h
1 /*
2  * Copyright 2017 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_FLEXBUFFERS_H_
18 #define FLATBUFFERS_FLEXBUFFERS_H_
19
20 #include <map>
21 // Used to select STL variant.
22 #include "flatbuffers/base.h"
23 // We use the basic binary writing functions from the regular FlatBuffers.
24 #include "flatbuffers/util.h"
25
26 #ifdef _MSC_VER
27 #  include <intrin.h>
28 #endif
29
30 #if defined(_MSC_VER)
31 #  pragma warning(push)
32 #  pragma warning(disable : 4127)  // C4127: conditional expression is constant
33 #endif
34
35 namespace flexbuffers {
36
37 class Reference;
38 class Map;
39
40 // These are used in the lower 2 bits of a type field to determine the size of
41 // the elements (and or size field) of the item pointed to (e.g. vector).
42 enum BitWidth {
43   BIT_WIDTH_8 = 0,
44   BIT_WIDTH_16 = 1,
45   BIT_WIDTH_32 = 2,
46   BIT_WIDTH_64 = 3,
47 };
48
49 // These are used as the upper 6 bits of a type field to indicate the actual
50 // type.
51 enum Type {
52   FBT_NULL = 0,
53   FBT_INT = 1,
54   FBT_UINT = 2,
55   FBT_FLOAT = 3,
56   // Types above stored inline, types below store an offset.
57   FBT_KEY = 4,
58   FBT_STRING = 5,
59   FBT_INDIRECT_INT = 6,
60   FBT_INDIRECT_UINT = 7,
61   FBT_INDIRECT_FLOAT = 8,
62   FBT_MAP = 9,
63   FBT_VECTOR = 10,      // Untyped.
64   FBT_VECTOR_INT = 11,  // Typed any size (stores no type table).
65   FBT_VECTOR_UINT = 12,
66   FBT_VECTOR_FLOAT = 13,
67   FBT_VECTOR_KEY = 14,
68   FBT_VECTOR_STRING = 15,
69   FBT_VECTOR_INT2 = 16,  // Typed tuple (no type table, no size field).
70   FBT_VECTOR_UINT2 = 17,
71   FBT_VECTOR_FLOAT2 = 18,
72   FBT_VECTOR_INT3 = 19,  // Typed triple (no type table, no size field).
73   FBT_VECTOR_UINT3 = 20,
74   FBT_VECTOR_FLOAT3 = 21,
75   FBT_VECTOR_INT4 = 22,  // Typed quad (no type table, no size field).
76   FBT_VECTOR_UINT4 = 23,
77   FBT_VECTOR_FLOAT4 = 24,
78   FBT_BLOB = 25,
79   FBT_BOOL = 26,
80   FBT_VECTOR_BOOL =
81       36,  // To Allow the same type of conversion of type to vector type
82 };
83
84 inline bool IsInline(Type t) { return t <= FBT_FLOAT || t == FBT_BOOL; }
85
86 inline bool IsTypedVectorElementType(Type t) {
87   return (t >= FBT_INT && t <= FBT_STRING) || t == FBT_BOOL;
88 }
89
90 inline bool IsTypedVector(Type t) {
91   return (t >= FBT_VECTOR_INT && t <= FBT_VECTOR_STRING) ||
92          t == FBT_VECTOR_BOOL;
93 }
94
95 inline bool IsFixedTypedVector(Type t) {
96   return t >= FBT_VECTOR_INT2 && t <= FBT_VECTOR_FLOAT4;
97 }
98
99 inline Type ToTypedVector(Type t, size_t fixed_len = 0) {
100   FLATBUFFERS_ASSERT(IsTypedVectorElementType(t));
101   switch (fixed_len) {
102     case 0: return static_cast<Type>(t - FBT_INT + FBT_VECTOR_INT);
103     case 2: return static_cast<Type>(t - FBT_INT + FBT_VECTOR_INT2);
104     case 3: return static_cast<Type>(t - FBT_INT + FBT_VECTOR_INT3);
105     case 4: return static_cast<Type>(t - FBT_INT + FBT_VECTOR_INT4);
106     default: FLATBUFFERS_ASSERT(0); return FBT_NULL;
107   }
108 }
109
110 inline Type ToTypedVectorElementType(Type t) {
111   FLATBUFFERS_ASSERT(IsTypedVector(t));
112   return static_cast<Type>(t - FBT_VECTOR_INT + FBT_INT);
113 }
114
115 inline Type ToFixedTypedVectorElementType(Type t, uint8_t *len) {
116   FLATBUFFERS_ASSERT(IsFixedTypedVector(t));
117   auto fixed_type = t - FBT_VECTOR_INT2;
118   *len = static_cast<uint8_t>(fixed_type / 3 +
119                               2);  // 3 types each, starting from length 2.
120   return static_cast<Type>(fixed_type % 3 + FBT_INT);
121 }
122
123 // TODO: implement proper support for 8/16bit floats, or decide not to
124 // support them.
125 typedef int16_t half;
126 typedef int8_t quarter;
127
128 // TODO: can we do this without conditionals using intrinsics or inline asm
129 // on some platforms? Given branch prediction the method below should be
130 // decently quick, but it is the most frequently executed function.
131 // We could do an (unaligned) 64-bit read if we ifdef out the platforms for
132 // which that doesn't work (or where we'd read into un-owned memory).
133 template<typename R, typename T1, typename T2, typename T4, typename T8>
134 R ReadSizedScalar(const uint8_t *data, uint8_t byte_width) {
135   return byte_width < 4
136              ? (byte_width < 2
137                     ? static_cast<R>(flatbuffers::ReadScalar<T1>(data))
138                     : static_cast<R>(flatbuffers::ReadScalar<T2>(data)))
139              : (byte_width < 8
140                     ? static_cast<R>(flatbuffers::ReadScalar<T4>(data))
141                     : static_cast<R>(flatbuffers::ReadScalar<T8>(data)));
142 }
143
144 inline int64_t ReadInt64(const uint8_t *data, uint8_t byte_width) {
145   return ReadSizedScalar<int64_t, int8_t, int16_t, int32_t, int64_t>(
146       data, byte_width);
147 }
148
149 inline uint64_t ReadUInt64(const uint8_t *data, uint8_t byte_width) {
150   // This is the "hottest" function (all offset lookups use this), so worth
151   // optimizing if possible.
152   // TODO: GCC apparently replaces memcpy by a rep movsb, but only if count is a
153   // constant, which here it isn't. Test if memcpy is still faster than
154   // the conditionals in ReadSizedScalar. Can also use inline asm.
155   // clang-format off
156   #if defined(_MSC_VER) && (defined(_M_X64) || defined _M_IX86)
157     uint64_t u = 0;
158     __movsb(reinterpret_cast<uint8_t *>(&u),
159             reinterpret_cast<const uint8_t *>(data), byte_width);
160     return flatbuffers::EndianScalar(u);
161   #else
162     return ReadSizedScalar<uint64_t, uint8_t, uint16_t, uint32_t, uint64_t>(
163              data, byte_width);
164   #endif
165   // clang-format on
166 }
167
168 inline double ReadDouble(const uint8_t *data, uint8_t byte_width) {
169   return ReadSizedScalar<double, quarter, half, float, double>(data,
170                                                                byte_width);
171 }
172
173 inline const uint8_t *Indirect(const uint8_t *offset, uint8_t byte_width) {
174   return offset - ReadUInt64(offset, byte_width);
175 }
176
177 template<typename T> const uint8_t *Indirect(const uint8_t *offset) {
178   return offset - flatbuffers::ReadScalar<T>(offset);
179 }
180
181 inline BitWidth WidthU(uint64_t u) {
182 #define FLATBUFFERS_GET_FIELD_BIT_WIDTH(value, width)                   \
183   {                                                                     \
184     if (!((u) & ~((1ULL << (width)) - 1ULL))) return BIT_WIDTH_##width; \
185   }
186   FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 8);
187   FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 16);
188   FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 32);
189 #undef FLATBUFFERS_GET_FIELD_BIT_WIDTH
190   return BIT_WIDTH_64;
191 }
192
193 inline BitWidth WidthI(int64_t i) {
194   auto u = static_cast<uint64_t>(i) << 1;
195   return WidthU(i >= 0 ? u : ~u);
196 }
197
198 inline BitWidth WidthF(double f) {
199   return static_cast<double>(static_cast<float>(f)) == f ? BIT_WIDTH_32
200                                                          : BIT_WIDTH_64;
201 }
202
203 // Base class of all types below.
204 // Points into the data buffer and allows access to one type.
205 class Object {
206  public:
207   Object(const uint8_t *data, uint8_t byte_width)
208       : data_(data), byte_width_(byte_width) {}
209
210  protected:
211   const uint8_t *data_;
212   uint8_t byte_width_;
213 };
214
215 // Stores size in `byte_width_` bytes before data_ pointer.
216 class Sized : public Object {
217  public:
218   Sized(const uint8_t *data, uint8_t byte_width) : Object(data, byte_width) {}
219   size_t size() const {
220     return static_cast<size_t>(ReadUInt64(data_ - byte_width_, byte_width_));
221   }
222 };
223
224 class String : public Sized {
225  public:
226   String(const uint8_t *data, uint8_t byte_width) : Sized(data, byte_width) {}
227
228   size_t length() const { return size(); }
229   const char *c_str() const { return reinterpret_cast<const char *>(data_); }
230   std::string str() const { return std::string(c_str(), length()); }
231
232   static String EmptyString() {
233     static const uint8_t empty_string[] = { 0 /*len*/, 0 /*terminator*/ };
234     return String(empty_string + 1, 1);
235   }
236   bool IsTheEmptyString() const { return data_ == EmptyString().data_; }
237 };
238
239 class Blob : public Sized {
240  public:
241   Blob(const uint8_t *data_buf, uint8_t byte_width)
242       : Sized(data_buf, byte_width) {}
243
244   static Blob EmptyBlob() {
245     static const uint8_t empty_blob[] = { 0 /*len*/ };
246     return Blob(empty_blob + 1, 1);
247   }
248   bool IsTheEmptyBlob() const { return data_ == EmptyBlob().data_; }
249   const uint8_t *data() const { return data_; }
250 };
251
252 class Vector : public Sized {
253  public:
254   Vector(const uint8_t *data, uint8_t byte_width) : Sized(data, byte_width) {}
255
256   Reference operator[](size_t i) const;
257
258   static Vector EmptyVector() {
259     static const uint8_t empty_vector[] = { 0 /*len*/ };
260     return Vector(empty_vector + 1, 1);
261   }
262   bool IsTheEmptyVector() const { return data_ == EmptyVector().data_; }
263 };
264
265 class TypedVector : public Sized {
266  public:
267   TypedVector(const uint8_t *data, uint8_t byte_width, Type element_type)
268       : Sized(data, byte_width), type_(element_type) {}
269
270   Reference operator[](size_t i) const;
271
272   static TypedVector EmptyTypedVector() {
273     static const uint8_t empty_typed_vector[] = { 0 /*len*/ };
274     return TypedVector(empty_typed_vector + 1, 1, FBT_INT);
275   }
276   bool IsTheEmptyVector() const {
277     return data_ == TypedVector::EmptyTypedVector().data_;
278   }
279
280   Type ElementType() { return type_; }
281
282  private:
283   Type type_;
284
285   friend Map;
286 };
287
288 class FixedTypedVector : public Object {
289  public:
290   FixedTypedVector(const uint8_t *data, uint8_t byte_width, Type element_type,
291                    uint8_t len)
292       : Object(data, byte_width), type_(element_type), len_(len) {}
293
294   Reference operator[](size_t i) const;
295
296   static FixedTypedVector EmptyFixedTypedVector() {
297     static const uint8_t fixed_empty_vector[] = { 0 /* unused */ };
298     return FixedTypedVector(fixed_empty_vector, 1, FBT_INT, 0);
299   }
300   bool IsTheEmptyFixedTypedVector() const {
301     return data_ == FixedTypedVector::EmptyFixedTypedVector().data_;
302   }
303
304   Type ElementType() { return type_; }
305   uint8_t size() { return len_; }
306
307  private:
308   Type type_;
309   uint8_t len_;
310 };
311
312 class Map : public Vector {
313  public:
314   Map(const uint8_t *data, uint8_t byte_width) : Vector(data, byte_width) {}
315
316   Reference operator[](const char *key) const;
317   Reference operator[](const std::string &key) const;
318
319   Vector Values() const { return Vector(data_, byte_width_); }
320
321   TypedVector Keys() const {
322     const size_t num_prefixed_fields = 3;
323     auto keys_offset = data_ - byte_width_ * num_prefixed_fields;
324     return TypedVector(Indirect(keys_offset, byte_width_),
325                        static_cast<uint8_t>(
326                            ReadUInt64(keys_offset + byte_width_, byte_width_)),
327                        FBT_KEY);
328   }
329
330   static Map EmptyMap() {
331     static const uint8_t empty_map[] = {
332       0 /*keys_len*/, 0 /*keys_offset*/, 1 /*keys_width*/, 0 /*len*/
333     };
334     return Map(empty_map + 4, 1);
335   }
336
337   bool IsTheEmptyMap() const { return data_ == EmptyMap().data_; }
338 };
339
340 template<typename T>
341 void AppendToString(std::string &s, T &&v, bool keys_quoted) {
342     s += "[ ";
343     for (size_t i = 0; i < v.size(); i++) {
344       if (i) s += ", ";
345       v[i].ToString(true, keys_quoted, s);
346     }
347     s += " ]";
348 }
349
350 class Reference {
351  public:
352   Reference()
353       : data_(nullptr),
354         parent_width_(0),
355         byte_width_(BIT_WIDTH_8),
356         type_(FBT_NULL) {}
357
358   Reference(const uint8_t *data, uint8_t parent_width, uint8_t byte_width,
359             Type type)
360       : data_(data),
361         parent_width_(parent_width),
362         byte_width_(byte_width),
363         type_(type) {}
364
365   Reference(const uint8_t *data, uint8_t parent_width, uint8_t packed_type)
366       : data_(data), parent_width_(parent_width) {
367     byte_width_ = 1U << static_cast<BitWidth>(packed_type & 3);
368     type_ = static_cast<Type>(packed_type >> 2);
369   }
370
371   Type GetType() const { return type_; }
372
373   bool IsNull() const { return type_ == FBT_NULL; }
374   bool IsBool() const { return type_ == FBT_BOOL; }
375   bool IsInt() const { return type_ == FBT_INT || type_ == FBT_INDIRECT_INT; }
376   bool IsUInt() const {
377     return type_ == FBT_UINT || type_ == FBT_INDIRECT_UINT;
378   }
379   bool IsIntOrUint() const { return IsInt() || IsUInt(); }
380   bool IsFloat() const {
381     return type_ == FBT_FLOAT || type_ == FBT_INDIRECT_FLOAT;
382   }
383   bool IsNumeric() const { return IsIntOrUint() || IsFloat(); }
384   bool IsString() const { return type_ == FBT_STRING; }
385   bool IsKey() const { return type_ == FBT_KEY; }
386   bool IsVector() const { return type_ == FBT_VECTOR || type_ == FBT_MAP; }
387   bool IsUntypedVector() const { return type_ == FBT_VECTOR; }
388   bool IsTypedVector() const { return flexbuffers::IsTypedVector(type_); }
389   bool IsFixedTypedVector() const { return flexbuffers::IsFixedTypedVector(type_); }
390   bool IsAnyVector() const { return (IsTypedVector() || IsFixedTypedVector() || IsVector());}
391   bool IsMap() const { return type_ == FBT_MAP; }
392   bool IsBlob() const { return type_ == FBT_BLOB; }
393   bool AsBool() const {
394     return (type_ == FBT_BOOL ? ReadUInt64(data_, parent_width_)
395                                : AsUInt64()) != 0;
396   }
397
398   // Reads any type as a int64_t. Never fails, does most sensible conversion.
399   // Truncates floats, strings are attempted to be parsed for a number,
400   // vectors/maps return their size. Returns 0 if all else fails.
401   int64_t AsInt64() const {
402     if (type_ == FBT_INT) {
403       // A fast path for the common case.
404       return ReadInt64(data_, parent_width_);
405     } else
406       switch (type_) {
407         case FBT_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
408         case FBT_UINT: return ReadUInt64(data_, parent_width_);
409         case FBT_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
410         case FBT_FLOAT:
411           return static_cast<int64_t>(ReadDouble(data_, parent_width_));
412         case FBT_INDIRECT_FLOAT:
413           return static_cast<int64_t>(ReadDouble(Indirect(), byte_width_));
414         case FBT_NULL: return 0;
415         case FBT_STRING: return flatbuffers::StringToInt(AsString().c_str());
416         case FBT_VECTOR: return static_cast<int64_t>(AsVector().size());
417         case FBT_BOOL: return ReadInt64(data_, parent_width_);
418         default:
419           // Convert other things to int.
420           return 0;
421       }
422   }
423
424   // TODO: could specialize these to not use AsInt64() if that saves
425   // extension ops in generated code, and use a faster op than ReadInt64.
426   int32_t AsInt32() const { return static_cast<int32_t>(AsInt64()); }
427   int16_t AsInt16() const { return static_cast<int16_t>(AsInt64()); }
428   int8_t AsInt8() const { return static_cast<int8_t>(AsInt64()); }
429
430   uint64_t AsUInt64() const {
431     if (type_ == FBT_UINT) {
432       // A fast path for the common case.
433       return ReadUInt64(data_, parent_width_);
434     } else
435       switch (type_) {
436         case FBT_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
437         case FBT_INT: return ReadInt64(data_, parent_width_);
438         case FBT_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
439         case FBT_FLOAT:
440           return static_cast<uint64_t>(ReadDouble(data_, parent_width_));
441         case FBT_INDIRECT_FLOAT:
442           return static_cast<uint64_t>(ReadDouble(Indirect(), byte_width_));
443         case FBT_NULL: return 0;
444         case FBT_STRING: return flatbuffers::StringToUInt(AsString().c_str());
445         case FBT_VECTOR: return static_cast<uint64_t>(AsVector().size());
446         case FBT_BOOL: return ReadUInt64(data_, parent_width_);
447         default:
448           // Convert other things to uint.
449           return 0;
450       }
451   }
452
453   uint32_t AsUInt32() const { return static_cast<uint32_t>(AsUInt64()); }
454   uint16_t AsUInt16() const { return static_cast<uint16_t>(AsUInt64()); }
455   uint8_t AsUInt8() const { return static_cast<uint8_t>(AsUInt64()); }
456
457   double AsDouble() const {
458     if (type_ == FBT_FLOAT) {
459       // A fast path for the common case.
460       return ReadDouble(data_, parent_width_);
461     } else
462       switch (type_) {
463         case FBT_INDIRECT_FLOAT: return ReadDouble(Indirect(), byte_width_);
464         case FBT_INT:
465           return static_cast<double>(ReadInt64(data_, parent_width_));
466         case FBT_UINT:
467           return static_cast<double>(ReadUInt64(data_, parent_width_));
468         case FBT_INDIRECT_INT:
469           return static_cast<double>(ReadInt64(Indirect(), byte_width_));
470         case FBT_INDIRECT_UINT:
471           return static_cast<double>(ReadUInt64(Indirect(), byte_width_));
472         case FBT_NULL: return 0.0;
473         case FBT_STRING: return strtod(AsString().c_str(), nullptr);
474         case FBT_VECTOR: return static_cast<double>(AsVector().size());
475         case FBT_BOOL:
476           return static_cast<double>(ReadUInt64(data_, parent_width_));
477         default:
478           // Convert strings and other things to float.
479           return 0;
480       }
481   }
482
483   float AsFloat() const { return static_cast<float>(AsDouble()); }
484
485   const char *AsKey() const {
486     if (type_ == FBT_KEY) {
487       return reinterpret_cast<const char *>(Indirect());
488     } else {
489       return "";
490     }
491   }
492
493   // This function returns the empty string if you try to read a not-string.
494   String AsString() const {
495     if (type_ == FBT_STRING) {
496       return String(Indirect(), byte_width_);
497     } else {
498       return String::EmptyString();
499     }
500   }
501
502   // Unlike AsString(), this will convert any type to a std::string.
503   std::string ToString() const {
504     std::string s;
505     ToString(false, false, s);
506     return s;
507   }
508
509   // Convert any type to a JSON-like string. strings_quoted determines if
510   // string values at the top level receive "" quotes (inside other values
511   // they always do). keys_quoted determines if keys are quoted, at any level.
512   // TODO(wvo): add further options to have indentation/newlines.
513   void ToString(bool strings_quoted, bool keys_quoted, std::string &s) const {
514     if (type_ == FBT_STRING) {
515       String str(Indirect(), byte_width_);
516       if (strings_quoted) {
517         flatbuffers::EscapeString(str.c_str(), str.length(), &s, true, false);
518       } else {
519         s.append(str.c_str(), str.length());
520       }
521     } else if (IsKey()) {
522       auto str = AsKey();
523       if (keys_quoted) {
524         flatbuffers::EscapeString(str, strlen(str), &s, true, false);
525       } else {
526         s += str;
527       }
528     } else if (IsInt()) {
529       s += flatbuffers::NumToString(AsInt64());
530     } else if (IsUInt()) {
531       s += flatbuffers::NumToString(AsUInt64());
532     } else if (IsFloat()) {
533       s += flatbuffers::NumToString(AsDouble());
534     } else if (IsNull()) {
535       s += "null";
536     } else if (IsBool()) {
537       s += AsBool() ? "true" : "false";
538     } else if (IsMap()) {
539       s += "{ ";
540       auto m = AsMap();
541       auto keys = m.Keys();
542       auto vals = m.Values();
543       for (size_t i = 0; i < keys.size(); i++) {
544         keys[i].ToString(true, keys_quoted, s);
545         s += ": ";
546         vals[i].ToString(true, keys_quoted, s);
547         if (i < keys.size() - 1) s += ", ";
548       }
549       s += " }";
550     } else if (IsVector()) {
551       AppendToString<Vector>(s, AsVector(), keys_quoted);
552     } else if (IsTypedVector()) {
553       AppendToString<TypedVector>(s, AsTypedVector(), keys_quoted);
554     } else if (IsFixedTypedVector()) {
555       AppendToString<FixedTypedVector>(s, AsFixedTypedVector(), keys_quoted);
556     } else if (IsBlob()) {
557       auto blob = AsBlob();
558       flatbuffers::EscapeString(reinterpret_cast<const char*>(blob.data()), blob.size(), &s, true, false);
559     } else {
560       s += "(?)";
561     }
562   }
563
564   // This function returns the empty blob if you try to read a not-blob.
565   // Strings can be viewed as blobs too.
566   Blob AsBlob() const {
567     if (type_ == FBT_BLOB || type_ == FBT_STRING) {
568       return Blob(Indirect(), byte_width_);
569     } else {
570       return Blob::EmptyBlob();
571     }
572   }
573
574   // This function returns the empty vector if you try to read a not-vector.
575   // Maps can be viewed as vectors too.
576   Vector AsVector() const {
577     if (type_ == FBT_VECTOR || type_ == FBT_MAP) {
578       return Vector(Indirect(), byte_width_);
579     } else {
580       return Vector::EmptyVector();
581     }
582   }
583
584   TypedVector AsTypedVector() const {
585     if (IsTypedVector()) {
586       return TypedVector(Indirect(), byte_width_,
587                          ToTypedVectorElementType(type_));
588     } else {
589       return TypedVector::EmptyTypedVector();
590     }
591   }
592
593   FixedTypedVector AsFixedTypedVector() const {
594     if (IsFixedTypedVector()) {
595       uint8_t len = 0;
596       auto vtype = ToFixedTypedVectorElementType(type_, &len);
597       return FixedTypedVector(Indirect(), byte_width_, vtype, len);
598     } else {
599       return FixedTypedVector::EmptyFixedTypedVector();
600     }
601   }
602
603   Map AsMap() const {
604     if (type_ == FBT_MAP) {
605       return Map(Indirect(), byte_width_);
606     } else {
607       return Map::EmptyMap();
608     }
609   }
610
611   template<typename T> T As() const;
612
613   // Experimental: Mutation functions.
614   // These allow scalars in an already created buffer to be updated in-place.
615   // Since by default scalars are stored in the smallest possible space,
616   // the new value may not fit, in which case these functions return false.
617   // To avoid this, you can construct the values you intend to mutate using
618   // Builder::ForceMinimumBitWidth.
619   bool MutateInt(int64_t i) {
620     if (type_ == FBT_INT) {
621       return Mutate(data_, i, parent_width_, WidthI(i));
622     } else if (type_ == FBT_INDIRECT_INT) {
623       return Mutate(Indirect(), i, byte_width_, WidthI(i));
624     } else if (type_ == FBT_UINT) {
625       auto u = static_cast<uint64_t>(i);
626       return Mutate(data_, u, parent_width_, WidthU(u));
627     } else if (type_ == FBT_INDIRECT_UINT) {
628       auto u = static_cast<uint64_t>(i);
629       return Mutate(Indirect(), u, byte_width_, WidthU(u));
630     } else {
631       return false;
632     }
633   }
634
635   bool MutateBool(bool b) {
636     return type_ == FBT_BOOL && Mutate(data_, b, parent_width_, BIT_WIDTH_8);
637   }
638
639   bool MutateUInt(uint64_t u) {
640     if (type_ == FBT_UINT) {
641       return Mutate(data_, u, parent_width_, WidthU(u));
642     } else if (type_ == FBT_INDIRECT_UINT) {
643       return Mutate(Indirect(), u, byte_width_, WidthU(u));
644     } else if (type_ == FBT_INT) {
645       auto i = static_cast<int64_t>(u);
646       return Mutate(data_, i, parent_width_, WidthI(i));
647     } else if (type_ == FBT_INDIRECT_INT) {
648       auto i = static_cast<int64_t>(u);
649       return Mutate(Indirect(), i, byte_width_, WidthI(i));
650     } else {
651       return false;
652     }
653   }
654
655   bool MutateFloat(float f) {
656     if (type_ == FBT_FLOAT) {
657       return MutateF(data_, f, parent_width_, BIT_WIDTH_32);
658     } else if (type_ == FBT_INDIRECT_FLOAT) {
659       return MutateF(Indirect(), f, byte_width_, BIT_WIDTH_32);
660     } else {
661       return false;
662     }
663   }
664
665   bool MutateFloat(double d) {
666     if (type_ == FBT_FLOAT) {
667       return MutateF(data_, d, parent_width_, WidthF(d));
668     } else if (type_ == FBT_INDIRECT_FLOAT) {
669       return MutateF(Indirect(), d, byte_width_, WidthF(d));
670     } else {
671       return false;
672     }
673   }
674
675   bool MutateString(const char *str, size_t len) {
676     auto s = AsString();
677     if (s.IsTheEmptyString()) return false;
678     // This is very strict, could allow shorter strings, but that creates
679     // garbage.
680     if (s.length() != len) return false;
681     memcpy(const_cast<char *>(s.c_str()), str, len);
682     return true;
683   }
684   bool MutateString(const char *str) { return MutateString(str, strlen(str)); }
685   bool MutateString(const std::string &str) {
686     return MutateString(str.data(), str.length());
687   }
688
689  private:
690   const uint8_t *Indirect() const {
691     return flexbuffers::Indirect(data_, parent_width_);
692   }
693
694   template<typename T>
695   bool Mutate(const uint8_t *dest, T t, size_t byte_width,
696               BitWidth value_width) {
697     auto fits = static_cast<size_t>(static_cast<size_t>(1U) << value_width) <=
698                 byte_width;
699     if (fits) {
700       t = flatbuffers::EndianScalar(t);
701       memcpy(const_cast<uint8_t *>(dest), &t, byte_width);
702     }
703     return fits;
704   }
705
706   template<typename T>
707   bool MutateF(const uint8_t *dest, T t, size_t byte_width,
708                BitWidth value_width) {
709     if (byte_width == sizeof(double))
710       return Mutate(dest, static_cast<double>(t), byte_width, value_width);
711     if (byte_width == sizeof(float))
712       return Mutate(dest, static_cast<float>(t), byte_width, value_width);
713     FLATBUFFERS_ASSERT(false);
714     return false;
715   }
716
717   const uint8_t *data_;
718   uint8_t parent_width_;
719   uint8_t byte_width_;
720   Type type_;
721 };
722
723 // Template specialization for As().
724 template<> inline bool Reference::As<bool>() const { return AsBool(); }
725
726 template<> inline int8_t Reference::As<int8_t>() const { return AsInt8(); }
727 template<> inline int16_t Reference::As<int16_t>() const { return AsInt16(); }
728 template<> inline int32_t Reference::As<int32_t>() const { return AsInt32(); }
729 template<> inline int64_t Reference::As<int64_t>() const { return AsInt64(); }
730
731 template<> inline uint8_t Reference::As<uint8_t>() const { return AsUInt8(); }
732 template<> inline uint16_t Reference::As<uint16_t>() const { return AsUInt16(); }
733 template<> inline uint32_t Reference::As<uint32_t>() const { return AsUInt32(); }
734 template<> inline uint64_t Reference::As<uint64_t>() const { return AsUInt64(); }
735
736 template<> inline double Reference::As<double>() const { return AsDouble(); }
737 template<> inline float Reference::As<float>() const { return AsFloat(); }
738
739 template<> inline String Reference::As<String>() const { return AsString(); }
740 template<> inline std::string Reference::As<std::string>() const {
741   return AsString().str();
742 }
743
744 template<> inline Blob Reference::As<Blob>() const { return AsBlob(); }
745 template<> inline Vector Reference::As<Vector>() const { return AsVector(); }
746 template<> inline TypedVector Reference::As<TypedVector>() const {
747   return AsTypedVector();
748 }
749 template<> inline FixedTypedVector Reference::As<FixedTypedVector>() const {
750   return AsFixedTypedVector();
751 }
752 template<> inline Map Reference::As<Map>() const { return AsMap(); }
753
754 inline uint8_t PackedType(BitWidth bit_width, Type type) {
755   return static_cast<uint8_t>(bit_width | (type << 2));
756 }
757
758 inline uint8_t NullPackedType() { return PackedType(BIT_WIDTH_8, FBT_NULL); }
759
760 // Vector accessors.
761 // Note: if you try to access outside of bounds, you get a Null value back
762 // instead. Normally this would be an assert, but since this is "dynamically
763 // typed" data, you may not want that (someone sends you a 2d vector and you
764 // wanted 3d).
765 // The Null converts seamlessly into a default value for any other type.
766 // TODO(wvo): Could introduce an #ifdef that makes this into an assert?
767 inline Reference Vector::operator[](size_t i) const {
768   auto len = size();
769   if (i >= len) return Reference(nullptr, 1, NullPackedType());
770   auto packed_type = (data_ + len * byte_width_)[i];
771   auto elem = data_ + i * byte_width_;
772   return Reference(elem, byte_width_, packed_type);
773 }
774
775 inline Reference TypedVector::operator[](size_t i) const {
776   auto len = size();
777   if (i >= len) return Reference(nullptr, 1, NullPackedType());
778   auto elem = data_ + i * byte_width_;
779   return Reference(elem, byte_width_, 1, type_);
780 }
781
782 inline Reference FixedTypedVector::operator[](size_t i) const {
783   if (i >= len_) return Reference(nullptr, 1, NullPackedType());
784   auto elem = data_ + i * byte_width_;
785   return Reference(elem, byte_width_, 1, type_);
786 }
787
788 template<typename T> int KeyCompare(const void *key, const void *elem) {
789   auto str_elem = reinterpret_cast<const char *>(
790       Indirect<T>(reinterpret_cast<const uint8_t *>(elem)));
791   auto skey = reinterpret_cast<const char *>(key);
792   return strcmp(skey, str_elem);
793 }
794
795 inline Reference Map::operator[](const char *key) const {
796   auto keys = Keys();
797   // We can't pass keys.byte_width_ to the comparison function, so we have
798   // to pick the right one ahead of time.
799   int (*comp)(const void *, const void *) = nullptr;
800   switch (keys.byte_width_) {
801     case 1: comp = KeyCompare<uint8_t>; break;
802     case 2: comp = KeyCompare<uint16_t>; break;
803     case 4: comp = KeyCompare<uint32_t>; break;
804     case 8: comp = KeyCompare<uint64_t>; break;
805   }
806   auto res = std::bsearch(key, keys.data_, keys.size(), keys.byte_width_, comp);
807   if (!res) return Reference(nullptr, 1, NullPackedType());
808   auto i = (reinterpret_cast<uint8_t *>(res) - keys.data_) / keys.byte_width_;
809   return (*static_cast<const Vector *>(this))[i];
810 }
811
812 inline Reference Map::operator[](const std::string &key) const {
813   return (*this)[key.c_str()];
814 }
815
816 inline Reference GetRoot(const uint8_t *buffer, size_t size) {
817   // See Finish() below for the serialization counterpart of this.
818   // The root starts at the end of the buffer, so we parse backwards from there.
819   auto end = buffer + size;
820   auto byte_width = *--end;
821   auto packed_type = *--end;
822   end -= byte_width;  // The root data item.
823   return Reference(end, byte_width, packed_type);
824 }
825
826 inline Reference GetRoot(const std::vector<uint8_t> &buffer) {
827   return GetRoot(flatbuffers::vector_data(buffer), buffer.size());
828 }
829
830 // Flags that configure how the Builder behaves.
831 // The "Share" flags determine if the Builder automatically tries to pool
832 // this type. Pooling can reduce the size of serialized data if there are
833 // multiple maps of the same kind, at the expense of slightly slower
834 // serialization (the cost of lookups) and more memory use (std::set).
835 // By default this is on for keys, but off for strings.
836 // Turn keys off if you have e.g. only one map.
837 // Turn strings on if you expect many non-unique string values.
838 // Additionally, sharing key vectors can save space if you have maps with
839 // identical field populations.
840 enum BuilderFlag {
841   BUILDER_FLAG_NONE = 0,
842   BUILDER_FLAG_SHARE_KEYS = 1,
843   BUILDER_FLAG_SHARE_STRINGS = 2,
844   BUILDER_FLAG_SHARE_KEYS_AND_STRINGS = 3,
845   BUILDER_FLAG_SHARE_KEY_VECTORS = 4,
846   BUILDER_FLAG_SHARE_ALL = 7,
847 };
848
849 class Builder FLATBUFFERS_FINAL_CLASS {
850  public:
851   Builder(size_t initial_size = 256,
852           BuilderFlag flags = BUILDER_FLAG_SHARE_KEYS)
853       : buf_(initial_size),
854         finished_(false),
855         flags_(flags),
856         force_min_bit_width_(BIT_WIDTH_8),
857         key_pool(KeyOffsetCompare(buf_)),
858         string_pool(StringOffsetCompare(buf_)) {
859     buf_.clear();
860   }
861
862   /// @brief Get the serialized buffer (after you call `Finish()`).
863   /// @return Returns a vector owned by this class.
864   const std::vector<uint8_t> &GetBuffer() const {
865     Finished();
866     return buf_;
867   }
868
869   // Size of the buffer. Does not include unfinished values.
870   size_t GetSize() const { return buf_.size(); }
871
872   // Reset all state so we can re-use the buffer.
873   void Clear() {
874     buf_.clear();
875     stack_.clear();
876     finished_ = false;
877     // flags_ remains as-is;
878     force_min_bit_width_ = BIT_WIDTH_8;
879     key_pool.clear();
880     string_pool.clear();
881   }
882
883   // All value constructing functions below have two versions: one that
884   // takes a key (for placement inside a map) and one that doesn't (for inside
885   // vectors and elsewhere).
886
887   void Null() { stack_.push_back(Value()); }
888   void Null(const char *key) {
889     Key(key);
890     Null();
891   }
892
893   void Int(int64_t i) { stack_.push_back(Value(i, FBT_INT, WidthI(i))); }
894   void Int(const char *key, int64_t i) {
895     Key(key);
896     Int(i);
897   }
898
899   void UInt(uint64_t u) { stack_.push_back(Value(u, FBT_UINT, WidthU(u))); }
900   void UInt(const char *key, uint64_t u) {
901     Key(key);
902     UInt(u);
903   }
904
905   void Float(float f) { stack_.push_back(Value(f)); }
906   void Float(const char *key, float f) {
907     Key(key);
908     Float(f);
909   }
910
911   void Double(double f) { stack_.push_back(Value(f)); }
912   void Double(const char *key, double d) {
913     Key(key);
914     Double(d);
915   }
916
917   void Bool(bool b) { stack_.push_back(Value(b)); }
918   void Bool(const char *key, bool b) {
919     Key(key);
920     Bool(b);
921   }
922
923   void IndirectInt(int64_t i) {
924     PushIndirect(i, FBT_INDIRECT_INT, WidthI(i));
925   }
926   void IndirectInt(const char *key, int64_t i) {
927     Key(key);
928     IndirectInt(i);
929   }
930
931   void IndirectUInt(uint64_t u) {
932     PushIndirect(u, FBT_INDIRECT_UINT, WidthU(u));
933   }
934   void IndirectUInt(const char *key, uint64_t u) {
935     Key(key);
936     IndirectUInt(u);
937   }
938
939   void IndirectFloat(float f) {
940     PushIndirect(f, FBT_INDIRECT_FLOAT, BIT_WIDTH_32);
941   }
942   void IndirectFloat(const char *key, float f) {
943     Key(key);
944     IndirectFloat(f);
945   }
946
947   void IndirectDouble(double f) {
948     PushIndirect(f, FBT_INDIRECT_FLOAT, WidthF(f));
949   }
950   void IndirectDouble(const char *key, double d) {
951     Key(key);
952     IndirectDouble(d);
953   }
954
955   size_t Key(const char *str, size_t len) {
956     auto sloc = buf_.size();
957     WriteBytes(str, len + 1);
958     if (flags_ & BUILDER_FLAG_SHARE_KEYS) {
959       auto it = key_pool.find(sloc);
960       if (it != key_pool.end()) {
961         // Already in the buffer. Remove key we just serialized, and use
962         // existing offset instead.
963         buf_.resize(sloc);
964         sloc = *it;
965       } else {
966         key_pool.insert(sloc);
967       }
968     }
969     stack_.push_back(Value(static_cast<uint64_t>(sloc), FBT_KEY, BIT_WIDTH_8));
970     return sloc;
971   }
972
973   size_t Key(const char *str) { return Key(str, strlen(str)); }
974   size_t Key(const std::string &str) { return Key(str.c_str(), str.size()); }
975
976   size_t String(const char *str, size_t len) {
977     auto reset_to = buf_.size();
978     auto sloc = CreateBlob(str, len, 1, FBT_STRING);
979     if (flags_ & BUILDER_FLAG_SHARE_STRINGS) {
980       StringOffset so(sloc, len);
981       auto it = string_pool.find(so);
982       if (it != string_pool.end()) {
983         // Already in the buffer. Remove string we just serialized, and use
984         // existing offset instead.
985         buf_.resize(reset_to);
986         sloc = it->first;
987         stack_.back().u_ = sloc;
988       } else {
989         string_pool.insert(so);
990       }
991     }
992     return sloc;
993   }
994   size_t String(const char *str) { return String(str, strlen(str)); }
995   size_t String(const std::string &str) {
996     return String(str.c_str(), str.size());
997   }
998   void String(const flexbuffers::String &str) {
999     String(str.c_str(), str.length());
1000   }
1001
1002   void String(const char *key, const char *str) {
1003     Key(key);
1004     String(str);
1005   }
1006   void String(const char *key, const std::string &str) {
1007     Key(key);
1008     String(str);
1009   }
1010   void String(const char *key, const flexbuffers::String &str) {
1011     Key(key);
1012     String(str);
1013   }
1014
1015   size_t Blob(const void *data, size_t len) {
1016     return CreateBlob(data, len, 0, FBT_BLOB);
1017   }
1018   size_t Blob(const std::vector<uint8_t> &v) {
1019     return CreateBlob(flatbuffers::vector_data(v), v.size(), 0, FBT_BLOB);
1020   }
1021
1022   // TODO(wvo): support all the FlexBuffer types (like flexbuffers::String),
1023   // e.g. Vector etc. Also in overloaded versions.
1024   // Also some FlatBuffers types?
1025
1026   size_t StartVector() { return stack_.size(); }
1027   size_t StartVector(const char *key) {
1028     Key(key);
1029     return stack_.size();
1030   }
1031   size_t StartMap() { return stack_.size(); }
1032   size_t StartMap(const char *key) {
1033     Key(key);
1034     return stack_.size();
1035   }
1036
1037   // TODO(wvo): allow this to specify an aligment greater than the natural
1038   // alignment.
1039   size_t EndVector(size_t start, bool typed, bool fixed) {
1040     auto vec = CreateVector(start, stack_.size() - start, 1, typed, fixed);
1041     // Remove temp elements and return vector.
1042     stack_.resize(start);
1043     stack_.push_back(vec);
1044     return static_cast<size_t>(vec.u_);
1045   }
1046
1047   size_t EndMap(size_t start) {
1048     // We should have interleaved keys and values on the stack.
1049     // Make sure it is an even number:
1050     auto len = stack_.size() - start;
1051     FLATBUFFERS_ASSERT(!(len & 1));
1052     len /= 2;
1053     // Make sure keys are all strings:
1054     for (auto key = start; key < stack_.size(); key += 2) {
1055       FLATBUFFERS_ASSERT(stack_[key].type_ == FBT_KEY);
1056     }
1057     // Now sort values, so later we can do a binary search lookup.
1058     // We want to sort 2 array elements at a time.
1059     struct TwoValue {
1060       Value key;
1061       Value val;
1062     };
1063     // TODO(wvo): strict aliasing?
1064     // TODO(wvo): allow the caller to indicate the data is already sorted
1065     // for maximum efficiency? With an assert to check sortedness to make sure
1066     // we're not breaking binary search.
1067     // Or, we can track if the map is sorted as keys are added which would be
1068     // be quite cheap (cheaper than checking it here), so we can skip this
1069     // step automatically when appliccable, and encourage people to write in
1070     // sorted fashion.
1071     // std::sort is typically already a lot faster on sorted data though.
1072     auto dict =
1073         reinterpret_cast<TwoValue *>(flatbuffers::vector_data(stack_) + start);
1074     std::sort(dict, dict + len,
1075               [&](const TwoValue &a, const TwoValue &b) -> bool {
1076                 auto as = reinterpret_cast<const char *>(
1077                     flatbuffers::vector_data(buf_) + a.key.u_);
1078                 auto bs = reinterpret_cast<const char *>(
1079                     flatbuffers::vector_data(buf_) + b.key.u_);
1080                 auto comp = strcmp(as, bs);
1081                 // If this assertion hits, you've added two keys with the same
1082                 // value to this map.
1083                 // TODO: Have to check for pointer equality, as some sort
1084                 // implementation apparently call this function with the same
1085                 // element?? Why?
1086                 FLATBUFFERS_ASSERT(comp || &a == &b);
1087                 return comp < 0;
1088               });
1089     // First create a vector out of all keys.
1090     // TODO(wvo): if kBuilderFlagShareKeyVectors is true, see if we can share
1091     // the first vector.
1092     auto keys = CreateVector(start, len, 2, true, false);
1093     auto vec = CreateVector(start + 1, len, 2, false, false, &keys);
1094     // Remove temp elements and return map.
1095     stack_.resize(start);
1096     stack_.push_back(vec);
1097     return static_cast<size_t>(vec.u_);
1098   }
1099
1100   template<typename F> size_t Vector(F f) {
1101     auto start = StartVector();
1102     f();
1103     return EndVector(start, false, false);
1104   }
1105   template<typename F, typename T> size_t Vector(F f, T &state) {
1106     auto start = StartVector();
1107     f(state);
1108     return EndVector(start, false, false);
1109   }
1110   template<typename F> size_t Vector(const char *key, F f) {
1111     auto start = StartVector(key);
1112     f();
1113     return EndVector(start, false, false);
1114   }
1115   template<typename F, typename T>
1116   size_t Vector(const char *key, F f, T &state) {
1117     auto start = StartVector(key);
1118     f(state);
1119     return EndVector(start, false, false);
1120   }
1121
1122   template<typename T> void Vector(const T *elems, size_t len) {
1123     if (flatbuffers::is_scalar<T>::value) {
1124       // This path should be a lot quicker and use less space.
1125       ScalarVector(elems, len, false);
1126     } else {
1127       auto start = StartVector();
1128       for (size_t i = 0; i < len; i++) Add(elems[i]);
1129       EndVector(start, false, false);
1130     }
1131   }
1132   template<typename T>
1133   void Vector(const char *key, const T *elems, size_t len) {
1134     Key(key);
1135     Vector(elems, len);
1136   }
1137   template<typename T> void Vector(const std::vector<T> &vec) {
1138     Vector(flatbuffers::vector_data(vec), vec.size());
1139   }
1140
1141   template<typename F> size_t TypedVector(F f) {
1142     auto start = StartVector();
1143     f();
1144     return EndVector(start, true, false);
1145   }
1146   template<typename F, typename T> size_t TypedVector(F f, T &state) {
1147     auto start = StartVector();
1148     f(state);
1149     return EndVector(start, true, false);
1150   }
1151   template<typename F> size_t TypedVector(const char *key, F f) {
1152     auto start = StartVector(key);
1153     f();
1154     return EndVector(start, true, false);
1155   }
1156   template<typename F, typename T>
1157   size_t TypedVector(const char *key, F f, T &state) {
1158     auto start = StartVector(key);
1159     f(state);
1160     return EndVector(start, true, false);
1161   }
1162
1163   template<typename T> size_t FixedTypedVector(const T *elems, size_t len) {
1164     // We only support a few fixed vector lengths. Anything bigger use a
1165     // regular typed vector.
1166     FLATBUFFERS_ASSERT(len >= 2 && len <= 4);
1167     // And only scalar values.
1168     static_assert(flatbuffers::is_scalar<T>::value, "Unrelated types");
1169     return ScalarVector(elems, len, true);
1170   }
1171
1172   template<typename T>
1173   size_t FixedTypedVector(const char *key, const T *elems, size_t len) {
1174     Key(key);
1175     return FixedTypedVector(elems, len);
1176   }
1177
1178   template<typename F> size_t Map(F f) {
1179     auto start = StartMap();
1180     f();
1181     return EndMap(start);
1182   }
1183   template<typename F, typename T> size_t Map(F f, T &state) {
1184     auto start = StartMap();
1185     f(state);
1186     return EndMap(start);
1187   }
1188   template<typename F> size_t Map(const char *key, F f) {
1189     auto start = StartMap(key);
1190     f();
1191     return EndMap(start);
1192   }
1193   template<typename F, typename T> size_t Map(const char *key, F f, T &state) {
1194     auto start = StartMap(key);
1195     f(state);
1196     return EndMap(start);
1197   }
1198   template<typename T> void Map(const std::map<std::string, T> &map) {
1199     auto start = StartMap();
1200     for (auto it = map.begin(); it != map.end(); ++it)
1201       Add(it->first.c_str(), it->second);
1202     EndMap(start);
1203   }
1204
1205   // If you wish to share a value explicitly (a value not shared automatically
1206   // through one of the BUILDER_FLAG_SHARE_* flags) you can do so with these
1207   // functions. Or if you wish to turn those flags off for performance reasons
1208   // and still do some explicit sharing. For example:
1209   // builder.IndirectDouble(M_PI);
1210   // auto id = builder.LastValue();  // Remember where we stored it.
1211   // .. more code goes here ..
1212   // builder.ReuseValue(id);  // Refers to same double by offset.
1213   // LastValue works regardless of wether the value has a key or not.
1214   // Works on any data type.
1215   struct Value;
1216   Value LastValue() { return stack_.back(); }
1217   void ReuseValue(Value v) {
1218     stack_.push_back(v);
1219   }
1220   void ReuseValue(const char *key, Value v) {
1221     Key(key);
1222     ReuseValue(v);
1223   }
1224
1225   // Overloaded Add that tries to call the correct function above.
1226   void Add(int8_t i) { Int(i); }
1227   void Add(int16_t i) { Int(i); }
1228   void Add(int32_t i) { Int(i); }
1229   void Add(int64_t i) { Int(i); }
1230   void Add(uint8_t u) { UInt(u); }
1231   void Add(uint16_t u) { UInt(u); }
1232   void Add(uint32_t u) { UInt(u); }
1233   void Add(uint64_t u) { UInt(u); }
1234   void Add(float f) { Float(f); }
1235   void Add(double d) { Double(d); }
1236   void Add(bool b) { Bool(b); }
1237   void Add(const char *str) { String(str); }
1238   void Add(const std::string &str) { String(str); }
1239   void Add(const flexbuffers::String &str) { String(str); }
1240
1241   template<typename T> void Add(const std::vector<T> &vec) { Vector(vec); }
1242
1243   template<typename T> void Add(const char *key, const T &t) {
1244     Key(key);
1245     Add(t);
1246   }
1247
1248   template<typename T> void Add(const std::map<std::string, T> &map) {
1249     Map(map);
1250   }
1251
1252   template<typename T> void operator+=(const T &t) { Add(t); }
1253
1254   // This function is useful in combination with the Mutate* functions above.
1255   // It forces elements of vectors and maps to have a minimum size, such that
1256   // they can later be updated without failing.
1257   // Call with no arguments to reset.
1258   void ForceMinimumBitWidth(BitWidth bw = BIT_WIDTH_8) {
1259     force_min_bit_width_ = bw;
1260   }
1261
1262   void Finish() {
1263     // If you hit this assert, you likely have objects that were never included
1264     // in a parent. You need to have exactly one root to finish a buffer.
1265     // Check your Start/End calls are matched, and all objects are inside
1266     // some other object.
1267     FLATBUFFERS_ASSERT(stack_.size() == 1);
1268
1269     // Write root value.
1270     auto byte_width = Align(stack_[0].ElemWidth(buf_.size(), 0));
1271     WriteAny(stack_[0], byte_width);
1272     // Write root type.
1273     Write(stack_[0].StoredPackedType(), 1);
1274     // Write root size. Normally determined by parent, but root has no parent :)
1275     Write(byte_width, 1);
1276
1277     finished_ = true;
1278   }
1279
1280  private:
1281   void Finished() const {
1282     // If you get this assert, you're attempting to get access a buffer
1283     // which hasn't been finished yet. Be sure to call
1284     // Builder::Finish with your root object.
1285     FLATBUFFERS_ASSERT(finished_);
1286   }
1287
1288   // Align to prepare for writing a scalar with a certain size.
1289   uint8_t Align(BitWidth alignment) {
1290     auto byte_width = 1U << alignment;
1291     buf_.insert(buf_.end(), flatbuffers::PaddingBytes(buf_.size(), byte_width),
1292                 0);
1293     return static_cast<uint8_t>(byte_width);
1294   }
1295
1296   void WriteBytes(const void *val, size_t size) {
1297     buf_.insert(buf_.end(), reinterpret_cast<const uint8_t *>(val),
1298                 reinterpret_cast<const uint8_t *>(val) + size);
1299   }
1300
1301   template<typename T> void Write(T val, size_t byte_width) {
1302     FLATBUFFERS_ASSERT(sizeof(T) >= byte_width);
1303     val = flatbuffers::EndianScalar(val);
1304     WriteBytes(&val, byte_width);
1305   }
1306
1307   void WriteDouble(double f, uint8_t byte_width) {
1308     switch (byte_width) {
1309       case 8: Write(f, byte_width); break;
1310       case 4: Write(static_cast<float>(f), byte_width); break;
1311       // case 2: Write(static_cast<half>(f), byte_width); break;
1312       // case 1: Write(static_cast<quarter>(f), byte_width); break;
1313       default: FLATBUFFERS_ASSERT(0);
1314     }
1315   }
1316
1317   void WriteOffset(uint64_t o, uint8_t byte_width) {
1318     auto reloff = buf_.size() - o;
1319     FLATBUFFERS_ASSERT(byte_width == 8 || reloff < 1ULL << (byte_width * 8));
1320     Write(reloff, byte_width);
1321   }
1322
1323   template<typename T> void PushIndirect(T val, Type type, BitWidth bit_width) {
1324     auto byte_width = Align(bit_width);
1325     auto iloc = buf_.size();
1326     Write(val, byte_width);
1327     stack_.push_back(Value(static_cast<uint64_t>(iloc), type, bit_width));
1328   }
1329
1330   static BitWidth WidthB(size_t byte_width) {
1331     switch (byte_width) {
1332       case 1: return BIT_WIDTH_8;
1333       case 2: return BIT_WIDTH_16;
1334       case 4: return BIT_WIDTH_32;
1335       case 8: return BIT_WIDTH_64;
1336       default: FLATBUFFERS_ASSERT(false); return BIT_WIDTH_64;
1337     }
1338   }
1339
1340   template<typename T> static Type GetScalarType() {
1341     static_assert(flatbuffers::is_scalar<T>::value, "Unrelated types");
1342     return flatbuffers::is_floating_point<T>::value
1343                ? FBT_FLOAT
1344                : flatbuffers::is_same<T, bool>::value
1345                      ? FBT_BOOL
1346                      : (flatbuffers::is_unsigned<T>::value ? FBT_UINT
1347                                                            : FBT_INT);
1348   }
1349
1350  public:
1351   // This was really intended to be private, except for LastValue/ReuseValue.
1352   struct Value {
1353     union {
1354       int64_t i_;
1355       uint64_t u_;
1356       double f_;
1357     };
1358
1359     Type type_;
1360
1361     // For scalars: of itself, for vector: of its elements, for string: length.
1362     BitWidth min_bit_width_;
1363
1364     Value() : i_(0), type_(FBT_NULL), min_bit_width_(BIT_WIDTH_8) {}
1365
1366     Value(bool b)
1367         : u_(static_cast<uint64_t>(b)),
1368           type_(FBT_BOOL),
1369           min_bit_width_(BIT_WIDTH_8) {}
1370
1371     Value(int64_t i, Type t, BitWidth bw)
1372         : i_(i), type_(t), min_bit_width_(bw) {}
1373     Value(uint64_t u, Type t, BitWidth bw)
1374         : u_(u), type_(t), min_bit_width_(bw) {}
1375
1376     Value(float f) : f_(f), type_(FBT_FLOAT), min_bit_width_(BIT_WIDTH_32) {}
1377     Value(double f) : f_(f), type_(FBT_FLOAT), min_bit_width_(WidthF(f)) {}
1378
1379     uint8_t StoredPackedType(BitWidth parent_bit_width_ = BIT_WIDTH_8) const {
1380       return PackedType(StoredWidth(parent_bit_width_), type_);
1381     }
1382
1383     BitWidth ElemWidth(size_t buf_size, size_t elem_index) const {
1384       if (IsInline(type_)) {
1385         return min_bit_width_;
1386       } else {
1387         // We have an absolute offset, but want to store a relative offset
1388         // elem_index elements beyond the current buffer end. Since whether
1389         // the relative offset fits in a certain byte_width depends on
1390         // the size of the elements before it (and their alignment), we have
1391         // to test for each size in turn.
1392         for (size_t byte_width = 1;
1393              byte_width <= sizeof(flatbuffers::largest_scalar_t);
1394              byte_width *= 2) {
1395           // Where are we going to write this offset?
1396           auto offset_loc = buf_size +
1397                             flatbuffers::PaddingBytes(buf_size, byte_width) +
1398                             elem_index * byte_width;
1399           // Compute relative offset.
1400           auto offset = offset_loc - u_;
1401           // Does it fit?
1402           auto bit_width = WidthU(offset);
1403           if (static_cast<size_t>(static_cast<size_t>(1U) << bit_width) ==
1404               byte_width)
1405             return bit_width;
1406         }
1407         FLATBUFFERS_ASSERT(false);  // Must match one of the sizes above.
1408         return BIT_WIDTH_64;
1409       }
1410     }
1411
1412     BitWidth StoredWidth(BitWidth parent_bit_width_ = BIT_WIDTH_8) const {
1413       if (IsInline(type_)) {
1414         return (std::max)(min_bit_width_, parent_bit_width_);
1415       } else {
1416         return min_bit_width_;
1417       }
1418     }
1419   };
1420
1421  private:
1422   void WriteAny(const Value &val, uint8_t byte_width) {
1423     switch (val.type_) {
1424       case FBT_NULL:
1425       case FBT_INT: Write(val.i_, byte_width); break;
1426       case FBT_BOOL:
1427       case FBT_UINT: Write(val.u_, byte_width); break;
1428       case FBT_FLOAT: WriteDouble(val.f_, byte_width); break;
1429       default: WriteOffset(val.u_, byte_width); break;
1430     }
1431   }
1432
1433   size_t CreateBlob(const void *data, size_t len, size_t trailing, Type type) {
1434     auto bit_width = WidthU(len);
1435     auto byte_width = Align(bit_width);
1436     Write<uint64_t>(len, byte_width);
1437     auto sloc = buf_.size();
1438     WriteBytes(data, len + trailing);
1439     stack_.push_back(Value(static_cast<uint64_t>(sloc), type, bit_width));
1440     return sloc;
1441   }
1442
1443   template<typename T>
1444   size_t ScalarVector(const T *elems, size_t len, bool fixed) {
1445     auto vector_type = GetScalarType<T>();
1446     auto byte_width = sizeof(T);
1447     auto bit_width = WidthB(byte_width);
1448     // If you get this assert, you're trying to write a vector with a size
1449     // field that is bigger than the scalars you're trying to write (e.g. a
1450     // byte vector > 255 elements). For such types, write a "blob" instead.
1451     // TODO: instead of asserting, could write vector with larger elements
1452     // instead, though that would be wasteful.
1453     FLATBUFFERS_ASSERT(WidthU(len) <= bit_width);
1454     if (!fixed) Write<uint64_t>(len, byte_width);
1455     auto vloc = buf_.size();
1456     for (size_t i = 0; i < len; i++) Write(elems[i], byte_width);
1457     stack_.push_back(Value(static_cast<uint64_t>(vloc),
1458                            ToTypedVector(vector_type, fixed ? len : 0),
1459                            bit_width));
1460     return vloc;
1461   }
1462
1463   Value CreateVector(size_t start, size_t vec_len, size_t step, bool typed,
1464                      bool fixed, const Value *keys = nullptr) {
1465     FLATBUFFERS_ASSERT(!fixed || typed); // typed=false, fixed=true combination is not supported.
1466     // Figure out smallest bit width we can store this vector with.
1467     auto bit_width = (std::max)(force_min_bit_width_, WidthU(vec_len));
1468     auto prefix_elems = 1;
1469     if (keys) {
1470       // If this vector is part of a map, we will pre-fix an offset to the keys
1471       // to this vector.
1472       bit_width = (std::max)(bit_width, keys->ElemWidth(buf_.size(), 0));
1473       prefix_elems += 2;
1474     }
1475     Type vector_type = FBT_KEY;
1476     // Check bit widths and types for all elements.
1477     for (size_t i = start; i < stack_.size(); i += step) {
1478       auto elem_width = stack_[i].ElemWidth(buf_.size(), i + prefix_elems);
1479       bit_width = (std::max)(bit_width, elem_width);
1480       if (typed) {
1481         if (i == start) {
1482           vector_type = stack_[i].type_;
1483         } else {
1484           // If you get this assert, you are writing a typed vector with
1485           // elements that are not all the same type.
1486           FLATBUFFERS_ASSERT(vector_type == stack_[i].type_);
1487         }
1488       }
1489     }
1490     // If you get this assert, your fixed types are not one of:
1491     // Int / UInt / Float / Key.
1492     FLATBUFFERS_ASSERT(!fixed || IsTypedVectorElementType(vector_type));
1493     auto byte_width = Align(bit_width);
1494     // Write vector. First the keys width/offset if available, and size.
1495     if (keys) {
1496       WriteOffset(keys->u_, byte_width);
1497       Write<uint64_t>(1ULL << keys->min_bit_width_, byte_width);
1498     }
1499     if (!fixed) Write<uint64_t>(vec_len, byte_width);
1500     // Then the actual data.
1501     auto vloc = buf_.size();
1502     for (size_t i = start; i < stack_.size(); i += step) {
1503       WriteAny(stack_[i], byte_width);
1504     }
1505     // Then the types.
1506     if (!typed) {
1507       for (size_t i = start; i < stack_.size(); i += step) {
1508         buf_.push_back(stack_[i].StoredPackedType(bit_width));
1509       }
1510     }
1511     return Value(static_cast<uint64_t>(vloc),
1512                  keys ? FBT_MAP
1513                       : (typed ? ToTypedVector(vector_type, fixed ? vec_len : 0)
1514                                : FBT_VECTOR),
1515                  bit_width);
1516   }
1517
1518   // You shouldn't really be copying instances of this class.
1519   Builder(const Builder &);
1520   Builder &operator=(const Builder &);
1521
1522   std::vector<uint8_t> buf_;
1523   std::vector<Value> stack_;
1524
1525   bool finished_;
1526
1527   BuilderFlag flags_;
1528
1529   BitWidth force_min_bit_width_;
1530
1531   struct KeyOffsetCompare {
1532     explicit KeyOffsetCompare(const std::vector<uint8_t> &buf) : buf_(&buf) {}
1533     bool operator()(size_t a, size_t b) const {
1534       auto stra =
1535           reinterpret_cast<const char *>(flatbuffers::vector_data(*buf_) + a);
1536       auto strb =
1537           reinterpret_cast<const char *>(flatbuffers::vector_data(*buf_) + b);
1538       return strcmp(stra, strb) < 0;
1539     }
1540     const std::vector<uint8_t> *buf_;
1541   };
1542
1543   typedef std::pair<size_t, size_t> StringOffset;
1544   struct StringOffsetCompare {
1545     explicit StringOffsetCompare(const std::vector<uint8_t> &buf) : buf_(&buf) {}
1546     bool operator()(const StringOffset &a, const StringOffset &b) const {
1547       auto stra = reinterpret_cast<const char *>(
1548           flatbuffers::vector_data(*buf_) + a.first);
1549       auto strb = reinterpret_cast<const char *>(
1550           flatbuffers::vector_data(*buf_) + b.first);
1551       return strncmp(stra, strb, (std::min)(a.second, b.second) + 1) < 0;
1552     }
1553     const std::vector<uint8_t> *buf_;
1554   };
1555
1556   typedef std::set<size_t, KeyOffsetCompare> KeyOffsetMap;
1557   typedef std::set<StringOffset, StringOffsetCompare> StringOffsetMap;
1558
1559   KeyOffsetMap key_pool;
1560   StringOffsetMap string_pool;
1561 };
1562
1563 }  // namespace flexbuffers
1564
1565 #  if defined(_MSC_VER)
1566 #    pragma warning(pop)
1567 #  endif
1568
1569 #endif  // FLATBUFFERS_FLEXBUFFERS_H_