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:
2026-05-09 19:55:26 +02:00
parent 9e9f5bbfcf
commit 35665b5ae7
8 changed files with 113 additions and 7 deletions

View File

@@ -142,6 +142,10 @@ const static FlatCOption flatc_options[] = {
"and T::empty() must be supported. The custom type also needs to be "
"constructible from std::string (see the --cpp-str-flex-ctor option to "
"change this behavior)"},
{"", "cpp-vec-type", "T",
"Set object API vector type (default std::vector). T must support the "
"same interface as std::vector: size(), resize(), reserve(), "
"emplace_back(), operator[], begin(), end(), and data()."},
{"", "cpp-str-flex-ctor", "",
"Don't construct custom string types by passing std::string from "
"Flatbuffers, but (char* + length)."},
@@ -543,6 +547,9 @@ FlatCOptions FlatCompiler::ParseFromCommandLineArguments(int argc,
} else if (arg == "--cpp-str-type") {
if (++argi >= argc) Error("missing type following: " + arg, true);
opts.cpp_object_api_string_type = argv[argi];
} else if (arg == "--cpp-vec-type") {
if (++argi >= argc) Error("missing type following: " + arg, true);
opts.cpp_object_api_vector_type = argv[argi];
} else if (arg == "--cpp-str-flex-ctor") {
opts.cpp_object_api_string_flexible_constructor = true;
} else if (arg == "--no-cpp-direct-copy") {