EnumeratedArray.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. /// \file
  15. /// This file defines an array type that can be indexed using scoped enum
  16. /// values.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_ADT_ENUMERATEDARRAY_H
  20. #define LLVM_ADT_ENUMERATEDARRAY_H
  21. #include <cassert>
  22. namespace llvm {
  23. template <typename ValueType, typename Enumeration,
  24. Enumeration LargestEnum = Enumeration::Last, typename IndexType = int,
  25. IndexType Size = 1 + static_cast<IndexType>(LargestEnum)>
  26. class EnumeratedArray {
  27. public:
  28. EnumeratedArray() = default;
  29. EnumeratedArray(ValueType V) {
  30. for (IndexType IX = 0; IX < Size; ++IX) {
  31. Underlying[IX] = V;
  32. }
  33. }
  34. inline const ValueType &operator[](const Enumeration Index) const {
  35. auto IX = static_cast<const IndexType>(Index);
  36. assert(IX >= 0 && IX < Size && "Index is out of bounds.");
  37. return Underlying[IX];
  38. }
  39. inline ValueType &operator[](const Enumeration Index) {
  40. return const_cast<ValueType &>(
  41. static_cast<const EnumeratedArray<ValueType, Enumeration, LargestEnum,
  42. IndexType, Size> &>(*this)[Index]);
  43. }
  44. inline IndexType size() { return Size; }
  45. private:
  46. ValueType Underlying[Size];
  47. };
  48. } // namespace llvm
  49. #endif // LLVM_ADT_ENUMERATEDARRAY_H
  50. #ifdef __GNUC__
  51. #pragma GCC diagnostic pop
  52. #endif