mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-04 20:48:59 +00:00
Add cpp_vec_type attribute for object API vector customization
Mirrors cpp_str_type and cpp_ptr_type: a per-field cpp_vec_type attribute lets users substitute any std::vector-compatible container in generated T-structs, and --cpp-vec-type sets the global default. NativeVector() resolves the attribute (falling back to the global option, then std::vector). Changes applied to GenTypeNative, GenMember, GenParam (CreateDirect), and the CreateVectorOfStrings fast-path guard. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -929,6 +929,15 @@ class CppGenerator : public BaseGenerator {
|
||||
return ret;
|
||||
}
|
||||
|
||||
const std::string NativeVector(const FieldDef* field) {
|
||||
auto attr = field ? field->attributes.Lookup("cpp_vec_type") : nullptr;
|
||||
auto& ret = attr ? attr->constant : opts_.cpp_object_api_vector_type;
|
||||
if (ret.empty()) {
|
||||
return "std::vector";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool FlexibleStringConstructor(const FieldDef* field) {
|
||||
auto attr = field != nullptr &&
|
||||
(field->attributes.Lookup("cpp_str_flex_ctor") != nullptr);
|
||||
@@ -974,15 +983,15 @@ class CppGenerator : public BaseGenerator {
|
||||
case BASE_TYPE_VECTOR64:
|
||||
case BASE_TYPE_VECTOR: {
|
||||
const auto type_name = GenTypeNative(type.VectorType(), true, field);
|
||||
if (type.struct_def &&
|
||||
const auto vec_type = NativeVector(&field);
|
||||
if (vec_type == "std::vector" && type.struct_def &&
|
||||
type.struct_def->attributes.Lookup("native_custom_alloc")) {
|
||||
auto native_custom_alloc =
|
||||
type.struct_def->attributes.Lookup("native_custom_alloc");
|
||||
return "std::vector<" + type_name + "," +
|
||||
native_custom_alloc->constant + "<" + type_name + ">>";
|
||||
} else {
|
||||
return "std::vector<" + type_name + ">";
|
||||
}
|
||||
return vec_type + "<" + type_name + ">";
|
||||
}
|
||||
case BASE_TYPE_STRUCT: {
|
||||
auto type_name = WrapInNameSpace(*type.struct_def);
|
||||
@@ -1957,9 +1966,10 @@ class CppGenerator : public BaseGenerator {
|
||||
field.offset64);
|
||||
}
|
||||
if (TypeHasKey(vtype)) {
|
||||
code_.SetValue("PARAM_TYPE", "std::vector<" + type + "> *");
|
||||
code_.SetValue("PARAM_TYPE", NativeVector(&field) + "<" + type + "> *");
|
||||
} else {
|
||||
code_.SetValue("PARAM_TYPE", "const std::vector<" + type + "> *");
|
||||
code_.SetValue("PARAM_TYPE",
|
||||
"const " + NativeVector(&field) + "<" + type + "> *");
|
||||
}
|
||||
code_.SetValue("PARAM_VALUE", "nullptr");
|
||||
} else {
|
||||
@@ -1985,7 +1995,7 @@ class CppGenerator : public BaseGenerator {
|
||||
const std::string& full_type =
|
||||
(cpp_type
|
||||
? (IsVector(field.value.type)
|
||||
? "std::vector<" +
|
||||
? NativeVector(&field) + "<" +
|
||||
GenTypeNativePtr(cpp_type->constant, &field,
|
||||
false) +
|
||||
"> "
|
||||
@@ -3791,7 +3801,8 @@ class CppGenerator : public BaseGenerator {
|
||||
auto vector_type = field.value.type.VectorType();
|
||||
switch (vector_type.base_type) {
|
||||
case BASE_TYPE_STRING: {
|
||||
if (NativeString(&field) == "std::string") {
|
||||
if (NativeString(&field) == "std::string" &&
|
||||
NativeVector(&field) == "std::vector") {
|
||||
code += "_fbb.CreateVectorOfStrings(" + value + ")";
|
||||
} else {
|
||||
// Use by-function serialization to emulate
|
||||
|
||||
Reference in New Issue
Block a user