diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 6755ec279..e3b10f6b1 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -288,7 +288,7 @@ public: } VectorIterator operator++(int) { - VectorIterator temp(data_); + VectorIterator temp(data_,0); data_ += IndirectHelper::element_stride; return temp; } diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index c690240d6..72a7dc519 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -719,6 +719,83 @@ class CppGenerator : public BaseGenerator { } } code += " return builder_.Finish();\n}\n\n"; + + //Generate a CreateX function with vector types as parameters + std::vectorvect_pars_ids; + code += "inline flatbuffers::Offset<" + struct_def.name + "> Create"; + code += struct_def.name; + code += "(flatbuffers::FlatBufferBuilder &_fbb"; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + auto &field = **it; + if (!field.deprecated) { + if (field.value.type.base_type == BASE_TYPE_STRING) { + vect_pars_ids.push_back(*it); + code += ",\n const char *"; + code += field.name + " = \"\""; + } + else if (field.value.type.base_type == BASE_TYPE_VECTOR) { + vect_pars_ids.push_back(*it); + code += ",\n std::vector<" + GenTypeWire(field.value.type.VectorType(), "", false) + "> " + field.name; + code += " = std::vector<" + GenTypeWire(field.value.type.VectorType(), "", false) + ">()"; + } + else { + code += ",\n " + GenTypeWire(field.value.type, " ", true); + code += field.name + " = "; + if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) { + auto ev = field.value.type.enum_def->ReverseLookup( + static_cast(StringToInt(field.value.constant.c_str())), + false); + if (ev) { + code += WrapInNameSpace( + field.value.type.enum_def->defined_namespace, + GetEnumVal(*field.value.type.enum_def, *ev, parser_.opts)); + } + else { + code += GenUnderlyingCast(field, true, field.value.constant); + } + } + else if (field.value.type.base_type == BASE_TYPE_BOOL) { + code += field.value.constant == "0" ? "false" : "true"; + } + else { + code += GenDefaultConstant(field); + } + } + } + } + code += ") {\n "; + //getting offsets + + for (auto it = vect_pars_ids.begin(); + it != vect_pars_ids.end(); ++it) { + auto&field = **it; + code += "auto " + field.name + "_off = "; + if (field.value.type.base_type == BASE_TYPE_STRING) { + code += "_fbb.CreateString(" + field.name + ");\n "; + } + else if (field.value.type.base_type == BASE_TYPE_VECTOR) { + code += "_fbb.CreateVector<" + GenTypeWire(field.value.type.VectorType(), "", false) + ">(" + field.name + ");\n "; + } + } + + //... + code += struct_def.name + "Builder builder_(_fbb);\n"; + for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; + 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 += (!IsScalar(field.value.type.base_type) && (!IsStruct(field.value.type))) ? //if vector or string + (field.name + "_off") : field.name; + code += ");\n"; + } + } + } + code += " return builder_.Finish();\n}\n\n"; } static void GenPadding(const FieldDef &field, std::string &code,