EnumeratedArray.h 1.8 KB

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