mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 12:05:50 +00:00
Made all C++ files clang-formatted.
Also added missing generated files. Change-Id: Ifd22a643a08e3f2edfce92812ed57b87fc0e1875
This commit is contained in:
@@ -18,10 +18,10 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "flatbuffers/code_generators.h"
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
#include "flatbuffers/code_generators.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
namespace python {
|
||||
@@ -35,7 +35,6 @@ static std::string GenTypeBasic(const Type &type);
|
||||
static std::string GenTypeGet(const Type &type);
|
||||
static std::string TypeName(const FieldDef &field);
|
||||
|
||||
|
||||
// Hardcode spaces per indentation.
|
||||
const std::string Indent = " ";
|
||||
|
||||
@@ -44,9 +43,8 @@ const std::string Indent = " ";
|
||||
std::string OffsetPrefix(const FieldDef &field) {
|
||||
return "\n" + Indent + Indent +
|
||||
"o = flatbuffers.number_types.UOffsetTFlags.py_type" +
|
||||
"(self._tab.Offset(" +
|
||||
NumToString(field.value.offset) +
|
||||
"))\n" + Indent + Indent + "if o != 0:\n";
|
||||
"(self._tab.Offset(" + NumToString(field.value.offset) + "))\n" +
|
||||
Indent + Indent + "if o != 0:\n";
|
||||
}
|
||||
|
||||
// Begin a class declaration.
|
||||
@@ -109,8 +107,7 @@ static void InitializeExisting(const StructDef &struct_def,
|
||||
}
|
||||
|
||||
// Get the length of a vector.
|
||||
static void GetVectorLen(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
static void GetVectorLen(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
@@ -188,8 +185,7 @@ static void GetStructFieldOfTable(const StructDef &struct_def,
|
||||
}
|
||||
|
||||
// Get the value of a string.
|
||||
static void GetStringField(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
static void GetStringField(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
@@ -202,8 +198,7 @@ static void GetStringField(const StructDef &struct_def,
|
||||
}
|
||||
|
||||
// Get the value of a union from an object.
|
||||
static void GetUnionField(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
static void GetUnionField(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
@@ -284,9 +279,7 @@ static void GetVectorOfNonStructAsNumpy(const StructDef &struct_def,
|
||||
|
||||
// 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(field.name) + "AsNumpy(self):";
|
||||
@@ -319,22 +312,19 @@ static void BeginBuilderArgs(const StructDef &struct_def,
|
||||
// Recursively generate arguments for a constructor, to deal with nested
|
||||
// structs.
|
||||
static void StructBuilderArgs(const StructDef &struct_def,
|
||||
const char *nameprefix,
|
||||
std::string *code_ptr) {
|
||||
const char *nameprefix, std::string *code_ptr) {
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
auto &field = **it;
|
||||
if (IsStruct(field.value.type)) {
|
||||
// Generate arguments for a struct inside a struct. To ensure names
|
||||
// don't clash, and to make it obvious these arguments are constructing
|
||||
// a nested struct, prefix the name with the field name.
|
||||
StructBuilderArgs(*field.value.type.struct_def,
|
||||
(nameprefix + (field.name + "_")).c_str(),
|
||||
code_ptr);
|
||||
(nameprefix + (field.name + "_")).c_str(), code_ptr);
|
||||
} else {
|
||||
std::string &code = *code_ptr;
|
||||
code += (std::string)", " + nameprefix;
|
||||
code += (std::string) ", " + nameprefix;
|
||||
code += MakeCamel(field.name, false);
|
||||
}
|
||||
}
|
||||
@@ -349,21 +339,18 @@ static void EndBuilderArgs(std::string *code_ptr) {
|
||||
// Recursively generate struct construction statements and instert manual
|
||||
// padding.
|
||||
static void StructBuilderBody(const StructDef &struct_def,
|
||||
const char *nameprefix,
|
||||
std::string *code_ptr) {
|
||||
const char *nameprefix, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += " builder.Prep(" + NumToString(struct_def.minalign) + ", ";
|
||||
code += NumToString(struct_def.bytesize) + ")\n";
|
||||
for (auto it = struct_def.fields.vec.rbegin();
|
||||
it != struct_def.fields.vec.rend();
|
||||
++it) {
|
||||
it != struct_def.fields.vec.rend(); ++it) {
|
||||
auto &field = **it;
|
||||
if (field.padding)
|
||||
code += " builder.Pad(" + NumToString(field.padding) + ")\n";
|
||||
if (IsStruct(field.value.type)) {
|
||||
StructBuilderBody(*field.value.type.struct_def,
|
||||
(nameprefix + (field.name + "_")).c_str(),
|
||||
code_ptr);
|
||||
(nameprefix + (field.name + "_")).c_str(), code_ptr);
|
||||
} else {
|
||||
code += " builder.Prepend" + GenMethod(field) + "(";
|
||||
code += nameprefix + MakeCamel(field.name, false) + ")\n";
|
||||
@@ -389,8 +376,7 @@ static void GetStartOfTable(const StructDef &struct_def,
|
||||
|
||||
// Set the value of a table's field.
|
||||
static void BuildFieldOfTable(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
const size_t offset,
|
||||
const FieldDef &field, const size_t offset,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "def " + struct_def.name + "Add" + MakeCamel(field.name);
|
||||
@@ -413,8 +399,7 @@ static void BuildFieldOfTable(const StructDef &struct_def,
|
||||
|
||||
// Set the value of one of the members of a table's vector.
|
||||
static void BuildVectorOfTable(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
const FieldDef &field, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "def " + struct_def.name + "Start";
|
||||
code += MakeCamel(field.name);
|
||||
@@ -445,8 +430,7 @@ static void GenReceiver(const StructDef &struct_def, std::string *code_ptr) {
|
||||
|
||||
// Generate a struct field, conditioned on its child type(s).
|
||||
static void GenStructAccessor(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
const FieldDef &field, std::string *code_ptr) {
|
||||
GenComment(field.doc_comment, code_ptr, nullptr, "# ");
|
||||
if (IsScalar(field.value.type.base_type)) {
|
||||
if (struct_def.fixed) {
|
||||
@@ -463,9 +447,7 @@ static void GenStructAccessor(const StructDef &struct_def,
|
||||
GetStructFieldOfTable(struct_def, field, code_ptr);
|
||||
}
|
||||
break;
|
||||
case BASE_TYPE_STRING:
|
||||
GetStringField(struct_def, field, code_ptr);
|
||||
break;
|
||||
case BASE_TYPE_STRING: GetStringField(struct_def, field, code_ptr); break;
|
||||
case BASE_TYPE_VECTOR: {
|
||||
auto vectortype = field.value.type.VectorType();
|
||||
if (vectortype.base_type == BASE_TYPE_STRUCT) {
|
||||
@@ -476,11 +458,8 @@ static void GenStructAccessor(const StructDef &struct_def,
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_UNION:
|
||||
GetUnionField(struct_def, field, code_ptr);
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
case BASE_TYPE_UNION: GetUnionField(struct_def, field, code_ptr); break;
|
||||
default: assert(0);
|
||||
}
|
||||
}
|
||||
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
|
||||
@@ -494,8 +473,7 @@ static void GenTableBuilders(const StructDef &struct_def,
|
||||
GetStartOfTable(struct_def, code_ptr);
|
||||
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
auto &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
|
||||
@@ -510,8 +488,7 @@ static void GenTableBuilders(const StructDef &struct_def,
|
||||
}
|
||||
|
||||
// Generate struct or table methods.
|
||||
static void GenStruct(const StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
static void GenStruct(const StructDef &struct_def, std::string *code_ptr) {
|
||||
if (struct_def.generated) return;
|
||||
|
||||
GenComment(struct_def.doc_comment, code_ptr, nullptr, "# ");
|
||||
@@ -525,8 +502,7 @@ static void GenStruct(const StructDef &struct_def,
|
||||
// accessor object. This is to allow object reuse.
|
||||
InitializeExisting(struct_def, code_ptr);
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
auto &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
|
||||
@@ -548,8 +524,7 @@ static void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
|
||||
|
||||
GenComment(enum_def.doc_comment, code_ptr, nullptr, "# ");
|
||||
BeginEnum(enum_def.name, code_ptr);
|
||||
for (auto it = enum_def.vals.vec.begin();
|
||||
it != enum_def.vals.vec.end();
|
||||
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
|
||||
++it) {
|
||||
auto &ev = **it;
|
||||
GenComment(ev.doc_comment, code_ptr, nullptr, "# ");
|
||||
@@ -565,49 +540,44 @@ static std::string GenGetter(const Type &type) {
|
||||
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, ";
|
||||
return "self._tab.Get(flatbuffers.number_types." +
|
||||
MakeCamel(GenTypeGet(type)) + "Flags, ";
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the method name for use with add/put calls.
|
||||
static std::string GenMethod(const FieldDef &field) {
|
||||
return IsScalar(field.value.type.base_type)
|
||||
? MakeCamel(GenTypeBasic(field.value.type))
|
||||
: (IsStruct(field.value.type) ? "Struct" : "UOffsetTRelative");
|
||||
? MakeCamel(GenTypeBasic(field.value.type))
|
||||
: (IsStruct(field.value.type) ? "Struct" : "UOffsetTRelative");
|
||||
}
|
||||
|
||||
static std::string GenTypeBasic(const Type &type) {
|
||||
static const char *ctypename[] = {
|
||||
// clang-format off
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
|
||||
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
|
||||
#PTYPE,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
// clang-format on
|
||||
};
|
||||
return ctypename[type.base_type];
|
||||
}
|
||||
|
||||
static 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";
|
||||
}
|
||||
}
|
||||
|
||||
static std::string GenTypeGet(const Type &type) {
|
||||
return IsScalar(type.base_type)
|
||||
? GenTypeBasic(type)
|
||||
: GenTypePointer(type);
|
||||
return IsScalar(type.base_type) ? GenTypeBasic(type) : GenTypePointer(type);
|
||||
}
|
||||
|
||||
static std::string TypeName(const FieldDef &field) {
|
||||
@@ -666,9 +636,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string &code = *code_ptr;
|
||||
code = code + "# " + FlatBuffersGeneratedWarning() + "\n\n";
|
||||
code += "# namespace: " + name_space_name + "\n\n";
|
||||
if (needs_imports) {
|
||||
code += "import flatbuffers\n\n";
|
||||
}
|
||||
if (needs_imports) { code += "import flatbuffers\n\n"; }
|
||||
}
|
||||
|
||||
// Save out the generated code for a Python Table type.
|
||||
@@ -688,8 +656,8 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string code = "";
|
||||
BeginFile(LastNamespacePart(*def.defined_namespace), needs_imports, &code);
|
||||
code += classcode;
|
||||
std::string filename = NamespaceDir(*def.defined_namespace) +
|
||||
def.name + ".py";
|
||||
std::string filename =
|
||||
NamespaceDir(*def.defined_namespace) + def.name + ".py";
|
||||
return SaveFile(filename.c_str(), code, false);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user