From 6d5ba85520e91fe8b811393e0b49475684cf3d1d Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Fri, 24 Apr 2020 13:31:38 +0000 Subject: [PATCH] Bug 1628715 - Part 9: Add MOZ_NONNULL_RETURN to InsertElementSorted. r=xpcom-reviewers,nika Differential Revision: https://phabricator.services.mozilla.com/D70833 --- dom/media/TextTrackList.cpp | 8 +++----- xpcom/ds/nsTArray.h | 41 ++++++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/dom/media/TextTrackList.cpp b/dom/media/TextTrackList.cpp index 8dfeecc72f10..a67cd98226ee 100644 --- a/dom/media/TextTrackList.cpp +++ b/dom/media/TextTrackList.cpp @@ -81,11 +81,9 @@ void TextTrackList::AddTextTrack(TextTrack* aTextTrack, if (mTextTracks.Contains(aTextTrack)) { return; } - if (mTextTracks.InsertElementSorted(aTextTrack, aCompareTT)) { - aTextTrack->SetTextTrackList(this); - CreateAndDispatchTrackEventRunner(aTextTrack, - NS_LITERAL_STRING("addtrack")); - } + mTextTracks.InsertElementSorted(aTextTrack, aCompareTT); + aTextTrack->SetTextTrackList(this); + CreateAndDispatchTrackEventRunner(aTextTrack, NS_LITERAL_STRING("addtrack")); } TextTrack* TextTrackList::GetTrackById(const nsAString& aId) { diff --git a/xpcom/ds/nsTArray.h b/xpcom/ds/nsTArray.h index 0e1ae936431d..a369ea62eefe 100644 --- a/xpcom/ds/nsTArray.h +++ b/xpcom/ds/nsTArray.h @@ -1712,40 +1712,34 @@ class nsTArray_Impl return IndexOfFirstElementGt(aItem, nsDefaultComparator()); } - // Inserts |aItem| at such an index to guarantee that if the array - // was previously sorted, it will remain sorted after this - // insertion. - protected: - template - elem_type* InsertElementSorted(Item&& aItem, const Comparator& aComp) { + private: + template + elem_type* InsertElementSortedInternal(Item&& aItem, + const Comparator& aComp) { index_type index = IndexOfFirstElementGt(aItem, aComp); return InsertElementAtInternal(index, std::forward(aItem)); } + // Inserts |aItem| at such an index to guarantee that if the array + // was previously sorted, it will remain sorted after this + // insertion. public: template [[nodiscard]] elem_type* InsertElementSorted(Item&& aItem, const Comparator& aComp, const mozilla::fallible_t&) { - return InsertElementSorted( - std::forward(aItem), aComp); + return InsertElementSortedInternal(std::forward(aItem), + aComp); } // A variation on the InsertElementSorted method defined above. - protected: - template - elem_type* InsertElementSorted(Item&& aItem) { - nsDefaultComparator comp; - return InsertElementSorted( - std::forward(aItem), comp); - } - public: template [[nodiscard]] elem_type* InsertElementSorted(Item&& aItem, const mozilla::fallible_t&) { - return InsertElementSorted(std::forward(aItem)); + return InsertElementSortedInternal( + std::forward(aItem), nsDefaultComparator{}); } private: @@ -2820,6 +2814,19 @@ class nsTArray : public nsTArray_Impl { return ReplaceElementsAt(aStart, aCount, &aItem, 1); } + template + MOZ_NONNULL_RETURN elem_type* InsertElementSorted(Item&& aItem, + const Comparator& aComp) { + return this->template InsertElementSortedInternal( + std::forward(aItem), aComp); + } + + template + MOZ_NONNULL_RETURN elem_type* InsertElementSorted(Item&& aItem) { + return this->template InsertElementSortedInternal( + std::forward(aItem), nsDefaultComparator{}); + } + template MOZ_NONNULL_RETURN typename base_type::elem_type* EmplaceBack( Args&&... aArgs) {