2021-02-10 20:46:32 +00:00
|
|
|
// Copyright 2021 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-02-10 20:46:32 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-04-25 02:26:27 +00:00
|
|
|
#include "Common/EnumMap.h"
|
|
|
|
|
2021-02-10 20:46:32 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper for using enums with fmt.
|
|
|
|
*
|
|
|
|
* Usage example:
|
|
|
|
*
|
|
|
|
* enum class Foo
|
|
|
|
* {
|
|
|
|
* A = 0,
|
|
|
|
* B = 1,
|
|
|
|
* C = 2,
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* template <>
|
|
|
|
* struct fmt::formatter<Foo> : EnumFormatter<Foo::C>
|
|
|
|
* {
|
2022-01-13 01:28:17 +00:00
|
|
|
* constexpr formatter() : EnumFormatter({"A", "B", "C"}) {}
|
2021-02-10 20:46:32 +00:00
|
|
|
* };
|
|
|
|
*
|
|
|
|
* enum class Bar
|
|
|
|
* {
|
|
|
|
* D = 0,
|
|
|
|
* E = 1,
|
|
|
|
* F = 3,
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* template <>
|
|
|
|
* struct fmt::formatter<Bar> : EnumFormatter<Bar::F>
|
|
|
|
* {
|
|
|
|
* // using std::array here fails due to nullptr not being const char*, at least in MSVC
|
|
|
|
* // (but only when a field is used; directly in the constructor is OK)
|
|
|
|
* static constexpr array_type names = {"D", "E", nullptr, "F"};
|
2022-01-13 01:28:17 +00:00
|
|
|
* constexpr formatter() : EnumFormatter(names) {}
|
2021-02-10 20:46:32 +00:00
|
|
|
* };
|
|
|
|
*/
|
2022-09-23 02:12:54 +00:00
|
|
|
template <auto last_member>
|
2021-02-10 20:46:32 +00:00
|
|
|
class EnumFormatter
|
|
|
|
{
|
2021-04-25 02:26:27 +00:00
|
|
|
using T = decltype(last_member);
|
|
|
|
static_assert(std::is_enum_v<T>);
|
|
|
|
|
2021-02-10 20:46:32 +00:00
|
|
|
public:
|
|
|
|
constexpr auto parse(fmt::format_parse_context& ctx)
|
|
|
|
{
|
|
|
|
auto it = ctx.begin(), end = ctx.end();
|
2021-06-12 22:06:06 +00:00
|
|
|
// 'u' for user display, 's' for shader generation, 'n' for name only
|
|
|
|
if (it != end && (*it == 'u' || *it == 's' || *it == 'n'))
|
|
|
|
format_type = *it++;
|
2021-02-10 20:46:32 +00:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FormatContext>
|
2022-01-13 01:16:29 +00:00
|
|
|
auto format(const T& e, FormatContext& ctx) const
|
2021-02-10 20:46:32 +00:00
|
|
|
{
|
2021-03-07 20:55:53 +00:00
|
|
|
const auto value_s = static_cast<std::underlying_type_t<T>>(e); // Possibly signed
|
|
|
|
const auto value_u = static_cast<std::make_unsigned_t<T>>(value_s); // Always unsigned
|
2021-04-25 02:26:27 +00:00
|
|
|
const bool has_name = m_names.InBounds(e) && m_names[e] != nullptr;
|
2021-02-10 20:46:32 +00:00
|
|
|
|
2021-06-12 22:06:06 +00:00
|
|
|
switch (format_type)
|
2021-02-10 20:46:32 +00:00
|
|
|
{
|
2021-06-12 22:06:06 +00:00
|
|
|
default:
|
|
|
|
case 'u':
|
2021-03-07 20:55:53 +00:00
|
|
|
if (has_name)
|
2021-04-25 02:26:27 +00:00
|
|
|
return fmt::format_to(ctx.out(), "{} ({})", m_names[e], value_s);
|
2021-02-10 20:46:32 +00:00
|
|
|
else
|
2021-03-07 20:55:53 +00:00
|
|
|
return fmt::format_to(ctx.out(), "Invalid ({})", value_s);
|
2021-06-12 22:06:06 +00:00
|
|
|
case 's':
|
2021-03-07 20:55:53 +00:00
|
|
|
if (has_name)
|
2021-04-25 02:26:27 +00:00
|
|
|
return fmt::format_to(ctx.out(), "{:#x}u /* {} */", value_u, m_names[e]);
|
2021-02-10 20:46:32 +00:00
|
|
|
else
|
2021-03-07 20:55:53 +00:00
|
|
|
return fmt::format_to(ctx.out(), "{:#x}u /* Invalid */", value_u);
|
2021-06-12 22:06:06 +00:00
|
|
|
case 'n':
|
|
|
|
if (has_name)
|
|
|
|
return fmt::format_to(ctx.out(), "{}", m_names[e]);
|
|
|
|
else
|
|
|
|
return fmt::format_to(ctx.out(), "Invalid ({})", value_s);
|
2021-02-10 20:46:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// This is needed because std::array deduces incorrectly if nullptr is included in the list
|
2021-04-25 02:26:27 +00:00
|
|
|
using array_type = Common::EnumMap<const char*, last_member>;
|
2021-02-10 20:46:32 +00:00
|
|
|
|
|
|
|
constexpr explicit EnumFormatter(const array_type names) : m_names(std::move(names)) {}
|
|
|
|
|
|
|
|
const array_type m_names;
|
2021-06-12 22:06:06 +00:00
|
|
|
char format_type = 'u';
|
2021-02-10 20:46:32 +00:00
|
|
|
};
|