Fix CreateVectorOfNativeStructs for non-std vector types (cpp_vec_type + native_type)

When a field combines cpp_vec_type (e.g. eastl::vector) with native_type on a
struct, the generated Pack method now uses the pointer+size overload of
CreateVectorOfNativeStructs instead of the std::vector overload, which only
accepts std::vector. Adds a dedicated test covering the combined attribute case.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-09 20:35:36 +02:00
parent 35665b5ae7
commit 19aa2ce420
6 changed files with 108 additions and 1 deletions

View File

@@ -65,6 +65,7 @@
#include "monster_test_bfbs_generated.h" // Generated using --bfbs-comments --bfbs-builtins --cpp --bfbs-gen-embed
#include "cpp_vec_type_test_generated.h"
#include "native_type_test_generated.h"
#include "cpp_vec_type_native_type_test_generated.h"
#include "test_assert.h"
#include "util_test.h"
#include "vector_table_naked_ptr_test.h"
@@ -1041,6 +1042,37 @@ void CppVecTypeTest() {
TEST_ASSERT(*dst == *dst);
}
void CppVecTypeNativeTypeTest() {
// Verify that combining cpp_vec_type + native_type on a vector of structs
// produces the correct container type in the NativeTable.
static_assert(
std::is_same<
decltype(CppVecNativeTypeTest::ContainerT{}.points),
CppVecNativeTypeTest::CustomVec<
CppVecNativeTypeTest::Native::Vec3>>::value,
"points should be CustomVec<Native::Vec3>");
const int N = 3;
CppVecNativeTypeTest::ContainerT src;
for (int i = 0; i < N; ++i) {
src.points.push_back(
CppVecNativeTypeTest::Native::Vec3(1.0f * i, 2.0f * i, 3.0f * i));
}
flatbuffers::FlatBufferBuilder fbb;
fbb.Finish(CppVecNativeTypeTest::Container::Pack(fbb, &src));
auto dst =
CppVecNativeTypeTest::UnPackContainer(fbb.GetBufferPointer());
TEST_EQ(dst->points.size(), static_cast<size_t>(N));
for (int i = 0; i < N; ++i) {
TEST_EQ(dst->points[i].x, 1.0f * i);
TEST_EQ(dst->points[i].y, 2.0f * i);
TEST_EQ(dst->points[i].z, 3.0f * i);
}
}
// Guard against -Wunused-function on platforms without file tests.
#ifndef FLATBUFFERS_NO_FILE_TESTS
// VS10 does not support typed enums, exclude from tests
@@ -1877,6 +1909,7 @@ int FlatBufferTests(const std::string& tests_data_path) {
FixedLengthArrayTest();
NativeTypeTest();
CppVecTypeTest();
CppVecTypeNativeTypeTest();
OptionalScalarsTest();
ParseFlexbuffersFromJsonWithNullTest();
FlatbuffersSpanTest();