diff options
author | kleines Filmröllchen <filmroellchen@serenityos.org> | 2022-04-04 18:19:00 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-04-06 12:47:50 +0200 |
commit | ff8ca811c786e23d0c35408f911dc13aab5f5cfa (patch) | |
tree | 58561f6cd39333944a276caf465f87b6c0dd4e90 /AK/NonnullPtrVector.h | |
parent | 67b5aea2f9eae9819733d0649fe75ae611ff72c8 (diff) | |
download | serenity-ff8ca811c786e23d0c35408f911dc13aab5f5cfa.zip |
AK: Add find_first_index to NonnullPtrVector that strips smart pointer
When we want to use the find_first_index that base Vector provides, we
need to provide an element of the real contained type. That's impossible
for OwnPtr, however, and even with RefPtr there might be instances where
we have a raw reference to the object we want to find, but no smart
pointer. Therefore, overloading this function (with an identical body,
the magic is done by the find_index templatization) with `T const&` as a
parameter allows there use cases.
Diffstat (limited to 'AK/NonnullPtrVector.h')
-rw-r--r-- | AK/NonnullPtrVector.h | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/AK/NonnullPtrVector.h b/AK/NonnullPtrVector.h index b71f85c002..c95e8c2a50 100644 --- a/AK/NonnullPtrVector.h +++ b/AK/NonnullPtrVector.h @@ -47,6 +47,15 @@ public: ALWAYS_INLINE constexpr auto in_reverse() { return ReverseWrapper::in_reverse(*this); } ALWAYS_INLINE constexpr auto in_reverse() const { return ReverseWrapper::in_reverse(*this); } + Optional<size_t> find_first_index(T const& value) const + { + if (auto const index = AK::find_index(begin(), end(), value); + index < size()) { + return index; + } + return {}; + } + ALWAYS_INLINE PtrType& ptr_at(size_t index) { return Base::at(index); } ALWAYS_INLINE PtrType const& ptr_at(size_t index) const { return Base::at(index); } |