[owning-list] add RemoveAndFreeAllMatching() to OwningList class (#10210)

This commit introduces a new method, `RemoveAndFreeAllMatching()`, to
the `OwningList<Type>` class. This method removes and frees all
entries in the list that match a given indicator. This is used to
simplify the native mDNS implementation. The unit test
`test_linked_list` is also updated to validate the newly added helper
method.
This commit is contained in:
Abtin Keshavarzian
2024-05-08 11:32:42 -07:00
committed by GitHub
parent 00f7c3131b
commit 226f239c5d
3 changed files with 79 additions and 36 deletions
+37
View File
@@ -313,6 +313,7 @@ void TestOwningList(void)
OwningList<Entry> list;
OwningList<Entry> removedList;
OwnedPtr<Entry> ptr;
bool didRemove;
printf("TestOwningList\n");
@@ -433,6 +434,42 @@ void TestOwningList(void)
VerifyOrQuit(list.IsEmpty());
VerifyLinkedListContent(&removedList, &c, &d, &f, nullptr);
VerifyOrQuit(!c.WasFreed());
// Test `RemoveAndFreeAllMatching()`
a.ResetTestFlags();
b.ResetTestFlags();
c.ResetTestFlags();
d.ResetTestFlags();
e.ResetTestFlags();
f.ResetTestFlags();
list.Push(a);
list.Push(b);
list.Push(c);
list.Push(d);
list.Push(e);
list.Push(f);
VerifyLinkedListContent(&list, &f, &e, &d, &c, &b, &a, nullptr);
didRemove = list.RemoveAndFreeAllMatching(kAlphaType);
VerifyLinkedListContent(&list, &f, &d, &c, nullptr);
VerifyOrQuit(didRemove);
VerifyOrQuit(a.WasFreed());
VerifyOrQuit(b.WasFreed());
VerifyOrQuit(e.WasFreed());
VerifyOrQuit(!c.WasFreed());
didRemove = list.RemoveAndFreeAllMatching(kAlphaType);
VerifyOrQuit(!didRemove);
VerifyLinkedListContent(&list, &f, &d, &c, nullptr);
VerifyOrQuit(!c.WasFreed());
didRemove = list.RemoveAndFreeAllMatching(kBetaType);
VerifyOrQuit(list.IsEmpty());
VerifyOrQuit(didRemove);
VerifyOrQuit(c.WasFreed());
VerifyOrQuit(d.WasFreed());
VerifyOrQuit(f.WasFreed());
}
} // namespace ot