[linked-list] allow use of Matches() with variable number of arguments (#11065)

This commit updates `LinkedList` and `OwningList` to enhance
`FindMatching()`, `RemoveMatching()`, and related methods to use
`Matches()` with a variable number of arguments. The implementation
uses a variadic template function and forwarding references.
This commit is contained in:
Abtin Keshavarzian
2024-12-23 18:23:06 -08:00
committed by GitHub
parent e9f45cb997
commit 588e93cd15
8 changed files with 92 additions and 100 deletions
+12 -8
View File
@@ -69,6 +69,7 @@ public:
bool Matches(const char *aName) const { return strcmp(mName, aName) == 0; }
bool Matches(uint16_t aId) const { return mId == aId; }
bool Matches(Type aType) const { return mType == aType; }
bool Matches(Type aType, uint16_t aId) const { return (mType == aType) && (mId == aId); }
void Free(void) { mWasFreed = true; }
void ResetTestFlags(void) { mWasFreed = false; }
@@ -182,8 +183,11 @@ void TestLinkedList(void)
VerifyOrQuit(prev == &c);
VerifyOrQuit(list.FindMatchingWithPrev(prev, a.GetId()) == &a);
VerifyOrQuit(prev == &b);
VerifyOrQuit(list.FindMatchingWithPrev(prev, kAlphaType, b.GetId()) == &b);
VerifyOrQuit(prev == &c);
VerifyOrQuit(list.FindMatchingWithPrev(prev, e.GetId()) == nullptr, "succeeded for a missing entry");
VerifyOrQuit(list.FindMatchingWithPrev(prev, e.GetName()) == nullptr, "succeeded for a missing entry");
VerifyOrQuit(list.FindMatchingWithPrev(prev, kBetaType, 2) == nullptr, "succeeded for a missing entry");
list.SetHead(&e);
VerifyLinkedListContent(&list, &e, &d, &c, &b, &a, nullptr);
@@ -265,21 +269,21 @@ void TestLinkedList(void)
list.Push(a);
VerifyLinkedListContent(&list, &a, &b, &c, &d, &e, &f, nullptr);
list.RemoveAllMatching(kAlphaType, removedList);
list.RemoveAllMatching(removedList, kAlphaType);
VerifyLinkedListContent(&list, &c, &d, &f, nullptr);
VerifyLinkedListContent(&removedList, &e, &b, &a, nullptr);
removedList.Clear();
list.RemoveAllMatching(kAlphaType, removedList);
list.RemoveAllMatching(removedList, kAlphaType);
VerifyLinkedListContent(&list, &c, &d, &f, nullptr);
VerifyOrQuit(removedList.IsEmpty());
list.RemoveAllMatching(kBetaType, removedList);
list.RemoveAllMatching(removedList, kBetaType);
VerifyOrQuit(list.IsEmpty());
VerifyLinkedListContent(&removedList, &f, &d, &c, nullptr);
removedList.Clear();
list.RemoveAllMatching(kAlphaType, removedList);
list.RemoveAllMatching(removedList, kAlphaType);
VerifyOrQuit(list.IsEmpty());
VerifyOrQuit(removedList.IsEmpty());
@@ -291,7 +295,7 @@ void TestLinkedList(void)
list.Push(a);
VerifyLinkedListContent(&list, &a, &b, &c, &d, &e, &f, nullptr);
list.RemoveAllMatching(kBetaType, removedList);
list.RemoveAllMatching(removedList, kBetaType);
VerifyLinkedListContent(&list, &a, &b, &e, nullptr);
VerifyLinkedListContent(&removedList, &f, &d, &c, nullptr);
@@ -419,18 +423,18 @@ void TestOwningList(void)
list.Push(f);
VerifyLinkedListContent(&list, &f, &e, &d, &c, &b, &a, nullptr);
list.RemoveAllMatching(kAlphaType, removedList);
list.RemoveAllMatching(removedList, kAlphaType);
VerifyLinkedListContent(&list, &f, &d, &c, nullptr);
VerifyLinkedListContent(&removedList, &a, &b, &e, nullptr);
VerifyOrQuit(!a.WasFreed());
VerifyOrQuit(!c.WasFreed());
removedList.Clear();
list.RemoveAllMatching(kAlphaType, removedList);
list.RemoveAllMatching(removedList, kAlphaType);
VerifyOrQuit(removedList.IsEmpty());
VerifyLinkedListContent(&list, &f, &d, &c, nullptr);
list.RemoveAllMatching(kBetaType, removedList);
list.RemoveAllMatching(removedList, kBetaType);
VerifyOrQuit(list.IsEmpty());
VerifyLinkedListContent(&removedList, &c, &d, &f, nullptr);
VerifyOrQuit(!c.WasFreed());