1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:47:34 +00:00

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 (->). :^)
This commit is contained in:
Andreas Kling 2019-06-27 12:04:27 +02:00
parent f83263a72b
commit 25a1bf0c90
2 changed files with 117 additions and 76 deletions

35
AK/NonnullRefPtrVector.h Normal file
View file

@ -0,0 +1,35 @@
#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;