Add default values (if they exist) to native tables.

From cl/142307012.

Change-Id: I54d550573f6506b92ad18e7cc90bcd8589259e52
parent c66683f2
...@@ -111,6 +111,10 @@ struct MonsterT : public flatbuffers::NativeTable { ...@@ -111,6 +111,10 @@ struct MonsterT : public flatbuffers::NativeTable {
Color color; Color color;
std::vector<std::unique_ptr<WeaponT>> weapons; std::vector<std::unique_ptr<WeaponT>> weapons;
EquipmentUnion equipped; EquipmentUnion equipped;
MonsterT()
: mana(150),
hp(100),
color(Color_Blue) {}
}; };
struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
...@@ -228,6 +232,8 @@ struct WeaponT : public flatbuffers::NativeTable { ...@@ -228,6 +232,8 @@ struct WeaponT : public flatbuffers::NativeTable {
typedef Weapon TableType; typedef Weapon TableType;
std::string name; std::string name;
int16_t damage; int16_t damage;
WeaponT()
: damage(0) {}
}; };
struct Weapon FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { struct Weapon FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
......
...@@ -691,24 +691,70 @@ class CppGenerator : public BaseGenerator { ...@@ -691,24 +691,70 @@ class CppGenerator : public BaseGenerator {
: field.value.constant; : field.value.constant;
} }
void GenSimpleParam(std::string &code, FieldDef &field) { std::string GetDefaultScalarValue(const FieldDef &field) {
code += ",\n " + GenTypeWire(field.value.type, " ", true);
code += field.name + " = ";
if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) { if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) {
auto ev = field.value.type.enum_def->ReverseLookup( auto ev = field.value.type.enum_def->ReverseLookup(
static_cast<int>(StringToInt(field.value.constant.c_str())), false); static_cast<int>(StringToInt(field.value.constant.c_str())), false);
if (ev) { if (ev) {
code += WrapInNameSpace( return WrapInNameSpace(
field.value.type.enum_def->defined_namespace, field.value.type.enum_def->defined_namespace,
GetEnumValUse(*field.value.type.enum_def, *ev, parser_.opts)); GetEnumValUse(*field.value.type.enum_def, *ev, parser_.opts));
} else { } else {
code += GenUnderlyingCast(field, true, field.value.constant); return GenUnderlyingCast(field, true, field.value.constant);
} }
} else if (field.value.type.base_type == BASE_TYPE_BOOL) { } else if (field.value.type.base_type == BASE_TYPE_BOOL) {
code += field.value.constant == "0" ? "false" : "true"; return field.value.constant == "0" ? "false" : "true";
} else { } else {
code += GenDefaultConstant(field); return GenDefaultConstant(field);
}
}
void GenSimpleParam(std::string &code, FieldDef &field) {
code += ",\n " + GenTypeWire(field.value.type, " ", true);
code += field.name + " = " + GetDefaultScalarValue(field);
}
// Generate a member, including a default value for scalars and raw pointers.
void GenMember(std::string& code, const FieldDef &field) {
if (!field.deprecated && // Deprecated fields won't be accessible.
field.value.type.base_type != BASE_TYPE_UTYPE) {
auto type = GenTypeNative(field.value.type, false, field);
auto cpp_type = field.attributes.Lookup("cpp_type");
code += " " + (cpp_type ? cpp_type->constant + " *" : type+ " ") +
field.name + ";\n";
}
}
// Generate the default constructor for this struct. Properly initialize all
// scalar members with default values.
void GenDefaultConstructor(std::string& code, const StructDef& struct_def) {
code += " " + NativeName(struct_def.name) + "()";
std::string initializer_list;
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
if (!field.deprecated && // Deprecated fields won't be accessible.
field.value.type.base_type != BASE_TYPE_UTYPE) {
auto cpp_type = field.attributes.Lookup("cpp_type");
// Scalar types get parsed defaults, raw pointers get nullptrs.
if (IsScalar(field.value.type.base_type)) {
if (!initializer_list.empty()) {
initializer_list += ",\n ";
}
initializer_list += field.name + "(" +GetDefaultScalarValue(field) +
")";
} else if (cpp_type) {
if (!initializer_list.empty()) {
code += ",\n ";
}
initializer_list += field.name + "(0)";
}
}
} }
if (!initializer_list.empty()) {
code += "\n : " + initializer_list;
}
code += " {}\n";
} }
// Generate an accessor struct, builder structs & function for a table. // Generate an accessor struct, builder structs & function for a table.
...@@ -726,14 +772,9 @@ class CppGenerator : public BaseGenerator { ...@@ -726,14 +772,9 @@ class CppGenerator : public BaseGenerator {
for (auto it = struct_def.fields.vec.begin(); 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; auto &field = **it;
if (!field.deprecated && // Deprecated fields won't be accessible. GenMember(code, field);
field.value.type.base_type != BASE_TYPE_UTYPE) {
auto type = GenTypeNative(field.value.type, false, field);
auto cpp_type = field.attributes.Lookup("cpp_type");
code += " " + (cpp_type ? cpp_type->constant + " *" : type+ " ") +
field.name + ";\n";
}
} }
GenDefaultConstructor(code, struct_def);
code += "};\n\n"; code += "};\n\n";
} }
......
...@@ -162,6 +162,7 @@ namespace Example2 { ...@@ -162,6 +162,7 @@ namespace Example2 {
struct MonsterT : public flatbuffers::NativeTable { struct MonsterT : public flatbuffers::NativeTable {
typedef Monster TableType; typedef Monster TableType;
MonsterT() {}
}; };
struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
...@@ -199,6 +200,8 @@ namespace Example { ...@@ -199,6 +200,8 @@ namespace Example {
struct TestSimpleTableWithEnumT : public flatbuffers::NativeTable { struct TestSimpleTableWithEnumT : public flatbuffers::NativeTable {
typedef TestSimpleTableWithEnum TableType; typedef TestSimpleTableWithEnum TableType;
Color color; Color color;
TestSimpleTableWithEnumT()
: color(Color_Green) {}
}; };
struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
...@@ -243,6 +246,9 @@ struct StatT : public flatbuffers::NativeTable { ...@@ -243,6 +246,9 @@ struct StatT : public flatbuffers::NativeTable {
std::string id; std::string id;
int64_t val; int64_t val;
uint16_t count; uint16_t count;
StatT()
: val(0),
count(0) {}
}; };
struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
...@@ -333,6 +339,22 @@ struct MonsterT : public flatbuffers::NativeTable { ...@@ -333,6 +339,22 @@ struct MonsterT : public flatbuffers::NativeTable {
float testf2; float testf2;
float testf3; float testf3;
std::vector<std::string> testarrayofstring2; std::vector<std::string> testarrayofstring2;
MonsterT()
: mana(150),
hp(100),
color(Color_Blue),
testbool(false),
testhashs32_fnv1(0),
testhashu32_fnv1(0),
testhashs64_fnv1(0),
testhashu64_fnv1(0),
testhashs32_fnv1a(0),
testhashu32_fnv1a(0),
testhashs64_fnv1a(0),
testhashu64_fnv1a(0),
testf(3.14159f),
testf2(3.0f),
testf3(0.0f) {}
}; };
/// an example documentation comment: monster object /// an example documentation comment: monster object
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment