From d0e49a297f1b57cdc9d27d7796c385c375ab3255 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sun, 8 Jun 2025 20:45:22 +0100 Subject: [PATCH] Codechange: Add TypedIndexContainer adapter type This is equivalent in functionality to ReferenceThroughBaseContainer, except only for the correct index type, instead of any type matching ConvertibleThroughBase. The also serves to unambiguously document the index type at the point of definition of the container. --- src/core/convertible_through_base.hpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/core/convertible_through_base.hpp b/src/core/convertible_through_base.hpp index 8b9fec0152..11a1051033 100644 --- a/src/core/convertible_through_base.hpp +++ b/src/core/convertible_through_base.hpp @@ -49,4 +49,25 @@ public: Container::const_reference operator[](const ConvertibleThroughBase auto &pos) const { return this->Container::operator[](pos.base()); } }; +/** + * A sort-of mixin that implements 'at(pos)' and 'operator[](pos)' only for a specific type. + * The type must have a suitable '.base()' method and therefore must inherently match 'ConvertibleThroughBase'. + * This to prevent having to call '.base()' for many container accesses, whilst preventing accidental use of the wrong index type. + */ +template +class TypedIndexContainer : public Container { +public: + Container::reference at(size_t pos) { return this->Container::at(pos); } + Container::reference at(const Index &pos) { return this->Container::at(pos.base()); } + + Container::const_reference at(size_t pos) const { return this->Container::at(pos); } + Container::const_reference at(const Index &pos) const { return this->Container::at(pos.base()); } + + Container::reference operator[](size_t pos) { return this->Container::operator[](pos); } + Container::reference operator[](const Index &pos) { return this->Container::operator[](pos.base()); } + + Container::const_reference operator[](size_t pos) const { return this->Container::operator[](pos); } + Container::const_reference operator[](const Index &pos) const { return this->Container::operator[](pos.base()); } +}; + #endif /* CONVERTIBLE_THROUGH_BASE_HPP */