diff --git a/include/ylt/struct_pack/derived_helper.hpp b/include/ylt/struct_pack/derived_helper.hpp index 8a78f06f9..8b3f70aee 100644 --- a/include/ylt/struct_pack/derived_helper.hpp +++ b/include/ylt/struct_pack/derived_helper.hpp @@ -168,6 +168,9 @@ struct deserialize_one_derived_class_helper { } }; +template +void struct_pack_derived_decl(const T*) = delete; + template using derived_class_set_t = decltype(struct_pack_derived_decl((Base *)nullptr)); diff --git a/include/ylt/struct_pack/derived_marco.hpp b/include/ylt/struct_pack/derived_marco.hpp index af50399f3..8e193ef4b 100644 --- a/include/ylt/struct_pack/derived_marco.hpp +++ b/include/ylt/struct_pack/derived_marco.hpp @@ -14,9 +14,8 @@ * limitations under the License. */ -#include "foreach_macro.h" - #pragma once +#include "foreach_macro.h" #define GET_STRUCT_PACK_ID_IMPL_FOR_LOOP(idx, type) \ inline uint32_t type::get_struct_pack_id() const { \ @@ -26,7 +25,7 @@ #define STRUCT_PACK_DERIVED_DECL(base, ...) \ \ inline decltype(struct_pack::detail::derived_decl_impl()) \ - struct_pack_derived_decl(base*); + struct_pack_derived_decl(const base*); #define STRUCT_PACK_DERIVED_IMPL(base, ...) \ STRUCT_PACK_EXPAND_EACH(, GET_STRUCT_PACK_ID_IMPL_FOR_LOOP, base, \ diff --git a/src/struct_pack/tests/test_derived.cpp b/src/struct_pack/tests/test_derived.cpp index 742044313..2fb83fd41 100644 --- a/src/struct_pack/tests/test_derived.cpp +++ b/src/struct_pack/tests/test_derived.cpp @@ -167,4 +167,56 @@ TEST_CASE("test unique_ptr with virtual base") { auto res2 = struct_pack::deserialize>(buffer2); CHECK(res2); CHECK(res2.value()->get_name() == std::make_unique()->get_name()); -} \ No newline at end of file +} + +namespace derived_class_contain_another_derived_class { + +struct base { + virtual uint32_t get_struct_pack_id() const = 0; + virtual std::string get_name() const = 0; + static std::unique_ptr deserialize(std::string& serialized); + virtual ~base(){}; +}; + +struct derived1 : public base { + int b; + virtual uint32_t get_struct_pack_id() const override; + std::string get_name() const override { return "derived1"; } +}; +STRUCT_PACK_REFL(derived1, b); + +struct derived2 : public base { + std::string c; + std::unique_ptr child; + virtual uint32_t get_struct_pack_id() const override; + std::string get_name() const override { return "derived2"; } +}; + +STRUCT_PACK_REFL(derived2, c, child); + +STRUCT_PACK_DERIVED_IMPL(base, derived1, derived2); + +std::unique_ptr base::deserialize(std::string& serialized) { + return struct_pack::deserialize_derived_class( + serialized) + .value(); +} + +} // namespace derived_class_contain_another_derived_class + +TEST_CASE("test derived class contain by other derived class") { + using namespace derived_class_contain_another_derived_class; + { + auto serialized = struct_pack::serialize(derived1{}); + auto x = base::deserialize(serialized); + REQUIRE(x); + CHECK(x->get_name()=="derived1"); + } + { + auto serialized = struct_pack::serialize(derived2{}); + auto x = base::deserialize(serialized); + REQUIRE(x); + CHECK(x->get_name()=="derived2"); + } + +} diff --git a/test.cpp b/test.cpp new file mode 100644 index 000000000..145507ccd --- /dev/null +++ b/test.cpp @@ -0,0 +1,2 @@ +#include +#include