Made all C++ files clang-formatted.

Also added missing generated files.

Change-Id: Ifd22a643a08e3f2edfce92812ed57b87fc0e1875
This commit is contained in:
Wouter van Oortmerssen
2017-12-21 10:54:28 -08:00
parent 5d9930aa0d
commit 89711c9c47
43 changed files with 5850 additions and 5992 deletions

View File

@@ -24,12 +24,12 @@
#include "flatbuffers/util.h"
#ifdef _MSC_VER
#include <intrin.h>
# include <intrin.h>
#endif
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4127) // C4127: conditional expression is constant
# pragma warning(push)
# pragma warning(disable : 4127) // C4127: conditional expression is constant
#endif
namespace flexbuffers {
@@ -60,24 +60,25 @@ enum Type {
TYPE_INDIRECT_UINT = 7,
TYPE_INDIRECT_FLOAT = 8,
TYPE_MAP = 9,
TYPE_VECTOR = 10, // Untyped.
TYPE_VECTOR_INT = 11, // Typed any size (stores no type table).
TYPE_VECTOR = 10, // Untyped.
TYPE_VECTOR_INT = 11, // Typed any size (stores no type table).
TYPE_VECTOR_UINT = 12,
TYPE_VECTOR_FLOAT = 13,
TYPE_VECTOR_KEY = 14,
TYPE_VECTOR_STRING = 15,
TYPE_VECTOR_INT2 = 16, // Typed tuple (no type table, no size field).
TYPE_VECTOR_INT2 = 16, // Typed tuple (no type table, no size field).
TYPE_VECTOR_UINT2 = 17,
TYPE_VECTOR_FLOAT2 = 18,
TYPE_VECTOR_INT3 = 19, // Typed triple (no type table, no size field).
TYPE_VECTOR_INT3 = 19, // Typed triple (no type table, no size field).
TYPE_VECTOR_UINT3 = 20,
TYPE_VECTOR_FLOAT3 = 21,
TYPE_VECTOR_INT4 = 22, // Typed quad (no type table, no size field).
TYPE_VECTOR_INT4 = 22, // Typed quad (no type table, no size field).
TYPE_VECTOR_UINT4 = 23,
TYPE_VECTOR_FLOAT4 = 24,
TYPE_BLOB = 25,
TYPE_BOOL = 26,
TYPE_VECTOR_BOOL = 36, // To Allow the same type of conversion of type to vector type
TYPE_VECTOR_BOOL =
36, // To Allow the same type of conversion of type to vector type
};
inline bool IsInline(Type t) { return t <= TYPE_FLOAT || t == TYPE_BOOL; }
@@ -87,7 +88,8 @@ inline bool IsTypedVectorElementType(Type t) {
}
inline bool IsTypedVector(Type t) {
return (t >= TYPE_VECTOR_INT && t <= TYPE_VECTOR_STRING) || t == TYPE_VECTOR_BOOL;
return (t >= TYPE_VECTOR_INT && t <= TYPE_VECTOR_STRING) ||
t == TYPE_VECTOR_BOOL;
}
inline bool IsFixedTypedVector(Type t) {
@@ -113,7 +115,8 @@ inline Type ToTypedVectorElementType(Type t) {
inline Type ToFixedTypedVectorElementType(Type t, uint8_t *len) {
assert(IsFixedTypedVector(t));
auto fixed_type = t - TYPE_VECTOR_INT2;
*len = static_cast<uint8_t>(fixed_type / 3 + 2); // 3 types each, starting from length 2.
*len = static_cast<uint8_t>(fixed_type / 3 +
2); // 3 types each, starting from length 2.
return static_cast<Type>(fixed_type % 3 + TYPE_INT);
}
@@ -127,19 +130,20 @@ typedef int8_t quarter;
// decently quick, but it is the most frequently executed function.
// We could do an (unaligned) 64-bit read if we ifdef out the platforms for
// which that doesn't work (or where we'd read into un-owned memory).
template <typename R, typename T1, typename T2, typename T4, typename T8>
template<typename R, typename T1, typename T2, typename T4, typename T8>
R ReadSizedScalar(const uint8_t *data, uint8_t byte_width) {
return byte_width < 4
? (byte_width < 2 ? static_cast<R>(flatbuffers::ReadScalar<T1>(data))
: static_cast<R>(flatbuffers::ReadScalar<T2>(data)))
: (byte_width < 8 ? static_cast<R>(flatbuffers::ReadScalar<T4>(data))
: static_cast<R>(flatbuffers::ReadScalar<T8>(data)));
? (byte_width < 2
? static_cast<R>(flatbuffers::ReadScalar<T1>(data))
: static_cast<R>(flatbuffers::ReadScalar<T2>(data)))
: (byte_width < 8
? static_cast<R>(flatbuffers::ReadScalar<T4>(data))
: static_cast<R>(flatbuffers::ReadScalar<T8>(data)));
}
inline int64_t ReadInt64(const uint8_t *data, uint8_t byte_width) {
return ReadSizedScalar<int64_t, int8_t, int16_t, int32_t, int64_t>(data,
byte_width);
return ReadSizedScalar<int64_t, int8_t, int16_t, int32_t, int64_t>(
data, byte_width);
}
inline uint64_t ReadUInt64(const uint8_t *data, uint8_t byte_width) {
@@ -148,6 +152,7 @@ inline uint64_t ReadUInt64(const uint8_t *data, uint8_t byte_width) {
// TODO: GCC apparently replaces memcpy by a rep movsb, but only if count is a
// constant, which here it isn't. Test if memcpy is still faster than
// the conditionals in ReadSizedScalar. Can also use inline asm.
// clang-format off
#ifdef _MSC_VER
uint64_t u = 0;
__movsb(reinterpret_cast<uint8_t *>(&u),
@@ -157,11 +162,12 @@ inline uint64_t ReadUInt64(const uint8_t *data, uint8_t byte_width) {
return ReadSizedScalar<uint64_t, uint8_t, uint16_t, uint32_t, uint64_t>(
data, byte_width);
#endif
// clang-format on
}
inline double ReadDouble(const uint8_t *data, uint8_t byte_width) {
return ReadSizedScalar<double, quarter, half, float, double>(data,
byte_width);
byte_width);
}
inline const uint8_t *Indirect(const uint8_t *offset, uint8_t byte_width) {
@@ -173,13 +179,14 @@ template<typename T> const uint8_t *Indirect(const uint8_t *offset) {
}
inline BitWidth WidthU(uint64_t u) {
#define FLATBUFFERS_GET_FIELD_BIT_WIDTH(value, width) { \
#define FLATBUFFERS_GET_FIELD_BIT_WIDTH(value, width) \
{ \
if (!((u) & ~((1ULL << (width)) - 1ULL))) return BIT_WIDTH_##width; \
}
FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 8);
FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 16);
FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 32);
#undef FLATBUFFERS_GET_FIELD_BIT_WIDTH
#undef FLATBUFFERS_GET_FIELD_BIT_WIDTH
return BIT_WIDTH_64;
}
@@ -198,7 +205,7 @@ inline BitWidth WidthF(double f) {
class Object {
public:
Object(const uint8_t *data, uint8_t byte_width)
: data_(data), byte_width_(byte_width) {}
: data_(data), byte_width_(byte_width) {}
protected:
const uint8_t *data_;
@@ -216,15 +223,14 @@ class Sized : public Object {
class String : public Sized {
public:
String(const uint8_t *data, uint8_t byte_width)
: Sized(data, byte_width) {}
String(const uint8_t *data, uint8_t byte_width) : Sized(data, byte_width) {}
size_t length() const { return size(); }
const char *c_str() const { return reinterpret_cast<const char *>(data_); }
std::string str() const { return std::string(c_str(), length()); }
static String EmptyString() {
static const uint8_t empty_string[] = { 0/*len*/, 0/*terminator*/ };
static const uint8_t empty_string[] = { 0 /*len*/, 0 /*terminator*/ };
return String(empty_string + 1, 1);
}
bool IsTheEmptyString() const { return data_ == EmptyString().data_; }
@@ -233,10 +239,10 @@ class String : public Sized {
class Blob : public Sized {
public:
Blob(const uint8_t *data_buf, uint8_t byte_width)
: Sized(data_buf, byte_width) {}
: Sized(data_buf, byte_width) {}
static Blob EmptyBlob() {
static const uint8_t empty_blob[] = { 0/*len*/ };
static const uint8_t empty_blob[] = { 0 /*len*/ };
return Blob(empty_blob + 1, 1);
}
bool IsTheEmptyBlob() const { return data_ == EmptyBlob().data_; }
@@ -245,13 +251,12 @@ class Blob : public Sized {
class Vector : public Sized {
public:
Vector(const uint8_t *data, uint8_t byte_width)
: Sized(data, byte_width) {}
Vector(const uint8_t *data, uint8_t byte_width) : Sized(data, byte_width) {}
Reference operator[](size_t i) const;
static Vector EmptyVector() {
static const uint8_t empty_vector[] = { 0/*len*/ };
static const uint8_t empty_vector[] = { 0 /*len*/ };
return Vector(empty_vector + 1, 1);
}
bool IsTheEmptyVector() const { return data_ == EmptyVector().data_; }
@@ -260,12 +265,12 @@ class Vector : public Sized {
class TypedVector : public Sized {
public:
TypedVector(const uint8_t *data, uint8_t byte_width, Type element_type)
: Sized(data, byte_width), type_(element_type) {}
: Sized(data, byte_width), type_(element_type) {}
Reference operator[](size_t i) const;
static TypedVector EmptyTypedVector() {
static const uint8_t empty_typed_vector[] = { 0/*len*/ };
static const uint8_t empty_typed_vector[] = { 0 /*len*/ };
return TypedVector(empty_typed_vector + 1, 1, TYPE_INT);
}
bool IsTheEmptyVector() const {
@@ -284,12 +289,12 @@ class FixedTypedVector : public Object {
public:
FixedTypedVector(const uint8_t *data, uint8_t byte_width, Type element_type,
uint8_t len)
: Object(data, byte_width), type_(element_type), len_(len) {}
: Object(data, byte_width), type_(element_type), len_(len) {}
Reference operator[](size_t i) const;
static FixedTypedVector EmptyFixedTypedVector() {
static const uint8_t fixed_empty_vector[] = { 0/* unused */ };
static const uint8_t fixed_empty_vector[] = { 0 /* unused */ };
return FixedTypedVector(fixed_empty_vector, 1, TYPE_INT, 0);
}
bool IsTheEmptyFixedTypedVector() const {
@@ -306,8 +311,7 @@ class FixedTypedVector : public Object {
class Map : public Vector {
public:
Map(const uint8_t *data, uint8_t byte_width)
: Vector(data, byte_width) {}
Map(const uint8_t *data, uint8_t byte_width) : Vector(data, byte_width) {}
Reference operator[](const char *key) const;
Reference operator[](const std::string &key) const;
@@ -319,31 +323,31 @@ class Map : public Vector {
auto keys_offset = data_ - byte_width_ * num_prefixed_fields;
return TypedVector(Indirect(keys_offset, byte_width_),
static_cast<uint8_t>(
ReadUInt64(keys_offset + byte_width_, byte_width_)),
ReadUInt64(keys_offset + byte_width_, byte_width_)),
TYPE_KEY);
}
static Map EmptyMap() {
static const uint8_t empty_map[] = {
0/*keys_len*/, 0/*keys_offset*/, 1/*keys_width*/, 0/*len*/
0 /*keys_len*/, 0 /*keys_offset*/, 1 /*keys_width*/, 0 /*len*/
};
return Map(empty_map + 4, 1);
}
bool IsTheEmptyMap() const {
return data_ == EmptyMap().data_;
}
bool IsTheEmptyMap() const { return data_ == EmptyMap().data_; }
};
class Reference {
public:
Reference(const uint8_t *data, uint8_t parent_width, uint8_t byte_width,
Type type)
: data_(data), parent_width_(parent_width), byte_width_(byte_width),
type_(type) {}
: data_(data),
parent_width_(parent_width),
byte_width_(byte_width),
type_(type) {}
Reference(const uint8_t *data, uint8_t parent_width, uint8_t packed_type)
: data_(data), parent_width_(parent_width) {
: data_(data), parent_width_(parent_width) {
byte_width_ = 1U << static_cast<BitWidth>(packed_type & 3);
type_ = static_cast<Type>(packed_type >> 2);
}
@@ -352,13 +356,14 @@ class Reference {
bool IsNull() const { return type_ == TYPE_NULL; }
bool IsBool() const { return type_ == TYPE_BOOL; }
bool IsInt() const { return type_ == TYPE_INT ||
type_ == TYPE_INDIRECT_INT; }
bool IsUInt() const { return type_ == TYPE_UINT||
type_ == TYPE_INDIRECT_UINT;; }
bool IsInt() const { return type_ == TYPE_INT || type_ == TYPE_INDIRECT_INT; }
bool IsUInt() const {
return type_ == TYPE_UINT || type_ == TYPE_INDIRECT_UINT;
}
bool IsIntOrUint() const { return IsInt() || IsUInt(); }
bool IsFloat() const { return type_ == TYPE_FLOAT ||
type_ == TYPE_INDIRECT_FLOAT; }
bool IsFloat() const {
return type_ == TYPE_FLOAT || type_ == TYPE_INDIRECT_FLOAT;
}
bool IsNumeric() const { return IsIntOrUint() || IsFloat(); }
bool IsString() const { return type_ == TYPE_STRING; }
bool IsKey() const { return type_ == TYPE_KEY; }
@@ -367,7 +372,8 @@ class Reference {
bool IsBlob() const { return type_ == TYPE_BLOB; }
bool AsBool() const {
return (type_ == TYPE_BOOL ? ReadUInt64(data_, parent_width_) : AsUInt64()) != 0;
return (type_ == TYPE_BOOL ? ReadUInt64(data_, parent_width_)
: AsUInt64()) != 0;
}
// Reads any type as a int64_t. Never fails, does most sensible conversion.
@@ -377,79 +383,82 @@ class Reference {
if (type_ == TYPE_INT) {
// A fast path for the common case.
return ReadInt64(data_, parent_width_);
} else switch (type_) {
case TYPE_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
case TYPE_UINT: return ReadUInt64(data_, parent_width_);
case TYPE_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
case TYPE_FLOAT: return static_cast<int64_t>(
ReadDouble(data_, parent_width_));
case TYPE_INDIRECT_FLOAT: return static_cast<int64_t>(
ReadDouble(Indirect(), byte_width_));
case TYPE_NULL: return 0;
case TYPE_STRING: return flatbuffers::StringToInt(AsString().c_str());
case TYPE_VECTOR: return static_cast<int64_t>(AsVector().size());
case TYPE_BOOL: return ReadInt64(data_, parent_width_);
default:
// Convert other things to int.
return 0;
}
} else
switch (type_) {
case TYPE_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
case TYPE_UINT: return ReadUInt64(data_, parent_width_);
case TYPE_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
case TYPE_FLOAT:
return static_cast<int64_t>(ReadDouble(data_, parent_width_));
case TYPE_INDIRECT_FLOAT:
return static_cast<int64_t>(ReadDouble(Indirect(), byte_width_));
case TYPE_NULL: return 0;
case TYPE_STRING: return flatbuffers::StringToInt(AsString().c_str());
case TYPE_VECTOR: return static_cast<int64_t>(AsVector().size());
case TYPE_BOOL: return ReadInt64(data_, parent_width_);
default:
// Convert other things to int.
return 0;
}
}
// TODO: could specialize these to not use AsInt64() if that saves
// extension ops in generated code, and use a faster op than ReadInt64.
int32_t AsInt32() const { return static_cast<int32_t>(AsInt64()); }
int16_t AsInt16() const { return static_cast<int16_t>(AsInt64()); }
int8_t AsInt8() const { return static_cast<int8_t> (AsInt64()); }
int8_t AsInt8() const { return static_cast<int8_t>(AsInt64()); }
uint64_t AsUInt64() const {
if (type_ == TYPE_UINT) {
// A fast path for the common case.
return ReadUInt64(data_, parent_width_);
} else switch (type_) {
case TYPE_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
case TYPE_INT: return ReadInt64(data_, parent_width_);
case TYPE_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
case TYPE_FLOAT: return static_cast<uint64_t>(
ReadDouble(data_, parent_width_));
case TYPE_INDIRECT_FLOAT: return static_cast<uint64_t>(
ReadDouble(Indirect(), byte_width_));
case TYPE_NULL: return 0;
case TYPE_STRING: return flatbuffers::StringToUInt(AsString().c_str());
case TYPE_VECTOR: return static_cast<uint64_t>(AsVector().size());
case TYPE_BOOL: return ReadUInt64(data_, parent_width_);
default:
// Convert other things to uint.
return 0;
}
} else
switch (type_) {
case TYPE_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
case TYPE_INT: return ReadInt64(data_, parent_width_);
case TYPE_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
case TYPE_FLOAT:
return static_cast<uint64_t>(ReadDouble(data_, parent_width_));
case TYPE_INDIRECT_FLOAT:
return static_cast<uint64_t>(ReadDouble(Indirect(), byte_width_));
case TYPE_NULL: return 0;
case TYPE_STRING: return flatbuffers::StringToUInt(AsString().c_str());
case TYPE_VECTOR: return static_cast<uint64_t>(AsVector().size());
case TYPE_BOOL: return ReadUInt64(data_, parent_width_);
default:
// Convert other things to uint.
return 0;
}
}
uint32_t AsUInt32() const { return static_cast<uint32_t>(AsUInt64()); }
uint16_t AsUInt16() const { return static_cast<uint16_t>(AsUInt64()); }
uint8_t AsUInt8() const { return static_cast<uint8_t> (AsUInt64()); }
uint8_t AsUInt8() const { return static_cast<uint8_t>(AsUInt64()); }
double AsDouble() const {
if (type_ == TYPE_FLOAT) {
// A fast path for the common case.
return ReadDouble(data_, parent_width_);
} else switch (type_) {
case TYPE_INDIRECT_FLOAT: return ReadDouble(Indirect(), byte_width_);
case TYPE_INT: return static_cast<double>(
ReadInt64(data_, parent_width_));
case TYPE_UINT: return static_cast<double>(
ReadUInt64(data_, parent_width_));
case TYPE_INDIRECT_INT: return static_cast<double>(
ReadInt64(Indirect(), byte_width_));
case TYPE_INDIRECT_UINT: return static_cast<double>(
ReadUInt64(Indirect(), byte_width_));
case TYPE_NULL: return 0.0;
case TYPE_STRING: return strtod(AsString().c_str(), nullptr);
case TYPE_VECTOR: return static_cast<double>(AsVector().size());
case TYPE_BOOL: return static_cast<double>(
ReadUInt64(data_, parent_width_));
default:
// Convert strings and other things to float.
return 0;
}
} else
switch (type_) {
case TYPE_INDIRECT_FLOAT: return ReadDouble(Indirect(), byte_width_);
case TYPE_INT:
return static_cast<double>(ReadInt64(data_, parent_width_));
case TYPE_UINT:
return static_cast<double>(ReadUInt64(data_, parent_width_));
case TYPE_INDIRECT_INT:
return static_cast<double>(ReadInt64(Indirect(), byte_width_));
case TYPE_INDIRECT_UINT:
return static_cast<double>(ReadUInt64(Indirect(), byte_width_));
case TYPE_NULL: return 0.0;
case TYPE_STRING: return strtod(AsString().c_str(), nullptr);
case TYPE_VECTOR: return static_cast<double>(AsVector().size());
case TYPE_BOOL:
return static_cast<double>(ReadUInt64(data_, parent_width_));
default:
// Convert strings and other things to float.
return 0;
}
}
float AsFloat() const { return static_cast<float>(AsDouble()); }
@@ -652,9 +661,7 @@ class Reference {
memcpy(const_cast<char *>(s.c_str()), str, len);
return true;
}
bool MutateString(const char *str) {
return MutateString(str, strlen(str));
}
bool MutateString(const char *str) { return MutateString(str, strlen(str)); }
bool MutateString(const std::string &str) {
return MutateString(str.data(), str.length());
}
@@ -664,9 +671,11 @@ class Reference {
return flexbuffers::Indirect(data_, parent_width_);
}
template<typename T> bool Mutate(const uint8_t *dest, T t, size_t byte_width,
BitWidth value_width) {
auto fits = static_cast<size_t>(static_cast<size_t>(1U) << value_width) <= byte_width;
template<typename T>
bool Mutate(const uint8_t *dest, T t, size_t byte_width,
BitWidth value_width) {
auto fits = static_cast<size_t>(static_cast<size_t>(1U) << value_width) <=
byte_width;
if (fits) {
t = flatbuffers::EndianScalar(t);
memcpy(const_cast<uint8_t *>(dest), &t, byte_width);
@@ -674,8 +683,9 @@ class Reference {
return fits;
}
template<typename T> bool MutateF(const uint8_t *dest, T t, size_t byte_width,
BitWidth value_width) {
template<typename T>
bool MutateF(const uint8_t *dest, T t, size_t byte_width,
BitWidth value_width) {
if (byte_width == sizeof(double))
return Mutate(dest, static_cast<double>(t), byte_width, value_width);
if (byte_width == sizeof(float))
@@ -707,21 +717,25 @@ template<> inline double Reference::As<double>() { return AsDouble(); }
template<> inline float Reference::As<float>() { return AsFloat(); }
template<> inline String Reference::As<String>() { return AsString(); }
template<> inline std::string Reference::As<std::string>() { return AsString().str(); }
template<> inline std::string Reference::As<std::string>() {
return AsString().str();
}
template<> inline Blob Reference::As<Blob>() { return AsBlob(); }
template<> inline Vector Reference::As<Vector>() { return AsVector(); }
template<> inline TypedVector Reference::As<TypedVector>() { return AsTypedVector(); }
template<> inline FixedTypedVector Reference::As<FixedTypedVector>() { return AsFixedTypedVector(); }
template<> inline TypedVector Reference::As<TypedVector>() {
return AsTypedVector();
}
template<> inline FixedTypedVector Reference::As<FixedTypedVector>() {
return AsFixedTypedVector();
}
template<> inline Map Reference::As<Map>() { return AsMap(); }
inline uint8_t PackedType(BitWidth bit_width, Type type) {
return static_cast<uint8_t>(bit_width | (type << 2));
}
inline uint8_t NullPackedType() {
return PackedType(BIT_WIDTH_8, TYPE_NULL);
}
inline uint8_t NullPackedType() { return PackedType(BIT_WIDTH_8, TYPE_NULL); }
// Vector accessors.
// Note: if you try to access outside of bounds, you get a Null value back
@@ -730,7 +744,7 @@ inline uint8_t NullPackedType() {
// wanted 3d).
// The Null converts seamlessly into a default value for any other type.
// TODO(wvo): Could introduce an #ifdef that makes this into an assert?
inline Reference Vector::operator[](size_t i) const {
inline Reference Vector::operator[](size_t i) const {
auto len = size();
if (i >= len) return Reference(nullptr, 1, NullPackedType());
auto packed_type = (data_ + len * byte_width_)[i];
@@ -738,14 +752,14 @@ inline Reference Vector::operator[](size_t i) const {
return Reference(elem, byte_width_, packed_type);
}
inline Reference TypedVector::operator[](size_t i) const {
inline Reference TypedVector::operator[](size_t i) const {
auto len = size();
if (i >= len) return Reference(nullptr, 1, NullPackedType());
auto elem = data_ + i * byte_width_;
return Reference(elem, byte_width_, 1, type_);
}
inline Reference FixedTypedVector::operator[](size_t i) const {
inline Reference FixedTypedVector::operator[](size_t i) const {
if (i >= len_) return Reference(nullptr, 1, NullPackedType());
auto elem = data_ + i * byte_width_;
return Reference(elem, byte_width_, 1, type_);
@@ -753,7 +767,7 @@ inline Reference FixedTypedVector::operator[](size_t i) const {
template<typename T> int KeyCompare(const void *key, const void *elem) {
auto str_elem = reinterpret_cast<const char *>(
Indirect<T>(reinterpret_cast<const uint8_t *>(elem)));
Indirect<T>(reinterpret_cast<const uint8_t *>(elem)));
auto skey = reinterpret_cast<const char *>(key);
return strcmp(skey, str_elem);
}
@@ -770,8 +784,7 @@ inline Reference Map::operator[](const char *key) const {
case 8: comp = KeyCompare<uint64_t>; break;
}
auto res = std::bsearch(key, keys.data_, keys.size(), keys.byte_width_, comp);
if (!res)
return Reference(nullptr, 1, NullPackedType());
if (!res) return Reference(nullptr, 1, NullPackedType());
auto i = (reinterpret_cast<uint8_t *>(res) - keys.data_) / keys.byte_width_;
return (*static_cast<const Vector *>(this))[i];
}
@@ -817,8 +830,11 @@ class Builder FLATBUFFERS_FINAL_CLASS {
public:
Builder(size_t initial_size = 256,
BuilderFlag flags = BUILDER_FLAG_SHARE_KEYS)
: buf_(initial_size), finished_(false), flags_(flags),
force_min_bit_width_(BIT_WIDTH_8), key_pool(KeyOffsetCompare(buf_)),
: buf_(initial_size),
finished_(false),
flags_(flags),
force_min_bit_width_(BIT_WIDTH_8),
key_pool(KeyOffsetCompare(buf_)),
string_pool(StringOffsetCompare(buf_)) {
buf_.clear();
}
@@ -831,9 +847,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
}
// Size of the buffer. Does not include unfinished values.
size_t GetSize() const {
return buf_.size();
}
size_t GetSize() const { return buf_.size(); }
// Reset all state so we can re-use the buffer.
void Clear() {
@@ -851,26 +865,42 @@ class Builder FLATBUFFERS_FINAL_CLASS {
// vectors and elsewhere).
void Null() { stack_.push_back(Value()); }
void Null(const char *key) { Key(key); Null(); }
void Null(const char *key) {
Key(key);
Null();
}
void Int(int64_t i) { stack_.push_back(Value(i, TYPE_INT, WidthI(i))); }
void Int(const char *key, int64_t i) { Key(key); Int(i); }
void Int(const char *key, int64_t i) {
Key(key);
Int(i);
}
void UInt(uint64_t u) { stack_.push_back(Value(u, TYPE_UINT, WidthU(u))); }
void UInt(const char *key, uint64_t u) { Key(key); Int(u); }
void UInt(const char *key, uint64_t u) {
Key(key);
Int(u);
}
void Float(float f) { stack_.push_back(Value(f)); }
void Float(const char *key, float f) { Key(key); Float(f); }
void Float(const char *key, float f) {
Key(key);
Float(f);
}
void Double(double f) { stack_.push_back(Value(f)); }
void Double(const char *key, double d) { Key(key); Double(d); }
void Double(const char *key, double d) {
Key(key);
Double(d);
}
void Bool(bool b) { stack_.push_back(Value(b)); }
void Bool(const char *key, bool b) { Key(key); Bool(b); }
void IndirectInt(int64_t i) {
PushIndirect(i, TYPE_INDIRECT_INT, WidthI(i));
void Bool(const char *key, bool b) {
Key(key);
Bool(b);
}
void IndirectInt(int64_t i) { PushIndirect(i, TYPE_INDIRECT_INT, WidthI(i)); }
void IndirectInt(const char *key, int64_t i) {
Key(key);
IndirectInt(i);
@@ -939,9 +969,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
}
return sloc;
}
size_t String(const char *str) {
return String(str, strlen(str));
}
size_t String(const char *str) { return String(str, strlen(str)); }
size_t String(const std::string &str) {
return String(str.c_str(), str.size());
}
@@ -974,9 +1002,15 @@ class Builder FLATBUFFERS_FINAL_CLASS {
// Also some FlatBuffers types?
size_t StartVector() { return stack_.size(); }
size_t StartVector(const char *key) { Key(key); return stack_.size(); }
size_t StartVector(const char *key) {
Key(key);
return stack_.size();
}
size_t StartMap() { return stack_.size(); }
size_t StartMap(const char *key) { Key(key); return stack_.size(); }
size_t StartMap(const char *key) {
Key(key);
return stack_.size();
}
// TODO(wvo): allow this to specify an aligment greater than the natural
// alignment.
@@ -1000,7 +1034,10 @@ class Builder FLATBUFFERS_FINAL_CLASS {
}
// Now sort values, so later we can do a binary seach lookup.
// We want to sort 2 array elements at a time.
struct TwoValue { Value key; Value val; };
struct TwoValue {
Value key;
Value val;
};
// TODO(wvo): strict aliasing?
// TODO(wvo): allow the caller to indicate the data is already sorted
// for maximum efficiency? With an assert to check sortedness to make sure
@@ -1011,22 +1048,22 @@ class Builder FLATBUFFERS_FINAL_CLASS {
// sorted fashion.
// std::sort is typically already a lot faster on sorted data though.
auto dict =
reinterpret_cast<TwoValue *>(flatbuffers::vector_data(stack_) +
start);
reinterpret_cast<TwoValue *>(flatbuffers::vector_data(stack_) + start);
std::sort(dict, dict + len,
[&](const TwoValue &a, const TwoValue &b) -> bool {
auto as = reinterpret_cast<const char *>(
flatbuffers::vector_data(buf_) + a.key.u_);
auto bs = reinterpret_cast<const char *>(
flatbuffers::vector_data(buf_) + b.key.u_);
auto comp = strcmp(as, bs);
// If this assertion hits, you've added two keys with the same value to
// this map.
// TODO: Have to check for pointer equality, as some sort implementation
// apparently call this function with the same element?? Why?
assert(comp || &a == &b);
return comp < 0;
});
auto as = reinterpret_cast<const char *>(
flatbuffers::vector_data(buf_) + a.key.u_);
auto bs = reinterpret_cast<const char *>(
flatbuffers::vector_data(buf_) + b.key.u_);
auto comp = strcmp(as, bs);
// If this assertion hits, you've added two keys with the same
// value to this map.
// TODO: Have to check for pointer equality, as some sort
// implementation apparently call this function with the same
// element?? Why?
assert(comp || &a == &b);
return comp < 0;
});
// First create a vector out of all keys.
// TODO(wvo): if kBuilderFlagShareKeyVectors is true, see if we can share
// the first vector.
@@ -1043,7 +1080,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
f();
return EndVector(start, false, false);
}
template <typename F, typename T> size_t Vector(F f, T &state) {
template<typename F, typename T> size_t Vector(F f, T &state) {
auto start = StartVector();
f(state);
return EndVector(start, false, false);
@@ -1053,8 +1090,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
f();
return EndVector(start, false, false);
}
template <typename F, typename T> size_t Vector(const char *key, F f,
T &state) {
template<typename F, typename T>
size_t Vector(const char *key, F f, T &state) {
auto start = StartVector(key);
f(state);
return EndVector(start, false, false);
@@ -1070,8 +1107,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
EndVector(start, false, false);
}
}
template<typename T> void Vector(const char *key, const T *elems,
size_t len) {
template<typename T>
void Vector(const char *key, const T *elems, size_t len) {
Key(key);
Vector(elems, len);
}
@@ -1084,7 +1121,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
f();
return EndVector(start, true, false);
}
template <typename F, typename T> size_t TypedVector(F f, T &state) {
template<typename F, typename T> size_t TypedVector(F f, T &state) {
auto start = StartVector();
f(state);
return EndVector(start, true, false);
@@ -1094,8 +1131,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
f();
return EndVector(start, true, false);
}
template <typename F, typename T> size_t TypedVector(const char *key, F f,
T &state) {
template<typename F, typename T>
size_t TypedVector(const char *key, F f, T &state) {
auto start = StartVector(key);
f(state);
return EndVector(start, true, false);
@@ -1110,8 +1147,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
return ScalarVector(elems, len, true);
}
template<typename T> size_t FixedTypedVector(const char *key, const T *elems,
size_t len) {
template<typename T>
size_t FixedTypedVector(const char *key, const T *elems, size_t len) {
Key(key);
return FixedTypedVector(elems, len);
}
@@ -1121,7 +1158,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
f();
return EndMap(start);
}
template <typename F, typename T> size_t Map(F f, T &state) {
template<typename F, typename T> size_t Map(F f, T &state) {
auto start = StartMap();
f(state);
return EndMap(start);
@@ -1131,8 +1168,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
f();
return EndMap(start);
}
template <typename F, typename T> size_t Map(const char *key, F f,
T &state) {
template<typename F, typename T> size_t Map(const char *key, F f, T &state) {
auto start = StartMap(key);
f(state);
return EndMap(start);
@@ -1160,9 +1196,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
void Add(const std::string &str) { String(str); }
void Add(const flexbuffers::String &str) { String(str); }
template<typename T> void Add(const std::vector<T> &vec) {
Vector(vec);
}
template<typename T> void Add(const std::vector<T> &vec) { Vector(vec); }
template<typename T> void Add(const char *key, const T &t) {
Key(key);
@@ -1173,9 +1207,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
Map(map);
}
template<typename T> void operator+=(const T &t) {
Add(t);
}
template<typename T> void operator+=(const T &t) { Add(t); }
// This function is useful in combination with the Mutate* functions above.
// It forces elements of vectors and maps to have a minimum size, such that
@@ -1220,8 +1252,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
}
void WriteBytes(const void *val, size_t size) {
buf_.insert(buf_.end(),
reinterpret_cast<const uint8_t *>(val),
buf_.insert(buf_.end(), reinterpret_cast<const uint8_t *>(val),
reinterpret_cast<const uint8_t *>(val) + size);
}
@@ -1235,8 +1266,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
switch (byte_width) {
case 8: Write(f, byte_width); break;
case 4: Write(static_cast<float>(f), byte_width); break;
//case 2: Write(static_cast<half>(f), byte_width); break;
//case 1: Write(static_cast<quarter>(f), byte_width); break;
// case 2: Write(static_cast<half>(f), byte_width); break;
// case 1: Write(static_cast<quarter>(f), byte_width); break;
default: assert(0);
}
}
@@ -1267,9 +1298,11 @@ class Builder FLATBUFFERS_FINAL_CLASS {
template<typename T> static Type GetScalarType() {
assert(flatbuffers::is_scalar<T>::value);
return flatbuffers::is_floating_point<T>::value
? TYPE_FLOAT
: flatbuffers::is_same<T, bool>::value ? TYPE_BOOL
: (flatbuffers::is_unsigned<T>::value ? TYPE_UINT : TYPE_INT);
? TYPE_FLOAT
: flatbuffers::is_same<T, bool>::value
? TYPE_BOOL
: (flatbuffers::is_unsigned<T>::value ? TYPE_UINT
: TYPE_INT);
}
struct Value {
@@ -1286,19 +1319,20 @@ class Builder FLATBUFFERS_FINAL_CLASS {
Value() : i_(0), type_(TYPE_NULL), min_bit_width_(BIT_WIDTH_8) {}
Value(bool b) : u_(static_cast<uint64_t>(b)), type_(TYPE_BOOL), min_bit_width_(BIT_WIDTH_8) {}
Value(bool b)
: u_(static_cast<uint64_t>(b)),
type_(TYPE_BOOL),
min_bit_width_(BIT_WIDTH_8) {}
Value(int64_t i, Type t, BitWidth bw)
: i_(i), type_(t), min_bit_width_(bw) {}
: i_(i), type_(t), min_bit_width_(bw) {}
Value(uint64_t u, Type t, BitWidth bw)
: u_(u), type_(t), min_bit_width_(bw) {}
: u_(u), type_(t), min_bit_width_(bw) {}
Value(float f)
: f_(f), type_(TYPE_FLOAT), min_bit_width_(BIT_WIDTH_32) {}
Value(double f)
: f_(f), type_(TYPE_FLOAT), min_bit_width_(WidthF(f)) {}
Value(float f) : f_(f), type_(TYPE_FLOAT), min_bit_width_(BIT_WIDTH_32) {}
Value(double f) : f_(f), type_(TYPE_FLOAT), min_bit_width_(WidthF(f)) {}
uint8_t StoredPackedType(BitWidth parent_bit_width_= BIT_WIDTH_8) const {
uint8_t StoredPackedType(BitWidth parent_bit_width_ = BIT_WIDTH_8) const {
return PackedType(StoredWidth(parent_bit_width_), type_);
}
@@ -1315,15 +1349,15 @@ class Builder FLATBUFFERS_FINAL_CLASS {
byte_width <= sizeof(flatbuffers::largest_scalar_t);
byte_width *= 2) {
// Where are we going to write this offset?
auto offset_loc =
buf_size +
flatbuffers::PaddingBytes(buf_size, byte_width) +
elem_index * byte_width;
auto offset_loc = buf_size +
flatbuffers::PaddingBytes(buf_size, byte_width) +
elem_index * byte_width;
// Compute relative offset.
auto offset = offset_loc - u_;
// Does it fit?
auto bit_width = WidthU(offset);
if (static_cast<size_t>(static_cast<size_t>(1U) << bit_width) == byte_width)
if (static_cast<size_t>(static_cast<size_t>(1U) << bit_width) ==
byte_width)
return bit_width;
}
assert(false); // Must match one of the sizes above.
@@ -1333,9 +1367,9 @@ class Builder FLATBUFFERS_FINAL_CLASS {
BitWidth StoredWidth(BitWidth parent_bit_width_ = BIT_WIDTH_8) const {
if (IsInline(type_)) {
return (std::max)(min_bit_width_, parent_bit_width_);
return (std::max)(min_bit_width_, parent_bit_width_);
} else {
return min_bit_width_;
return min_bit_width_;
}
}
};
@@ -1343,19 +1377,11 @@ class Builder FLATBUFFERS_FINAL_CLASS {
void WriteAny(const Value &val, uint8_t byte_width) {
switch (val.type_) {
case TYPE_NULL:
case TYPE_INT:
Write(val.i_, byte_width);
break;
case TYPE_INT: Write(val.i_, byte_width); break;
case TYPE_BOOL:
case TYPE_UINT:
Write(val.u_, byte_width);
break;
case TYPE_FLOAT:
WriteDouble(val.f_, byte_width);
break;
default:
WriteOffset(val.u_, byte_width);
break;
case TYPE_UINT: Write(val.u_, byte_width); break;
case TYPE_FLOAT: WriteDouble(val.f_, byte_width); break;
default: WriteOffset(val.u_, byte_width); break;
}
}
@@ -1369,8 +1395,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
return sloc;
}
template<typename T> size_t ScalarVector(const T *elems, size_t len,
bool fixed) {
template<typename T>
size_t ScalarVector(const T *elems, size_t len, bool fixed) {
auto vector_type = GetScalarType<T>();
auto byte_width = sizeof(T);
auto bit_width = WidthB(byte_width);
@@ -1436,12 +1462,11 @@ class Builder FLATBUFFERS_FINAL_CLASS {
buf_.push_back(stack_[i].StoredPackedType(bit_width));
}
}
return Value(static_cast<uint64_t>(vloc), keys
? TYPE_MAP
: (typed
? ToTypedVector(vector_type, fixed ? vec_len : 0)
: TYPE_VECTOR),
bit_width);
return Value(static_cast<uint64_t>(vloc),
keys ? TYPE_MAP
: (typed ? ToTypedVector(vector_type, fixed ? vec_len : 0)
: TYPE_VECTOR),
bit_width);
}
// You shouldn't really be copying instances of this class.
@@ -1473,10 +1498,10 @@ class Builder FLATBUFFERS_FINAL_CLASS {
struct StringOffsetCompare {
StringOffsetCompare(const std::vector<uint8_t> &buf) : buf_(&buf) {}
bool operator()(const StringOffset &a, const StringOffset &b) const {
auto stra = reinterpret_cast<const char *>(flatbuffers::vector_data(*buf_) +
a.first);
auto strb = reinterpret_cast<const char *>(flatbuffers::vector_data(*buf_) +
b.first);
auto stra = reinterpret_cast<const char *>(
flatbuffers::vector_data(*buf_) + a.first);
auto strb = reinterpret_cast<const char *>(
flatbuffers::vector_data(*buf_) + b.first);
return strncmp(stra, strb, (std::min)(a.second, b.second) + 1) < 0;
}
const std::vector<uint8_t> *buf_;
@@ -1491,8 +1516,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
} // namespace flexbuffers
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
# if defined(_MSC_VER)
# pragma warning(pop)
# endif
#endif // FLATBUFFERS_FLEXBUFFERS_H_