1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:34:59 +00:00
serenity/AK/NonnullRefPtrVector.h
Andreas Kling 25a1bf0c90 AK: Add NonnullRefPtrVector<T>.
This is a slot-in convenience replacement for Vector<NonnullRefPtr<T>> that
makes accessors return T& instead of NonnullRefPtr<T>&.
Since NonnullRefPtr guarantees non-nullness, this allows you to access these
vector elements using dot (.) rather than arrow (->). :^)
2019-06-27 12:04:27 +02:00

35 lines
956 B
C++

#pragma once
#include <AK/NonnullRefPtr.h>
#include <AK/Vector.h>
namespace AK {
template<typename T, int inline_capacity = 0>
class NonnullRefPtrVector : public Vector<NonnullRefPtr<T>, inline_capacity> {
typedef Vector<NonnullRefPtr<T>> Base;
public:
NonnullRefPtrVector(Vector<NonnullRefPtr<T>>&& other)
: Base(static_cast<Base&&>(other))
{
}
NonnullRefPtrVector(const Vector<NonnullRefPtr<T>>& other)
: Base(static_cast<const Base&>(other))
{
}
using Base::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); }
const T& operator[](int index) const { return at(index); }
T& first() { return at(0); }
const T& first() const { return at(0); }
T& last() { return at(size() - 1); }
const T& last() const { return at(size() - 1); }
};
}
using AK::NonnullRefPtrVector;