AsmGrader 0.0.0
Loading...
Searching...
No Matches
std.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <boost/describe.hpp>
6#include <boost/describe/enum.hpp>
7#include <boost/describe/enumerators.hpp>
8#include <boost/mp11.hpp>
9#include <boost/pfr.hpp>
10#include <boost/type_index.hpp>
11#include <fmt/base.h>
12#include <fmt/format.h>
13#include <range/v3/range/concepts.hpp>
14
15#include <concepts>
16#include <cstring>
17#include <ctime>
18#include <exception>
19#include <filesystem>
20#include <optional>
21#include <source_location>
22#include <string>
23#include <string_view>
24#include <system_error>
25#include <tuple>
26#include <variant>
27
28template <>
29struct fmt::formatter<std::exception> : formatter<std::string>
30{
31 auto format(const std::exception& from, fmt::format_context& ctx) const {
32 std::string str = fmt::format("{}: '{}'", boost::typeindex::type_id_runtime(from).pretty_name(), from.what());
33
34 return formatter<std::string>::format(str, ctx);
35 }
36};
37
38template <>
39struct fmt::formatter<std::filesystem::path> : formatter<std::string>
40{
41 auto format(const std::filesystem::path& from, fmt::format_context& ctx) const {
42 return formatter<std::string>::format(from.string(), ctx);
43 }
44};
45
46template <>
47struct fmt::formatter<std::timespec> : formatter<std::string>
48{
49 auto format(const std::timespec& from, fmt::format_context& ctx) const {
50 return format_to(ctx.out(), "timespec{{tv_sec={}, tv_nsec={}}}", from.tv_sec, from.tv_nsec);
51 }
52};
53
54template <>
55struct fmt::formatter<std::source_location> : formatter<std::string>
56{
57 auto format(const std::source_location& from, fmt::format_context& ctx) const {
58 return formatter<std::string>::format(
59 fmt::format("[{}@{}:{}:{}]", from.file_name(), from.line(), from.column(), from.function_name()), ctx);
60 }
61};
62
63// /// Formatter for std::timespec
64// inline std::string format_as(const timespec& from) {
65// return fmt::format("timespec{{tv_sec={}, tv_nsec={}}}", from.tv_sec, from.tv_nsec);
66// }
67
69template <>
70struct fmt::formatter<std::error_code> : ::asmgrader::DebugFormatter
71{
72 auto format(const std::error_code& from, format_context& ctx) const {
73 return format_to(ctx.out(), "{} : {}", strerrorname_np(from.value()), from.message());
74 }
75};
76
77template <typename T>
78struct fmt::formatter<std::optional<T>> : ::asmgrader::DebugFormatter
79{
80 auto format(const std::optional<T>& from, format_context& ctx) const {
81 if (!from) {
82 return fmt::format_to(ctx.out(), "nullopt");
83 }
84
85 if (is_debug_format) {
86 return fmt::format_to(ctx.out(), "Optional({})", from.value());
87 }
88
89 return fmt::format_to(ctx.out(), "{}", from.value());
90 }
91};
92
93template <typename... Ts>
94struct fmt::formatter<std::variant<Ts...>> : ::asmgrader::DebugFormatter
95{
96 auto format(const std::variant<Ts...>& from, format_context& ctx) const {
97 const auto& [type_str, inner_str] = std::visit(
98 [](auto&& val) {
99 std::string inner;
100 if constexpr (std::convertible_to<decltype(val), std::string_view>) {
101 inner = fmt::format("{:?}", val);
102 } else {
103 inner = fmt::format("{}", val);
104 }
105 return std::tuple{boost::typeindex::type_id<decltype(val)>().pretty_name(), inner};
106 },
107 from);
108
109 if (is_debug_format) {
110 return fmt::format_to(ctx.out(), "Variant<T={}>({})", type_str, inner_str);
111 }
112
113 return fmt::format_to(ctx.out(), "{}", inner_str);
114 }
115};
Definition debug.hpp:9
auto format(const std::error_code &from, format_context &ctx) const
Definition std.hpp:72
auto format(const std::exception &from, fmt::format_context &ctx) const
Definition std.hpp:31
auto format(const std::filesystem::path &from, fmt::format_context &ctx) const
Definition std.hpp:41
auto format(const std::optional< T > &from, format_context &ctx) const
Definition std.hpp:80
auto format(const std::source_location &from, fmt::format_context &ctx) const
Definition std.hpp:57
auto format(const std::timespec &from, fmt::format_context &ctx) const
Definition std.hpp:49
auto format(const std::variant< Ts... > &from, format_context &ctx) const
Definition std.hpp:96