Skip to content

Commit 780f327

Browse files
committed
targeting append only buffer move constructor and move operator=
1 parent 589f7f3 commit 780f327

File tree

2 files changed

+31
-42
lines changed

2 files changed

+31
-42
lines changed

src/app/data-model-provider/MetadataList.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ class ListBuilder : public detail::GenericAppendOnlyBuffer
156156
ListBuilder(const ListBuilder &) = delete;
157157
ListBuilder & operator=(const ListBuilder & other) = delete;
158158

159-
ListBuilder(ListBuilder && other) : GenericAppendOnlyBuffer(sizeof(T)) { *this = std::move(other); }
159+
ListBuilder(ListBuilder && other) : GenericAppendOnlyBuffer{std::move(other)} {}
160160

161161
ListBuilder & operator=(ListBuilder && other)
162162
{
163-
*static_cast<GenericAppendOnlyBuffer *>(this) = std::move(other);
163+
detail::GenericAppendOnlyBuffer::operator=(std::move(other));
164164
return *this;
165165
}
166166

src/app/data-model-provider/tests/TestMetadataList.cpp

+29-40
Original file line numberDiff line numberDiff line change
@@ -207,63 +207,52 @@ TEST_F(TestMetadataList, ListBuilderConvertersWorks)
207207

208208
TEST_F(TestMetadataList, BufferMoveOperationsWork)
209209
{
210-
211210
{
212-
ListBuilder<int> list;
211+
using LIST = ListBuilder<int>;
213212

214-
ASSERT_EQ(list.EnsureAppendCapacity(3), CHIP_NO_ERROR);
213+
LIST movedFromList{};
215214

216-
list.Append(10);
217-
list.Append(11);
218-
list.Append(12);
215+
ASSERT_EQ(movedFromList.EnsureAppendCapacity(3), CHIP_NO_ERROR);
219216

220-
// Get a ListBuilder base class object
221-
GenericAppendOnlyBuffer originalBuffer{ static_cast<GenericAppendOnlyBuffer &&>(std::move(list)) };
217+
movedFromList.Append(10);
218+
movedFromList.Append(11);
219+
movedFromList.Append(12);
222220

223-
ASSERT_EQ(originalBuffer.Size(), size_t{ 3 });
224-
ASSERT_FALSE(originalBuffer.IsEmpty());
221+
LIST movedToList{std::move(movedFromList)};
225222

226-
/// move constructor called for the second time here
227-
GenericAppendOnlyBuffer newBuffer{ std::move(originalBuffer) };
223+
ASSERT_EQ(movedFromList.Size(), size_t{ 0 }); // NOLINT(bugprone-use-after-move)
224+
ASSERT_TRUE(movedFromList.IsEmpty()); // NOLINT(bugprone-use-after-move)
228225

229-
ASSERT_EQ(originalBuffer.Size(), size_t{ 0 }); // NOLINT(bugprone-use-after-move)
230-
ASSERT_TRUE(originalBuffer.IsEmpty()); // NOLINT(bugprone-use-after-move)
226+
ASSERT_EQ(movedToList.Size(), size_t{ 3 });
227+
ASSERT_FALSE(movedToList.IsEmpty());
231228

232-
ASSERT_EQ(newBuffer.Size(), size_t{ 3 });
233-
ASSERT_FALSE(newBuffer.IsEmpty());
229+
auto movedToListSpan = movedToList.TakeBuffer();
230+
231+
EXPECT_EQ(movedToListSpan[0], 10);
232+
EXPECT_EQ(movedToListSpan[1], 11);
233+
EXPECT_EQ(movedToListSpan[2], 12);
234234
}
235235

236236
{
237-
ListBuilder<int> list1;
238-
239-
ASSERT_EQ(list1.EnsureAppendCapacity(3), CHIP_NO_ERROR);
240-
241-
list1.Append(10);
242-
list1.Append(11);
243-
list1.Append(12);
244-
245-
ListBuilder<int> list2;
246-
247-
ASSERT_EQ(list2.EnsureAppendCapacity(2), CHIP_NO_ERROR);
237+
using LIST = ListBuilder<int>;
248238

249-
list2.Append(20);
250-
list2.Append(21);
239+
LIST movedFromList{};
240+
LIST movedToList{};
251241

252-
// Get a ListBuilder base class object
253-
GenericAppendOnlyBuffer originalBuffer{ static_cast<GenericAppendOnlyBuffer &&>(std::move(list1)) };
242+
ASSERT_EQ(movedFromList.EnsureAppendCapacity(3), CHIP_NO_ERROR);
243+
ASSERT_EQ(movedToList.EnsureAppendCapacity(3), CHIP_NO_ERROR);
254244

255-
// Get another ListBuilder base class object
256-
GenericAppendOnlyBuffer anotherBuffer{ static_cast<GenericAppendOnlyBuffer &&>(std::move(list2)) };
245+
movedFromList.Append(10);
246+
movedFromList.Append(11);
247+
movedFromList.Append(12);
257248

258-
ASSERT_EQ(originalBuffer.Size(), size_t{ 3 });
259-
ASSERT_EQ(anotherBuffer.Size(), size_t{ 2 });
249+
movedToList = std::move(movedFromList);
260250

261-
// move assignemnt operator called here
262-
originalBuffer = std::move(anotherBuffer);
251+
ASSERT_EQ(movedFromList.Size(), size_t{ 0 }); // NOLINT(bugprone-use-after-move)
252+
ASSERT_TRUE(movedFromList.IsEmpty()); // NOLINT(bugprone-use-after-move)
263253

264-
ASSERT_EQ(originalBuffer.Size(), size_t{ 2 });
265-
ASSERT_EQ(anotherBuffer.Size(), size_t{ 0 }); // NOLINT(bugprone-use-after-move)
266-
ASSERT_TRUE(anotherBuffer.IsEmpty()); // NOLINT(bugprone-use-after-move)
254+
ASSERT_EQ(movedToList.Size(), size_t{ 3 });
255+
ASSERT_FALSE(movedToList.IsEmpty());
267256
}
268257
}
269258
} // namespace

0 commit comments

Comments
 (0)