-
Notifications
You must be signed in to change notification settings - Fork 2
/
callables_wrapper.hh
281 lines (240 loc) · 7.26 KB
/
callables_wrapper.hh
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
/*
* Copyright (C) 2019 SUSE Software Solutions Germany GmbH
*
* This file is part of klp-ccp.
*
* klp-ccp is free software: you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* klp-ccp is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with klp-ccp. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef CALLABLES_WRAPPER_HH
#define CALLABLES_WRAPPER_HH
#include <utility>
#include <type_traits>
namespace klp
{
namespace ccp
{
namespace impl
{
template<typename enable, typename callable_type, typename... args_types>
class _has_overload : public std::false_type
{};
template<typename callable_type, typename... args_types>
class _has_overload
<decltype(std::declval<callable_type>()(std::declval<args_types>()...),
std::declval<void>()),
callable_type, args_types...>
: public std::true_type
{};
template<typename callable_type, typename... args_types>
using has_overload =
_has_overload<void, callable_type, args_types...>;
}
template<template<typename... __args_types> class default_action,
typename... callables_types>
class callables_wrapper;
template<template<typename... __args_types> class default_action,
typename callable_type, typename... callables_types>
class callables_wrapper<default_action,
callable_type, callables_types...>
{
public:
callables_wrapper(callable_type &&c, callables_types&&... cs) noexcept
: _c(std::forward<callable_type>(c)),
_cs(std::forward<callables_types>(cs)...)
{}
static constexpr std::size_t size()
{
return 1 + sizeof...(callables_types);
}
private:
callable_type _c;
callables_wrapper<default_action, callables_types...> _cs;
private:
template<template<typename... __args_types> class __default_action,
typename... __callable_types>
friend class callables_wrapper;
template<typename enable, typename... args_types>
struct _has_overload :
public callables_wrapper<default_action, callables_types...>::
template has_overload<args_types...>
{};
template<typename... args_types>
struct _has_overload
<typename std::enable_if<impl::has_overload<callable_type&&,
args_types...>
::value>
::type,
args_types...>
: public std::true_type
{};
public:
template<typename... args_types>
using has_overload = _has_overload<void, args_types...>;
private:
template<typename enable, typename... args_types>
struct _caller;
// This one handles the case where this instance can't handle the
// arguments' types but either the tail can or there is a
// non-no_default_action default_action.
template<typename... args_types>
struct _caller
<typename std::enable_if
<(!impl::has_overload<callable_type&&, args_types...>::value &&
(has_overload<args_types...>::value ||
!std::is_same<default_action<args_types...>, void>::value))>::type,
args_types...>
{
_caller(callable_type&,
callables_wrapper<default_action, callables_types...> &cs)
noexcept
: _cs(cs)
{}
callables_wrapper<default_action, callables_types...> &_cs;
auto operator()(args_types&&... args) ->
decltype(_cs(std::forward<args_types>(args)...))
{
return _cs(std::forward<args_types>(args)...);
}
};
// This handles the case where this instance can handle the
// arguments' types.
template<typename... args_types>
struct _caller
<typename std::enable_if<impl::has_overload<callable_type&&,
args_types...>
::value>
::type,
args_types...>
{
_caller(callable_type &c,
callables_wrapper<default_action, callables_types...>&)
noexcept
: _c(c)
{}
callable_type &_c;
auto operator()(args_types&&... args) ->
decltype(_c(std::forward<args_types>(args)...))
{
return _c(std::forward<args_types>(args)...);
}
};
public:
template<typename... args_types>
auto operator()(args_types&&... args) ->
decltype(std::declval<_caller<void, args_types...> >()
(std::forward<args_types>(args)...))
{
_caller<void, args_types...> caller(_c, _cs);
return caller(std::forward<args_types>(args)...);
}
};
template<template<typename... __args_types> class default_action>
class callables_wrapper<default_action>
{
public:
callables_wrapper() noexcept
{}
static constexpr std::size_t size()
{
return 0;
}
template<typename... args_types>
using has_overload = std::false_type;
// No match for the arguments' types. Invoke the default_action
// if it is not no_default_action.
template<typename... args_types>
auto operator()(args_types&&... args) ->
typename std::enable_if
<!std::is_same<default_action<args_types...>, void>::value,
decltype(std::declval<const default_action<args_types...> >()
(std::forward<args_types>(args)...))>
::type
{
const default_action<args_types...> _caller;
return _caller(std::forward<args_types>(args)...);
}
};
template<typename...>
using no_default_action = void;
template <typename val_type, val_type val>
class default_action_return_value
{
private:
template<typename... args_types>
struct impl
{
impl() noexcept
{}
val_type operator()(args_types&&...) const noexcept
{
return val;
}
};
public:
template<typename... args_types>
using type = impl<args_types...>;
};
template<typename... args_types>
struct default_action_nop
{
default_action_nop() noexcept
{}
void operator()(args_types&&...) const noexcept
{}
};
template <typename ret_type, typename mask>
class default_action_unreachable
{
private:
template<typename enable, typename... args_types>
struct impl;
template<typename arg_type>
struct impl
<typename std::enable_if<!mask::template is_member<arg_type>()>::type,
arg_type>
{
impl() noexcept
{}
ret_type operator()(arg_type&&) const noexcept
{
assert(0);
__builtin_unreachable();
}
};
template<typename arg_type, typename... args_types>
struct impl
<typename std::enable_if<!mask::template is_member<arg_type>()>::type,
arg_type, args_types...>
{
impl() noexcept
{}
ret_type operator()(arg_type&&, args_types&&... args) const noexcept
{
return impl<void, args_types...>()(std::forward<args_types>(args)...);
}
};
public:
template<typename... args_types>
using type = impl<void, args_types...>;
};
template<template<typename... __args_types> class default_action,
typename... callables_types>
callables_wrapper<default_action, callables_types...>
wrap_callables(callables_types&&... cs)
{
return (callables_wrapper<default_action, callables_types...>
(std::forward<callables_types>(cs)...));
}
}
}
#endif