Skip to content

Commit

Permalink
iox-#2414 Reset index after move construction
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanxingyang committed Jan 18, 2025
1 parent f33d582 commit 9d4b3ca
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
36 changes: 36 additions & 0 deletions iceoryx_hoofs/test/moduletests/test_vocabulary_variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,42 @@ TEST_F(variant_Test, MoveCTorWithoutValueResultsInInvalidVariant)
ASSERT_THAT(ignatz.index(), Eq(iox::INVALID_VARIANT_INDEX));
}

TEST_F(variant_Test, MoveCTorWithVariantLeadToSameValue)
{
::testing::Test::RecordProperty("TEST_ID", "dc2a2aff-1fcd-4679-9bfc-b2fb4d2ae928");
iox::variant<int, float, ComplexClass> schlomo;
schlomo = ComplexClass(2, 3.14F);
iox::variant<int, float, ComplexClass> ignatz(std::move(schlomo));
ASSERT_THAT(schlomo.index(), Eq(iox::INVALID_VARIANT_INDEX));
EXPECT_THAT(ignatz.get<ComplexClass>()->a, Eq(2));
EXPECT_THAT(ignatz.get<ComplexClass>()->b, Eq(3.14F));
}

TEST_F(variant_Test, MoveAssignmentWithDifferentTypeVariantLeadsToSameValue)
{
::testing::Test::RecordProperty("TEST_ID", "562a38c3-aac2-4b1f-be55-c2d1b49e6c53");
iox::variant<int, float, ComplexClass> schlomo;
schlomo = ComplexClass(2, 3.14F);
iox::variant<int, float, ComplexClass> ignatz(2.14F);
ignatz = std::move(schlomo);
ASSERT_THAT(schlomo.index(), Eq(iox::INVALID_VARIANT_INDEX));
EXPECT_THAT(ignatz.get<ComplexClass>()->a, Eq(2));
EXPECT_THAT(ignatz.get<ComplexClass>()->b, Eq(3.14F));
}

TEST_F(variant_Test, MoveAssignmentWithSameTypeVariantLeadsToSameValue)
{
::testing::Test::RecordProperty("TEST_ID", "e4a530af-05c0-49e5-ae04-f3512f299fbe");
iox::variant<int, float, ComplexClass> schlomo;
schlomo = ComplexClass(2, 3.14F);
iox::variant<int, float, ComplexClass> ignatz;
ignatz = ComplexClass(3, 4.14F);
ignatz = std::move(schlomo);
ASSERT_THAT(schlomo.index(), Eq(iox::INVALID_VARIANT_INDEX));
EXPECT_THAT(ignatz.get<ComplexClass>()->a, Eq(2));
EXPECT_THAT(ignatz.get<ComplexClass>()->b, Eq(3.14F));
}

TEST_F(variant_Test, MoveAssignmentWithValueLeadsToSameValue)
{
::testing::Test::RecordProperty("TEST_ID", "ee36df28-545f-42bc-9ef6-3699284f1a42");
Expand Down
3 changes: 3 additions & 0 deletions iceoryx_hoofs/vocabulary/include/iox/detail/variant.inl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ inline constexpr variant<Types...>::variant(variant&& rhs) noexcept
if (m_type_index != INVALID_VARIANT_INDEX)
{
internal::call_at_index<0, Types...>::moveConstructor(m_type_index, &rhs.m_storage, &m_storage);
rhs.m_type_index = INVALID_VARIANT_INDEX;
}
}

Expand All @@ -114,13 +115,15 @@ inline constexpr variant<Types...>& variant<Types...>::operator=(variant&& rhs)
if (m_type_index != INVALID_VARIANT_INDEX)
{
internal::call_at_index<0, Types...>::moveConstructor(m_type_index, &rhs.m_storage, &m_storage);
rhs.m_type_index = INVALID_VARIANT_INDEX;
}
}
else
{
if (m_type_index != INVALID_VARIANT_INDEX)
{
internal::call_at_index<0, Types...>::move(m_type_index, &rhs.m_storage, &m_storage);
rhs.m_type_index = INVALID_VARIANT_INDEX;
}
}
}
Expand Down

0 comments on commit 9d4b3ca

Please sign in to comment.