Skip to content

Commit

Permalink
[struct_pack][bugfix] base class can't optimized as no container type…
Browse files Browse the repository at this point in the history
… in compile-time.
  • Loading branch information
poor-circle committed Feb 19, 2024
1 parent 1b5f519 commit 1bf3467
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
10 changes: 8 additions & 2 deletions include/ylt/struct_pack/type_calculate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,14 @@ constexpr bool check_if_has_container() {
return false;
}
else if constexpr (unique_ptr<Arg>) {
return check_if_has_container<
remove_cvref_t<typename Arg::element_type>, Arg, ParentArgs...>();
if constexpr (is_base_class<typename Arg::element_type>) {
// We can't make sure if derived class has container or not
return true;
}
else {
return check_if_has_container<
remove_cvref_t<typename Arg::element_type>, Arg, ParentArgs...>();
}
}
else if constexpr (id == type_id::container_t ||
id == type_id::string_t ||
Expand Down
11 changes: 10 additions & 1 deletion src/struct_pack/tests/test_derived.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ TEST_CASE("test unique_ptr<Base>") {
}
}

TEST_CASE("test unique_ptr<Base> with virtual base") {
TEST_CASE("test vector<unique_ptr<Base>> with virtual base") {
using namespace test3;
static_assert(struct_pack::detail::is_base_class<base>);
std::vector<std::unique_ptr<base>> vec;
Expand All @@ -158,4 +158,13 @@ TEST_CASE("test unique_ptr<Base> with virtual base") {
for (std::size_t i = 0; i < vec.size(); ++i) {
CHECK(vec[i]->get_name() == vec2[i]->get_name());
}
}

TEST_CASE("test unique_ptr<Base> with virtual base") {
using namespace test3;
std::unique_ptr<base> ptr = std::make_unique<derived4>();
auto buffer2 = struct_pack::serialize<std::string>(ptr);
auto res2 = struct_pack::deserialize<std::unique_ptr<base>>(buffer2);
CHECK(res2);
CHECK(res2.value()->get_name() == std::make_unique<derived4>()->get_name());
}

0 comments on commit 1bf3467

Please sign in to comment.