1 /** 2 * Copyright (c) 2021-2022 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_VERIFIER_UTIL_ENUM_ARRAY_H__ 17 #define PANDA_VERIFIER_UTIL_ENUM_ARRAY_H__ 18 19 #include "macros.h" 20 21 #include "enum_tag.h" 22 23 #include <type_traits> 24 #include <array> 25 26 namespace panda::verifier { 27 28 template <typename T, typename Enum, Enum... Rest> 29 class EnumArray { 30 public: 31 template <typename = std::enable_if_t<std::is_default_constructible_v<T>>> EnumArray()32 EnumArray() : arr {} 33 { 34 } 35 36 template <typename... Args> EnumArray(Args...args)37 EnumArray(Args... args) : arr {} 38 { 39 arr.fill(T {args...}); 40 } 41 42 ~EnumArray() = default; 43 44 T &operator[](Enum e) 45 { 46 return arr[EnumTag::GetIndexFor(e)]; 47 } 48 49 const T &operator[](Enum e) const 50 { 51 return arr[EnumTag::GetIndexFor(e)]; 52 } 53 54 private: 55 using EnumTag = TagForEnum<Enum, Rest...>; 56 std::array<T, EnumTag::Size> arr; 57 }; 58 59 } // namespace panda::verifier 60 61 #endif // !PANDA_VERIFIER_UTIL_ENUM_ARRAY_H__ 62