mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 04:04:19 +00:00
clang-all (#6941)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#ifndef FLATBUFFERS_BASE_H_
|
||||
# define FLATBUFFERS_BASE_H_
|
||||
#define FLATBUFFERS_BASE_H_
|
||||
|
||||
// clang-format off
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
#ifndef FLATBUFFERS_FLATBUFFER_BUILDER_H_
|
||||
#define FLATBUFFERS_FLATBUFFER_BUILDER_H_
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "flatbuffers/allocator.h"
|
||||
#include "flatbuffers/array.h"
|
||||
#include "flatbuffers/base.h"
|
||||
@@ -31,8 +33,6 @@
|
||||
#include "flatbuffers/vector_downward.h"
|
||||
#include "flatbuffers/verifier.h"
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
// Converts a Field ID to a virtual table offset.
|
||||
@@ -96,18 +96,19 @@ class FlatBufferBuilder {
|
||||
|
||||
/// @brief Move constructor for FlatBufferBuilder.
|
||||
FlatBufferBuilder(FlatBufferBuilder &&other)
|
||||
: buf_(1024, nullptr, false, AlignOf<largest_scalar_t>()),
|
||||
num_field_loc(0),
|
||||
max_voffset_(0),
|
||||
nested(false),
|
||||
finished(false),
|
||||
minalign_(1),
|
||||
force_defaults_(false),
|
||||
dedup_vtables_(true),
|
||||
string_pool(nullptr) {
|
||||
: buf_(1024, nullptr, false, AlignOf<largest_scalar_t>()),
|
||||
num_field_loc(0),
|
||||
max_voffset_(0),
|
||||
nested(false),
|
||||
finished(false),
|
||||
minalign_(1),
|
||||
force_defaults_(false),
|
||||
dedup_vtables_(true),
|
||||
string_pool(nullptr) {
|
||||
EndianCheck();
|
||||
// Default construct and swap idiom.
|
||||
// Lack of delegating constructors in vs2010 makes it more verbose than needed.
|
||||
// Lack of delegating constructors in vs2010 makes it more verbose than
|
||||
// needed.
|
||||
Swap(other);
|
||||
}
|
||||
|
||||
@@ -676,8 +677,9 @@ class FlatBufferBuilder {
|
||||
/// returns any type that you can construct a FlatBuffers vector out of.
|
||||
/// @return Returns a typed `Offset` into the serialized data indicating
|
||||
/// where the vector is stored.
|
||||
template<typename T> Offset<Vector<T>> CreateVector(size_t vector_size,
|
||||
const std::function<T (size_t i)> &f) {
|
||||
template<typename T>
|
||||
Offset<Vector<T>> CreateVector(size_t vector_size,
|
||||
const std::function<T(size_t i)> &f) {
|
||||
FLATBUFFERS_ASSERT(FLATBUFFERS_GENERAL_HEAP_ALLOC_OK);
|
||||
std::vector<T> elems(vector_size);
|
||||
for (size_t i = 0; i < vector_size; i++) elems[i] = f(i);
|
||||
@@ -795,15 +797,16 @@ class FlatBufferBuilder {
|
||||
|
||||
/// @brief Serialize an array of structs into a FlatBuffer `vector`.
|
||||
/// @tparam T The data type of the struct array elements.
|
||||
/// @param[in] filler A function that takes the current iteration 0..vector_size-1
|
||||
/// and a pointer to the struct that must be filled.
|
||||
/// @param[in] filler A function that takes the current iteration
|
||||
/// 0..vector_size-1 and a pointer to the struct that must be filled.
|
||||
/// @return Returns a typed `Offset` into the serialized data indicating
|
||||
/// where the vector is stored.
|
||||
/// This is mostly useful when flatbuffers are generated with mutation
|
||||
/// accessors.
|
||||
template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
|
||||
template<typename T>
|
||||
Offset<Vector<const T *>> CreateVectorOfStructs(
|
||||
size_t vector_size, const std::function<void(size_t i, T *)> &filler) {
|
||||
T* structs = StartVectorOfStructs<T>(vector_size);
|
||||
T *structs = StartVectorOfStructs<T>(vector_size);
|
||||
for (size_t i = 0; i < vector_size; i++) {
|
||||
filler(i, structs);
|
||||
structs++;
|
||||
|
||||
@@ -92,9 +92,9 @@ struct NativeTable {};
|
||||
/// if you wish. The resolver does the opposite lookup, for when the object
|
||||
/// is being serialized again.
|
||||
typedef uint64_t hash_value_t;
|
||||
typedef std::function<void (void **pointer_adr, hash_value_t hash)>
|
||||
resolver_function_t;
|
||||
typedef std::function<hash_value_t (void *pointer)> rehasher_function_t;
|
||||
typedef std::function<void(void **pointer_adr, hash_value_t hash)>
|
||||
resolver_function_t;
|
||||
typedef std::function<hash_value_t(void *pointer)> rehasher_function_t;
|
||||
|
||||
// Helper function to test if a field is present, using any of the field
|
||||
// enums in the generated code.
|
||||
|
||||
@@ -1132,23 +1132,23 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
||||
// sorted fashion.
|
||||
// std::sort is typically already a lot faster on sorted data though.
|
||||
auto dict = reinterpret_cast<TwoValue *>(stack_.data() + start);
|
||||
std::sort(dict, dict + len,
|
||||
[&](const TwoValue &a, const TwoValue &b) -> bool {
|
||||
auto as = reinterpret_cast<const char *>(buf_.data() + a.key.u_);
|
||||
auto bs = reinterpret_cast<const char *>(buf_.data() + b.key.u_);
|
||||
auto comp = strcmp(as, bs);
|
||||
// We want to disallow duplicate keys, since this results in a
|
||||
// map where values cannot be found.
|
||||
// But we can't assert here (since we don't want to fail on
|
||||
// random JSON input) or have an error mechanism.
|
||||
// Instead, we set has_duplicate_keys_ in the builder to
|
||||
// signal this.
|
||||
// TODO: Have to check for pointer equality, as some sort
|
||||
// implementation apparently call this function with the same
|
||||
// element?? Why?
|
||||
if (!comp && &a != &b) has_duplicate_keys_ = true;
|
||||
return comp < 0;
|
||||
});
|
||||
std::sort(
|
||||
dict, dict + len, [&](const TwoValue &a, const TwoValue &b) -> bool {
|
||||
auto as = reinterpret_cast<const char *>(buf_.data() + a.key.u_);
|
||||
auto bs = reinterpret_cast<const char *>(buf_.data() + b.key.u_);
|
||||
auto comp = strcmp(as, bs);
|
||||
// We want to disallow duplicate keys, since this results in a
|
||||
// map where values cannot be found.
|
||||
// But we can't assert here (since we don't want to fail on
|
||||
// random JSON input) or have an error mechanism.
|
||||
// Instead, we set has_duplicate_keys_ in the builder to
|
||||
// signal this.
|
||||
// TODO: Have to check for pointer equality, as some sort
|
||||
// implementation apparently call this function with the same
|
||||
// element?? Why?
|
||||
if (!comp && &a != &b) has_duplicate_keys_ = true;
|
||||
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.
|
||||
@@ -1404,12 +1404,10 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
||||
|
||||
template<typename T> static Type GetScalarType() {
|
||||
static_assert(flatbuffers::is_scalar<T>::value, "Unrelated types");
|
||||
return flatbuffers::is_floating_point<T>::value
|
||||
? FBT_FLOAT
|
||||
: flatbuffers::is_same<T, bool>::value
|
||||
? FBT_BOOL
|
||||
: (flatbuffers::is_unsigned<T>::value ? FBT_UINT
|
||||
: FBT_INT);
|
||||
return flatbuffers::is_floating_point<T>::value ? FBT_FLOAT
|
||||
: flatbuffers::is_same<T, bool>::value
|
||||
? FBT_BOOL
|
||||
: (flatbuffers::is_unsigned<T>::value ? FBT_UINT : FBT_INT);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#ifndef FLATBUFFERS_IDL_H_
|
||||
#define FLATBUFFERS_IDL_H_
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <stack>
|
||||
@@ -27,8 +28,6 @@
|
||||
#include "flatbuffers/hash.h"
|
||||
#include "flatbuffers/reflection.h"
|
||||
|
||||
#include <functional>
|
||||
|
||||
// This file defines the data types representing a parsed IDL (Interface
|
||||
// Definition Language) / schema file.
|
||||
|
||||
|
||||
@@ -702,7 +702,9 @@ class CppGenerator : public BaseGenerator {
|
||||
}
|
||||
case BASE_TYPE_UNION:
|
||||
// fall through
|
||||
default: { return "void"; }
|
||||
default: {
|
||||
return "void";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1044,10 +1046,9 @@ class CppGenerator : public BaseGenerator {
|
||||
? bt - BASE_TYPE_UTYPE + ET_UTYPE
|
||||
: ET_SEQUENCE;
|
||||
int ref_idx = -1;
|
||||
std::string ref_name =
|
||||
type.struct_def
|
||||
? WrapInNameSpace(*type.struct_def)
|
||||
: type.enum_def ? WrapInNameSpace(*type.enum_def) : "";
|
||||
std::string ref_name = type.struct_def ? WrapInNameSpace(*type.struct_def)
|
||||
: type.enum_def ? WrapInNameSpace(*type.enum_def)
|
||||
: "";
|
||||
if (!ref_name.empty()) {
|
||||
auto rit = type_refs.begin();
|
||||
for (; rit != type_refs.end(); ++rit) {
|
||||
@@ -2000,7 +2001,9 @@ class CppGenerator : public BaseGenerator {
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: { break; }
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2258,7 +2261,9 @@ class CppGenerator : public BaseGenerator {
|
||||
code_ += " bool mutate_{{FIELD_NAME}}({{FIELD_TYPE}} _{{FIELD_NAME}}\\";
|
||||
if (false == field.IsScalarOptional()) {
|
||||
code_.SetValue("DEFAULT_VALUE", GenDefaultConstant(field));
|
||||
code_.SetValue("INTERFACE_DEFAULT_VALUE", GenUnderlyingCast(field, true, GenDefaultConstant(field)));
|
||||
code_.SetValue(
|
||||
"INTERFACE_DEFAULT_VALUE",
|
||||
GenUnderlyingCast(field, true, GenDefaultConstant(field)));
|
||||
|
||||
// GenUnderlyingCast for a bool field generates 0 != 0
|
||||
// So the type has to be checked and the appropriate default chosen
|
||||
|
||||
@@ -44,7 +44,8 @@ class CSharpGenerator : public BaseGenerator {
|
||||
public:
|
||||
CSharpGenerator(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name)
|
||||
: BaseGenerator(parser, path, file_name, parser.opts.cs_global_alias ? "global::" : "", ".", "cs"),
|
||||
: BaseGenerator(parser, path, file_name,
|
||||
parser.opts.cs_global_alias ? "global::" : "", ".", "cs"),
|
||||
cur_name_space_(nullptr) {
|
||||
// clang-format off
|
||||
|
||||
@@ -305,11 +306,13 @@ class CSharpGenerator : public BaseGenerator {
|
||||
// would be cast down to int before being put onto the buffer. In C#, one cast
|
||||
// directly cast an Enum to its underlying type, which is essential before
|
||||
// putting it onto the buffer.
|
||||
std::string SourceCast(const Type &type, const bool isOptional=false) const {
|
||||
std::string SourceCast(const Type &type,
|
||||
const bool isOptional = false) const {
|
||||
if (IsSeries(type)) {
|
||||
return SourceCast(type.VectorType());
|
||||
} else {
|
||||
if (IsEnum(type)) return "(" + GenTypeBasic(type, false) + (isOptional ? "?": "") + ")";
|
||||
if (IsEnum(type))
|
||||
return "(" + GenTypeBasic(type, false) + (isOptional ? "?" : "") + ")";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
@@ -1221,8 +1224,10 @@ class CSharpGenerator : public BaseGenerator {
|
||||
code += "Add";
|
||||
code += GenMethod(vector_type);
|
||||
code += "(";
|
||||
// At the moment there is no support of the type Vector with optional enum,
|
||||
// e.g. if we have enum type SomeEnum there is no way to define `SomeEmum?[] enums` in FlatBuffer schema, so isOptional = false
|
||||
// At the moment there is no support of the type Vector with
|
||||
// optional enum, e.g. if we have enum type SomeEnum there is no way
|
||||
// to define `SomeEmum?[] enums` in FlatBuffer schema, so isOptional
|
||||
// = false
|
||||
code += SourceCastBasic(vector_type, false);
|
||||
code += "data[i]";
|
||||
if (vector_type.base_type == BASE_TYPE_STRUCT ||
|
||||
@@ -1641,11 +1646,10 @@ class CSharpGenerator : public BaseGenerator {
|
||||
case BASE_TYPE_ARRAY: {
|
||||
auto type_name = GenTypeGet_ObjectAPI(field.value.type, opts);
|
||||
auto length_str = NumToString(field.value.type.fixed_length);
|
||||
auto unpack_method = field.value.type.struct_def == nullptr
|
||||
? ""
|
||||
: field.value.type.struct_def->fixed
|
||||
? ".UnPack()"
|
||||
: "?.UnPack()";
|
||||
auto unpack_method = field.value.type.struct_def == nullptr ? ""
|
||||
: field.value.type.struct_def->fixed
|
||||
? ".UnPack()"
|
||||
: "?.UnPack()";
|
||||
code += start + "new " + type_name.substr(0, type_name.length() - 1) +
|
||||
length_str + "];\n";
|
||||
code += " for (var _j = 0; _j < " + length_str + "; ++_j) { _o." +
|
||||
|
||||
@@ -130,7 +130,9 @@ std::string GenType(const Type &type) {
|
||||
return union_type_string;
|
||||
}
|
||||
case BASE_TYPE_UTYPE: return GenTypeRef(type.enum_def);
|
||||
default: { return GenBaseType(type); }
|
||||
default: {
|
||||
return GenBaseType(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,9 +176,10 @@ class JsonSchemaGenerator : public BaseGenerator {
|
||||
// remove leading and trailing spaces from comment line
|
||||
const auto start = std::find_if(comment_line.begin(), comment_line.end(),
|
||||
[](char c) { return !isspace(c); });
|
||||
const auto end = std::find_if(comment_line.rbegin(), comment_line.rend(),
|
||||
[](char c) { return !isspace(c); })
|
||||
.base();
|
||||
const auto end =
|
||||
std::find_if(comment_line.rbegin(), comment_line.rend(), [](char c) {
|
||||
return !isspace(c);
|
||||
}).base();
|
||||
if (start < end) {
|
||||
comment.append(start, end);
|
||||
} else {
|
||||
@@ -198,9 +201,9 @@ class JsonSchemaGenerator : public BaseGenerator {
|
||||
|
||||
bool generate() {
|
||||
code_ = "";
|
||||
if (parser_.root_struct_def_ == nullptr) {
|
||||
if (parser_.root_struct_def_ == nullptr) {
|
||||
std::cerr << "Error: Binary schema not generated, no root struct found\n";
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
code_ += "{" + NewLine();
|
||||
code_ += Indent(1) +
|
||||
|
||||
@@ -300,15 +300,15 @@ class KotlinGenerator : public BaseGenerator {
|
||||
}
|
||||
writer += ")";
|
||||
});
|
||||
GenerateFunOneLine(writer, "name", "e: Int", "String",
|
||||
[&]() {
|
||||
writer += "names[e\\";
|
||||
if (enum_def.MinValue()->IsNonZero())
|
||||
writer += " - " + enum_def.MinValue()->name +
|
||||
".toInt()\\";
|
||||
writer += "]";
|
||||
},
|
||||
parser_.opts.gen_jvmstatic);
|
||||
GenerateFunOneLine(
|
||||
writer, "name", "e: Int", "String",
|
||||
[&]() {
|
||||
writer += "names[e\\";
|
||||
if (enum_def.MinValue()->IsNonZero())
|
||||
writer += " - " + enum_def.MinValue()->name + ".toInt()\\";
|
||||
writer += "]";
|
||||
},
|
||||
parser_.opts.gen_jvmstatic);
|
||||
}
|
||||
});
|
||||
writer.DecrementIdentLevel();
|
||||
@@ -422,7 +422,8 @@ class KotlinGenerator : public BaseGenerator {
|
||||
(nameprefix + (field.name + "_")).c_str());
|
||||
} else {
|
||||
writer.SetValue("type", GenMethod(field.value.type));
|
||||
writer.SetValue("argname", nameprefix + MakeCamel(Esc(field.name), false));
|
||||
writer.SetValue("argname",
|
||||
nameprefix + MakeCamel(Esc(field.name), false));
|
||||
writer.SetValue("cast", CastToSigned(field.value.type));
|
||||
writer += "builder.put{{type}}({{argname}}{{cast}})";
|
||||
}
|
||||
@@ -622,9 +623,10 @@ class KotlinGenerator : public BaseGenerator {
|
||||
auto id = identifier.length() > 0 ? ", \"" + identifier + "\"" : "";
|
||||
auto params = "builder: FlatBufferBuilder, offset: Int";
|
||||
auto method_name = "finish" + Esc(struct_def.name) + "Buffer";
|
||||
GenerateFunOneLine(writer, method_name, params, "",
|
||||
[&]() { writer += "builder.finish(offset" + id + ")"; },
|
||||
options.gen_jvmstatic);
|
||||
GenerateFunOneLine(
|
||||
writer, method_name, params, "",
|
||||
[&]() { writer += "builder.finish(offset" + id + ")"; },
|
||||
options.gen_jvmstatic);
|
||||
}
|
||||
|
||||
void GenerateEndStructMethod(StructDef &struct_def, CodeWriter &writer,
|
||||
@@ -635,21 +637,21 @@ class KotlinGenerator : public BaseGenerator {
|
||||
auto returns = "Int";
|
||||
auto field_vec = struct_def.fields.vec;
|
||||
|
||||
GenerateFun(writer, name, params, returns,
|
||||
[&]() {
|
||||
writer += "val o = builder.endTable()";
|
||||
writer.IncrementIdentLevel();
|
||||
for (auto it = field_vec.begin(); it != field_vec.end();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (field.deprecated || !field.IsRequired()) { continue; }
|
||||
writer.SetValue("offset", NumToString(field.value.offset));
|
||||
writer += "builder.required(o, {{offset}})";
|
||||
}
|
||||
writer.DecrementIdentLevel();
|
||||
writer += "return o";
|
||||
},
|
||||
options.gen_jvmstatic);
|
||||
GenerateFun(
|
||||
writer, name, params, returns,
|
||||
[&]() {
|
||||
writer += "val o = builder.endTable()";
|
||||
writer.IncrementIdentLevel();
|
||||
for (auto it = field_vec.begin(); it != field_vec.end(); ++it) {
|
||||
auto &field = **it;
|
||||
if (field.deprecated || !field.IsRequired()) { continue; }
|
||||
writer.SetValue("offset", NumToString(field.value.offset));
|
||||
writer += "builder.required(o, {{offset}})";
|
||||
}
|
||||
writer.DecrementIdentLevel();
|
||||
writer += "return o";
|
||||
},
|
||||
options.gen_jvmstatic);
|
||||
}
|
||||
|
||||
// Generate a method to create a vector from a Kotlin array.
|
||||
@@ -664,18 +666,18 @@ class KotlinGenerator : public BaseGenerator {
|
||||
writer.SetValue("root", GenMethod(vector_type));
|
||||
writer.SetValue("cast", CastToSigned(vector_type));
|
||||
|
||||
GenerateFun(writer, method_name, params, "Int",
|
||||
[&]() {
|
||||
writer +=
|
||||
"builder.startVector({{size}}, data.size, {{align}})";
|
||||
writer += "for (i in data.size - 1 downTo 0) {";
|
||||
writer.IncrementIdentLevel();
|
||||
writer += "builder.add{{root}}(data[i]{{cast}})";
|
||||
writer.DecrementIdentLevel();
|
||||
writer += "}";
|
||||
writer += "return builder.endVector()";
|
||||
},
|
||||
options.gen_jvmstatic);
|
||||
GenerateFun(
|
||||
writer, method_name, params, "Int",
|
||||
[&]() {
|
||||
writer += "builder.startVector({{size}}, data.size, {{align}})";
|
||||
writer += "for (i in data.size - 1 downTo 0) {";
|
||||
writer.IncrementIdentLevel();
|
||||
writer += "builder.add{{root}}(data[i]{{cast}})";
|
||||
writer.DecrementIdentLevel();
|
||||
writer += "}";
|
||||
writer += "return builder.endVector()";
|
||||
},
|
||||
options.gen_jvmstatic);
|
||||
}
|
||||
|
||||
void GenerateStartVectorField(FieldDef &field, CodeWriter &writer,
|
||||
@@ -701,21 +703,21 @@ class KotlinGenerator : public BaseGenerator {
|
||||
auto field_type = GenTypeBasic(field.value.type.base_type);
|
||||
auto secondArg = MakeCamel(Esc(field.name), false) + ": " + field_type;
|
||||
|
||||
GenerateFunOneLine(writer, "add" + MakeCamel(Esc(field.name), true),
|
||||
"builder: FlatBufferBuilder, " + secondArg, "",
|
||||
[&]() {
|
||||
auto method = GenMethod(field.value.type);
|
||||
writer.SetValue("field_name",
|
||||
MakeCamel(Esc(field.name), false));
|
||||
writer.SetValue("method_name", method);
|
||||
writer.SetValue("pos", field_pos);
|
||||
writer.SetValue("default", GenFBBDefaultValue(field));
|
||||
writer.SetValue("cast", GenFBBValueCast(field));
|
||||
GenerateFunOneLine(
|
||||
writer, "add" + MakeCamel(Esc(field.name), true),
|
||||
"builder: FlatBufferBuilder, " + secondArg, "",
|
||||
[&]() {
|
||||
auto method = GenMethod(field.value.type);
|
||||
writer.SetValue("field_name", MakeCamel(Esc(field.name), false));
|
||||
writer.SetValue("method_name", method);
|
||||
writer.SetValue("pos", field_pos);
|
||||
writer.SetValue("default", GenFBBDefaultValue(field));
|
||||
writer.SetValue("cast", GenFBBValueCast(field));
|
||||
|
||||
writer += "builder.add{{method_name}}({{pos}}, \\";
|
||||
writer += "{{field_name}}{{cast}}, {{default}})";
|
||||
},
|
||||
options.gen_jvmstatic);
|
||||
writer += "builder.add{{method_name}}({{pos}}, \\";
|
||||
writer += "{{field_name}}{{cast}}, {{default}})";
|
||||
},
|
||||
options.gen_jvmstatic);
|
||||
}
|
||||
|
||||
static std::string ToSignedType(const Type &type) {
|
||||
@@ -1011,8 +1013,9 @@ class KotlinGenerator : public BaseGenerator {
|
||||
break;
|
||||
default: found = "{{bbgetter}}({{index}}){{ucast}}";
|
||||
}
|
||||
OffsetWrapper(writer, offset_val, [&]() { writer += found; },
|
||||
[&]() { writer += not_found; });
|
||||
OffsetWrapper(
|
||||
writer, offset_val, [&]() { writer += found; },
|
||||
[&]() { writer += not_found; });
|
||||
});
|
||||
break;
|
||||
}
|
||||
@@ -1164,13 +1167,13 @@ class KotlinGenerator : public BaseGenerator {
|
||||
if (struct_def.fixed) {
|
||||
writer += "{{bbsetter}}({{index}}, {{params}}{{cast}})";
|
||||
} else {
|
||||
OffsetWrapper(writer, offset_val,
|
||||
[&]() {
|
||||
writer +=
|
||||
"{{bbsetter}}({{index}}, {{params}}{{cast}})";
|
||||
writer += "true";
|
||||
},
|
||||
[&]() { writer += "false"; });
|
||||
OffsetWrapper(
|
||||
writer, offset_val,
|
||||
[&]() {
|
||||
writer += "{{bbsetter}}({{index}}, {{params}}{{cast}})";
|
||||
writer += "true";
|
||||
},
|
||||
[&]() { writer += "false"; });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1313,12 +1316,13 @@ class KotlinGenerator : public BaseGenerator {
|
||||
const IDLOptions options) {
|
||||
// create a struct constructor function
|
||||
auto params = StructConstructorParams(struct_def);
|
||||
GenerateFun(code, "create" + Esc(struct_def.name), params, "Int",
|
||||
[&]() {
|
||||
GenStructBody(struct_def, code, "");
|
||||
code += "return builder.offset()";
|
||||
},
|
||||
options.gen_jvmstatic);
|
||||
GenerateFun(
|
||||
code, "create" + Esc(struct_def.name), params, "Int",
|
||||
[&]() {
|
||||
GenStructBody(struct_def, code, "");
|
||||
code += "return builder.offset()";
|
||||
},
|
||||
options.gen_jvmstatic);
|
||||
}
|
||||
|
||||
static std::string StructConstructorParams(const StructDef &struct_def,
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace flatbuffers {
|
||||
namespace python {
|
||||
|
||||
// Hardcode spaces per indentation.
|
||||
const CommentConfig def_comment = {nullptr, "#", nullptr};
|
||||
const CommentConfig def_comment = { nullptr, "#", nullptr };
|
||||
const std::string Indent = " ";
|
||||
|
||||
class PythonGenerator : public BaseGenerator {
|
||||
@@ -42,11 +42,12 @@ class PythonGenerator : public BaseGenerator {
|
||||
"" /* not used */, "py"),
|
||||
float_const_gen_("float('nan')", "float('inf')", "float('-inf')") {
|
||||
static const char *const keywords[] = {
|
||||
"False", "None", "True", "and", "as", "assert", "break",
|
||||
"class", "continue", "def", "del", "elif", "else", "except",
|
||||
"finally", "for", "from", "global", "if", "import", "in",
|
||||
"is", "lambda", "nonlocal", "not", "or", "pass", "raise",
|
||||
"return", "try", "while", "with", "yield"};
|
||||
"False", "None", "True", "and", "as", "assert", "break",
|
||||
"class", "continue", "def", "del", "elif", "else", "except",
|
||||
"finally", "for", "from", "global", "if", "import", "in",
|
||||
"is", "lambda", "nonlocal", "not", "or", "pass", "raise",
|
||||
"return", "try", "while", "with", "yield"
|
||||
};
|
||||
keywords_.insert(std::begin(keywords), std::end(keywords));
|
||||
}
|
||||
|
||||
@@ -141,8 +142,8 @@ class PythonGenerator : public BaseGenerator {
|
||||
code += NormalizedName(struct_def);
|
||||
code += "(cls, buf, offset=0):\n";
|
||||
code +=
|
||||
Indent + Indent +
|
||||
"\"\"\"This method is deprecated. Please switch to GetRootAs.\"\"\"\n";
|
||||
Indent + Indent +
|
||||
"\"\"\"This method is deprecated. Please switch to GetRootAs.\"\"\"\n";
|
||||
code += Indent + Indent + "return cls.GetRootAs(buf, offset)\n";
|
||||
}
|
||||
|
||||
@@ -206,9 +207,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
code += OffsetPrefix(field);
|
||||
getter += "o + self._tab.Pos)";
|
||||
auto is_bool = IsBool(field.value.type.base_type);
|
||||
if (is_bool) {
|
||||
getter = "bool(" + getter + ")";
|
||||
}
|
||||
if (is_bool) { getter = "bool(" + getter + ")"; }
|
||||
code += Indent + Indent + Indent + "return " + getter + "\n";
|
||||
std::string default_value;
|
||||
if (is_bool) {
|
||||
@@ -398,9 +397,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
|
||||
// Currently, we only support accessing as numpy array if
|
||||
// the vector type is a scalar.
|
||||
if (!(IsScalar(vectortype.base_type))) {
|
||||
return;
|
||||
}
|
||||
if (!(IsScalar(vectortype.base_type))) { return; }
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += MakeCamel(NormalizedName(field)) + "AsNumpy(self):";
|
||||
@@ -425,9 +422,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
auto nested = field.attributes.Lookup("nested_flatbuffer");
|
||||
if (!nested) {
|
||||
return;
|
||||
} // There is no nested flatbuffer.
|
||||
if (!nested) { return; } // There is no nested flatbuffer.
|
||||
|
||||
std::string unqualified_name = nested->constant;
|
||||
std::string qualified_name = nested->constant;
|
||||
@@ -490,9 +485,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
} else {
|
||||
auto &code = *code_ptr;
|
||||
code += std::string(", ") + nameprefix;
|
||||
if (has_field_name) {
|
||||
code += MakeCamel(NormalizedName(field), false);
|
||||
}
|
||||
if (has_field_name) { code += MakeCamel(NormalizedName(field), false); }
|
||||
code += namesuffix;
|
||||
}
|
||||
}
|
||||
@@ -646,9 +639,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
auto nested = field.attributes.Lookup("nested_flatbuffer");
|
||||
if (!nested) {
|
||||
return;
|
||||
} // There is no nested flatbuffer.
|
||||
if (!nested) { return; } // There is no nested flatbuffer.
|
||||
|
||||
std::string unqualified_name = nested->constant;
|
||||
std::string qualified_name = nested->constant;
|
||||
@@ -743,11 +734,8 @@ class PythonGenerator : public BaseGenerator {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_UNION:
|
||||
GetUnionField(struct_def, field, code_ptr);
|
||||
break;
|
||||
default:
|
||||
FLATBUFFERS_ASSERT(0);
|
||||
case BASE_TYPE_UNION: GetUnionField(struct_def, field, code_ptr); break;
|
||||
default: FLATBUFFERS_ASSERT(0);
|
||||
}
|
||||
}
|
||||
if (IsVector(field.value.type) || IsArray(field.value.type)) {
|
||||
@@ -918,14 +906,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
import_list->insert("import " + package_reference);
|
||||
}
|
||||
break;
|
||||
case BASE_TYPE_STRING:
|
||||
field_type += "str";
|
||||
break;
|
||||
case BASE_TYPE_NONE:
|
||||
field_type += "None";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
case BASE_TYPE_STRING: field_type += "str"; break;
|
||||
case BASE_TYPE_NONE: field_type += "None"; break;
|
||||
default: break;
|
||||
}
|
||||
field_types += field_type + separator_string;
|
||||
}
|
||||
@@ -1258,8 +1241,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
GenUnPackForScalarVector(struct_def, field, &code);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
GenUnPackForScalar(struct_def, field, &code);
|
||||
default: GenUnPackForScalar(struct_def, field, &code);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1366,45 +1348,19 @@ class PythonGenerator : public BaseGenerator {
|
||||
|
||||
std::string type_name;
|
||||
switch (vectortype.base_type) {
|
||||
case BASE_TYPE_BOOL:
|
||||
type_name = "Bool";
|
||||
break;
|
||||
case BASE_TYPE_CHAR:
|
||||
type_name = "Byte";
|
||||
break;
|
||||
case BASE_TYPE_UCHAR:
|
||||
type_name = "Uint8";
|
||||
break;
|
||||
case BASE_TYPE_SHORT:
|
||||
type_name = "Int16";
|
||||
break;
|
||||
case BASE_TYPE_USHORT:
|
||||
type_name = "Uint16";
|
||||
break;
|
||||
case BASE_TYPE_INT:
|
||||
type_name = "Int32";
|
||||
break;
|
||||
case BASE_TYPE_UINT:
|
||||
type_name = "Uint32";
|
||||
break;
|
||||
case BASE_TYPE_LONG:
|
||||
type_name = "Int64";
|
||||
break;
|
||||
case BASE_TYPE_ULONG:
|
||||
type_name = "Uint64";
|
||||
break;
|
||||
case BASE_TYPE_FLOAT:
|
||||
type_name = "Float32";
|
||||
break;
|
||||
case BASE_TYPE_DOUBLE:
|
||||
type_name = "Float64";
|
||||
break;
|
||||
case BASE_TYPE_STRING:
|
||||
type_name = "UOffsetTRelative";
|
||||
break;
|
||||
default:
|
||||
type_name = "VOffsetT";
|
||||
break;
|
||||
case BASE_TYPE_BOOL: type_name = "Bool"; break;
|
||||
case BASE_TYPE_CHAR: type_name = "Byte"; break;
|
||||
case BASE_TYPE_UCHAR: type_name = "Uint8"; break;
|
||||
case BASE_TYPE_SHORT: type_name = "Int16"; break;
|
||||
case BASE_TYPE_USHORT: type_name = "Uint16"; break;
|
||||
case BASE_TYPE_INT: type_name = "Int32"; break;
|
||||
case BASE_TYPE_UINT: type_name = "Uint32"; break;
|
||||
case BASE_TYPE_LONG: type_name = "Int64"; break;
|
||||
case BASE_TYPE_ULONG: type_name = "Uint64"; break;
|
||||
case BASE_TYPE_FLOAT: type_name = "Float32"; break;
|
||||
case BASE_TYPE_DOUBLE: type_name = "Float64"; break;
|
||||
case BASE_TYPE_STRING: type_name = "UOffsetTRelative"; break;
|
||||
default: type_name = "VOffsetT"; break;
|
||||
}
|
||||
code += type_name;
|
||||
}
|
||||
@@ -1665,8 +1621,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
case BASE_TYPE_STRING:
|
||||
GenUnionCreatorForString(enum_def, ev, &code);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
code += GenIndents(1) + "return None";
|
||||
@@ -1690,12 +1645,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
// Returns the function name that is able to read a value of the given type.
|
||||
std::string GenGetter(const Type &type) {
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_STRING:
|
||||
return "self._tab.String(";
|
||||
case BASE_TYPE_UNION:
|
||||
return "self._tab.Union(";
|
||||
case BASE_TYPE_VECTOR:
|
||||
return GenGetter(type.VectorType());
|
||||
case BASE_TYPE_STRING: return "self._tab.String(";
|
||||
case BASE_TYPE_UNION: return "self._tab.Union(";
|
||||
case BASE_TYPE_VECTOR: return GenGetter(type.VectorType());
|
||||
default:
|
||||
return "self._tab.Get(flatbuffers.number_types." +
|
||||
MakeCamel(GenTypeGet(type)) + "Flags, ";
|
||||
@@ -1725,16 +1677,12 @@ class PythonGenerator : public BaseGenerator {
|
||||
|
||||
std::string GenTypePointer(const Type &type) {
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_STRING:
|
||||
return "string";
|
||||
case BASE_TYPE_VECTOR:
|
||||
return GenTypeGet(type.VectorType());
|
||||
case BASE_TYPE_STRUCT:
|
||||
return type.struct_def->name;
|
||||
case BASE_TYPE_STRING: return "string";
|
||||
case BASE_TYPE_VECTOR: return GenTypeGet(type.VectorType());
|
||||
case BASE_TYPE_STRUCT: return type.struct_def->name;
|
||||
case BASE_TYPE_UNION:
|
||||
// fall through
|
||||
default:
|
||||
return "*flatbuffers.Table";
|
||||
default: return "*flatbuffers.Table";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -264,8 +264,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
code_ += "}";
|
||||
Outdent();
|
||||
code_ += "}\n";
|
||||
if (parser_.opts.gen_json_coders)
|
||||
GenerateJSONEncodingAPIs(struct_def);
|
||||
if (parser_.opts.gen_json_coders) GenerateJSONEncodingAPIs(struct_def);
|
||||
}
|
||||
|
||||
void BuildStructConstructor(const StructDef &struct_def) {
|
||||
@@ -421,8 +420,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
GenerateVerifier(struct_def);
|
||||
Outdent();
|
||||
code_ += "}\n";
|
||||
if (parser_.opts.gen_json_coders)
|
||||
GenerateJSONEncodingAPIs(struct_def);
|
||||
if (parser_.opts.gen_json_coders) GenerateJSONEncodingAPIs(struct_def);
|
||||
}
|
||||
|
||||
// Generates the reader for swift
|
||||
@@ -1196,8 +1194,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
AddMinOrMaxEnumValue(Name(*enum_def.MinValue()), "min");
|
||||
Outdent();
|
||||
code_ += "}\n";
|
||||
if (parser_.opts.gen_json_coders)
|
||||
EnumEncoder(enum_def);
|
||||
if (parser_.opts.gen_json_coders) EnumEncoder(enum_def);
|
||||
code_ += "";
|
||||
if (parser_.opts.generate_object_based_api && enum_def.is_union) {
|
||||
code_ += "{{ACCESS_TYPE}} struct {{ENUM_NAME}}Union {";
|
||||
|
||||
@@ -583,7 +583,8 @@ class TsGenerator : public BaseGenerator {
|
||||
std::string fileName) {
|
||||
ImportDefinition import;
|
||||
import.name = import_name;
|
||||
import.import_statement = "import " + import_name + " from '" + fileName + "';";
|
||||
import.import_statement =
|
||||
"import " + import_name + " from '" + fileName + "';";
|
||||
imports.insert(std::make_pair(import_name, import));
|
||||
}
|
||||
|
||||
|
||||
@@ -330,8 +330,7 @@ uint8_t *ResizeAnyVector(const reflection::Schema &schema, uoffset_t newsize,
|
||||
const reflection::Object *root_table) {
|
||||
auto delta_elem = static_cast<int>(newsize) - static_cast<int>(num_elems);
|
||||
auto delta_bytes = delta_elem * static_cast<int>(elem_size);
|
||||
auto vec_start =
|
||||
reinterpret_cast<const uint8_t *>(vec) - flatbuf->data();
|
||||
auto vec_start = reinterpret_cast<const uint8_t *>(vec) - flatbuf->data();
|
||||
auto start = static_cast<uoffset_t>(vec_start) +
|
||||
static_cast<uoffset_t>(sizeof(uoffset_t)) +
|
||||
elem_size * num_elems;
|
||||
|
||||
@@ -273,8 +273,7 @@ ClassicLocale ClassicLocale::instance_;
|
||||
|
||||
std::string RemoveStringQuotes(const std::string &s) {
|
||||
auto ch = *s.c_str();
|
||||
return ((s.size() >= 2) && (ch == '\"' || ch == '\'') &&
|
||||
(ch == s.back()))
|
||||
return ((s.size() >= 2) && (ch == '\"' || ch == '\'') && (ch == s.back()))
|
||||
? s.substr(1, s.length() - 2)
|
||||
: s;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ struct Vector3D {
|
||||
x = 0;
|
||||
y = 0;
|
||||
z = 0;
|
||||
};
|
||||
}
|
||||
Vector3D(float _x, float _y, float _z) {
|
||||
this->x = _x;
|
||||
this->y = _y;
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
#include "native_type_test_generated.h"
|
||||
#include "test_assert.h"
|
||||
|
||||
// clang-format off
|
||||
// clang-format off
|
||||
// Check that char* and uint8_t* are interoperable types.
|
||||
// The reinterpret_cast<> between the pointers are used to simplify data loading.
|
||||
static_assert(flatbuffers::is_same<uint8_t, char>::value ||
|
||||
@@ -100,9 +100,7 @@ flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {
|
||||
|
||||
// Create a vector of structures from a lambda.
|
||||
auto testv2 = builder.CreateVectorOfStructs<Test>(
|
||||
2, [&](size_t i, Test* s) -> void {
|
||||
*s = tests[i];
|
||||
});
|
||||
2, [&](size_t i, Test *s) -> void { *s = tests[i]; });
|
||||
|
||||
// create monster with very few fields set:
|
||||
// (same functionality as CreateMonster below, but sets fields manually)
|
||||
@@ -1053,7 +1051,7 @@ void ReflectionTest(uint8_t *flatbuf, size_t length) {
|
||||
// This time we wrap the result from GetAnyRoot in a smartpointer that
|
||||
// will keep rroot valid as resizingbuf resizes.
|
||||
auto rroot = flatbuffers::piv(flatbuffers::GetAnyRoot(resizingbuf.data()),
|
||||
resizingbuf);
|
||||
resizingbuf);
|
||||
SetString(schema, "totally new string", GetFieldS(**rroot, name_field),
|
||||
&resizingbuf);
|
||||
// Here resizingbuf has changed, but rroot is still valid.
|
||||
@@ -1596,7 +1594,9 @@ void FuzzTest2() {
|
||||
}
|
||||
}
|
||||
AddToSchemaAndInstances(deprecated ? "(deprecated);\n" : ";\n",
|
||||
deprecated ? "" : is_last_field ? "\n" : ",\n");
|
||||
deprecated ? ""
|
||||
: is_last_field ? "\n"
|
||||
: ",\n");
|
||||
}
|
||||
AddToSchemaAndInstances("}\n\n", "}");
|
||||
}
|
||||
@@ -2991,7 +2991,7 @@ void FlexBuffersTest() {
|
||||
|
||||
// It's possible to do this without std::function support as well.
|
||||
slb.Map([&]() {
|
||||
slb.Vector("vec", [&]() {
|
||||
slb.Vector("vec", [&]() {
|
||||
slb += -100; // Equivalent to slb.Add(-100) or slb.Int(-100);
|
||||
slb += "Fred";
|
||||
slb.IndirectFloat(4.0f);
|
||||
@@ -3004,7 +3004,7 @@ void FlexBuffersTest() {
|
||||
int ints[] = { 1, 2, 3 };
|
||||
slb.Vector("bar", ints, 3);
|
||||
slb.FixedTypedVector("bar3", ints, 3);
|
||||
bool bools[] = {true, false, true, false};
|
||||
bool bools[] = { true, false, true, false };
|
||||
slb.Vector("bools", bools, 4);
|
||||
slb.Bool("bool", true);
|
||||
slb.Double("foo", 100);
|
||||
|
||||
Reference in New Issue
Block a user