From fe83b68ac68f221a8a7cf105e541b6d6d682ad80 Mon Sep 17 00:00:00 2001 From: David Cowan <37003198+dacowan@users.noreply.github.com> Date: Mon, 20 May 2019 14:01:44 -0700 Subject: [PATCH] Added a CPP UnPackSizePrefixed generated helper function (#5350) * Added a cpp UnPackSizePrefixed generated helper function Missing helper function added to the cpp API generator for unpacking size prefixed structures * Added generated test files --- samples/monster_generated.h | 6 ++++++ src/idl_gen_cpp.cpp | 8 ++++++++ tests/monster_extra_generated.h | 6 ++++++ tests/monster_test_generated.h | 6 ++++++ tests/union_vector/union_vector_generated.h | 6 ++++++ 5 files changed, 32 insertions(+) diff --git a/samples/monster_generated.h b/samples/monster_generated.h index 70c41baee..fd867f24d 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -865,6 +865,12 @@ inline flatbuffers::unique_ptr UnPackMonster( return flatbuffers::unique_ptr(GetMonster(buf)->UnPack(res)); } +inline flatbuffers::unique_ptr UnPackSizePrefixedMonster( + const void *buf, + const flatbuffers::resolver_function_t *res = nullptr) { + return flatbuffers::unique_ptr(GetSizePrefixedMonster(buf)->UnPack(res)); +} + } // namespace Sample } // namespace MyGame diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index cfd25267a..33d7c14ad 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -474,6 +474,14 @@ class CppGenerator : public BaseGenerator { code_ += "(Get{{STRUCT_NAME}}(buf)->UnPack(res));"; code_ += "}"; code_ += ""; + + code_ += "inline {{UNPACK_RETURN}} UnPackSizePrefixed{{STRUCT_NAME}}("; + code_ += " const void *buf,"; + code_ += " const flatbuffers::resolver_function_t *res = nullptr) {"; + code_ += " return {{UNPACK_TYPE}}\\"; + code_ += "(GetSizePrefixed{{STRUCT_NAME}}(buf)->UnPack(res));"; + code_ += "}"; + code_ += ""; } } diff --git a/tests/monster_extra_generated.h b/tests/monster_extra_generated.h index 87e6d4281..6ad3ca851 100644 --- a/tests/monster_extra_generated.h +++ b/tests/monster_extra_generated.h @@ -349,6 +349,12 @@ inline std::unique_ptr UnPackMonsterExtra( return std::unique_ptr(GetMonsterExtra(buf)->UnPack(res)); } +inline std::unique_ptr UnPackSizePrefixedMonsterExtra( + const void *buf, + const flatbuffers::resolver_function_t *res = nullptr) { + return std::unique_ptr(GetSizePrefixedMonsterExtra(buf)->UnPack(res)); +} + } // namespace MyGame #endif // FLATBUFFERS_GENERATED_MONSTEREXTRA_MYGAME_H_ diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 693271b24..508a87ef2 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -3483,6 +3483,12 @@ inline flatbuffers::unique_ptr UnPackMonster( return flatbuffers::unique_ptr(GetMonster(buf)->UnPack(res)); } +inline flatbuffers::unique_ptr UnPackSizePrefixedMonster( + const void *buf, + const flatbuffers::resolver_function_t *res = nullptr) { + return flatbuffers::unique_ptr(GetSizePrefixedMonster(buf)->UnPack(res)); +} + } // namespace Example } // namespace MyGame diff --git a/tests/union_vector/union_vector_generated.h b/tests/union_vector/union_vector_generated.h index 60c80b01e..150c8f455 100644 --- a/tests/union_vector/union_vector_generated.h +++ b/tests/union_vector/union_vector_generated.h @@ -850,4 +850,10 @@ inline flatbuffers::unique_ptr UnPackMovie( return flatbuffers::unique_ptr(GetMovie(buf)->UnPack(res)); } +inline flatbuffers::unique_ptr UnPackSizePrefixedMovie( + const void *buf, + const flatbuffers::resolver_function_t *res = nullptr) { + return flatbuffers::unique_ptr(GetSizePrefixedMovie(buf)->UnPack(res)); +} + #endif // FLATBUFFERS_GENERATED_UNIONVECTOR_H_