mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-07 13:53:38 +00:00
Made all C++ files clang-formatted.
Also added missing generated files. Change-Id: Ifd22a643a08e3f2edfce92812ed57b87fc0e1875
This commit is contained in:
@@ -17,15 +17,14 @@
|
||||
// independent from idl_parser, since this code is not needed for most clients
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/flexbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
#include "flatbuffers/flexbuffers.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
static bool GenStruct(const StructDef &struct_def, const Table *table,
|
||||
int indent, const IDLOptions &opts,
|
||||
std::string *_text);
|
||||
int indent, const IDLOptions &opts, std::string *_text);
|
||||
|
||||
// If indentation is less than 0, that indicates we don't want any newlines
|
||||
// either.
|
||||
@@ -33,9 +32,7 @@ const char *NewLine(const IDLOptions &opts) {
|
||||
return opts.indent_step >= 0 ? "\n" : "";
|
||||
}
|
||||
|
||||
int Indent(const IDLOptions &opts) {
|
||||
return std::max(opts.indent_step, 0);
|
||||
}
|
||||
int Indent(const IDLOptions &opts) { return std::max(opts.indent_step, 0); }
|
||||
|
||||
// Output an identifier with or without quotes depending on strictness.
|
||||
void OutputIdentifier(const std::string &name, const IDLOptions &opts,
|
||||
@@ -49,10 +46,9 @@ void OutputIdentifier(const std::string &name, const IDLOptions &opts,
|
||||
// Print (and its template specialization below for pointers) generate text
|
||||
// for a single FlatBuffer value into JSON format.
|
||||
// The general case for scalars:
|
||||
template<typename T> bool Print(T val, Type type, int /*indent*/,
|
||||
Type * /*union_type*/,
|
||||
const IDLOptions &opts,
|
||||
std::string *_text) {
|
||||
template<typename T>
|
||||
bool Print(T val, Type type, int /*indent*/, Type * /*union_type*/,
|
||||
const IDLOptions &opts, std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
if (type.enum_def && opts.output_enum_identifiers) {
|
||||
auto enum_val = type.enum_def->ReverseLookup(static_cast<int>(val));
|
||||
@@ -72,9 +68,9 @@ template<typename T> bool Print(T val, Type type, int /*indent*/,
|
||||
}
|
||||
|
||||
// Print a vector a sequence of JSON values, comma separated, wrapped in "[]".
|
||||
template<typename T> bool PrintVector(const Vector<T> &v, Type type,
|
||||
int indent, const IDLOptions &opts,
|
||||
std::string *_text) {
|
||||
template<typename T>
|
||||
bool PrintVector(const Vector<T> &v, Type type, int indent,
|
||||
const IDLOptions &opts, std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
text += "[";
|
||||
text += NewLine(opts);
|
||||
@@ -90,8 +86,7 @@ template<typename T> bool PrintVector(const Vector<T> &v, Type type,
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (!Print(v[i], type, indent + Indent(opts), nullptr,
|
||||
opts, _text)) {
|
||||
if (!Print(v[i], type, indent + Indent(opts), nullptr, opts, _text)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -103,11 +98,10 @@ template<typename T> bool PrintVector(const Vector<T> &v, Type type,
|
||||
}
|
||||
|
||||
// Specialization of Print above for pointer types.
|
||||
template<> bool Print<const void *>(const void *val,
|
||||
Type type, int indent,
|
||||
Type *union_type,
|
||||
const IDLOptions &opts,
|
||||
std::string *_text) {
|
||||
template<>
|
||||
bool Print<const void *>(const void *val, Type type, int indent,
|
||||
Type *union_type, const IDLOptions &opts,
|
||||
std::string *_text) {
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_UNION:
|
||||
// If this assert hits, you have an corrupt buffer, a union type field
|
||||
@@ -116,11 +110,8 @@ template<> bool Print<const void *>(const void *val,
|
||||
return Print<const void *>(val, *union_type, indent, nullptr, opts,
|
||||
_text);
|
||||
case BASE_TYPE_STRUCT:
|
||||
if (!GenStruct(*type.struct_def,
|
||||
reinterpret_cast<const Table *>(val),
|
||||
indent,
|
||||
opts,
|
||||
_text)) {
|
||||
if (!GenStruct(*type.struct_def, reinterpret_cast<const Table *>(val),
|
||||
indent, opts, _text)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
@@ -135,6 +126,7 @@ template<> bool Print<const void *>(const void *val,
|
||||
type = type.VectorType();
|
||||
// Call PrintVector above specifically for each element type:
|
||||
switch (type.base_type) {
|
||||
// clang-format off
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
|
||||
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
@@ -146,6 +138,7 @@ template<> bool Print<const void *>(const void *val,
|
||||
break;
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
// clang-format on
|
||||
}
|
||||
break;
|
||||
default: assert(0);
|
||||
@@ -154,31 +147,28 @@ template<> bool Print<const void *>(const void *val,
|
||||
}
|
||||
|
||||
// Generate text for a scalar field.
|
||||
template<typename T> static bool GenField(const FieldDef &fd,
|
||||
const Table *table, bool fixed,
|
||||
const IDLOptions &opts,
|
||||
int indent,
|
||||
std::string *_text) {
|
||||
return Print(fixed ?
|
||||
reinterpret_cast<const Struct *>(table)->GetField<T>(fd.value.offset) :
|
||||
table->GetField<T>(fd.value.offset, 0), fd.value.type, indent, nullptr,
|
||||
opts, _text);
|
||||
template<typename T>
|
||||
static bool GenField(const FieldDef &fd, const Table *table, bool fixed,
|
||||
const IDLOptions &opts, int indent, std::string *_text) {
|
||||
return Print(fixed ? reinterpret_cast<const Struct *>(table)->GetField<T>(
|
||||
fd.value.offset)
|
||||
: table->GetField<T>(fd.value.offset, 0),
|
||||
fd.value.type, indent, nullptr, opts, _text);
|
||||
}
|
||||
|
||||
static bool GenStruct(const StructDef &struct_def, const Table *table,
|
||||
int indent, const IDLOptions &opts,
|
||||
std::string *_text);
|
||||
int indent, const IDLOptions &opts, std::string *_text);
|
||||
|
||||
// Generate text for non-scalar field.
|
||||
static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
|
||||
int indent, Type *union_type,
|
||||
const IDLOptions &opts, std::string *_text) {
|
||||
int indent, Type *union_type, const IDLOptions &opts,
|
||||
std::string *_text) {
|
||||
const void *val = nullptr;
|
||||
if (fixed) {
|
||||
// The only non-scalar fields in structs are structs.
|
||||
assert(IsStruct(fd.value.type));
|
||||
val = reinterpret_cast<const Struct *>(table)->
|
||||
GetStruct<const void *>(fd.value.offset);
|
||||
val = reinterpret_cast<const Struct *>(table)->GetStruct<const void *>(
|
||||
fd.value.offset);
|
||||
} else if (fd.flexbuffer) {
|
||||
auto vec = table->GetPointer<const Vector<uint8_t> *>(fd.value.offset);
|
||||
auto root = flexbuffers::GetRoot(vec->data(), vec->size());
|
||||
@@ -190,8 +180,8 @@ static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
|
||||
return GenStruct(*fd.nested_flatbuffer, root, indent, opts, _text);
|
||||
} else {
|
||||
val = IsStruct(fd.value.type)
|
||||
? table->GetStruct<const void *>(fd.value.offset)
|
||||
: table->GetPointer<const void *>(fd.value.offset);
|
||||
? table->GetStruct<const void *>(fd.value.offset)
|
||||
: table->GetPointer<const void *>(fd.value.offset);
|
||||
}
|
||||
return Print(val, fd.value.type, indent, union_type, opts, _text);
|
||||
}
|
||||
@@ -199,20 +189,17 @@ static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
|
||||
// Generate text for a struct or table, values separated by commas, indented,
|
||||
// and bracketed by "{}"
|
||||
static bool GenStruct(const StructDef &struct_def, const Table *table,
|
||||
int indent, const IDLOptions &opts,
|
||||
std::string *_text) {
|
||||
int indent, const IDLOptions &opts, std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
text += "{";
|
||||
int fieldout = 0;
|
||||
Type *union_type = nullptr;
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
FieldDef &fd = **it;
|
||||
auto is_present = struct_def.fixed || table->CheckField(fd.value.offset);
|
||||
auto output_anyway = opts.output_default_scalars_in_json &&
|
||||
IsScalar(fd.value.type.base_type) &&
|
||||
!fd.deprecated;
|
||||
IsScalar(fd.value.type.base_type) && !fd.deprecated;
|
||||
if (is_present || output_anyway) {
|
||||
if (fieldout++) {
|
||||
if (!opts.protobuf_ascii_alike) text += ",";
|
||||
@@ -222,18 +209,20 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
|
||||
OutputIdentifier(fd.name, opts, _text);
|
||||
if (!opts.protobuf_ascii_alike ||
|
||||
(fd.value.type.base_type != BASE_TYPE_STRUCT &&
|
||||
fd.value.type.base_type != BASE_TYPE_VECTOR)) text += ":";
|
||||
fd.value.type.base_type != BASE_TYPE_VECTOR))
|
||||
text += ":";
|
||||
text += " ";
|
||||
if (is_present) {
|
||||
switch (fd.value.type.base_type) {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
|
||||
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
if (!GenField<CTYPE>(fd, table, struct_def.fixed, \
|
||||
opts, indent + Indent(opts), _text)) { \
|
||||
return false; \
|
||||
} \
|
||||
break;
|
||||
// clang-format off
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
|
||||
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
if (!GenField<CTYPE>(fd, table, struct_def.fixed, \
|
||||
opts, indent + Indent(opts), _text)) { \
|
||||
return false; \
|
||||
} \
|
||||
break;
|
||||
FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
// Generate drop-thru case statements for all pointer types:
|
||||
@@ -247,16 +236,15 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
// clang-format on
|
||||
}
|
||||
if (fd.value.type.base_type == BASE_TYPE_UTYPE) {
|
||||
auto enum_val = fd.value.type.enum_def->ReverseLookup(
|
||||
table->GetField<uint8_t>(fd.value.offset, 0));
|
||||
table->GetField<uint8_t>(fd.value.offset, 0));
|
||||
assert(enum_val);
|
||||
union_type = &enum_val->union_type;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
text += fd.value.constant;
|
||||
}
|
||||
}
|
||||
@@ -272,12 +260,9 @@ bool GenerateText(const Parser &parser, const void *flatbuffer,
|
||||
std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
assert(parser.root_struct_def_); // call SetRootType()
|
||||
text.reserve(1024); // Reduce amount of inevitable reallocs.
|
||||
if (!GenStruct(*parser.root_struct_def_,
|
||||
GetRoot<Table>(flatbuffer),
|
||||
0,
|
||||
parser.opts,
|
||||
_text)) {
|
||||
text.reserve(1024); // Reduce amount of inevitable reallocs.
|
||||
if (!GenStruct(*parser.root_struct_def_, GetRoot<Table>(flatbuffer), 0,
|
||||
parser.opts, _text)) {
|
||||
return false;
|
||||
}
|
||||
text += NewLine(parser.opts);
|
||||
@@ -289,34 +274,29 @@ std::string TextFileName(const std::string &path,
|
||||
return path + file_name + ".json";
|
||||
}
|
||||
|
||||
bool GenerateTextFile(const Parser &parser,
|
||||
const std::string &path,
|
||||
bool GenerateTextFile(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
if (!parser.builder_.GetSize() || !parser.root_struct_def_) return true;
|
||||
std::string text;
|
||||
if (!GenerateText(parser, parser.builder_.GetBufferPointer(), &text)) {
|
||||
return false;
|
||||
}
|
||||
return flatbuffers::SaveFile(TextFileName(path, file_name).c_str(),
|
||||
text,
|
||||
return flatbuffers::SaveFile(TextFileName(path, file_name).c_str(), text,
|
||||
false);
|
||||
}
|
||||
|
||||
std::string TextMakeRule(const Parser &parser,
|
||||
const std::string &path,
|
||||
std::string TextMakeRule(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
if (!parser.builder_.GetSize() || !parser.root_struct_def_) return "";
|
||||
std::string filebase = flatbuffers::StripPath(
|
||||
flatbuffers::StripExtension(file_name));
|
||||
std::string filebase =
|
||||
flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
|
||||
std::string make_rule = TextFileName(path, filebase) + ": " + file_name;
|
||||
auto included_files = parser.GetIncludedFilesRecursive(
|
||||
parser.root_struct_def_->file);
|
||||
for (auto it = included_files.begin();
|
||||
it != included_files.end(); ++it) {
|
||||
auto included_files =
|
||||
parser.GetIncludedFilesRecursive(parser.root_struct_def_->file);
|
||||
for (auto it = included_files.begin(); it != included_files.end(); ++it) {
|
||||
make_rule += " " + *it;
|
||||
}
|
||||
return make_rule;
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
|
||||
Reference in New Issue
Block a user