mirror of
https://github.com/OpenTTD/OpenTTD
synced 2026-01-26 05:34:12 +01:00
Add: EnumClassIndexContainer class.
This commit is contained in:
committed by
rubidium42
parent
e1d474d47d
commit
810dae240a
@@ -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 <typename Container, typename Index>
|
||||
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 */
|
||||
|
||||
Reference in New Issue
Block a user