-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta.h
347 lines (300 loc) · 7.99 KB
/
meta.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
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
// -*-coding: mule-utf-8-unix; fill-column: 58; -*-
/**
* @file
* Metaprogramming primitives.
*
* This file (originally) was a part of public
* https://github.com/lodyagin/types repository.
*
* @author Sergei Lodyagin
* @copyright Copyright (c) 2014, Sergei Lodyagin
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that
* the following conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TYPES_META_H_
#define TYPES_META_H_
#include <type_traits>
#include <tuple>
#include <utility>
namespace types {
//! Check whether `Check' is true (it is typically a
//! metaprogramming predicate with a value member of a
//! type bool)
//!
//! template<class T>
//! auto fun(...) ->
//! enable_fun_if(is_same<T, QString>, FunReturnType)&
template<class Check, class RetType>
using enable_fun_if = typename std::remove_reference <
decltype(typename std::enable_if<Check::value>::type(),
std::declval<RetType>())
> :: type;
//! Check whether `Check' is false (it is typically a
//! metaprogramming predicate with a value member of a
//! type bool)
//!
//! template<class T>
//! auto fun(...) ->
//! enable_fun_if_not(is_same<T, QString>, FunReturnType)&
template<class Check, class RetType>
using enable_fun_if_not = typename std::remove_reference <
decltype(typename std::enable_if<!Check::value>::type(),
std::declval<RetType>())
> :: type;
namespace tuple {
// tuple -> Args...
// Thanks to Johannes Schaub
// http://stackoverflow.com/a/7858971
template<int...>
struct seq { };
template<int N, int... S>
struct gens : gens<N-1, N-1, S...> { };
template<int... S>
struct gens<0, S...> {
typedef seq<S...> type;
};
template<class Fun, class Tuple, int... S>
auto call_(seq<S...>, const Tuple& tup)
-> decltype(Fun(std::get<S>(tup)...))
{
return Fun(std::get<S>(tup)...);
}
template<class Fun, class Tuple, int... S, class... Pars>
auto call_(Fun fun, seq<S...>, Tuple&& tup, Pars&&... pars)
-> decltype(fun(
std::forward<Pars>(pars)..., // non-tuple parameters
std::get<S>(std::forward<Tuple>(tup))...
))
{
return fun(
std::forward<Pars>(pars)..., // non-tuple parameters
std::get<S>(std::forward<Tuple>(tup))...
);
}
template<class Fun, class Tuple>
auto call(const Tuple& pars)
-> decltype(
call_<Fun>(
typename gens<std::tuple_size<Tuple>::value>::type(),
pars
)
)
{
return call_<Fun>(
typename gens<std::tuple_size<Tuple>::value>::type(),
pars
);
}
template<class Fun, class Tuple, class... Pars2>
auto call(Fun fun, Tuple&& pars, Pars2&&... pars2)
-> decltype(
call_(
fun,
typename gens<std::tuple_size<Tuple>::value>::type(),
std::forward<Tuple>(pars),
std::forward<Pars2>(pars2)...
)
)
{
return call_<Fun>(
fun,
typename gens<std::tuple_size<Tuple>::value>::type(),
std::forward<Tuple>(pars),
std::forward<Pars2>(pars2)...
);
}
template<class Type, class Tuple, int... S>
auto aggregate_construct_(seq<S...>, const Tuple& tup)
-> decltype(Type{std::get<S>(tup)...})
{
return Type{std::get<S>(tup)...};
}
template<class Type, class Tuple>
auto aggregate_construct(const Tuple& pars)
-> decltype(
aggregate_construct_<Type>(
typename gens<std::tuple_size<Tuple>::value>::type(),
pars
)
)
{
return aggregate_construct_<Type>(
typename gens<std::tuple_size<Tuple>::value>::type(),
pars
);
}
} // tuple
} // curr
namespace types {
//! Returns a most derived type
template<class T1, class T2, class Enabled = void>
struct most_derived;
template<class T>
struct most_derived<T, T>
{
using type = T;
};
template<class T1, class T2>
struct most_derived<
T1,
T2,
typename std::enable_if<
std::is_base_of<T1, T2>::value
&& !std::is_same<T1, T2>::value
>::type
>
{
using type = T2;
};
template<class T1, class T2>
struct most_derived<
T1,
T2,
typename std::enable_if<
std::is_base_of<T2, T1>::value
&& !std::is_same<T1, T2>::value
>::type
>
{
using type = T1;
};
#if 0
namespace {
template<
template<class...> class Parent,
template<class...> class... Ancestors,
class... Ts
>
class aggregate_ : public aggregate<Ancestors..., Ts...>
{
public:
using aggregate<Ancestors..., Ts...>::aggregate;
};
}
struct end_of_templates {};
template<
template<class...> class Parent,
template<class...> class... Ancestors,
class... Ts
>
class aggregate : public aggregate<Ancestors..., Ts...>
{
public:
using aggregate<Ancestors..., Ts...>::aggregate;
};
template<template<class...> class Parent, class... Ts>
class aggregate<Parent, end_of_templates, Ts...>
#endif
//! Iteration over std::tuple
template<
template<class> class UnaryFunction,
std::size_t I = 0,
class... T,
class... P
>
bool for_each(const std::tuple<T...>& t, P&&... pars)
{
return UnaryFunction
<typename std::tuple_element<I, decltype(t)>::type>
(std::forward<P>(pars)...) // constructor parameters
(std::get<I>(t))
&& for_each<UnaryFunction, I + 1, T..., P...>(t);
}
template<
template<class> class UnaryFunction,
template<class> class... PT,
std::size_t I = 0,
class... T,
class... P
>
bool for_eachT(const std::tuple<T...>& t, P&&... pars)
{
using TPar = typename std::tuple_element<I, decltype(t)>::type;
return UnaryFunction<TPar>
(PT<TPar>()..., std::forward<P>(pars)...) // constructor parameters
(std::get<I>(t))
&& for_eachT<UnaryFunction, PT..., I + 1, T..., P...>(t);
}
//! This is type expression to check whether `base' is a
//! base of `derived'
#define CURR_ENABLE_BASE_TYPE(base, derived) \
typename std::enable_if \
<std::is_base_of<base, derived>::value>::type
//! The quirk to pass a template parameters as an macro
//! argument
#define CURR_TEMPLATE_AND_PARS(templ, pars...) templ<pars>
#if 0
template<class C, class Enable = void>
struct ctr_args;
template<class C>
struct ctr_args<C, decltype(C())>
{
static constexpr int n = 0;
};
template<class C, class A1>
struct ctr_args<C, decltype(C(std::declval<A1>()))>
{
using tuple = std::tuple<A1>;
static constexpr int n = 1;
};
#endif
template<class A, class B, class Enable = void>
struct is_member;
template<class A>
struct is_member<A, std::tuple<>> : std::false_type {};
template<class A, class... B>
struct is_member<A, std::tuple<A, B...>> : std::true_type {};
template<class A, class B0, class... B>
struct is_member<A, std::tuple<B0, B...>>
: std::integral_constant<bool, is_member<A, std::tuple<B...>>::value>
{
};
template<class A, class B, class Enable = void>
struct is_subset;
template<class... B>
struct is_subset< std::tuple<>, std::tuple<B...> > : std::true_type
{
};
template<class A0, class... A, class... B>
struct is_subset<
std::tuple<A0, A...>,
std::tuple<B...>
>
: std::integral_constant<
bool,
is_member<A0, std::tuple<B...>>::value
&& is_subset<std::tuple<A...>, std::tuple<B...>>::value
>
{
};
} // types
#endif