AsmGrader 0.0.0
Loading...
Searching...
No Matches
aggregate.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <boost/describe/enumerators.hpp>
6#include <boost/mp11/algorithm.hpp>
7#include <boost/pfr.hpp>
8#include <boost/type_index.hpp>
9#include <fmt/base.h>
10#include <fmt/format.h>
11#include <range/v3/range/concepts.hpp>
12
13#include <concepts>
14#include <ctime>
15#include <string>
16#include <string_view>
17#include <type_traits>
18
19namespace asmgrader::detail {
20
21template <typename Aggregate>
22 requires(std::is_aggregate_v<Aggregate> && //
23 std::is_standard_layout_v<Aggregate> && //
24 !std::is_array_v<Aggregate> && //
25 !ranges::range<Aggregate> && //
26 !std::same_as<std::tm, Aggregate>)
28{
29 static auto format(const Aggregate& from, fmt::format_context& ctx) {
30 // if (is_debug_format) {
31 // return debug_format(from, ctx);
32 // }
33 return normal_format(from, ctx);
34 }
35
36 static auto normal_format(const Aggregate& from, fmt::format_context& ctx) {
37 ctx.advance_to(fmt::format_to(ctx.out(), "{}", boost::typeindex::type_id<Aggregate>().pretty_name()));
38
39 *ctx.out()++ = '{';
40
41 boost::pfr::for_each_field_with_name(from,
42 [&, first = true](std::string_view field_name, const auto& val) mutable {
43 if (!first) {
44 ctx.advance_to(fmt::format_to(ctx.out(), ", "));
45 }
46 first = false;
47 ctx.advance_to(fmt::format_to(ctx.out(), ".{}={}", field_name, val));
48 });
49 *ctx.out()++ = '}';
50
51 return ctx.out();
52 }
53
54 static auto debug_format(const Aggregate& from, fmt::format_context& ctx) {
55 static std::string indent;
56
57 ctx.advance_to(
58 fmt::format_to(ctx.out(), "{}{} {{\n", indent, boost::typeindex::type_id<Aggregate>().pretty_name()));
59
60 indent += '\t';
61
62 boost::pfr::for_each_field_with_name(
63 from, [&, first = true](std::string_view field_name, const auto& val) mutable {
64 if (!first) {
65 ctx.advance_to(fmt::format_to(ctx.out(), ",\n"));
66 }
67 first = false;
68 ctx.advance_to(fmt::format_to(ctx.out(), "{}.{} = {}", indent, field_name, val));
69 });
70
71 indent.pop_back();
72
73 ctx.advance_to(fmt::format_to(ctx.out(), "\n{}}}", indent));
74
75 return ctx.out();
76 }
77};
78
79} // namespace asmgrader::detail
Definition registers_state.hpp:57
static auto debug_format(const Aggregate &from, fmt::format_context &ctx)
Definition aggregate.hpp:54
static auto format(const Aggregate &from, fmt::format_context &ctx)
Definition aggregate.hpp:29
static auto normal_format(const Aggregate &from, fmt::format_context &ctx)
Definition aggregate.hpp:36
Definition generic_impl.hpp:10