New CreateXXX (with vectors and strings) calls old CreateXXX (with offsets).

This commit is contained in:
daksenik 2016-07-08 12:35:57 +03:00
parent c2411e9c8c
commit dfbda986d7
1 changed files with 23 additions and 33 deletions

View File

@ -680,7 +680,7 @@ class CppGenerator : public BaseGenerator {
// Generate a convenient CreateX function that uses the above builder // Generate a convenient CreateX function that uses the above builder
// to create a table in one go. // to create a table in one go.
std::vector<FieldDef*>vect_pars; bool gen_vector_pars = false;
code += "inline flatbuffers::Offset<" + struct_def.name + "> Create"; code += "inline flatbuffers::Offset<" + struct_def.name + "> Create";
code += struct_def.name; code += struct_def.name;
@ -689,7 +689,7 @@ class CppGenerator : public BaseGenerator {
it != struct_def.fields.vec.end(); ++it) { it != struct_def.fields.vec.end(); ++it) {
auto &field = **it; auto &field = **it;
if (!field.deprecated) { if (!field.deprecated) {
if (field.value.type.base_type == BASE_TYPE_STRING || field.value.type.base_type == BASE_TYPE_VECTOR) vect_pars.push_back(*it); if (field.value.type.base_type == BASE_TYPE_STRING || field.value.type.base_type == BASE_TYPE_VECTOR) gen_vector_pars = true;
code += ",\n " + GenTypeWire(field.value.type, " ", true); code += ",\n " + GenTypeWire(field.value.type, " ", true);
code += field.name + " = "; 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)) {
@ -725,7 +725,7 @@ class CppGenerator : public BaseGenerator {
code += " return builder_.Finish();\n}\n\n"; code += " return builder_.Finish();\n}\n\n";
//Generate a CreateX function with vector types as parameters //Generate a CreateX function with vector types as parameters
if (vect_pars.size()) { if (gen_vector_pars) {
code += "inline flatbuffers::Offset<" + struct_def.name + "> Create"; code += "inline flatbuffers::Offset<" + struct_def.name + "> Create";
code += struct_def.name; code += struct_def.name;
code += "(flatbuffers::FlatBufferBuilder &_fbb"; code += "(flatbuffers::FlatBufferBuilder &_fbb";
@ -735,11 +735,11 @@ class CppGenerator : public BaseGenerator {
if (!field.deprecated) { if (!field.deprecated) {
if (field.value.type.base_type == BASE_TYPE_STRING) { if (field.value.type.base_type == BASE_TYPE_STRING) {
code += ",\n const char *"; code += ",\n const char *";
code += field.name + " = \"\""; code += field.name + " = nullptr";
} }
else if (field.value.type.base_type == BASE_TYPE_VECTOR) { else if (field.value.type.base_type == BASE_TYPE_VECTOR) {
code += ",\n std::vector<" + GenTypeWire(field.value.type.VectorType(), "", false) + "> " + field.name; code += ",\n std::vector<" + GenTypeWire(field.value.type.VectorType(), "", false) + "> *" + field.name;
code += " = std::vector<" + GenTypeWire(field.value.type.VectorType(), "", false) + ">()"; code += " = nullptr";
} }
else { else {
code += ",\n " + GenTypeWire(field.value.type, " ", true); code += ",\n " + GenTypeWire(field.value.type, " ", true);
@ -767,36 +767,26 @@ class CppGenerator : public BaseGenerator {
} }
} }
code += ") {\n "; code += ") {\n ";
//getting offsets
for (auto it = vect_pars.begin(); code += "return Create";
it != vect_pars.end(); ++it) { code += struct_def.name;
auto&field = **it; code += "(_fbb";
code += "auto " + field.name + "_off = "; for (auto it = struct_def.fields.vec.begin();
if (field.value.type.base_type == BASE_TYPE_STRING) { it != struct_def.fields.vec.end(); ++it) {
code += "_fbb.CreateString(" + field.name + ");\n "; auto &field = **it;
} if (!field.deprecated) {
else if (field.value.type.base_type == BASE_TYPE_VECTOR) { if (field.value.type.base_type == BASE_TYPE_STRING) {
code += "_fbb.CreateVector<" + GenTypeWire(field.value.type.VectorType(), "", false) + ">(" + field.name + ");\n "; code += ", " + field.name + "==nullptr? 0 : ";
code += "_fbb.CreateString(" + field.name + ")";
} else if (field.value.type.base_type == BASE_TYPE_VECTOR) {
code += ", " + field.name + "==nullptr? 0 : ";
code += "_fbb.CreateVector<" + GenTypeWire(field.value.type.VectorType(), "", false) + ">(*" + field.name + ")";
} else code += ", " + field.name;
} }
} }
code += struct_def.name + "Builder builder_(_fbb);\n";
for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; code += ");\n}\n\n";
size; size /= 2) {
for (auto it = struct_def.fields.vec.rbegin();
it != struct_def.fields.vec.rend(); ++it) {
auto &field = **it;
if (!field.deprecated && (!struct_def.sortbysize ||
size == SizeOf(field.value.type.base_type))) {
code += " builder_.add_" + field.name + "(";
code += (field.value.type.base_type == BASE_TYPE_STRING || field.value.type.base_type == BASE_TYPE_VECTOR) ? //if vector or string
(field.name + "_off") : field.name;
code += ");\n";
}
}
}
code += " return builder_.Finish();\n}\n\n";
} }
} }