forked from ifsmirnov/jngen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinters.h
232 lines (195 loc) · 5.35 KB
/
printers.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
#pragma once
#include "repr.h"
#include <iostream>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
namespace jngen {
namespace detail {
// TODO: maybe make it more clear SFINAE, like boost::has_left_shift<X,Y>?
// TODO: make these defines namespace independent
#define JNGEN_DEFINE_FUNCTION_CHECKER(name, expr)\
template<typename T, typename Enable = void>\
class Has ## name ## Helper: public std::false_type {};\
\
template<typename T>\
class Has ## name ## Helper<T,\
decltype(void(\
expr\
))\
> : public std::true_type {};\
#define JNGEN_HAS_FUNCTION(name)\
detail::Has ## name ## Helper<T>::value
JNGEN_DEFINE_FUNCTION_CHECKER(
OstreamMethod,
std::declval<std::ostream&>().operator<< (std::declval<T>())
)
JNGEN_DEFINE_FUNCTION_CHECKER(
OstreamFreeFunction,
std::operator<<(std::declval<std::ostream&>(), std::declval<T>())
)
JNGEN_DEFINE_FUNCTION_CHECKER(
Plus,
T(std::declval<T>() + 1)
)
JNGEN_DEFINE_FUNCTION_CHECKER(
Container,
std::distance(std::declval<T>().begin(), std::declval<T>().end())
)
#define JNGEN_HAS_OSTREAM()\
(JNGEN_HAS_FUNCTION(OstreamMethod) ||\
JNGEN_HAS_FUNCTION(OstreamFreeFunction))
template<typename T>
struct VectorDepth {
constexpr static int value = 0;
};
template<typename T, template <typename...> class C>
struct VectorDepth<C<T>> {
constexpr static int value =
std::is_base_of<
std::vector<T>,
C<T>
>::value ? VectorDepth<T>::value + 1 : 0;
};
} // namespace detail
#define JNGEN_DECLARE_PRINTER(constraint, priority)\
template<typename T>\
auto printValue(\
std::ostream& out, const T& t, const OutputModifier& mod, PTag<priority>)\
-> enable_if_t<constraint, void>
#define JNGEN_DECLARE_SIMPLE_PRINTER(type, priority)\
inline void printValue(std::ostream& out, const type& t,\
const OutputModifier& mod, PTag<priority>)
#define JNGEN_PRINT(value)\
printValue(out, value, mod, PTagMax{})
#define JNGEN_PRINT_NO_MOD(value)\
printValue(out, value, OutputModifier{}, PTagMax{})
JNGEN_DECLARE_PRINTER(!JNGEN_HAS_OSTREAM(), 0)
{
// can't just write 'false' here because assertion always fails
static_assert(!std::is_same<T, T>::value, "operator<< is undefined");
(void)out;
(void)mod;
(void)t;
}
JNGEN_DECLARE_PRINTER(JNGEN_HAS_OSTREAM(), 10)
{
(void)mod;
out << t;
}
JNGEN_DECLARE_PRINTER(
JNGEN_HAS_OSTREAM() && JNGEN_HAS_FUNCTION(Plus), 11)
{
if (std::is_integral<T>::value) {
out << T(t + mod.addition);
} else {
out << t;
}
}
JNGEN_DECLARE_PRINTER(detail::VectorDepth<T>::value == 1, 3)
{
if (mod.printN) {
out << t.size() << "\n";
}
bool first = true;
for (const auto& x: t) {
if (first) {
first = false;
} else {
out << mod.sep;
}
JNGEN_PRINT(x);
}
}
JNGEN_DECLARE_PRINTER(detail::VectorDepth<T>::value == 1 &&
std::tuple_size<typename T::value_type>::value == 2, 4)
{
if (mod.printN) {
out << t.size() << "\n";
}
bool first = true;
for (const auto& x: t) {
if (first) {
first = false;
} else {
out << "\n";
}
JNGEN_PRINT(x);
}
}
JNGEN_DECLARE_PRINTER(detail::VectorDepth<T>::value == 2, 4)
{
if (mod.printN) {
out << t.size() << "\n";
}
for (const auto& x: t) {
JNGEN_PRINT(x);
out << "\n";
}
}
JNGEN_DECLARE_PRINTER(JNGEN_HAS_FUNCTION(Container), 2)
{
if (mod.printN) {
out << t.size() << "\n";
}
bool first = true;
for (const auto& x: t) {
if (first) {
first = false;
} else {
out << " ";
}
JNGEN_PRINT(x);
}
}
JNGEN_DECLARE_PRINTER(JNGEN_HAS_FUNCTION(Container)
&& std::tuple_size<typename T::value_type>::value == 2, 3)
{
if (mod.printN) {
out << t.size() << "\n";
}
bool first = true;
for (const auto& x: t) {
if (first) {
first = false;
} else {
out << "\n";
}
JNGEN_PRINT(x);
}
}
// http://stackoverflow.com/a/19841470/2159939
#define JNGEN_COMMA ,
template<typename Lhs, typename Rhs>
JNGEN_DECLARE_SIMPLE_PRINTER(std::pair<Lhs JNGEN_COMMA Rhs>, 3)
{
JNGEN_PRINT(t.first);
out << " ";
JNGEN_PRINT(t.second);
}
#undef JNGEN_COMMA
// Following snippet allows writing
// cout << pair<int, int>(1, 2) << endl;
// in user code. I have to put it into separate namespace because
// 1) I don't want to 'use' all operator<< from jngen
// 2) I cannot do it in global namespace because JNGEN_HAS_OSTREAM relies
// on that it is in jngen.
namespace namespace_for_fake_operator_ltlt {
template<typename T>
auto operator<<(std::ostream& out, const T& t)
-> enable_if_t<
!JNGEN_HAS_OSTREAM() && !std::is_base_of<BaseReprProxy, T>::value,
std::ostream&
>
{
// not jngen::printValue, because relying on ADL here for printers declared
// later (see, e.g., http://stackoverflow.com/questions/42833134)
printValue(out, t, jngen::defaultMod, jngen::PTagMax{});
return out;
}
} // namespace namespace_for_fake_operator_ltlt
// Calling this operator inside jngen namespace doesn't work without this line.
using namespace jngen::namespace_for_fake_operator_ltlt;
} // namespace jngen
using namespace jngen::namespace_for_fake_operator_ltlt;