diff --git a/src/core/common/linked_list.hpp b/src/core/common/linked_list.hpp index abef85861..c51d8a394 100644 --- a/src/core/common/linked_list.hpp +++ b/src/core/common/linked_list.hpp @@ -251,22 +251,21 @@ public: } /** - * Indicates whether the linked list contains an entry matching a given entry indicator. + * Indicates whether the linked list contains an entry matching a set of conditions. * - * The template type `Indicator` specifies the type of @p aIndicator object which is used to match against entries - * in the list. To check that an entry matches the given indicator, the `Matches()` method is invoked on each - * `Type` entry in the list. The `Matches()` method should be provided by `Type` class accordingly: + * To check that an entry matches, the `Matches()` method is invoked on each `Type` entry in the list. The + * `Matches()` method with the same set of `Args` input types should be provided by the `Type` class accordingly: * - * bool Type::Matches(const Indicator &aIndicator) const + * bool Type::Matches(const Args &...) const * - * @param[in] aIndicator An entry indicator to match against entries in the list. + * @param[in] aArgs The args to pass to `Matches()`. * - * @retval TRUE The linked list contains an entry matching @p aIndicator. - * @retval FALSE The linked list contains no entry matching @p aIndicator. + * @retval TRUE The linked list contains a matching entry. + * @retval FALSE The linked list does not contain a matching entry. */ - template bool ContainsMatching(const Indicator &aIndicator) const + template bool ContainsMatching(const Args &...aArgs) const { - return FindMatching(aIndicator) != nullptr; + return FindMatching(aArgs...) != nullptr; } /** @@ -318,27 +317,25 @@ public: } /** - * Removes an entry matching a given entry indicator from the linked list. + * Removes an entry matching a given set of conditions from the linked list. * - * The template type `Indicator` specifies the type of @p aIndicator object which is used to match against entries - * in the list. To check that an entry matches the given indicator, the `Matches()` method is invoked on each - * `Type` entry in the list. The `Matches()` method should be provided by `Type` class accordingly: + * To check that an entry matches, the `Matches()` method is invoked on each `Type` entry in the list. The + * `Matches()` method with the same set of `Args` input types should be provided by the `Type` class accordingly: * - * bool Type::Matches(const Indicator &aIndicator) const + * bool Type::Matches(const Args &...) const * * @note This method does not change the removed entry itself (which is returned in case of success), i.e., the * entry next pointer stays as before. * - * - * @param[in] aIndicator An entry indicator to match against entries in the list. + * @param[in] aArgs The args to pass to `Matches()`. * * @returns A pointer to the removed matching entry if one could be found, or `nullptr` if no matching entry is * found. */ - template Type *RemoveMatching(const Indicator &aIndicator) + template Type *RemoveMatching(const Args &...aArgs) { Type *prev; - Type *entry = FindMatchingWithPrev(prev, aIndicator); + Type *entry = FindMatchingWithPrev(prev, aArgs...); if (entry != nullptr) { @@ -352,16 +349,15 @@ public: * Removes all entries in the list matching a given entry indicator from the list and adds * them to a new list. * - * The template type `Indicator` specifies the type of @p aIndicator object which is used to match against entries - * in the list. To check that an entry matches the given indicator, the `Matches()` method is invoked on each - * `Type` entry in the list. The `Matches()` method should be provided by `Type` class accordingly: + * To check that an entry matches, the `Matches()` method is invoked on each `Type` entry in the list. The + * `Matches()` method with the same set of `Args` input types should be provided by the `Type` class accordingly: * - * bool Type::Matches(const Indicator &aIndicator) const + * bool Type::Matches(const Args &...) const * - * @param[in] aIndicator An entry indicator to match against entries in the list. * @param[in] aRemovedList The list to add the removed entries to. + * @param[in] aArgs The args to pass to `Matches()`. */ - template void RemoveAllMatching(const Indicator &aIndicator, LinkedList &aRemovedList) + template void RemoveAllMatching(LinkedList &aRemovedList, const Args &...aArgs) { Type *entry; Type *prev; @@ -371,7 +367,7 @@ public: { next = entry->GetNext(); - if (entry->Matches(aIndicator)) + if (entry->Matches(aArgs...)) { PopAfter(prev); aRemovedList.Push(*entry); @@ -432,24 +428,23 @@ public: } /** - * Searches within the linked list to find an entry matching a given indicator. + * Searches within the linked list to find an entry matching a set of conditions, and if found also returns a + * pointer to its previous entry in the list. * - * The template type `Indicator` specifies the type of @p aIndicator object which is used to match against entries - * in the list. To check that an entry matches the given indicator, the `Matches()` method is invoked on each - * `Type` entry in the list. The `Matches()` method should be provided by `Type` class accordingly: + * To check that an entry matches, the `Matches()` method is invoked on each `Type` entry in the list. The + * `Matches()` method with the same set of `Args` input types should be provided by the `Type` class accordingly: * - * bool Type::Matches(const Indicator &aIndicator) const + * bool Type::Matches(const Args &...) const * * @param[out] aPrevEntry A pointer to output the previous entry on success (when a match is found in the list). * @p aPrevEntry is set to `nullptr` if the matching entry is the head of the list. * Otherwise it is updated to point to the previous entry before the matching entry in the * list. - * @param[in] aIndicator An indicator to match with entries in the list. + * @param[in] aArgs The args to pass to `Matches()`. * * @returns A pointer to the matching entry if one is found, or `nullptr` if no matching entry was found. */ - template - const Type *FindMatchingWithPrev(const Type *&aPrevEntry, const Indicator &aIndicator) const + template const Type *FindMatchingWithPrev(const Type *&aPrevEntry, Args &&...aArgs) const { const Type *entry; @@ -457,7 +452,7 @@ public: for (entry = mHead; entry != nullptr; aPrevEntry = entry, entry = entry->GetNext()) { - if (entry->Matches(aIndicator)) + if (entry->Matches(aArgs...)) { break; } @@ -467,64 +462,61 @@ public: } /** - * Searches within the linked list to find an entry matching a given indicator, and if found - * returns a pointer to its previous entry in the list. + * Searches within the linked list to find an entry matching a set of conditions, and if found also returns a + * pointer to its previous entry in the list. * - * The template type `Indicator` specifies the type of @p aIndicator object which is used to match against entries - * in the list. To check that an entry matches the given indicator, the `Matches()` method is invoked on each - * `Type` entry in the list. The `Matches()` method should be provided by `Type` class accordingly: + * To check that an entry matches, the `Matches()` method is invoked on each `Type` entry in the list. The + * `Matches()` method with the same set of `Args` input types should be provided by the `Type` class accordingly: * - * bool Type::Matches(const Indicator &aIndicator) const + * bool Type::Matches(const Args &...) const * * @param[out] aPrevEntry A pointer to output the previous entry on success (when a match is found in the list). * @p aPrevEntry is set to `nullptr` if the matching entry is the head of the list. * Otherwise it is updated to point to the previous entry before the matching entry in the * list. - * @param[in] aIndicator An indicator to match with entries in the list. + * @param[in] aArgs The args to pass to `Matches()`. * * @returns A pointer to the matching entry if one is found, or `nullptr` if no matching entry was found. */ - template Type *FindMatchingWithPrev(Type *&aPrevEntry, const Indicator &aIndicator) + template Type *FindMatchingWithPrev(Type *&aPrevEntry, Args &&...aArgs) { - return AsNonConst(AsConst(this)->FindMatchingWithPrev(const_cast(aPrevEntry), aIndicator)); + return AsNonConst(AsConst(this)->FindMatchingWithPrev(const_cast(aPrevEntry), aArgs...)); } /** - * Searches within the linked list to find an entry matching a given indicator. + * Searches within the linked list to find an entry matching a set of conditions. * - * The template type `Indicator` specifies the type of @p aIndicator object which is used to match against entries - * in the list. To check that an entry matches the given indicator, the `Matches()` method is invoked on each - * `Type` entry in the list. The `Matches()` method should be provided by `Type` class accordingly: + * To check that an entry matches, the `Matches()` method is invoked on each `Type` entry in the list. The + * `Matches()` method with the same set of `Args` input types should be provided by the `Type` class accordingly: * - * bool Type::Matches(const Indicator &aIndicator) const + * bool Type::Matches(const Args &...) const * - * @param[in] aIndicator An indicator to match with entries in the list. + * @param[in] aArgs The args to pass to `Matches()`. * * @returns A pointer to the matching entry if one is found, or `nullptr` if no matching entry was found. */ - template const Type *FindMatching(const Indicator &aIndicator) const + template const Type *FindMatching(const Args &...aArgs) const { const Type *prev; - return FindMatchingWithPrev(prev, aIndicator); + return FindMatchingWithPrev(prev, aArgs...); } /** - * Searches within the linked list to find an entry matching a given indicator. + * Searches within the linked list to find an entry matching a set of conditions. * - * The template type `Indicator` specifies the type of @p aIndicator object which is used to match against entries - * in the list. To check that an entry matches the given indicator, the `Matches()` method is invoked on each - * `Type` entry in the list. The `Matches()` method should be provided by `Type` class accordingly: + * To check that an entry matches, the `Matches()` method is invoked on each `Type` entry in the list. The + * `Matches()` method with the same set of `Args` input types should be provided by the `Type` class accordingly: * - * bool Type::Matches(const Indicator &aIndicator) const + * bool Type::Matches(const Args &...) const * - * @param[in] aIndicator An indicator to match with entries in the list. + * @param[in] aArgs The args to pass to `Matches()`. * * @returns A pointer to the matching entry if one is found, or `nullptr` if no matching entry was found. */ - template Type *FindMatching(const Indicator &aIndicator) + template Type *FindMatching(const Args &...aArgs) { - return AsNonConst(AsConst(this)->FindMatching(aIndicator)); + return AsNonConst(AsConst(this)->FindMatching(aArgs...)); } /** diff --git a/src/core/common/owning_list.hpp b/src/core/common/owning_list.hpp index 2cf182e43..13e88cf20 100644 --- a/src/core/common/owning_list.hpp +++ b/src/core/common/owning_list.hpp @@ -101,65 +101,61 @@ public: OwnedPtr PopAfter(Type *aPrevEntry) { return OwnedPtr(LinkedList::PopAfter(aPrevEntry)); } /** - * Removes an entry matching a given entry indicator from the linked list. + * Removes an entry matching a given set of conditions from the linked list. * - * The template type `Indicator` specifies the type of @p aIndicator object which is used to match against entries - * in the list. To check that an entry matches the given indicator, the `Matches()` method is invoked on each - * `Type` entry in the list. The `Matches()` method should be provided by `Type` class accordingly: + * To check that an entry matches, the `Matches()` method is invoked on each `Type` entry in the list. The + * `Matches()` method with the same set of `Args` input types should be provided by the `Type` class accordingly: * - * bool Type::Matches(const Indicator &aIndicator) const + * bool Type::Matches(const Args &...) const * * @note This method does not change the removed entry itself (which is returned in case of success), i.e., the * entry next pointer stays as before. * - * @param[in] aIndicator An entry indicator to match against entries in the list. + * @param[in] aArgs The args to pass to `Matches()`. * * @returns An `OwnedPtr` to the entry that was removed (set to null if there is no matching entry to remove). */ - template OwnedPtr RemoveMatching(const Indicator &aIndicator) + template OwnedPtr RemoveMatching(const Args &...aArgs) { - return OwnedPtr(LinkedList::RemoveMatching(aIndicator)); + return OwnedPtr(LinkedList::RemoveMatching(aArgs...)); } /** - * Removes all entries in the list matching a given entry indicator from the list and adds - * them to a new list. + * Removes all entries in the list matching given set of conditions from the list and adds them to a new list. * - * The template type `Indicator` specifies the type of @p aIndicator object which is used to match against entries - * in the list. To check that an entry matches the given indicator, the `Matches()` method is invoked on each - * `Type` entry in the list. The `Matches()` method should be provided by `Type` class accordingly: + * To check that an entry matches, the `Matches()` method is invoked on each `Type` entry in the list. The + * `Matches()` method with the same set of `Args` input types should be provided by the `Type` class accordingly: * - * bool Type::Matches(const Indicator &aIndicator) const + * bool Type::Matches(const Args &...) const * * The ownership of the removed entries is transferred from the original list to the @p aRemovedList. * - * @param[in] aIndicator An entry indicator to match against entries in the list. * @param[in] aRemovedList The list to add the removed entries to. + * @param[in] aArgs The args to pass to `Matches()`. */ - template void RemoveAllMatching(const Indicator &aIndicator, OwningList &aRemovedList) + template void RemoveAllMatching(OwningList &aRemovedList, const Args &...aArgs) { - LinkedList::RemoveAllMatching(aIndicator, aRemovedList); + LinkedList::RemoveAllMatching(aRemovedList, aArgs...); } /** - * Removes and frees all entries in the list matching a given entry indicator. + * Removes and frees all entries in the list matching a given set of conditions. * - * The template type `Indicator` specifies the type of @p aIndicator object which is used to match against entries - * in the list. To check that an entry matches the given indicator, the `Matches()` method is invoked on each - * `Type` entry in the list. The `Matches()` method should be provided by `Type` class accordingly: + * To check that an entry matches, the `Matches()` method is invoked on each `Type` entry in the list. The + * `Matches()` method with the same set of `Args` input types should be provided by the `Type` class accordingly: * - * bool Type::Matches(const Indicator &aIndicator) const + * bool Type::Matches(const Args &...) const * - * @param[in] aIndicator An entry indicator to match against entries in the list. + * @param[in] aArgs The args to pass to `Matches()`. * * @retval TRUE At least one matching entry was removed. * @retval FALSE No matching entry was found. */ - template bool RemoveAndFreeAllMatching(const Indicator &aIndicator) + template bool RemoveAndFreeAllMatching(const Args &...aArgs) { OwningList removedList; - RemoveAllMatching(aIndicator, removedList); + RemoveAllMatching(removedList, aArgs...); return !removedList.IsEmpty(); } }; diff --git a/src/core/net/mdns.cpp b/src/core/net/mdns.cpp index 69023eee8..7273036ff 100644 --- a/src/core/net/mdns.cpp +++ b/src/core/net/mdns.cpp @@ -4276,7 +4276,7 @@ void Core::MultiPacketRxMessages::HandleTimer(void) NextFireTime nextTime; OwningList expiredEntries; - mRxMsgEntries.RemoveAllMatching(ExpireChecker(nextTime.GetNow()), expiredEntries); + mRxMsgEntries.RemoveAllMatching(expiredEntries, ExpireChecker(nextTime.GetNow())); for (RxMsgEntry &expiredEntry : expiredEntries) { @@ -5445,7 +5445,7 @@ void Core::BrowseCache::ProcessExpiredRecords(TimeMilli aNow) { OwningList expiredEntries; - mPtrEntries.RemoveAllMatching(ExpireChecker(aNow), expiredEntries); + mPtrEntries.RemoveAllMatching(expiredEntries, ExpireChecker(aNow)); for (PtrEntry &exiredEntry : expiredEntries) { diff --git a/src/core/net/nat64_translator.cpp b/src/core/net/nat64_translator.cpp index 6cf143d6a..e46656a01 100644 --- a/src/core/net/nat64_translator.cpp +++ b/src/core/net/nat64_translator.cpp @@ -341,7 +341,7 @@ uint16_t Translator::ReleaseExpiredMappings(void) { LinkedList idleMappings; - mActiveAddressMappings.RemoveAllMatching(TimerMilli::GetNow(), idleMappings); + mActiveAddressMappings.RemoveAllMatching(idleMappings, TimerMilli::GetNow()); return ReleaseMappings(idleMappings); } diff --git a/src/core/net/srp_advertising_proxy.cpp b/src/core/net/srp_advertising_proxy.cpp index 7b1d690af..2acefded4 100644 --- a/src/core/net/srp_advertising_proxy.cpp +++ b/src/core/net/srp_advertising_proxy.cpp @@ -1252,7 +1252,7 @@ void AdvertisingProxy::HandleTimer(void) VerifyOrExit(mState == kStateRunning); - mAdvInfoList.RemoveAllMatching(AdvInfo::ExpirationChecker(nextTime.GetNow()), expiredList); + mAdvInfoList.RemoveAllMatching(expiredList, AdvInfo::ExpirationChecker(nextTime.GetNow())); for (AdvInfo &adv : mAdvInfoList) { @@ -1281,7 +1281,7 @@ void AdvertisingProxy::HandleTasklet(void) { OwningList completedList; - mAdvInfoList.RemoveAllMatching(AdvInfo::CompletionChecker(), completedList); + mAdvInfoList.RemoveAllMatching(completedList, AdvInfo::CompletionChecker()); VerifyOrExit(!completedList.IsEmpty()); diff --git a/src/core/net/srp_client.cpp b/src/core/net/srp_client.cpp index c6fe993da..7391d0b53 100644 --- a/src/core/net/srp_client.cpp +++ b/src/core/net/srp_client.cpp @@ -1959,7 +1959,7 @@ void Client::HandleUpdateDone(void) void Client::GetRemovedServices(LinkedList &aRemovedServices) { - mServices.RemoveAllMatching(kRemoved, aRemovedServices); + mServices.RemoveAllMatching(aRemovedServices, kRemoved); } Error Client::ReadResourceRecord(const Message &aMessage, uint16_t &aOffset, Dns::ResourceRecord &aRecord) diff --git a/src/core/utils/link_metrics_manager.cpp b/src/core/utils/link_metrics_manager.cpp index 75a635415..201bf9c13 100644 --- a/src/core/utils/link_metrics_manager.cpp +++ b/src/core/utils/link_metrics_manager.cpp @@ -151,7 +151,7 @@ void LinkMetricsManager::UpdateLinkMetricsStates(void) { LinkedList staleSubjects; - mSubjectList.RemoveAllMatching(*this, staleSubjects); + mSubjectList.RemoveAllMatching(staleSubjects, *this); while (!staleSubjects.IsEmpty()) { diff --git a/tests/unit/test_linked_list.cpp b/tests/unit/test_linked_list.cpp index 32c0b374c..26426b307 100644 --- a/tests/unit/test_linked_list.cpp +++ b/tests/unit/test_linked_list.cpp @@ -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());