Add CodeWriter utility class.

Helps simplify code generation code.  Instead of this:
  code += "inline const " + cpp_qualified_name + " *Get";
  code += name;
  code += "(const void *buf) {\n  return flatbuffers::GetRoot<";
  code += cpp_qualified_name + ">(buf);\n}\n\n";

You do this:
  code.SetValue("NAME", struct_def.name);
  code.SetValue("CPP_NAME", cpp_qualified_name);
  code += "inline const {{CPP_NAME}} *Get{{NAME}}(const void *buf) {";
  code += "  return flatbuffers::GetRoot<{{CPP_NAME}}>(buf);";
  code += "}";
  code += "";

Updated the CPP code generator to use the CodeWriter class.  Most of the
changes in the generated code are white-space changes, esp. around new
lines (since the code generator class automatically appends new lines
when appending a string).  Actual code changes include:

* Renamed "rehasher" to "_rehasher" for consistency with other args in
  Pack function.

* Renamed "union_obj" to "obj: in UnPack function.

* Always do "(void)_o;" to prevent unused variable warning in Create
  function (instead of only doing it if there are no fields) in order
  to avoid two-passes.

* Renamed padding variables from __paddingX to paddingX__.
  "Each name that contains a double underscore (_ _) [...] is reserved
   to the implementation for any use."  C++ standards 17.4.3.1.2.

* Add braces around switch cases.

* Calculate index as a separate statement in EnumName function, eg.
    const size_t index = ...;
    return EnumNamesX()[index];
  vs.
    return EnumNamesX()[...];

* Stored end table offset in variable in Finish() functions, eg.
    const auto end = fbb_.EndTable(start_, ...);
    auto o = flatbuffers::Offset<T>(end);
  vs.
    auto o = flatbuffers::Offset<T>(fbb_.EndTable(start, ...));

* Separate reinterpret_cast calls from function calls in Union
  functions, eg.
    auto ptr = reinterpret_cast<const T *>(obj);
    return ptr->UnPack(resolver);
  vs.
    return reinterpret_cast<const T *>(obj)->UnPack(resolver);

* Removed unecessary (void)(padding__X) no-ops from constructors, eg.
    Test(int16_t a, int8_t b) : ... {
      (void)__padding0;  // <-- Removed this line.
    }

In the idl_gen_cpp.cpp file itself, I refactored some code generation into
new functions: GenParam, GenNativeTable, GenVerifyCall, GenBuilders,
GenUnpackFieldStatement, and GenCreateParam.

Change-Id: I727b1bd8719d05b7ce33cbce00eb58fda817b25d
This commit is contained in:
Wouter van Oortmerssen
2017-01-13 17:44:42 -08:00
parent 1a21b54560
commit 7b94eab2b1
12 changed files with 2431 additions and 1306 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
#ifndef FLATBUFFERS_GENERATED_NAMESPACETEST1_NAMESPACEA_NAMESPACEB_H_
#define FLATBUFFERS_GENERATED_NAMESPACETEST1_NAMESPACEA_NAMESPACEB_H_
@@ -21,11 +22,19 @@ enum EnumInNestedNS {
};
inline const char **EnumNamesEnumInNestedNS() {
static const char *names[] = { "A", "B", "C", nullptr };
static const char *names[] = {
"A",
"B",
"C",
nullptr
};
return names;
}
inline const char *EnumNameEnumInNestedNS(EnumInNestedNS e) { return EnumNamesEnumInNestedNS()[static_cast<int>(e)]; }
inline const char *EnumNameEnumInNestedNS(EnumInNestedNS e) {
const size_t index = static_cast<int>(e);
return EnumNamesEnumInNestedNS()[index];
}
MANUALLY_ALIGNED_STRUCT(4) StructInNestedNS FLATBUFFERS_FINAL_CLASS {
private:
@@ -33,15 +42,28 @@ MANUALLY_ALIGNED_STRUCT(4) StructInNestedNS FLATBUFFERS_FINAL_CLASS {
int32_t b_;
public:
StructInNestedNS() { memset(this, 0, sizeof(StructInNestedNS)); }
StructInNestedNS(const StructInNestedNS &_o) { memcpy(this, &_o, sizeof(StructInNestedNS)); }
StructInNestedNS() {
memset(this, 0, sizeof(StructInNestedNS));
}
StructInNestedNS(const StructInNestedNS &_o) {
memcpy(this, &_o, sizeof(StructInNestedNS));
}
StructInNestedNS(int32_t _a, int32_t _b)
: a_(flatbuffers::EndianScalar(_a)), b_(flatbuffers::EndianScalar(_b)) { }
int32_t a() const { return flatbuffers::EndianScalar(a_); }
void mutate_a(int32_t _a) { flatbuffers::WriteScalar(&a_, _a); }
int32_t b() const { return flatbuffers::EndianScalar(b_); }
void mutate_b(int32_t _b) { flatbuffers::WriteScalar(&b_, _b); }
: a_(flatbuffers::EndianScalar(_a)),
b_(flatbuffers::EndianScalar(_b)) {
}
int32_t a() const {
return flatbuffers::EndianScalar(a_);
}
void mutate_a(int32_t _a) {
flatbuffers::WriteScalar(&a_, _a);
}
int32_t b() const {
return flatbuffers::EndianScalar(b_);
}
void mutate_b(int32_t _b) {
flatbuffers::WriteScalar(&b_, _b);
}
};
STRUCT_END(StructInNestedNS, 8);
@@ -49,8 +71,12 @@ struct TableInNestedNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
enum {
VT_FOO = 4
};
int32_t foo() const { return GetField<int32_t>(VT_FOO, 0); }
bool mutate_foo(int32_t _foo) { return SetField(VT_FOO, _foo); }
int32_t foo() const {
return GetField<int32_t>(VT_FOO, 0);
}
bool mutate_foo(int32_t _foo) {
return SetField(VT_FOO, _foo);
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<int32_t>(verifier, VT_FOO) &&
@@ -64,15 +90,20 @@ struct TableInNestedNSBuilder {
void add_foo(int32_t foo) {
fbb_.AddElement<int32_t>(TableInNestedNS::VT_FOO, foo, 0);
}
TableInNestedNSBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
TableInNestedNSBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
TableInNestedNSBuilder &operator=(const TableInNestedNSBuilder &);
flatbuffers::Offset<TableInNestedNS> Finish() {
auto o = flatbuffers::Offset<TableInNestedNS>(fbb_.EndTable(start_, 1));
const auto end = fbb_.EndTable(start_, 1);
auto o = flatbuffers::Offset<TableInNestedNS>(end);
return o;
}
};
inline flatbuffers::Offset<TableInNestedNS> CreateTableInNestedNS(flatbuffers::FlatBufferBuilder &_fbb,
inline flatbuffers::Offset<TableInNestedNS> CreateTableInNestedNS(
flatbuffers::FlatBufferBuilder &_fbb,
int32_t foo = 0) {
TableInNestedNSBuilder builder_(_fbb);
builder_.add_foo(foo);

View File

@@ -1,5 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
#ifndef FLATBUFFERS_GENERATED_NAMESPACETEST2_NAMESPACEA_H_
#define FLATBUFFERS_GENERATED_NAMESPACETEST2_NAMESPACEA_H_
@@ -29,12 +30,24 @@ struct TableInFirstNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
VT_FOO_ENUM = 6,
VT_FOO_STRUCT = 8
};
const NamespaceA::NamespaceB::TableInNestedNS *foo_table() const { return GetPointer<const NamespaceA::NamespaceB::TableInNestedNS *>(VT_FOO_TABLE); }
NamespaceA::NamespaceB::TableInNestedNS *mutable_foo_table() { return GetPointer<NamespaceA::NamespaceB::TableInNestedNS *>(VT_FOO_TABLE); }
NamespaceA::NamespaceB::EnumInNestedNS foo_enum() const { return static_cast<NamespaceA::NamespaceB::EnumInNestedNS>(GetField<int8_t>(VT_FOO_ENUM, 0)); }
bool mutate_foo_enum(NamespaceA::NamespaceB::EnumInNestedNS _foo_enum) { return SetField(VT_FOO_ENUM, static_cast<int8_t>(_foo_enum)); }
const NamespaceA::NamespaceB::StructInNestedNS *foo_struct() const { return GetStruct<const NamespaceA::NamespaceB::StructInNestedNS *>(VT_FOO_STRUCT); }
NamespaceA::NamespaceB::StructInNestedNS *mutable_foo_struct() { return GetStruct<NamespaceA::NamespaceB::StructInNestedNS *>(VT_FOO_STRUCT); }
const NamespaceA::NamespaceB::TableInNestedNS *foo_table() const {
return GetPointer<const NamespaceA::NamespaceB::TableInNestedNS *>(VT_FOO_TABLE);
}
NamespaceA::NamespaceB::TableInNestedNS *mutable_foo_table() {
return GetPointer<NamespaceA::NamespaceB::TableInNestedNS *>(VT_FOO_TABLE);
}
NamespaceA::NamespaceB::EnumInNestedNS foo_enum() const {
return static_cast<NamespaceA::NamespaceB::EnumInNestedNS>(GetField<int8_t>(VT_FOO_ENUM, 0));
}
bool mutate_foo_enum(NamespaceA::NamespaceB::EnumInNestedNS _foo_enum) {
return SetField(VT_FOO_ENUM, static_cast<int8_t>(_foo_enum));
}
const NamespaceA::NamespaceB::StructInNestedNS *foo_struct() const {
return GetStruct<const NamespaceA::NamespaceB::StructInNestedNS *>(VT_FOO_STRUCT);
}
NamespaceA::NamespaceB::StructInNestedNS *mutable_foo_struct() {
return GetStruct<NamespaceA::NamespaceB::StructInNestedNS *>(VT_FOO_STRUCT);
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<flatbuffers::uoffset_t>(verifier, VT_FOO_TABLE) &&
@@ -57,15 +70,20 @@ struct TableInFirstNSBuilder {
void add_foo_struct(const NamespaceA::NamespaceB::StructInNestedNS *foo_struct) {
fbb_.AddStruct(TableInFirstNS::VT_FOO_STRUCT, foo_struct);
}
TableInFirstNSBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
TableInFirstNSBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
TableInFirstNSBuilder &operator=(const TableInFirstNSBuilder &);
flatbuffers::Offset<TableInFirstNS> Finish() {
auto o = flatbuffers::Offset<TableInFirstNS>(fbb_.EndTable(start_, 3));
const auto end = fbb_.EndTable(start_, 3);
auto o = flatbuffers::Offset<TableInFirstNS>(end);
return o;
}
};
inline flatbuffers::Offset<TableInFirstNS> CreateTableInFirstNS(flatbuffers::FlatBufferBuilder &_fbb,
inline flatbuffers::Offset<TableInFirstNS> CreateTableInFirstNS(
flatbuffers::FlatBufferBuilder &_fbb,
flatbuffers::Offset<NamespaceA::NamespaceB::TableInNestedNS> foo_table = 0,
NamespaceA::NamespaceB::EnumInNestedNS foo_enum = NamespaceA::NamespaceB::EnumInNestedNS_A,
const NamespaceA::NamespaceB::StructInNestedNS *foo_struct = 0) {
@@ -85,10 +103,18 @@ struct TableInC FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
VT_REFER_TO_A1 = 4,
VT_REFER_TO_A2 = 6
};
const NamespaceA::TableInFirstNS *refer_to_a1() const { return GetPointer<const NamespaceA::TableInFirstNS *>(VT_REFER_TO_A1); }
NamespaceA::TableInFirstNS *mutable_refer_to_a1() { return GetPointer<NamespaceA::TableInFirstNS *>(VT_REFER_TO_A1); }
const NamespaceA::SecondTableInA *refer_to_a2() const { return GetPointer<const NamespaceA::SecondTableInA *>(VT_REFER_TO_A2); }
NamespaceA::SecondTableInA *mutable_refer_to_a2() { return GetPointer<NamespaceA::SecondTableInA *>(VT_REFER_TO_A2); }
const NamespaceA::TableInFirstNS *refer_to_a1() const {
return GetPointer<const NamespaceA::TableInFirstNS *>(VT_REFER_TO_A1);
}
NamespaceA::TableInFirstNS *mutable_refer_to_a1() {
return GetPointer<NamespaceA::TableInFirstNS *>(VT_REFER_TO_A1);
}
const NamespaceA::SecondTableInA *refer_to_a2() const {
return GetPointer<const NamespaceA::SecondTableInA *>(VT_REFER_TO_A2);
}
NamespaceA::SecondTableInA *mutable_refer_to_a2() {
return GetPointer<NamespaceA::SecondTableInA *>(VT_REFER_TO_A2);
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<flatbuffers::uoffset_t>(verifier, VT_REFER_TO_A1) &&
@@ -108,15 +134,20 @@ struct TableInCBuilder {
void add_refer_to_a2(flatbuffers::Offset<NamespaceA::SecondTableInA> refer_to_a2) {
fbb_.AddOffset(TableInC::VT_REFER_TO_A2, refer_to_a2);
}
TableInCBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
TableInCBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
TableInCBuilder &operator=(const TableInCBuilder &);
flatbuffers::Offset<TableInC> Finish() {
auto o = flatbuffers::Offset<TableInC>(fbb_.EndTable(start_, 2));
const auto end = fbb_.EndTable(start_, 2);
auto o = flatbuffers::Offset<TableInC>(end);
return o;
}
};
inline flatbuffers::Offset<TableInC> CreateTableInC(flatbuffers::FlatBufferBuilder &_fbb,
inline flatbuffers::Offset<TableInC> CreateTableInC(
flatbuffers::FlatBufferBuilder &_fbb,
flatbuffers::Offset<NamespaceA::TableInFirstNS> refer_to_a1 = 0,
flatbuffers::Offset<NamespaceA::SecondTableInA> refer_to_a2 = 0) {
TableInCBuilder builder_(_fbb);
@@ -133,8 +164,12 @@ struct SecondTableInA FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
enum {
VT_REFER_TO_C = 4
};
const NamespaceC::TableInC *refer_to_c() const { return GetPointer<const NamespaceC::TableInC *>(VT_REFER_TO_C); }
NamespaceC::TableInC *mutable_refer_to_c() { return GetPointer<NamespaceC::TableInC *>(VT_REFER_TO_C); }
const NamespaceC::TableInC *refer_to_c() const {
return GetPointer<const NamespaceC::TableInC *>(VT_REFER_TO_C);
}
NamespaceC::TableInC *mutable_refer_to_c() {
return GetPointer<NamespaceC::TableInC *>(VT_REFER_TO_C);
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<flatbuffers::uoffset_t>(verifier, VT_REFER_TO_C) &&
@@ -149,15 +184,20 @@ struct SecondTableInABuilder {
void add_refer_to_c(flatbuffers::Offset<NamespaceC::TableInC> refer_to_c) {
fbb_.AddOffset(SecondTableInA::VT_REFER_TO_C, refer_to_c);
}
SecondTableInABuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
SecondTableInABuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
SecondTableInABuilder &operator=(const SecondTableInABuilder &);
flatbuffers::Offset<SecondTableInA> Finish() {
auto o = flatbuffers::Offset<SecondTableInA>(fbb_.EndTable(start_, 1));
const auto end = fbb_.EndTable(start_, 1);
auto o = flatbuffers::Offset<SecondTableInA>(end);
return o;
}
};
inline flatbuffers::Offset<SecondTableInA> CreateSecondTableInA(flatbuffers::FlatBufferBuilder &_fbb,
inline flatbuffers::Offset<SecondTableInA> CreateSecondTableInA(
flatbuffers::FlatBufferBuilder &_fbb,
flatbuffers::Offset<NamespaceC::TableInC> refer_to_c = 0) {
SecondTableInABuilder builder_(_fbb);
builder_.add_refer_to_c(refer_to_c);