diff --git a/src/core/enum_type.hpp b/src/core/enum_type.hpp index 26f3933447..1647addb14 100644 --- a/src/core/enum_type.hpp +++ b/src/core/enum_type.hpp @@ -202,4 +202,26 @@ public: static constexpr size_t DecayValueType(const BaseClass::ValueType &value) { return to_underlying(value); } }; +/** + * A sort-of mixin that implements 'at(pos)' and 'operator[](pos)' only for a specific enum class. + * This to prevent having to call 'to_underlying()' for many container accesses, whilst preventing accidental use of the wrong index type. + * @tparam Container A base container. + * @tparam Index The enum class to use for indexing. + */ +template +class EnumClassIndexContainer : public Container { +public: + Container::reference at(size_t pos) { return this->Container::at(pos); } + Container::reference at(const Index &pos) { return this->Container::at(to_underlying(pos)); } + + 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(to_underlying(pos)); } + + Container::reference operator[](size_t pos) { return this->Container::operator[](pos); } + Container::reference operator[](const Index &pos) { return this->Container::operator[](to_underlying(pos)); } + + 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[](to_underlying(pos)); } +}; + #endif /* ENUM_TYPE_HPP */