2 * Copyright 2017 Google Inc. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FLATBUFFERS_FLEXBUFFERS_H_
18 #define FLATBUFFERS_FLEXBUFFERS_H_
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"
31 # pragma warning(push)
32 # pragma warning(disable : 4127) // C4127: conditional expression is constant
35 namespace flexbuffers {
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).
49 // These are used as the upper 6 bits of a type field to indicate the actual
56 // Types above stored inline, types below store an offset.
60 FBT_INDIRECT_UINT = 7,
61 FBT_INDIRECT_FLOAT = 8,
63 FBT_VECTOR = 10, // Untyped.
64 FBT_VECTOR_INT = 11, // Typed any size (stores no type table).
66 FBT_VECTOR_FLOAT = 13,
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,
81 36, // To Allow the same type of conversion of type to vector type
84 inline bool IsInline(Type t) { return t <= FBT_FLOAT || t == FBT_BOOL; }
86 inline bool IsTypedVectorElementType(Type t) {
87 return (t >= FBT_INT && t <= FBT_STRING) || t == FBT_BOOL;
90 inline bool IsTypedVector(Type t) {
91 return (t >= FBT_VECTOR_INT && t <= FBT_VECTOR_STRING) ||
95 inline bool IsFixedTypedVector(Type t) {
96 return t >= FBT_VECTOR_INT2 && t <= FBT_VECTOR_FLOAT4;
99 inline Type ToTypedVector(Type t, size_t fixed_len = 0) {
100 FLATBUFFERS_ASSERT(IsTypedVectorElementType(t));
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;
110 inline Type ToTypedVectorElementType(Type t) {
111 FLATBUFFERS_ASSERT(IsTypedVector(t));
112 return static_cast<Type>(t - FBT_VECTOR_INT + FBT_INT);
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);
123 // TODO: implement proper support for 8/16bit floats, or decide not to
125 typedef int16_t half;
126 typedef int8_t quarter;
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
137 ? static_cast<R>(flatbuffers::ReadScalar<T1>(data))
138 : static_cast<R>(flatbuffers::ReadScalar<T2>(data)))
140 ? static_cast<R>(flatbuffers::ReadScalar<T4>(data))
141 : static_cast<R>(flatbuffers::ReadScalar<T8>(data)));
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>(
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.
156 #if defined(_MSC_VER) && (defined(_M_X64) || defined _M_IX86)
158 __movsb(reinterpret_cast<uint8_t *>(&u),
159 reinterpret_cast<const uint8_t *>(data), byte_width);
160 return flatbuffers::EndianScalar(u);
162 return ReadSizedScalar<uint64_t, uint8_t, uint16_t, uint32_t, uint64_t>(
168 inline double ReadDouble(const uint8_t *data, uint8_t byte_width) {
169 return ReadSizedScalar<double, quarter, half, float, double>(data,
173 inline const uint8_t *Indirect(const uint8_t *offset, uint8_t byte_width) {
174 return offset - ReadUInt64(offset, byte_width);
177 template<typename T> const uint8_t *Indirect(const uint8_t *offset) {
178 return offset - flatbuffers::ReadScalar<T>(offset);
181 inline BitWidth WidthU(uint64_t u) {
182 #define FLATBUFFERS_GET_FIELD_BIT_WIDTH(value, width) \
184 if (!((u) & ~((1ULL << (width)) - 1ULL))) return BIT_WIDTH_##width; \
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
193 inline BitWidth WidthI(int64_t i) {
194 auto u = static_cast<uint64_t>(i) << 1;
195 return WidthU(i >= 0 ? u : ~u);
198 inline BitWidth WidthF(double f) {
199 return static_cast<double>(static_cast<float>(f)) == f ? BIT_WIDTH_32
203 // Base class of all types below.
204 // Points into the data buffer and allows access to one type.
207 Object(const uint8_t *data, uint8_t byte_width)
208 : data_(data), byte_width_(byte_width) {}
211 const uint8_t *data_;
215 // Stores size in `byte_width_` bytes before data_ pointer.
216 class Sized : public Object {
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_));
224 class String : public Sized {
226 String(const uint8_t *data, uint8_t byte_width) : Sized(data, byte_width) {}
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()); }
232 static String EmptyString() {
233 static const uint8_t empty_string[] = { 0 /*len*/, 0 /*terminator*/ };
234 return String(empty_string + 1, 1);
236 bool IsTheEmptyString() const { return data_ == EmptyString().data_; }
239 class Blob : public Sized {
241 Blob(const uint8_t *data_buf, uint8_t byte_width)
242 : Sized(data_buf, byte_width) {}
244 static Blob EmptyBlob() {
245 static const uint8_t empty_blob[] = { 0 /*len*/ };
246 return Blob(empty_blob + 1, 1);
248 bool IsTheEmptyBlob() const { return data_ == EmptyBlob().data_; }
249 const uint8_t *data() const { return data_; }
252 class Vector : public Sized {
254 Vector(const uint8_t *data, uint8_t byte_width) : Sized(data, byte_width) {}
256 Reference operator[](size_t i) const;
258 static Vector EmptyVector() {
259 static const uint8_t empty_vector[] = { 0 /*len*/ };
260 return Vector(empty_vector + 1, 1);
262 bool IsTheEmptyVector() const { return data_ == EmptyVector().data_; }
265 class TypedVector : public Sized {
267 TypedVector(const uint8_t *data, uint8_t byte_width, Type element_type)
268 : Sized(data, byte_width), type_(element_type) {}
270 Reference operator[](size_t i) const;
272 static TypedVector EmptyTypedVector() {
273 static const uint8_t empty_typed_vector[] = { 0 /*len*/ };
274 return TypedVector(empty_typed_vector + 1, 1, FBT_INT);
276 bool IsTheEmptyVector() const {
277 return data_ == TypedVector::EmptyTypedVector().data_;
280 Type ElementType() { return type_; }
288 class FixedTypedVector : public Object {
290 FixedTypedVector(const uint8_t *data, uint8_t byte_width, Type element_type,
292 : Object(data, byte_width), type_(element_type), len_(len) {}
294 Reference operator[](size_t i) const;
296 static FixedTypedVector EmptyFixedTypedVector() {
297 static const uint8_t fixed_empty_vector[] = { 0 /* unused */ };
298 return FixedTypedVector(fixed_empty_vector, 1, FBT_INT, 0);
300 bool IsTheEmptyFixedTypedVector() const {
301 return data_ == FixedTypedVector::EmptyFixedTypedVector().data_;
304 Type ElementType() { return type_; }
305 uint8_t size() { return len_; }
312 class Map : public Vector {
314 Map(const uint8_t *data, uint8_t byte_width) : Vector(data, byte_width) {}
316 Reference operator[](const char *key) const;
317 Reference operator[](const std::string &key) const;
319 Vector Values() const { return Vector(data_, byte_width_); }
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_)),
330 static Map EmptyMap() {
331 static const uint8_t empty_map[] = {
332 0 /*keys_len*/, 0 /*keys_offset*/, 1 /*keys_width*/, 0 /*len*/
334 return Map(empty_map + 4, 1);
337 bool IsTheEmptyMap() const { return data_ == EmptyMap().data_; }
341 void AppendToString(std::string &s, T &&v, bool keys_quoted) {
343 for (size_t i = 0; i < v.size(); i++) {
345 v[i].ToString(true, keys_quoted, s);
355 byte_width_(BIT_WIDTH_8),
358 Reference(const uint8_t *data, uint8_t parent_width, uint8_t byte_width,
361 parent_width_(parent_width),
362 byte_width_(byte_width),
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);
371 Type GetType() const { return type_; }
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;
379 bool IsIntOrUint() const { return IsInt() || IsUInt(); }
380 bool IsFloat() const {
381 return type_ == FBT_FLOAT || type_ == FBT_INDIRECT_FLOAT;
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_)
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_);
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_);
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_);
419 // Convert other things to int.
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()); }
430 uint64_t AsUInt64() const {
431 if (type_ == FBT_UINT) {
432 // A fast path for the common case.
433 return ReadUInt64(data_, parent_width_);
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_);
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_);
448 // Convert other things to uint.
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()); }
457 double AsDouble() const {
458 if (type_ == FBT_FLOAT) {
459 // A fast path for the common case.
460 return ReadDouble(data_, parent_width_);
463 case FBT_INDIRECT_FLOAT: return ReadDouble(Indirect(), byte_width_);
465 return static_cast<double>(ReadInt64(data_, parent_width_));
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());
476 return static_cast<double>(ReadUInt64(data_, parent_width_));
478 // Convert strings and other things to float.
483 float AsFloat() const { return static_cast<float>(AsDouble()); }
485 const char *AsKey() const {
486 if (type_ == FBT_KEY) {
487 return reinterpret_cast<const char *>(Indirect());
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_);
498 return String::EmptyString();
502 // Unlike AsString(), this will convert any type to a std::string.
503 std::string ToString() const {
505 ToString(false, false, s);
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);
519 s.append(str.c_str(), str.length());
521 } else if (IsKey()) {
524 flatbuffers::EscapeString(str, strlen(str), &s, true, false);
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()) {
536 } else if (IsBool()) {
537 s += AsBool() ? "true" : "false";
538 } else if (IsMap()) {
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);
546 vals[i].ToString(true, keys_quoted, s);
547 if (i < keys.size() - 1) 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);
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_);
570 return Blob::EmptyBlob();
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_);
580 return Vector::EmptyVector();
584 TypedVector AsTypedVector() const {
585 if (IsTypedVector()) {
586 return TypedVector(Indirect(), byte_width_,
587 ToTypedVectorElementType(type_));
589 return TypedVector::EmptyTypedVector();
593 FixedTypedVector AsFixedTypedVector() const {
594 if (IsFixedTypedVector()) {
596 auto vtype = ToFixedTypedVectorElementType(type_, &len);
597 return FixedTypedVector(Indirect(), byte_width_, vtype, len);
599 return FixedTypedVector::EmptyFixedTypedVector();
604 if (type_ == FBT_MAP) {
605 return Map(Indirect(), byte_width_);
607 return Map::EmptyMap();
611 template<typename T> T As() const;
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));
635 bool MutateBool(bool b) {
636 return type_ == FBT_BOOL && Mutate(data_, b, parent_width_, BIT_WIDTH_8);
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));
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);
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));
675 bool MutateString(const char *str, size_t len) {
677 if (s.IsTheEmptyString()) return false;
678 // This is very strict, could allow shorter strings, but that creates
680 if (s.length() != len) return false;
681 memcpy(const_cast<char *>(s.c_str()), str, len);
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());
690 const uint8_t *Indirect() const {
691 return flexbuffers::Indirect(data_, parent_width_);
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) <=
700 t = flatbuffers::EndianScalar(t);
701 memcpy(const_cast<uint8_t *>(dest), &t, byte_width);
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);
717 const uint8_t *data_;
718 uint8_t parent_width_;
723 // Template specialization for As().
724 template<> inline bool Reference::As<bool>() const { return AsBool(); }
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(); }
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(); }
736 template<> inline double Reference::As<double>() const { return AsDouble(); }
737 template<> inline float Reference::As<float>() const { return AsFloat(); }
739 template<> inline String Reference::As<String>() const { return AsString(); }
740 template<> inline std::string Reference::As<std::string>() const {
741 return AsString().str();
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();
749 template<> inline FixedTypedVector Reference::As<FixedTypedVector>() const {
750 return AsFixedTypedVector();
752 template<> inline Map Reference::As<Map>() const { return AsMap(); }
754 inline uint8_t PackedType(BitWidth bit_width, Type type) {
755 return static_cast<uint8_t>(bit_width | (type << 2));
758 inline uint8_t NullPackedType() { return PackedType(BIT_WIDTH_8, FBT_NULL); }
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
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 {
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);
775 inline Reference TypedVector::operator[](size_t i) const {
777 if (i >= len) return Reference(nullptr, 1, NullPackedType());
778 auto elem = data_ + i * byte_width_;
779 return Reference(elem, byte_width_, 1, type_);
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_);
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);
795 inline Reference Map::operator[](const char *key) const {
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;
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];
812 inline Reference Map::operator[](const std::string &key) const {
813 return (*this)[key.c_str()];
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);
826 inline Reference GetRoot(const std::vector<uint8_t> &buffer) {
827 return GetRoot(flatbuffers::vector_data(buffer), buffer.size());
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.
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,
849 class Builder FLATBUFFERS_FINAL_CLASS {
851 Builder(size_t initial_size = 256,
852 BuilderFlag flags = BUILDER_FLAG_SHARE_KEYS)
853 : buf_(initial_size),
856 force_min_bit_width_(BIT_WIDTH_8),
857 key_pool(KeyOffsetCompare(buf_)),
858 string_pool(StringOffsetCompare(buf_)) {
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 {
869 // Size of the buffer. Does not include unfinished values.
870 size_t GetSize() const { return buf_.size(); }
872 // Reset all state so we can re-use the buffer.
877 // flags_ remains as-is;
878 force_min_bit_width_ = BIT_WIDTH_8;
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).
887 void Null() { stack_.push_back(Value()); }
888 void Null(const char *key) {
893 void Int(int64_t i) { stack_.push_back(Value(i, FBT_INT, WidthI(i))); }
894 void Int(const char *key, int64_t i) {
899 void UInt(uint64_t u) { stack_.push_back(Value(u, FBT_UINT, WidthU(u))); }
900 void UInt(const char *key, uint64_t u) {
905 void Float(float f) { stack_.push_back(Value(f)); }
906 void Float(const char *key, float f) {
911 void Double(double f) { stack_.push_back(Value(f)); }
912 void Double(const char *key, double d) {
917 void Bool(bool b) { stack_.push_back(Value(b)); }
918 void Bool(const char *key, bool b) {
923 void IndirectInt(int64_t i) {
924 PushIndirect(i, FBT_INDIRECT_INT, WidthI(i));
926 void IndirectInt(const char *key, int64_t i) {
931 void IndirectUInt(uint64_t u) {
932 PushIndirect(u, FBT_INDIRECT_UINT, WidthU(u));
934 void IndirectUInt(const char *key, uint64_t u) {
939 void IndirectFloat(float f) {
940 PushIndirect(f, FBT_INDIRECT_FLOAT, BIT_WIDTH_32);
942 void IndirectFloat(const char *key, float f) {
947 void IndirectDouble(double f) {
948 PushIndirect(f, FBT_INDIRECT_FLOAT, WidthF(f));
950 void IndirectDouble(const char *key, double d) {
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.
966 key_pool.insert(sloc);
969 stack_.push_back(Value(static_cast<uint64_t>(sloc), FBT_KEY, BIT_WIDTH_8));
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()); }
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);
987 stack_.back().u_ = sloc;
989 string_pool.insert(so);
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());
998 void String(const flexbuffers::String &str) {
999 String(str.c_str(), str.length());
1002 void String(const char *key, const char *str) {
1006 void String(const char *key, const std::string &str) {
1010 void String(const char *key, const flexbuffers::String &str) {
1015 size_t Blob(const void *data, size_t len) {
1016 return CreateBlob(data, len, 0, FBT_BLOB);
1018 size_t Blob(const std::vector<uint8_t> &v) {
1019 return CreateBlob(flatbuffers::vector_data(v), v.size(), 0, FBT_BLOB);
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?
1026 size_t StartVector() { return stack_.size(); }
1027 size_t StartVector(const char *key) {
1029 return stack_.size();
1031 size_t StartMap() { return stack_.size(); }
1032 size_t StartMap(const char *key) {
1034 return stack_.size();
1037 // TODO(wvo): allow this to specify an aligment greater than the natural
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_);
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));
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);
1057 // Now sort values, so later we can do a binary search lookup.
1058 // We want to sort 2 array elements at a time.
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
1071 // std::sort is typically already a lot faster on sorted data though.
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
1086 FLATBUFFERS_ASSERT(comp || &a == &b);
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_);
1100 template<typename F> size_t Vector(F f) {
1101 auto start = StartVector();
1103 return EndVector(start, false, false);
1105 template<typename F, typename T> size_t Vector(F f, T &state) {
1106 auto start = StartVector();
1108 return EndVector(start, false, false);
1110 template<typename F> size_t Vector(const char *key, F f) {
1111 auto start = StartVector(key);
1113 return EndVector(start, false, false);
1115 template<typename F, typename T>
1116 size_t Vector(const char *key, F f, T &state) {
1117 auto start = StartVector(key);
1119 return EndVector(start, false, false);
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);
1127 auto start = StartVector();
1128 for (size_t i = 0; i < len; i++) Add(elems[i]);
1129 EndVector(start, false, false);
1132 template<typename T>
1133 void Vector(const char *key, const T *elems, size_t len) {
1137 template<typename T> void Vector(const std::vector<T> &vec) {
1138 Vector(flatbuffers::vector_data(vec), vec.size());
1141 template<typename F> size_t TypedVector(F f) {
1142 auto start = StartVector();
1144 return EndVector(start, true, false);
1146 template<typename F, typename T> size_t TypedVector(F f, T &state) {
1147 auto start = StartVector();
1149 return EndVector(start, true, false);
1151 template<typename F> size_t TypedVector(const char *key, F f) {
1152 auto start = StartVector(key);
1154 return EndVector(start, true, false);
1156 template<typename F, typename T>
1157 size_t TypedVector(const char *key, F f, T &state) {
1158 auto start = StartVector(key);
1160 return EndVector(start, true, false);
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);
1172 template<typename T>
1173 size_t FixedTypedVector(const char *key, const T *elems, size_t len) {
1175 return FixedTypedVector(elems, len);
1178 template<typename F> size_t Map(F f) {
1179 auto start = StartMap();
1181 return EndMap(start);
1183 template<typename F, typename T> size_t Map(F f, T &state) {
1184 auto start = StartMap();
1186 return EndMap(start);
1188 template<typename F> size_t Map(const char *key, F f) {
1189 auto start = StartMap(key);
1191 return EndMap(start);
1193 template<typename F, typename T> size_t Map(const char *key, F f, T &state) {
1194 auto start = StartMap(key);
1196 return EndMap(start);
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);
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.
1216 Value LastValue() { return stack_.back(); }
1217 void ReuseValue(Value v) {
1218 stack_.push_back(v);
1220 void ReuseValue(const char *key, Value v) {
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); }
1241 template<typename T> void Add(const std::vector<T> &vec) { Vector(vec); }
1243 template<typename T> void Add(const char *key, const T &t) {
1248 template<typename T> void Add(const std::map<std::string, T> &map) {
1252 template<typename T> void operator+=(const T &t) { Add(t); }
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;
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);
1269 // Write root value.
1270 auto byte_width = Align(stack_[0].ElemWidth(buf_.size(), 0));
1271 WriteAny(stack_[0], byte_width);
1273 Write(stack_[0].StoredPackedType(), 1);
1274 // Write root size. Normally determined by parent, but root has no parent :)
1275 Write(byte_width, 1);
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_);
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),
1293 return static_cast<uint8_t>(byte_width);
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);
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);
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);
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);
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));
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;
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
1344 : flatbuffers::is_same<T, bool>::value
1346 : (flatbuffers::is_unsigned<T>::value ? FBT_UINT
1351 // This was really intended to be private, except for LastValue/ReuseValue.
1361 // For scalars: of itself, for vector: of its elements, for string: length.
1362 BitWidth min_bit_width_;
1364 Value() : i_(0), type_(FBT_NULL), min_bit_width_(BIT_WIDTH_8) {}
1367 : u_(static_cast<uint64_t>(b)),
1369 min_bit_width_(BIT_WIDTH_8) {}
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) {}
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)) {}
1379 uint8_t StoredPackedType(BitWidth parent_bit_width_ = BIT_WIDTH_8) const {
1380 return PackedType(StoredWidth(parent_bit_width_), type_);
1383 BitWidth ElemWidth(size_t buf_size, size_t elem_index) const {
1384 if (IsInline(type_)) {
1385 return min_bit_width_;
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);
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_;
1402 auto bit_width = WidthU(offset);
1403 if (static_cast<size_t>(static_cast<size_t>(1U) << bit_width) ==
1407 FLATBUFFERS_ASSERT(false); // Must match one of the sizes above.
1408 return BIT_WIDTH_64;
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_);
1416 return min_bit_width_;
1422 void WriteAny(const Value &val, uint8_t byte_width) {
1423 switch (val.type_) {
1425 case FBT_INT: Write(val.i_, byte_width); break;
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;
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));
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),
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;
1470 // If this vector is part of a map, we will pre-fix an offset to the keys
1472 bit_width = (std::max)(bit_width, keys->ElemWidth(buf_.size(), 0));
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);
1482 vector_type = stack_[i].type_;
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_);
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.
1496 WriteOffset(keys->u_, byte_width);
1497 Write<uint64_t>(1ULL << keys->min_bit_width_, byte_width);
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);
1507 for (size_t i = start; i < stack_.size(); i += step) {
1508 buf_.push_back(stack_[i].StoredPackedType(bit_width));
1511 return Value(static_cast<uint64_t>(vloc),
1513 : (typed ? ToTypedVector(vector_type, fixed ? vec_len : 0)
1518 // You shouldn't really be copying instances of this class.
1519 Builder(const Builder &);
1520 Builder &operator=(const Builder &);
1522 std::vector<uint8_t> buf_;
1523 std::vector<Value> stack_;
1529 BitWidth force_min_bit_width_;
1531 struct KeyOffsetCompare {
1532 explicit KeyOffsetCompare(const std::vector<uint8_t> &buf) : buf_(&buf) {}
1533 bool operator()(size_t a, size_t b) const {
1535 reinterpret_cast<const char *>(flatbuffers::vector_data(*buf_) + a);
1537 reinterpret_cast<const char *>(flatbuffers::vector_data(*buf_) + b);
1538 return strcmp(stra, strb) < 0;
1540 const std::vector<uint8_t> *buf_;
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;
1553 const std::vector<uint8_t> *buf_;
1556 typedef std::set<size_t, KeyOffsetCompare> KeyOffsetMap;
1557 typedef std::set<StringOffset, StringOffsetCompare> StringOffsetMap;
1559 KeyOffsetMap key_pool;
1560 StringOffsetMap string_pool;
1563 } // namespace flexbuffers
1565 # if defined(_MSC_VER)
1566 # pragma warning(pop)
1569 #endif // FLATBUFFERS_FLEXBUFFERS_H_