Skip to content

Commit 9d4b3ca

Browse files
committed
iox-#2414 Reset index after move construction
1 parent f33d582 commit 9d4b3ca

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ inline constexpr variant<Types...>::variant(variant&& rhs) noexcept
9797
if (m_type_index != INVALID_VARIANT_INDEX)
9898
{
9999
internal::call_at_index<0, Types...>::moveConstructor(m_type_index, &rhs.m_storage, &m_storage);
100+
rhs.m_type_index = INVALID_VARIANT_INDEX;
100101
}
101102
}
102103

@@ -114,13 +115,15 @@ inline constexpr variant<Types...>& variant<Types...>::operator=(variant&& rhs)
114115
if (m_type_index != INVALID_VARIANT_INDEX)
115116
{
116117
internal::call_at_index<0, Types...>::moveConstructor(m_type_index, &rhs.m_storage, &m_storage);
118+
rhs.m_type_index = INVALID_VARIANT_INDEX;
117119
}
118120
}
119121
else
120122
{
121123
if (m_type_index != INVALID_VARIANT_INDEX)
122124
{
123125
internal::call_at_index<0, Types...>::move(m_type_index, &rhs.m_storage, &m_storage);
126+
rhs.m_type_index = INVALID_VARIANT_INDEX;
124127
}
125128
}
126129
}

0 commit comments

Comments
 (0)