From 181c48de81bb3a5686823e357cc9011c36a104a5 Mon Sep 17 00:00:00 2001 From: Xingyang Yuan Date: Thu, 16 Jan 2025 19:59:35 +0800 Subject: [PATCH 1/2] Reset index after move construction --- iceoryx_hoofs/vocabulary/include/iox/detail/variant.inl | 1 + 1 file changed, 1 insertion(+) diff --git a/iceoryx_hoofs/vocabulary/include/iox/detail/variant.inl b/iceoryx_hoofs/vocabulary/include/iox/detail/variant.inl index 537dd48a0a..d1e4f4c1b0 100644 --- a/iceoryx_hoofs/vocabulary/include/iox/detail/variant.inl +++ b/iceoryx_hoofs/vocabulary/include/iox/detail/variant.inl @@ -97,6 +97,7 @@ inline constexpr variant::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; } } From 70d1817d80ff9c61e32b14d48ab5036fed44a7e8 Mon Sep 17 00:00:00 2001 From: Xingyang Yuan Date: Thu, 16 Jan 2025 19:59:35 +0800 Subject: [PATCH 2/2] iox-#2414 Reset index after move construction --- .../moduletests/test_vocabulary_variant.cpp | 36 +++++++++++++++++++ .../vocabulary/include/iox/detail/variant.inl | 2 ++ 2 files changed, 38 insertions(+) diff --git a/iceoryx_hoofs/test/moduletests/test_vocabulary_variant.cpp b/iceoryx_hoofs/test/moduletests/test_vocabulary_variant.cpp index 7746ab4ba9..30f567253d 100644 --- a/iceoryx_hoofs/test/moduletests/test_vocabulary_variant.cpp +++ b/iceoryx_hoofs/test/moduletests/test_vocabulary_variant.cpp @@ -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 schlomo; + schlomo = ComplexClass(2,3.14F); + iox::variant ignatz(std::move(schlomo)); + ASSERT_THAT(schlomo.index(), Eq(iox::INVALID_VARIANT_INDEX)); + EXPECT_THAT(ignatz.get()->a, Eq(2)); + EXPECT_THAT(ignatz.get()->b, Eq(3.14F)); +} + +TEST_F(variant_Test, MoveAssignmentWithDifferentTypeVariantLeadsToSameValue) +{ + ::testing::Test::RecordProperty("TEST_ID", "562a38c3-aac2-4b1f-be55-c2d1b49e6c53"); + iox::variant schlomo; + schlomo = ComplexClass(2,3.14F); + iox::variant ignatz(2.14F); + ignatz = std::move(schlomo); + ASSERT_THAT(schlomo.index(), Eq(iox::INVALID_VARIANT_INDEX)); + EXPECT_THAT(ignatz.get()->a, Eq(2)); + EXPECT_THAT(ignatz.get()->b, Eq(3.14F)); +} + +TEST_F(variant_Test, MoveAssignmentWithSameTypeVariantLeadsToSameValue) +{ + ::testing::Test::RecordProperty("TEST_ID", "e4a530af-05c0-49e5-ae04-f3512f299fbe"); + iox::variant schlomo; + schlomo = ComplexClass(2,3.14F); + iox::variant ignatz; + ignatz = ComplexClass(3,4.14F); + ignatz = std::move(schlomo); + ASSERT_THAT(schlomo.index(), Eq(iox::INVALID_VARIANT_INDEX)); + EXPECT_THAT(ignatz.get()->a, Eq(2)); + EXPECT_THAT(ignatz.get()->b, Eq(3.14F)); +} + TEST_F(variant_Test, MoveAssignmentWithValueLeadsToSameValue) { ::testing::Test::RecordProperty("TEST_ID", "ee36df28-545f-42bc-9ef6-3699284f1a42"); diff --git a/iceoryx_hoofs/vocabulary/include/iox/detail/variant.inl b/iceoryx_hoofs/vocabulary/include/iox/detail/variant.inl index d1e4f4c1b0..3a15cf4ce1 100644 --- a/iceoryx_hoofs/vocabulary/include/iox/detail/variant.inl +++ b/iceoryx_hoofs/vocabulary/include/iox/detail/variant.inl @@ -115,6 +115,7 @@ inline constexpr variant& variant::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 @@ -122,6 +123,7 @@ inline constexpr variant& variant::operator=(variant&& rhs) 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; } } }