| Chris Ye | 173871c | 2020-09-08 09:55:14 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2020 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #include <android-base/stringprintf.h> | 
|  | 18 |  | 
|  | 19 | #include <array> | 
|  | 20 | #include <cstdint> | 
|  | 21 | #include <optional> | 
|  | 22 | #include <string> | 
|  | 23 |  | 
|  | 24 | #ifndef __UI_INPUT_NAMEDENUM_H | 
|  | 25 | #define __UI_INPUT_NAMEDENUM_H | 
|  | 26 |  | 
|  | 27 | namespace android { | 
|  | 28 |  | 
|  | 29 | namespace details { | 
|  | 30 | template <typename E, E V> | 
|  | 31 | constexpr std::optional<std::string_view> enum_value_name() { | 
|  | 32 | // Should look something like (but all on one line): | 
|  | 33 | //   std::optional<std::string_view> | 
|  | 34 | //   android::details::enum_value_name() | 
|  | 35 | //   [E = android::test::TestEnums, V = android::test::TestEnums::ONE] | 
|  | 36 | std::string_view view = __PRETTY_FUNCTION__; | 
|  | 37 | size_t templateStart = view.rfind("["); | 
|  | 38 | size_t templateEnd = view.rfind("]"); | 
|  | 39 | if (templateStart == std::string::npos || templateEnd == std::string::npos) { | 
|  | 40 | return std::nullopt; | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | // Extract the template parameters without the enclosing braces. | 
|  | 44 | // Example (cont'd): E = android::test::TestEnums, V = android::test::TestEnums::ONE | 
|  | 45 | view = view.substr(templateStart + 1, templateEnd - templateStart - 1); | 
|  | 46 | size_t valStart = view.rfind("V = "); | 
|  | 47 | if (valStart == std::string::npos) { | 
|  | 48 | return std::nullopt; | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | // Example (cont'd): V = android::test::TestEnums::ONE | 
|  | 52 | view = view.substr(valStart); | 
| Yabin Cui | ecb965c | 2021-08-05 14:15:56 -0700 | [diff] [blame] | 53 | // Check invalid enum values with cast, like V = (android::test::TestEnums)8. | 
|  | 54 | if (view.find('(') != std::string::npos) { | 
|  | 55 | return std::nullopt; | 
|  | 56 | } | 
| Chris Ye | 173871c | 2020-09-08 09:55:14 -0700 | [diff] [blame] | 57 | size_t nameStart = view.rfind("::"); | 
|  | 58 | if (nameStart == std::string::npos) { | 
|  | 59 | return std::nullopt; | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | // Chop off the initial "::" | 
|  | 63 | nameStart += 2; | 
|  | 64 | return view.substr(nameStart); | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | template <typename E, typename T, T... I> | 
|  | 68 | constexpr auto generate_enum_values(std::integer_sequence<T, I...> seq) { | 
|  | 69 | constexpr size_t count = seq.size(); | 
|  | 70 |  | 
|  | 71 | std::array<E, count> values{}; | 
|  | 72 | for (size_t i = 0, v = 0; v < count; ++i) { | 
|  | 73 | values[v++] = static_cast<E>(T{0} + i); | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | return values; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | template <typename E, std::size_t N> | 
|  | 80 | inline constexpr auto enum_values = | 
|  | 81 | generate_enum_values<E>(std::make_integer_sequence<std::underlying_type_t<E>, N>{}); | 
|  | 82 |  | 
|  | 83 | template <typename E, std::size_t N, std::size_t... I> | 
|  | 84 | constexpr auto generate_enum_names(std::index_sequence<I...>) noexcept { | 
|  | 85 | return std::array<std::optional<std::string_view>, sizeof...(I)>{ | 
|  | 86 | {enum_value_name<E, enum_values<E, N>[I]>()...}}; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | template <typename E, std::size_t N> | 
|  | 90 | inline constexpr auto enum_names = generate_enum_names<E, N>(std::make_index_sequence<N>{}); | 
|  | 91 |  | 
|  | 92 | } // namespace details | 
|  | 93 |  | 
|  | 94 | class NamedEnum { | 
|  | 95 | public: | 
|  | 96 | // By default allowed enum value range is 0 ~ 7. | 
|  | 97 | template <typename E> | 
|  | 98 | static constexpr size_t max = 8; | 
|  | 99 |  | 
|  | 100 | template <auto V> | 
|  | 101 | static constexpr auto enum_name() { | 
|  | 102 | using E = decltype(V); | 
|  | 103 | return details::enum_value_name<E, V>(); | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | template <typename E> | 
|  | 107 | static constexpr std::optional<std::string_view> enum_name(E val) { | 
|  | 108 | auto idx = static_cast<size_t>(val); | 
|  | 109 | return idx < max<E> ? details::enum_names<E, max<E>>[idx] : std::nullopt; | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | // Helper function for parsing enum value to string. | 
|  | 113 | // Example : enum class TestEnums { ZERO = 0x0 }; | 
|  | 114 | // NamedEnum::string(TestEnums::ZERO) returns string of "ZERO". | 
|  | 115 | // Note the default maximum enum is 8, if the enum ID to be parsed if greater than 8 like 16, | 
|  | 116 | // it should be declared to specialized the maximum enum by below: | 
|  | 117 | // template <> constexpr size_t NamedEnum::max<TestEnums> = 16; | 
|  | 118 | // If the enum class definition is sparse and contains enum values starting from a large value, | 
|  | 119 | // Do not specialize it to a large number to avoid performance issues. | 
|  | 120 | // The recommended maximum enum number to specialize is 64. | 
|  | 121 | template <typename E> | 
| Chris Ye | 749b846 | 2020-11-15 14:14:36 -0800 | [diff] [blame] | 122 | static const std::string string(E val, const char* fallbackFormat = "%02d") { | 
| Chris Ye | 173871c | 2020-09-08 09:55:14 -0700 | [diff] [blame] | 123 | std::string result; | 
|  | 124 | std::optional<std::string_view> enumString = enum_name(val); | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 125 | result += enumString ? enumString.value() : base::StringPrintf(fallbackFormat, val); | 
| Chris Ye | 173871c | 2020-09-08 09:55:14 -0700 | [diff] [blame] | 126 | return result; | 
|  | 127 | } | 
|  | 128 | }; | 
|  | 129 |  | 
|  | 130 | } // namespace android | 
|  | 131 |  | 
|  | 132 | #endif // __UI_INPUT_NAMEDENUM_H |