-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormatHelpers.h
160 lines (138 loc) · 4.12 KB
/
FormatHelpers.h
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
#ifndef FORMATHELPERS_H
#define FORMATHELPERS_H
#include "Shape.h"
#include "Transform.h"
#include <nlohmann/json.hpp>
#include <fmt/core.h>
#include <fmt/format.h>
#include <iostream>
#include <vector>
/**
* fmt helper for printing almost anything iterable containing formattable items
*
* Containers of char are excluded to prevent clashes with string types
*/
template<typename Container>
requires std::ranges::range<Container> && (!std::is_same_v<typename Container::value_type, char>)
struct fmt::formatter<Container> : fmt::formatter<typename Container::value_type>
{
template <typename FormatContext>
auto format(const Container& container, FormatContext& context)
{
auto&& out= context.out();
format_to(out, "{{");
bool first = true;
for (const auto& item : container) {
if (!first) {
first = false;
} else {
format_to(out, ", ");
}
fmt::formatter<typename Container::value_type>::format(item, context);
}
return format_to(out, "}}");
}
};
/**
* iostream helper for printing std::pairs containing formattable items
*/
template<typename T1, typename T2>
std::ostream& operator<<(std::ostream& ostr, const std::pair<T1, T2>& pair)
{
return ostr << "{" << pair.first << ", " << pair.second << "}";
}
/**
* iostream helper for printing std::maps containing formattable keys and values
*/
template<typename KeyType, typename MappedType>
std::ostream& operator<<(std::ostream& ostr, const std::map<KeyType, MappedType>& map)
{
ostr << "{";
bool first = true;
for (const auto& item : map) {
if (!first) {
first = false;
} else {
ostr << ", ";
}
ostr << item;
}
return ostr << "}";
}
/**
* iostream helper for printing std::vectors containing formattable items
*/
template<typename ValueType>
std::ostream& operator<<(std::ostream& ostr, const std::vector<ValueType>& values)
{
ostr << "{";
bool first = true;
for (const auto& item : values) {
if (!first) {
first = false;
} else {
ostr << ", ";
}
ostr << item;
}
return ostr << "}";
}
/**
* iostream helper for printing util::Transform
*/
inline std::ostream& operator<<(std::ostream& ostr, const Transform& t)
{
ostr << "Transform{ ";
for (const auto& val : t.GetValues()) {
ostr << val << ", ";
}
return ostr << " }";
}
/**
* iostream helper for printing util::Circle
*/
inline std::ostream& operator<<(std::ostream& ostr, const Circle& c)
{
return ostr << "Circle{ {x: " << c.x << ", y: " << c.y << "}, r: " << c.radius << "}";
}
/**
* fmt helper for printing the type of variable stored inside nlohmann::json
* values
*/
template<>
struct fmt::formatter<nlohmann::json::value_t>
{
constexpr auto parse(format_parse_context& ctx)
{
return ctx.begin();
}
template <typename FormatContext>
auto format(const nlohmann::json::value_t& valueType, FormatContext& context)
{
auto&& out= context.out();
switch (valueType) {
case nlohmann::json::value_t::array:
return format_to(out,"array");
case nlohmann::json::value_t::binary:
return format_to(out,"binary");
case nlohmann::json::value_t::boolean:
return format_to(out,"bool");
case nlohmann::json::value_t::discarded:
return format_to(out,"discarded");
case nlohmann::json::value_t::null:
return format_to(out,"null");
case nlohmann::json::value_t::number_float:
return format_to(out,"float");
case nlohmann::json::value_t::number_integer:
return format_to(out,"int");
case nlohmann::json::value_t::number_unsigned:
return format_to(out,"unsigned");
case nlohmann::json::value_t::object:
return format_to(out,"object");
case nlohmann::json::value_t::string:
return format_to(out,"string");
}
return format_to(out, "<INVALID JSON::VALUE_T VALUE>");
}
};
#endif // FORMATHELPERS_H