-
Notifications
You must be signed in to change notification settings - Fork 1
/
meta_programming.hpp
370 lines (312 loc) · 9.35 KB
/
meta_programming.hpp
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#pragma once
#include <array>
#include <tuple>
#include <utility>
namespace cu
{
namespace detail
{
template <typename Tuple>
struct GetTupleSizeImpl;
template <typename ...Ts>
struct GetTupleSizeImpl<std::tuple<Ts...>>
: std::integral_constant<std::size_t, sizeof...(Ts)>
{};
} // namespace detail
/// Returns the size of a tuple as compile-time constant.
template <typename Tuple>
constexpr std::size_t get_tuple_size()
{
return detail::GetTupleSizeImpl<Tuple>::value;
}
template <typename ...Ts>
constexpr auto make_index_sequence( const std::tuple<Ts...> & )
{
return std::make_index_sequence<sizeof...(Ts)>();
}
namespace detail
{
template <typename Tuple, typename F, std::size_t ...indexes>
void for_each_impl( Tuple && tuple,
F && f, std::index_sequence<indexes...> )
{
const auto _ = {(static_cast<void>(std::forward<F>(f)(
std::get<indexes>(std::forward<Tuple>(tuple)) ) ),1)...};
static_cast<void>(_);
}
template <typename Tuple1, typename Tuple2, typename F, std::size_t ...indexes>
void for_each_impl( Tuple1 && tuple1,
Tuple2 && tuple2,
F && f,
std::index_sequence<indexes...> )
{
const auto _ = { (static_cast<void>( std::forward<F>(f)(
std::get<indexes>(std::forward<Tuple1>(tuple1)),
std::get<indexes>(std::forward<Tuple2>(tuple2)) ) ),1)... };
static_cast<void>(_);
}
} // namespace detail
/// Applies a functor to each element of a tuple.
///
/// The types of the elements can differ and it still works.
template <typename Tuple, typename F>
void for_each( Tuple && tuple, F && f )
{
detail::for_each_impl(
std::forward<Tuple>(tuple),
std::forward<F>(f),
make_index_sequence(tuple) );
}
/// Applies a functor to the elements of two tuples.
///
/// In effect, the following is called:
/// @code
/// f( get< 0 >(std::forward<Tuple1>(tuple1)),
/// get< 0 >(std::forward<Tuple2>(tuple2)) );
/// .
/// .
/// .
/// f( get<N-1>(std::forward<Tuple1>(tuple1)),
/// get<N-1>(std::forward<Tuple2>(tuple2)) );
/// @endcode
/// where @c N ist the size of both tuples. Note also, that proper forwarding
/// is applied to the elements of the tuples.
template <typename Tuple1, typename Tuple2, typename F>
void for_each( Tuple1 && tuple1,
Tuple2 && tuple2,
F && f )
{
static_assert( get_tuple_size<Tuple1>() ==
get_tuple_size<Tuple2>(),
"Tuples must have the same length." );
detail::for_each_impl(
std::forward<Tuple1>(tuple1),
std::forward<Tuple2>(tuple2),
std::forward<F>(f),
make_index_sequence(tuple1) );
}
namespace detail
{
template <typename Tuple, typename F, std::size_t ...indexes>
decltype(auto) transform_impl( Tuple && tuple,
F && f,
std::index_sequence<indexes...> )
{
return std::make_tuple(
f( std::get<indexes>( std::forward<Tuple>(tuple) ) )... );
}
template <typename Tuple1, typename Tuple2, typename F, std::size_t ...indexes>
decltype(auto) transform_impl( Tuple1 && tuple1,
Tuple2 && tuple2,
F && f,
std::index_sequence<indexes...> )
{
return std::make_tuple(
f( std::get<indexes>( std::forward<Tuple1>(tuple1) ),
std::get<indexes>( std::forward<Tuple2>(tuple2) ) )... );
}
} // namespace detail
/// A functor is applied to every element of a tuple and the returned values
/// are returned in a tuple.
template <typename Tuple, typename F>
decltype(auto) transform( Tuple && tuple,
F && f )
{
return detail::transform_impl(
std::forward<Tuple>(tuple),
std::forward<F>(f),
make_index_sequence(tuple) );
}
/// A binary functor is applied to the elements of two tuples and the returned values
/// are returned in a tuple.
template <typename Tuple1, typename Tuple2, typename F>
decltype(auto) transform( Tuple1 && tuple1,
Tuple2 && tuple2,
F && f )
{
#ifndef _MSC_VER
static_assert( get_tuple_size<Tuple1>() == get_tuple_size<Tuple2>(),
"The tuple sizes must coincide." );
#endif
return detail::transform_impl(
std::forward<Tuple1>(tuple1),
std::forward<Tuple2>(tuple2),
std::forward<F>(f),
make_index_sequence(tuple1) );
}
constexpr bool all_of()
{
return true;
}
template <typename ...T>
constexpr bool all_of( bool head, T ... tail )
{
return head ? all_of( tail... ) : false;
}
namespace detail
{
template <typename T, typename Tuple, std::size_t ...indexes>
auto to_array_impl1( Tuple && tuple, std::index_sequence<indexes...> )
{
return std::array<T,get_tuple_size<Tuple>()>{
std::get<indexes>( std::forward<Tuple>(tuple))... };
}
}
/// Turns a tuple into an array.
///
/// The element type is not inferred, but must be specified.
template <typename T, typename Tuple>
auto to_array( Tuple && tuple )
{
return detail::to_array_impl1<T>(
std::forward<Tuple>(tuple),
make_index_sequence(tuple) );
}
namespace detail
{
template <typename T, std::size_t ...indexes>
auto to_array_impl2( T * arr, std::index_sequence<indexes...> )
{
return std::array<std::decay_t<T>,sizeof...(indexes)>{ {
arr[indexes]...
} };
}
} // namespace detail
template <typename T, std::size_t N>
auto to_array( T(&arr)[N] )
{
return detail::to_array_impl2( arr, std::make_index_sequence<N>() );
}
template <typename ...Ts>
constexpr auto make_array( Ts &&... elems )
-> std::array<std::common_type_t<Ts...>, sizeof...(Ts) >
{
return { std::forward<Ts>(elems)... };
}
namespace detail
{
template <typename F>
struct ReverseAccumulator
{
ReverseAccumulator( F && f_ )
: f(std::forward<F>(f_))
{}
template <typename ...Ts, typename T>
decltype(auto) operator()( T && arg, Ts &&...args ) const
{
return f( std::move(*this)( std::forward<Ts>(args)... ),
std::forward<T>(arg) );
}
template <typename T>
T && operator()( T && arg ) const
{
return std::forward<T>(arg);
}
private:
F && f;
};
template <typename ...Ts, typename F, std::size_t ...indexes>
decltype(auto) accumulate_impl( const std::tuple<Ts...> & tuple,
F && f,
std::index_sequence<indexes...> )
{
// The reverse accumulator is used for left to right associativity.
// It must be reverse because elements of an argument pack must be
// removed from the front while inferring parameters. In other words,
// a template of the form
// @code
// template <typename ...Ts, typename T>
// void f( Ts &&...args, T && arg );
// @endcode
// is ill-formed code, while
// @code
// template <typename ...Ts, typename T>
// void f( T && arg, Ts &&...args );
// @endcode
// is perfectly valid.
return ReverseAccumulator<F>(std::forward<F>(f))(
std::get<sizeof...(Ts)-indexes-1>( tuple )... );
}
} // namespace detail
/// Accumulates the elements of a tuple given a specific functor.
///
/// If the elements of the tuple shall be added together, then the functor
/// parameter should be something like this:
/// @code
/// []( auto && rhs, auto && lhs ) { return rhs + lhs; }
/// @endcode
/// Note that the operation will be performed left to right associative
/// independent of the type of functor used.
template <typename Tuple, typename F>
decltype(auto) accumulate( Tuple && tuple,
F && f )
{
return detail::accumulate_impl(
std::forward<Tuple>(tuple),
std::forward<F>(f),
make_index_sequence(tuple) );
}
namespace detail
{
template <typename Tuple, typename F, std::size_t ... indexes>
bool any_of_impl( Tuple && tuple,
F && f,
std::index_sequence<indexes...> )
{
const std::array<bool,sizeof...(indexes)> values =
{ f( std::get<indexes>(tuple) )... };
for ( bool value : values )
if ( value )
return true;
return false;
}
} // namespace detail
/// Returns @c true, iff any of the @c tuple elements evaluates to @c true.
template <typename Tuple, typename F>
bool any_of( Tuple && tuple,
F && f )
{
return detail::any_of_impl(
std::forward<Tuple>(tuple),
std::forward<F>(f),
make_index_sequence(tuple) );
}
namespace detail
{
template <bool>
struct StaticIfImpl;
template <>
struct StaticIfImpl<true>
{
template <typename T, typename U>
decltype(auto) operator()( T && t, U && )
{
return std::forward<T>(t);
}
};
template <>
struct StaticIfImpl<false>
{
template <typename T, typename U>
decltype(auto) operator()( T &&, U && u )
{
return std::forward<U>(u);
}
};
} // namespace detail
/// Returns a perfectly forwarded @c then_ or @c else_, depending on the
/// compile-time value of @c cond.
///
/// The types of @c then_ and @c else_ may be different.
template <bool cond,
typename ThenVal,
typename ElseVal>
decltype(auto) static_if(
ThenVal && then_,
ElseVal && else_ )
{
return detail::StaticIfImpl<cond>()(
std::forward<ThenVal>(then_),
std::forward<ElseVal>(else_) );
}
} // namespace cu