AsmGrader
0.0.0
Loading...
Searching...
No Matches
enum.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include <
asmgrader/common/formatters/generic_impl.hpp
>
4
5
#include <boost/describe/enum.hpp>
6
#include <boost/describe/enumerators.hpp>
7
#include <boost/mp11/algorithm.hpp>
8
#include <boost/pfr.hpp>
9
#include <boost/type_index.hpp>
10
#include <fmt/base.h>
11
#include <fmt/format.h>
12
#include <range/v3/range/concepts.hpp>
13
14
#include <optional>
15
#include <type_traits>
16
17
namespace
asmgrader::detail
{
18
19
// See: https://www.boost.org/doc/libs/1_81_0/libs/describe/doc/html/describe.html#example_printing_enums_ct
20
template
<
typename
Enum>
21
requires
(std::is_enum_v<Enum> && boost::describe::has_describe_enumerators<Enum>::value)
22
constexpr
std::optional<const char*>
enum_to_string
(Enum enumerator) {
23
std::optional<const char*> res;
24
25
boost::mp11::mp_for_each<boost::describe::describe_enumerators<Enum>>([&](
auto
descriptor) {
26
if
(enumerator == descriptor.value) {
27
res = descriptor.name;
28
}
29
});
30
31
return
res;
32
}
33
34
template
<
typename
Enum>
35
requires
requires
(Enum scoped_enum) {
enum_to_string
(scoped_enum); }
36
struct
FormatterImpl
<Enum>
37
{
38
static
auto
format
(
const
Enum& from, fmt::format_context& ctx) {
39
auto
res =
enum_to_string
(from);
40
41
if
(res) {
42
return
fmt::format_to(ctx.out(),
"{}"
, *res);
43
}
44
45
return
fmt::format_to(ctx.out(),
"<unknown ({})>"
, fmt::underlying(from));
46
}
47
};
48
49
}
// namespace asmgrader::detail
generic_impl.hpp
asmgrader::detail
Definition
registers_state.hpp:57
asmgrader::detail::enum_to_string
constexpr std::optional< const char * > enum_to_string(Enum enumerator)
Definition
enum.hpp:22
asmgrader::detail::FormatterImpl< Enum >::format
static auto format(const Enum &from, fmt::format_context &ctx)
Definition
enum.hpp:38
asmgrader::detail::FormatterImpl
Definition
generic_impl.hpp:10
include
asmgrader
common
formatters
enum.hpp
Generated by
1.12.0