AK: Support range-for iteration over a NonnullRefPtrVector<T>.

This means you can now do this:

void harmonize(NonnullRefPtrVector<Voice>& voices)
{
    for (auto& voice : voices) {
        voice.sing(); // Look, no "->"!
    }
}

Pretty dang cool :^)
This commit is contained in:
Andreas Kling 2019-06-27 12:11:58 +02:00
parent 25a1bf0c90
commit 48108ec474
Notes: sideshowbarker 2024-07-19 13:28:38 +09:00

View file

@ -20,6 +20,15 @@ public:
}
using Base::size;
using Iterator = VectorIterator<NonnullRefPtrVector, T>;
Iterator begin() { return Iterator(*this, 0); }
Iterator end() { return Iterator(*this, size()); }
using ConstIterator = ConstVectorIterator<NonnullRefPtrVector, T>;
ConstIterator begin() const { return ConstIterator(*this, 0); }
ConstIterator end() const { return ConstIterator(*this, size()); }
T& at(int index) { return *Base::at(index); }
const T& at(int index) const { return *Base::at(index); }
T& operator[](int index) { return at(index); }