1 //===- llvm/ADT/EnumeratedArray.h - Enumerated Array-------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines an array type that can be indexed using scoped enum values. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_ADT_ENUMERATEDARRAY_H 14 #define LLVM_ADT_ENUMERATEDARRAY_H 15 16 #include <cassert> 17 18 namespace llvm { 19 20 template <typename ValueType, typename Enumeration, 21 Enumeration LargestEnum = Enumeration::Last, typename IndexType = int, 22 IndexType Size = 1 + static_cast<IndexType>(LargestEnum)> 23 class EnumeratedArray { 24 public: 25 EnumeratedArray() = default; EnumeratedArray(ValueType V)26 EnumeratedArray(ValueType V) { 27 for (IndexType IX = 0; IX < Size; ++IX) { 28 Underlying[IX] = V; 29 } 30 } 31 inline const ValueType &operator[](const Enumeration Index) const { 32 auto IX = static_cast<const IndexType>(Index); 33 assert(IX >= 0 && IX < Size && "Index is out of bounds."); 34 return Underlying[IX]; 35 } 36 inline ValueType &operator[](const Enumeration Index) { 37 return const_cast<ValueType &>( 38 static_cast<const EnumeratedArray<ValueType, Enumeration, LargestEnum, 39 IndexType, Size> &>(*this)[Index]); 40 } size()41 inline IndexType size() { return Size; } 42 43 private: 44 ValueType Underlying[Size]; 45 }; 46 47 } // namespace llvm 48 49 #endif // LLVM_ADT_ENUMERATEDARRAY_H 50