blob: f50ff46fe26092daca4706b0868cf6a6a82054b2 [file] [log] [blame]
Chris Ye173871c2020-09-08 09:55:14 -07001/*
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
chaviw3277faf2021-05-19 16:45:23 -050024#pragma once
Chris Ye173871c2020-09-08 09:55:14 -070025
26namespace android {
27
28namespace details {
29template <typename E, E V>
30constexpr std::optional<std::string_view> enum_value_name() {
31 // Should look something like (but all on one line):
32 // std::optional<std::string_view>
33 // android::details::enum_value_name()
34 // [E = android::test::TestEnums, V = android::test::TestEnums::ONE]
35 std::string_view view = __PRETTY_FUNCTION__;
36 size_t templateStart = view.rfind("[");
37 size_t templateEnd = view.rfind("]");
38 if (templateStart == std::string::npos || templateEnd == std::string::npos) {
39 return std::nullopt;
40 }
41
42 // Extract the template parameters without the enclosing braces.
43 // Example (cont'd): E = android::test::TestEnums, V = android::test::TestEnums::ONE
44 view = view.substr(templateStart + 1, templateEnd - templateStart - 1);
45 size_t valStart = view.rfind("V = ");
46 if (valStart == std::string::npos) {
47 return std::nullopt;
48 }
49
50 // Example (cont'd): V = android::test::TestEnums::ONE
51 view = view.substr(valStart);
52 size_t nameStart = view.rfind("::");
53 if (nameStart == std::string::npos) {
54 return std::nullopt;
55 }
56
57 // Chop off the initial "::"
58 nameStart += 2;
59 return view.substr(nameStart);
60}
61
62template <typename E, typename T, T... I>
63constexpr auto generate_enum_values(std::integer_sequence<T, I...> seq) {
64 constexpr size_t count = seq.size();
65
66 std::array<E, count> values{};
67 for (size_t i = 0, v = 0; v < count; ++i) {
68 values[v++] = static_cast<E>(T{0} + i);
69 }
70
71 return values;
72}
73
74template <typename E, std::size_t N>
75inline constexpr auto enum_values =
76 generate_enum_values<E>(std::make_integer_sequence<std::underlying_type_t<E>, N>{});
77
78template <typename E, std::size_t N, std::size_t... I>
79constexpr auto generate_enum_names(std::index_sequence<I...>) noexcept {
80 return std::array<std::optional<std::string_view>, sizeof...(I)>{
81 {enum_value_name<E, enum_values<E, N>[I]>()...}};
82}
83
84template <typename E, std::size_t N>
85inline constexpr auto enum_names = generate_enum_names<E, N>(std::make_index_sequence<N>{});
86
87} // namespace details
88
89class NamedEnum {
90public:
91 // By default allowed enum value range is 0 ~ 7.
92 template <typename E>
93 static constexpr size_t max = 8;
94
95 template <auto V>
96 static constexpr auto enum_name() {
97 using E = decltype(V);
98 return details::enum_value_name<E, V>();
99 }
100
101 template <typename E>
102 static constexpr std::optional<std::string_view> enum_name(E val) {
103 auto idx = static_cast<size_t>(val);
104 return idx < max<E> ? details::enum_names<E, max<E>>[idx] : std::nullopt;
105 }
106
107 // Helper function for parsing enum value to string.
108 // Example : enum class TestEnums { ZERO = 0x0 };
109 // NamedEnum::string(TestEnums::ZERO) returns string of "ZERO".
110 // Note the default maximum enum is 8, if the enum ID to be parsed if greater than 8 like 16,
111 // it should be declared to specialized the maximum enum by below:
112 // template <> constexpr size_t NamedEnum::max<TestEnums> = 16;
113 // If the enum class definition is sparse and contains enum values starting from a large value,
114 // Do not specialize it to a large number to avoid performance issues.
115 // The recommended maximum enum number to specialize is 64.
116 template <typename E>
Chris Ye749b8462020-11-15 14:14:36 -0800117 static const std::string string(E val, const char* fallbackFormat = "%02d") {
Chris Ye173871c2020-09-08 09:55:14 -0700118 std::string result;
119 std::optional<std::string_view> enumString = enum_name(val);
Bernardo Rufino53fc31e2020-11-03 11:01:07 +0000120 result += enumString ? enumString.value() : base::StringPrintf(fallbackFormat, val);
Chris Ye173871c2020-09-08 09:55:14 -0700121 return result;
122 }
123};
124
125} // namespace android