From 10d120dc852fce62eb11ec5ec80f68374ef01501 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 25 Jul 2019 11:00:26 +0200 Subject: AK: Share code between NonnullOwnPtrVector and NonnullRefPtrVector. These can just inherit from a shared base template. Thanks to Robin for the sweet idea :^) --- AK/NonnullOwnPtrVector.h | 45 +++--------------------------------------- AK/NonnullPtrVector.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ AK/NonnullRefPtrVector.h | 45 +++--------------------------------------- 3 files changed, 57 insertions(+), 84 deletions(-) create mode 100644 AK/NonnullPtrVector.h (limited to 'AK') diff --git a/AK/NonnullOwnPtrVector.h b/AK/NonnullOwnPtrVector.h index 0616658ced..d1d7024b7b 100644 --- a/AK/NonnullOwnPtrVector.h +++ b/AK/NonnullOwnPtrVector.h @@ -1,52 +1,13 @@ #pragma once +#include #include -#include namespace AK { template -class NonnullOwnPtrVector : public Vector, inline_capacity> { - typedef Vector, inline_capacity> Base; - -public: - NonnullOwnPtrVector() - { - } - - NonnullOwnPtrVector(Vector>&& other) - : Base(static_cast(other)) - { - } - NonnullOwnPtrVector(const Vector>& other) - : Base(static_cast(other)) - { - } - - using Base::size; - - using Iterator = VectorIterator; - Iterator begin() { return Iterator(*this, 0); } - Iterator end() { return Iterator(*this, size()); } - - using ConstIterator = VectorIterator; - 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); } - 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); } - -private: - // NOTE: You can't use resize() on a NonnullOwnPtrVector since making the vector - // bigger would require being able to default-construct NonnullOwnPtrs. - // Instead, use shrink(new_size). - void resize(int) = delete; +class NonnullOwnPtrVector : public NonnullPtrVector, T, inline_capacity> +{ }; } diff --git a/AK/NonnullPtrVector.h b/AK/NonnullPtrVector.h new file mode 100644 index 0000000000..345da11e08 --- /dev/null +++ b/AK/NonnullPtrVector.h @@ -0,0 +1,51 @@ +#pragma once + +#include + +namespace AK { + +template +class NonnullPtrVector : public Vector { + typedef Vector Base; + +public: + NonnullPtrVector() + { + } + + NonnullPtrVector(Vector&& other) + : Base(static_cast(other)) + { + } + NonnullPtrVector(const Vector& other) + : Base(static_cast(other)) + { + } + + using Base::size; + + using Iterator = VectorIterator; + Iterator begin() { return Iterator(*this, 0); } + Iterator end() { return Iterator(*this, size()); } + + using ConstIterator = VectorIterator; + 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); } + 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); } + +private: + // NOTE: You can't use resize() on a NonnullFooPtrVector since making the vector + // bigger would require being able to default-construct NonnullFooPtrs. + // Instead, use shrink(new_size). + void resize(int) = delete; +}; + +} diff --git a/AK/NonnullRefPtrVector.h b/AK/NonnullRefPtrVector.h index 296b92402e..1997e9fe45 100644 --- a/AK/NonnullRefPtrVector.h +++ b/AK/NonnullRefPtrVector.h @@ -1,52 +1,13 @@ #pragma once +#include #include -#include namespace AK { template -class NonnullRefPtrVector : public Vector, inline_capacity> { - typedef Vector, inline_capacity> Base; - -public: - NonnullRefPtrVector() - { - } - - NonnullRefPtrVector(Vector>&& other) - : Base(static_cast(other)) - { - } - NonnullRefPtrVector(const Vector>& other) - : Base(static_cast(other)) - { - } - - using Base::size; - - using Iterator = VectorIterator; - Iterator begin() { return Iterator(*this, 0); } - Iterator end() { return Iterator(*this, size()); } - - using ConstIterator = VectorIterator; - 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); } - 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); } - -private: - // NOTE: You can't use resize() on a NonnullRefPtrVector since making the vector - // bigger would require being able to default-construct NonnullRefPtrs. - // Instead, use shrink(new_size). - void resize(int) = delete; +class NonnullRefPtrVector : public NonnullPtrVector, T, inline_capacity> +{ }; } -- cgit v1.2.3