Skip to content

Commit 70d1817

Browse files
committed
iox-#2414 Reset index after move construction
1 parent 181c48d commit 70d1817

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

iceoryx_hoofs/test/moduletests/test_vocabulary_variant.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,42 @@ TEST_F(variant_Test, MoveCTorWithoutValueResultsInInvalidVariant)
352352
ASSERT_THAT(ignatz.index(), Eq(iox::INVALID_VARIANT_INDEX));
353353
}
354354

355+
TEST_F(variant_Test, MoveCTorWithVariantLeadToSameValue)
356+
{
357+
::testing::Test::RecordProperty("TEST_ID", "dc2a2aff-1fcd-4679-9bfc-b2fb4d2ae928");
358+
iox::variant<int, float, ComplexClass> schlomo;
359+
schlomo = ComplexClass(2,3.14F);
360+
iox::variant<int, float, ComplexClass> ignatz(std::move(schlomo));
361+
ASSERT_THAT(schlomo.index(), Eq(iox::INVALID_VARIANT_INDEX));
362+
EXPECT_THAT(ignatz.get<ComplexClass>()->a, Eq(2));
363+
EXPECT_THAT(ignatz.get<ComplexClass>()->b, Eq(3.14F));
364+
}
365+
366+
TEST_F(variant_Test, MoveAssignmentWithDifferentTypeVariantLeadsToSameValue)
367+
{
368+
::testing::Test::RecordProperty("TEST_ID", "562a38c3-aac2-4b1f-be55-c2d1b49e6c53");
369+
iox::variant<int, float, ComplexClass> schlomo;
370+
schlomo = ComplexClass(2,3.14F);
371+
iox::variant<int, float, ComplexClass> ignatz(2.14F);
372+
ignatz = std::move(schlomo);
373+
ASSERT_THAT(schlomo.index(), Eq(iox::INVALID_VARIANT_INDEX));
374+
EXPECT_THAT(ignatz.get<ComplexClass>()->a, Eq(2));
375+
EXPECT_THAT(ignatz.get<ComplexClass>()->b, Eq(3.14F));
376+
}
377+
378+
TEST_F(variant_Test, MoveAssignmentWithSameTypeVariantLeadsToSameValue)
379+
{
380+
::testing::Test::RecordProperty("TEST_ID", "e4a530af-05c0-49e5-ae04-f3512f299fbe");
381+
iox::variant<int, float, ComplexClass> schlomo;
382+
schlomo = ComplexClass(2,3.14F);
383+
iox::variant<int, float, ComplexClass> ignatz;
384+
ignatz = ComplexClass(3,4.14F);
385+
ignatz = std::move(schlomo);
386+
ASSERT_THAT(schlomo.index(), Eq(iox::INVALID_VARIANT_INDEX));
387+
EXPECT_THAT(ignatz.get<ComplexClass>()->a, Eq(2));
388+
EXPECT_THAT(ignatz.get<ComplexClass>()->b, Eq(3.14F));
389+
}
390+
355391
TEST_F(variant_Test, MoveAssignmentWithValueLeadsToSameValue)
356392
{
357393
::testing::Test::RecordProperty("TEST_ID", "ee36df28-545f-42bc-9ef6-3699284f1a42");

iceoryx_hoofs/vocabulary/include/iox/detail/variant.inl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,15 @@ inline constexpr variant<Types...>& variant<Types...>::operator=(variant&& rhs)
115115
if (m_type_index != INVALID_VARIANT_INDEX)
116116
{
117117
internal::call_at_index<0, Types...>::moveConstructor(m_type_index, &rhs.m_storage, &m_storage);
118+
rhs.m_type_index = INVALID_VARIANT_INDEX;
118119
}
119120
}
120121
else
121122
{
122123
if (m_type_index != INVALID_VARIANT_INDEX)
123124
{
124125
internal::call_at_index<0, Types...>::move(m_type_index, &rhs.m_storage, &m_storage);
126+
rhs.m_type_index = INVALID_VARIANT_INDEX;
125127
}
126128
}
127129
}

0 commit comments

Comments
 (0)