diff --git a/src/app/data-model-provider/MetadataList.h b/src/app/data-model-provider/MetadataList.h index 24a34c8223f655..070967ac755661 100644 --- a/src/app/data-model-provider/MetadataList.h +++ b/src/app/data-model-provider/MetadataList.h @@ -156,11 +156,11 @@ class ListBuilder : public detail::GenericAppendOnlyBuffer ListBuilder(const ListBuilder &) = delete; ListBuilder & operator=(const ListBuilder & other) = delete; - ListBuilder(ListBuilder && other) : GenericAppendOnlyBuffer(sizeof(T)) { *this = std::move(other); } + ListBuilder(ListBuilder && other) : GenericAppendOnlyBuffer{ std::move(other) } {} ListBuilder & operator=(ListBuilder && other) { - *static_cast(this) = std::move(other); + detail::GenericAppendOnlyBuffer::operator=(std::move(other)); return *this; } diff --git a/src/app/data-model-provider/tests/TestMetadataList.cpp b/src/app/data-model-provider/tests/TestMetadataList.cpp index f9fd7ab62be208..5f997f5508e7e8 100644 --- a/src/app/data-model-provider/tests/TestMetadataList.cpp +++ b/src/app/data-model-provider/tests/TestMetadataList.cpp @@ -32,6 +32,7 @@ using namespace chip; using namespace chip::app::DataModel; +using namespace chip::app::DataModel::detail; namespace { @@ -204,4 +205,54 @@ TEST_F(TestMetadataList, ListBuilderConvertersWorks) } } +TEST_F(TestMetadataList, BufferMoveOperationsWork) +{ + { + using LIST = ListBuilder; + + LIST movedFromList{}; + + ASSERT_EQ(movedFromList.EnsureAppendCapacity(3), CHIP_NO_ERROR); + + movedFromList.Append(10); + movedFromList.Append(11); + movedFromList.Append(12); + + LIST movedToList{ std::move(movedFromList) }; + + ASSERT_EQ(movedFromList.Size(), size_t{ 0 }); // NOLINT(bugprone-use-after-move) + ASSERT_TRUE(movedFromList.IsEmpty()); // NOLINT(bugprone-use-after-move) + + ASSERT_EQ(movedToList.Size(), size_t{ 3 }); + ASSERT_FALSE(movedToList.IsEmpty()); + + auto movedToListSpan = movedToList.TakeBuffer(); + + EXPECT_EQ(movedToListSpan[0], 10); + EXPECT_EQ(movedToListSpan[1], 11); + EXPECT_EQ(movedToListSpan[2], 12); + } + + { + using LIST = ListBuilder; + + LIST movedFromList{}; + LIST movedToList{}; + + ASSERT_EQ(movedFromList.EnsureAppendCapacity(3), CHIP_NO_ERROR); + ASSERT_EQ(movedToList.EnsureAppendCapacity(3), CHIP_NO_ERROR); + + movedFromList.Append(10); + movedFromList.Append(11); + movedFromList.Append(12); + + movedToList = std::move(movedFromList); + + ASSERT_EQ(movedFromList.Size(), size_t{ 0 }); // NOLINT(bugprone-use-after-move) + ASSERT_TRUE(movedFromList.IsEmpty()); // NOLINT(bugprone-use-after-move) + + ASSERT_EQ(movedToList.Size(), size_t{ 3 }); + ASSERT_FALSE(movedToList.IsEmpty()); + } +} } // namespace