/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ #pragma once #include #include #include #include namespace quic { // A generic class that extends std::array to be indexable using an Enum. // The enum K has to list enumerators with all values between 0 and K::MAX // (inclusive) and no others template class EnumArray : public std::array { public: using IntType = typename std::underlying_type::type; static constexpr IntType ArraySize = IntType(K::MAX) + 1; constexpr const V& operator[](K key) const { size_t ik = keyToInt(key); return this->std::array::operator[](ik); } constexpr V& operator[](K key) { size_t ik = keyToInt(key); return this->std::array::operator[](ik); } // Returns all valid values for the enum [[nodiscard]] constexpr std::array keys() const { return keyArrayHelper(std::make_integer_sequence{}); } private: constexpr IntType keyToInt(K key) const { auto ik = static_cast(key); DCHECK(ik >= 0 && ik < ArraySize); return ik; } template constexpr auto keyArrayHelper(std::integer_sequence) const { return std::array{static_cast(i)...}; } std::array arr; }; } // namespace quic