Skip to content

Commit 83c4f3c

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

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

doc/website/release-notes/iceoryx-unreleased.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
- Depend on @ncurses when building with Bazel [#2372](https://github.com/eclipse-iceoryx/iceoryx/issues/2372)
149149
- Fix windows performance issue [#2377](https://github.com/eclipse-iceoryx/iceoryx/issues/2377)
150150
- Max Client and Server cannot be configured independently of Max Publisher and Subscriber [#2394](https://github.com/eclipse-iceoryx/iceoryx/issues/2394)
151+
- Fix issue where 'variant' index is not reset after move [#2414](https://github.com/eclipse-iceoryx/iceoryx/issues/2414)
151152

152153
**Refactoring:**
153154

iceoryx_hoofs/test/moduletests/test_vocabulary_variant.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,48 @@ 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+
// NOLINTJUSTIFICATION check if move is invalidating the object
362+
// NOLINTNEXTLINE(bugprone-use-after-move,hicpp-invalid-access-moved,clang-analyzer-cplusplus.Move)
363+
ASSERT_THAT(schlomo.index(), Eq(iox::INVALID_VARIANT_INDEX));
364+
EXPECT_THAT(ignatz.get<ComplexClass>()->a, Eq(2));
365+
EXPECT_THAT(ignatz.get<ComplexClass>()->b, Eq(3.14F));
366+
}
367+
368+
TEST_F(variant_Test, MoveAssignmentWithDifferentTypeVariantLeadsToSameValue)
369+
{
370+
::testing::Test::RecordProperty("TEST_ID", "562a38c3-aac2-4b1f-be55-c2d1b49e6c53");
371+
iox::variant<int, float, ComplexClass> schlomo;
372+
schlomo = ComplexClass(2, 3.14F);
373+
iox::variant<int, float, ComplexClass> ignatz(2.14F);
374+
ignatz = std::move(schlomo);
375+
// NOLINTJUSTIFICATION check if move is invalidating the object
376+
// NOLINTNEXTLINE(bugprone-use-after-move,hicpp-invalid-access-moved,clang-analyzer-cplusplus.Move)
377+
ASSERT_THAT(schlomo.index(), Eq(iox::INVALID_VARIANT_INDEX));
378+
EXPECT_THAT(ignatz.get<ComplexClass>()->a, Eq(2));
379+
EXPECT_THAT(ignatz.get<ComplexClass>()->b, Eq(3.14F));
380+
}
381+
382+
TEST_F(variant_Test, MoveAssignmentWithSameTypeVariantLeadsToSameValue)
383+
{
384+
::testing::Test::RecordProperty("TEST_ID", "e4a530af-05c0-49e5-ae04-f3512f299fbe");
385+
iox::variant<int, float, ComplexClass> schlomo;
386+
schlomo = ComplexClass(2, 3.14F);
387+
iox::variant<int, float, ComplexClass> ignatz;
388+
ignatz = ComplexClass(3, 4.14F);
389+
ignatz = std::move(schlomo);
390+
// NOLINTJUSTIFICATION check if move is invalidating the object
391+
// NOLINTNEXTLINE(bugprone-use-after-move,hicpp-invalid-access-moved,clang-analyzer-cplusplus.Move)
392+
ASSERT_THAT(schlomo.index(), Eq(iox::INVALID_VARIANT_INDEX));
393+
EXPECT_THAT(ignatz.get<ComplexClass>()->a, Eq(2));
394+
EXPECT_THAT(ignatz.get<ComplexClass>()->b, Eq(3.14F));
395+
}
396+
355397
TEST_F(variant_Test, MoveAssignmentWithValueLeadsToSameValue)
356398
{
357399
::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)