-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOut.h
272 lines (221 loc) · 8.05 KB
/
Out.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#ifndef COOL_OUT_H_
#define COOL_OUT_H_
#include <cool/pretty_name.h>
#include <cool/CChar.h>
#include <cool/iomanip.h>
#include <cool/Spacer.h>
#include <boost/type_traits/has_left_shift.hpp>
#include <iomanip>
#include <ios>
#include <iterator>
#include <optional>
#include <ostream>
#include <string>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>
namespace cool
{
namespace type_traits
{
template<typename T, typename = void>
struct is_range
: std::false_type {};
template<typename T>
struct is_range<T, std::void_t<decltype(std::begin(std::declval<T>()))>>
: std::true_type {};
template<typename T, typename = void>
struct is_tuple_like
: std::false_type {};
template<typename T>
struct is_tuple_like<T, std::void_t<decltype(std::tuple_size<T>::value)>>
: std::true_type {};
template<typename E>
using is_scoped_enum = std::bool_constant<std::is_enum_v<E> && !std::is_convertible_v<E, int>>;
template<typename E, typename = void>
struct has_ostream_inserter
: std::false_type {};
template<typename T>
struct has_ostream_inserter<T, std::void_t<decltype(operator<<(std::declval<std::ostream&>(), std::declval<T>()))>>
: std::true_type {};
template<typename T>
struct is_optional
: std::false_type {};
template<typename T>
struct is_optional<std::optional<T>>
: std::true_type {};
}
template<typename T, bool SkipOstreamInsert = false>
class Out
{
using deduced_type = T;
using value_type = std::remove_reference_t<deduced_type>;
using noncv_type = std::remove_cv_t<value_type>;
class Hex
{
public:
explicit constexpr Hex(unsigned char uc) noexcept : m_uc{uc} {}
explicit constexpr Hex(signed char sc) noexcept : m_uc{sc} {}
explicit constexpr Hex(char c) noexcept : m_uc{c} {}
explicit constexpr Hex(std::byte b) noexcept : m_uc{static_cast<std::underlying_type_t<std::byte>>(b)} {}
friend std::ostream& operator<<(std::ostream& os, Hex that)
{
return os
<< "0123456789abcdef"[that.m_uc / 16]
<< "0123456789abcdef"[that.m_uc % 16]
;
}
constexpr unsigned char get() const noexcept { return m_uc; }
private:
unsigned char m_uc;
};
// Need a wrapper so we can have "mutable" references
// tuple already does this work
using wrapper_type = std::tuple<std::ostream*, deduced_type>;
std::ostream& os() const noexcept { return *std::get<0>(m_wrapper); }
decltype(auto) data() const noexcept { return std::forward<deduced_type>(std::get<1>(m_wrapper)); }
void TupleLike() const
{
std::apply([&os = os()](auto&&... args)
{
os << '{';
cool::Spacer comma{','};
// fold expression over a comma (not to be confused with the Spacer comma)
((os << comma << cool::Out{std::forward<decltype(args)>(args)}), ...);
os << '}';
}, data());
}
void Optional() const
{
if (data())
os() << "1[" << cool::Out{*data()} << ']';
else
os() << "0[]";
}
void OStreamInsert() const
{ os() << data(); }
void Bool() const
{ os() << std::boolalpha << data(); }
void Char() const
{ os() << '\'' << cool::CChar{data()} << '\''; }
void Byte() const
{ os() << "0x" << Hex{data()}; }
void IntegralPromotion() const
{ os() << +data(); }
void StringView() const
{
os() << '\"';
for (char c : data())
os() << cool::CChar{c};
os() << '\"';
}
void CStringLiteral() const
{
constexpr size_t extent = std::extent_v<value_type>;
if (extent && !data()[extent - 1])
os() << cool::Out<std::string_view>{std::string_view{data(), extent-1}};
else
Range();
}
void CharStar() const
{
if (data())
os() << cool::Out<std::string_view>{std::string_view{data()}};
else
os() << "nullptr";
}
void HasOstreamInsert() const
{
if constexpr(std::is_same_v<noncv_type, bool>)
Bool();
else if constexpr(std::is_same_v<noncv_type, char>)
Char();
else if constexpr(std::is_same_v<noncv_type, signed char>)
IntegralPromotion();
else if constexpr(std::is_same_v<noncv_type, unsigned char>)
IntegralPromotion();
else if constexpr(std::is_same_v<noncv_type, std::string_view>)
StringView();
else if constexpr(std::is_same_v<noncv_type, std::string>)
StringView();
else if constexpr(1 == std::rank_v<value_type> && std::is_same_v<std::remove_extent_t<value_type>, const char>)
CStringLiteral();
else if constexpr(std::is_pointer_v<noncv_type> && std::is_same_v<std::remove_const_t<std::remove_pointer_t<noncv_type>>, char>)
CharStar();
else if constexpr(std::is_enum_v<noncv_type> && !type_traits::is_scoped_enum<noncv_type>{})
UnscopedEnum();
else
OStreamInsert();
}
void Enum() const
{
os()
<< cool::pretty_name(data())
<< '('
<< cool::Out{static_cast<std::underlying_type_t<value_type>>(data())}
<< ')'
;
}
void UnscopedEnum() const
{
if constexpr(type_traits::has_ostream_inserter<deduced_type>{})
OStreamInsert();
else
Enum();
}
void Range() const
{
os() << +std::size(data()) << '[';
cool::Spacer comma{','};
for (auto&& v : data())
os() << comma << cool::Out{v};
os() << ']';
}
void PrettyName() const
{ os() << cool::pretty_name(data()); }
void NeedsOStreamInsert() const
{
if constexpr(std::is_same_v<noncv_type, std::byte>)
Byte();
else if constexpr(std::is_enum_v<value_type>)
Enum();
else if constexpr(type_traits::is_range<deduced_type>{})
Range();
else if constexpr(type_traits::is_tuple_like<value_type>{})
TupleLike();
else if constexpr(type_traits::is_optional<noncv_type>{})
Optional();
else
PrettyName();
}
public:
static constexpr bool skip_ostream_insert = SkipOstreamInsert;
template<typename U>
explicit Out(U&& u)
: m_wrapper{nullptr, std::forward<U>(u)}
{ /* std::cerr << cool::pretty_name(*this) << std::endl; */ }
Out(Out const&) = delete;
Out& operator=(Out const&) = delete;
Out& operator=(Out&&) = delete;
Out(Out&&) = delete;
friend std::ostream& operator<<(std::ostream& os, Out&& that)
{
cool::setiomanip iomanip{true, os};
std::get<0>(that.m_wrapper) = &os;
if constexpr(!SkipOstreamInsert && boost::has_left_shift<std::ostream&, deduced_type, std::ostream&>())
that.HasOstreamInsert();
else
that.NeedsOStreamInsert();
return os;
}
private:
mutable wrapper_type m_wrapper;
};
template<typename T>
explicit Out(T&&) -> Out<T>;
template<typename T>
Out<T, true> OutOp(T&& t)
{ return Out<T, true>{std::forward<T>(t)}; }
} // cool namespace
#endif /* COOL_OUT_H_ */