1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <cstddef>
#include <limits>
#include <optional>
#include <string_view>
#include <type_traits>
#include <utility>
#include <ftl/string.h>
// Returns the name of enumerator E::V and optionally the class (i.e. "E::V" or "V") as
// std::optional<std::string_view> by parsing the compiler-generated string literal for the
// signature of this function. The function is defined in the global namespace with a short name
// and inferred return type to reduce bloat in the read-only data segment.
template <bool S, typename E, E V>
constexpr auto ftl_enum_builder() {
static_assert(std::is_enum_v<E>);
using R = std::optional<std::string_view>;
using namespace std::literals;
// The "pretty" signature has the following format:
//
// auto ftl_enum() [E = android::test::Enum, V = android::test::Enum::kValue]
//
std::string_view view = __PRETTY_FUNCTION__;
const auto template_begin = view.rfind('[');
const auto template_end = view.rfind(']');
if (template_begin == view.npos || template_end == view.npos) return R{};
// Extract the template parameters without the enclosing brackets. Example (cont'd):
//
// E = android::test::Enum, V = android::test::Enum::kValue
//
view = view.substr(template_begin + 1, template_end - template_begin - 1);
const auto value_begin = view.rfind("V = "sv);
if (value_begin == view.npos) return R{};
// Example (cont'd):
//
// V = android::test::Enum::kValue
//
view = view.substr(value_begin);
const auto pos = S ? view.rfind("::"sv) - 2 : view.npos;
const auto name_begin = view.rfind("::"sv, pos);
if (name_begin == view.npos) return R{};
// Chop off the leading "::".
const auto name = view.substr(name_begin + 2);
// A value that is not enumerated has the format "Enum)42".
return name.find(')') == view.npos ? R{name} : R{};
}
// Returns the name of enumerator E::V (i.e. "V") as std::optional<std::string_view>
template <typename E, E V>
constexpr auto ftl_enum() {
return ftl_enum_builder<false, E, V>();
}
// Returns the name of enumerator and class E::V (i.e. "E::V") as std::optional<std::string_view>
template <typename E, E V>
constexpr auto ftl_enum_full() {
return ftl_enum_builder<true, E, V>();
}
namespace android::ftl {
// Trait for determining whether a type is specifically a scoped enum or not. By definition, a
// scoped enum is one that is not implicitly convertible to its underlying type.
//
// TODO: Replace with std::is_scoped_enum in C++23.
//
template <typename T, bool = std::is_enum_v<T>>
struct is_scoped_enum : std::false_type {};
template <typename T>
struct is_scoped_enum<T, true> : std::negation<std::is_convertible<T, std::underlying_type_t<T>>> {
};
template <typename T>
inline constexpr bool is_scoped_enum_v = is_scoped_enum<T>::value;
// Shorthand for casting an enumerator to its integral value.
//
// TODO: Replace with std::to_underlying in C++23.
//
// enum class E { A, B, C };
// static_assert(ftl::to_underlying(E::B) == 1);
//
template <typename E, typename = std::enable_if_t<std::is_enum_v<E>>>
constexpr auto to_underlying(E v) {
return static_cast<std::underlying_type_t<E>>(v);
}
// Traits for retrieving an enum's range. An enum specifies its range by defining enumerators named
// ftl_first and ftl_last. If omitted, ftl_first defaults to 0, whereas ftl_last defaults to N - 1
// where N is the bit width of the underlying type, but only if that type is unsigned, assuming the
// enumerators are flags. Also, note that unscoped enums must define both bounds, as casting out-of-
// range values results in undefined behavior if the underlying type is not fixed.
//
// enum class E { A, B, C, F = 5, ftl_last = F };
//
// static_assert(ftl::enum_begin_v<E> == E::A);
// static_assert(ftl::enum_last_v<E> == E::F);
// static_assert(ftl::enum_size_v<E> == 6);
//
// enum class F : std::uint16_t { X = 0b1, Y = 0b10, Z = 0b100 };
//
// static_assert(ftl::enum_begin_v<F> == F{0});
// static_assert(ftl::enum_last_v<F> == F{15});
// static_assert(ftl::enum_size_v<F> == 16);
//
template <typename E, typename = void>
struct enum_begin {
static_assert(is_scoped_enum_v<E>, "Missing ftl_first enumerator");
static constexpr E value{0};
};
template <typename E>
struct enum_begin<E, std::void_t<decltype(E::ftl_first)>> {
static constexpr E value = E::ftl_first;
};
template <typename E>
inline constexpr E enum_begin_v = enum_begin<E>::value;
template <typename E, typename = void>
struct enum_end {
using U = std::underlying_type_t<E>;
static_assert(is_scoped_enum_v<E> && std::is_unsigned_v<U>, "Missing ftl_last enumerator");
static constexpr E value{std::numeric_limits<U>::digits};
};
template <typename E>
struct enum_end<E, std::void_t<decltype(E::ftl_last)>> {
static constexpr E value = E{to_underlying(E::ftl_last) + 1};
};
template <typename E>
inline constexpr E enum_end_v = enum_end<E>::value;
template <typename E>
inline constexpr E enum_last_v = E{to_underlying(enum_end_v<E>) - 1};
template <typename E>
struct enum_size {
static constexpr auto kBegin = to_underlying(enum_begin_v<E>);
static constexpr auto kEnd = to_underlying(enum_end_v<E>);
static_assert(kBegin < kEnd, "Invalid range");
static constexpr std::size_t value = kEnd - kBegin;
static_assert(value <= 64, "Excessive range size");
};
template <typename E>
inline constexpr std::size_t enum_size_v = enum_size<E>::value;
namespace details {
template <auto V>
struct Identity {
static constexpr auto value = V;
};
template <typename E>
using make_enum_sequence = std::make_integer_sequence<std::underlying_type_t<E>, enum_size_v<E>>;
template <typename E, template <E> class = Identity, typename = make_enum_sequence<E>>
struct EnumRange;
template <typename E, template <E> class F, typename T, T... Vs>
struct EnumRange<E, F, std::integer_sequence<T, Vs...>> {
static constexpr auto kBegin = to_underlying(enum_begin_v<E>);
static constexpr auto kSize = enum_size_v<E>;
using R = decltype(F<E{}>::value);
const R values[kSize] = {F<static_cast<E>(Vs + kBegin)>::value...};
constexpr const auto* begin() const { return values; }
constexpr const auto* end() const { return values + kSize; }
};
template <auto V>
struct EnumName {
static constexpr auto value = ftl_enum<decltype(V), V>();
};
template <auto V>
struct EnumNameFull {
static constexpr auto value = ftl_enum_full<decltype(V), V>();
};
template <auto I>
struct FlagName {
using E = decltype(I);
using U = std::underlying_type_t<E>;
static constexpr E V{U{1} << to_underlying(I)};
static constexpr auto value = ftl_enum<E, V>();
};
} // namespace details
// Returns an iterable over the range of an enum.
//
// enum class E { A, B, C, F = 5, ftl_last = F };
//
// std::string string;
// for (E v : ftl::enum_range<E>()) {
// string += ftl::enum_name(v).value_or("?");
// }
//
// assert(string == "ABC??F");
//
template <typename E>
constexpr auto enum_range() {
return details::EnumRange<E>{};
}
// Returns a stringified enumerator at compile time.
//
// enum class E { A, B, C };
// static_assert(ftl::enum_name<E::B>() == "B");
//
template <auto V>
constexpr std::string_view enum_name() {
constexpr auto kName = ftl_enum<decltype(V), V>();
static_assert(kName, "Unknown enumerator");
return *kName;
}
// Returns a stringified enumerator with class at compile time.
//
// enum class E { A, B, C };
// static_assert(ftl::enum_name<E::B>() == "E::B");
//
template <auto V>
constexpr std::string_view enum_name_full() {
constexpr auto kName = ftl_enum_full<decltype(V), V>();
static_assert(kName, "Unknown enumerator");
return *kName;
}
// Returns a stringified enumerator, possibly at compile time.
//
// enum class E { A, B, C, F = 5, ftl_last = F };
//
// static_assert(ftl::enum_name(E::C).value_or("?") == "C");
// static_assert(ftl::enum_name(E{3}).value_or("?") == "?");
//
template <typename E>
constexpr std::optional<std::string_view> enum_name(E v) {
const auto value = to_underlying(v);
constexpr auto kBegin = to_underlying(enum_begin_v<E>);
constexpr auto kLast = to_underlying(enum_last_v<E>);
if (value < kBegin || value > kLast) return {};
constexpr auto kRange = details::EnumRange<E, details::EnumName>{};
return kRange.values[value - kBegin];
}
// Returns a stringified enumerator with class, possibly at compile time.
//
// enum class E { A, B, C, F = 5, ftl_last = F };
//
// static_assert(ftl::enum_name(E::C).value_or("?") == "E::C");
// static_assert(ftl::enum_name(E{3}).value_or("?") == "?");
//
template <typename E>
constexpr std::optional<std::string_view> enum_name_full(E v) {
const auto value = to_underlying(v);
constexpr auto kBegin = to_underlying(enum_begin_v<E>);
constexpr auto kLast = to_underlying(enum_last_v<E>);
if (value < kBegin || value > kLast) return {};
constexpr auto kRange = details::EnumRange<E, details::EnumNameFull>{};
return kRange.values[value - kBegin];
}
// Returns a stringified flag enumerator, possibly at compile time.
//
// enum class F : std::uint16_t { X = 0b1, Y = 0b10, Z = 0b100 };
//
// static_assert(ftl::flag_name(F::Z).value_or("?") == "Z");
// static_assert(ftl::flag_name(F{0b111}).value_or("?") == "?");
//
template <typename E>
constexpr std::optional<std::string_view> flag_name(E v) {
const auto value = to_underlying(v);
// TODO: Replace with std::popcount and std::countr_zero in C++20.
if (__builtin_popcountll(value) != 1) return {};
constexpr auto kRange = details::EnumRange<E, details::FlagName>{};
return kRange.values[__builtin_ctzll(value)];
}
// Returns a stringified enumerator, or its integral value if not named.
//
// enum class E { A, B, C, F = 5, ftl_last = F };
//
// assert(ftl::enum_string(E::C) == "C");
// assert(ftl::enum_string(E{3}) == "3");
//
template <typename E>
inline std::string enum_string(E v) {
if (const auto name = enum_name(v)) {
return std::string(*name);
}
return to_string(to_underlying(v));
}
// Returns a stringified enumerator with class, or its integral value if not named.
//
// enum class E { A, B, C, F = 5, ftl_last = F };
//
// assert(ftl::enum_string(E::C) == "E::C");
// assert(ftl::enum_string(E{3}) == "3");
//
template <typename E>
inline std::string enum_string_full(E v) {
if (const auto name = enum_name_full(v)) {
return std::string(*name);
}
return to_string(to_underlying(v));
}
// Returns a stringified flag enumerator, or its integral value if not named.
//
// enum class F : std::uint16_t { X = 0b1, Y = 0b10, Z = 0b100 };
//
// assert(ftl::flag_string(F::Z) == "Z");
// assert(ftl::flag_string(F{7}) == "0b111");
//
template <typename E>
inline std::string flag_string(E v) {
if (const auto name = flag_name(v)) {
return std::string(*name);
}
constexpr auto radix = sizeof(E) == 1 ? Radix::kBin : Radix::kHex;
return to_string(to_underlying(v), radix);
}
} // namespace android::ftl
|