1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PANDA_VERIFICATION_UTIL_ENUM_ARRAY_H_ 17 #define PANDA_VERIFICATION_UTIL_ENUM_ARRAY_H_ 18 19 #include "macros.h" 20 21 #include <type_traits> 22 23 namespace panda::verifier { 24 template <typename T, typename Enum, Enum...> 25 class EnumArray { 26 public: 27 template <typename... Args> EnumArray(Args...)28 EnumArray(Args...) 29 { 30 } 31 T &operator[]([[maybe_unused]] Enum) 32 { 33 UNREACHABLE(); 34 } 35 const T &operator[]([[maybe_unused]] Enum) const 36 { 37 UNREACHABLE(); 38 } 39 }; 40 41 template <typename T, typename Enum, Enum E, Enum... Rest> 42 class EnumArray<T, Enum, E, Rest...> : public EnumArray<T, Enum, Rest...> { 43 using Base = EnumArray<T, Enum, Rest...>; 44 45 public: 46 template <typename... Args> EnumArray(Args...args)47 EnumArray(Args... args) : Base(args...), T_ {args...} 48 { 49 } decltype(T{E})50 EnumArray(decltype(T {E}) *ptr [[maybe_unused]] = nullptr) : Base(), T_ {E} {} 51 ~EnumArray() = default; 52 T &operator[](Enum e) 53 { 54 if (e == E) { 55 return T_; 56 } 57 return Base::operator[](e); 58 } 59 60 const T &operator[](Enum e) const 61 { 62 if (e == E) { 63 return T_; 64 } 65 return Base::operator[](e); 66 } 67 68 private: 69 T T_; 70 }; 71 72 template <typename T, typename Enum, Enum...> 73 class EnumArraySimple { 74 public: 75 template <typename... Args> EnumArraySimple(Args...)76 EnumArraySimple(Args...) 77 { 78 } 79 T &operator[]([[maybe_unused]] Enum) 80 { 81 UNREACHABLE(); 82 } 83 const T &operator[]([[maybe_unused]] Enum) const 84 { 85 UNREACHABLE(); 86 } 87 }; 88 89 template <typename T, typename Enum, Enum E, Enum... Rest> 90 class EnumArraySimple<T, Enum, E, Rest...> : public EnumArraySimple<T, Enum, Rest...> { 91 using Base = EnumArraySimple<T, Enum, Rest...>; 92 93 public: 94 template <typename... Args> EnumArraySimple(Args...args)95 EnumArraySimple(Args... args) : Base(args...), T_ {args...} 96 { 97 } 98 ~EnumArraySimple() = default; 99 T &operator[](Enum e) 100 { 101 if (e == E) { 102 return T_; 103 } 104 return Base::operator[](e); 105 } 106 107 const T &operator[](Enum e) const 108 { 109 if (e == E) { 110 return T_; 111 } 112 return Base::operator[](e); 113 } 114 115 private: 116 T T_; 117 }; 118 119 } // namespace panda::verifier 120 121 #endif // PANDA_VERIFICATION_UTIL_ENUM_ARRAY_H_ 122