Repository navigation
Expand file tree
/
Copy pathon.hpp
More file actions
418 lines (333 loc) · 12.6 KB
/
Copy pathon.hpp
File metadata and controls
418 lines (333 loc) · 12.6 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_ *
* /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\ *
* \/____/ \/_/\/___/ \/____/ \ \ \/ \ \ \/ \/__/\/_/ *
* \ \_\ \ \_\ *
* \/_/ \/_/ *
* *
* Copyright (C) 2011-2013 *
* Dominik Charousset <dominik.charousset@haw-hamburg.de> *
* *
* This file is part of libcppa. *
* libcppa is free software: you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, *
* or (at your option) any later version. *
* *
* libcppa 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with libcppa. If not, see <http://www.gnu.org/licenses/>. *
\******************************************************************************/
#ifndef CPPA_ON_HPP
#define CPPA_ON_HPP
#include <chrono>
#include <memory>
#include <type_traits>
#include "cppa/atom.hpp"
#include "cppa/anything.hpp"
#include "cppa/behavior.hpp"
#include "cppa/any_tuple.hpp"
#include "cppa/guard_expr.hpp"
#include "cppa/match_expr.hpp"
#include "cppa/partial_function.hpp"
#include "cppa/util/duration.hpp"
#include "cppa/util/type_list.hpp"
#include "cppa/util/arg_match_t.hpp"
#include "cppa/util/callable_trait.hpp"
#include "cppa/detail/boxed.hpp"
#include "cppa/detail/unboxed.hpp"
#include "cppa/detail/value_guard.hpp"
#include "cppa/detail/implicit_conversions.hpp"
namespace cppa { namespace detail {
template<bool IsFun, typename T>
struct add_ptr_to_fun_ { typedef T* type; };
template<typename T>
struct add_ptr_to_fun_<false, T> { typedef T type; };
template<typename T>
struct add_ptr_to_fun : add_ptr_to_fun_<std::is_function<T>::value, T> { };
template<bool ToVoid, typename T>
struct to_void_impl { typedef util::void_type type; };
template<typename T>
struct to_void_impl<false,T> { typedef typename add_ptr_to_fun<T>::type type; };
template<typename T>
struct boxed_and_not_callable_to_void
: to_void_impl<is_boxed<T>::value || !util::is_callable<T>::value, T> { };
template<typename T>
struct boxed_and_callable_to_void
: to_void_impl<is_boxed<T>::value || util::is_callable<T>::value, T> { };
class behavior_rvalue_builder {
public:
constexpr behavior_rvalue_builder(const util::duration& d) : m_tout(d) { }
template<typename F>
timeout_definition<F> operator>>(F&& f) const {
return {m_tout, std::forward<F>(f)};
}
private:
util::duration m_tout;
};
struct rvalue_builder_args_ctor { };
template<class Left, class Right>
struct disjunct_rvalue_builders {
public:
disjunct_rvalue_builders(Left l, Right r)
: m_left(std::move(l)), m_right(std::move(r)) { }
template<typename Expr>
auto operator>>(Expr expr)
-> decltype((*(static_cast<Left*>(nullptr)) >> expr).or_else(
*(static_cast<Right*>(nullptr)) >> expr)) const {
return (m_left >> expr).or_else(m_right >> expr);
}
private:
Left m_left;
Right m_right;
};
template<class Guard, class Transformers, class Pattern>
struct rvalue_builder {
typedef typename util::tl_back<Pattern>::type back_type;
static constexpr bool is_complete =
!std::is_same<util::arg_match_t,back_type>::value;
typedef typename util::tl_apply<Transformers, tdata>::type fun_container;
Guard m_guard;
fun_container m_funs;
public:
rvalue_builder() = default;
template<typename... Ts>
rvalue_builder(rvalue_builder_args_ctor, const Ts&... args)
: m_guard(args...)
, m_funs(args...) {
}
rvalue_builder(Guard arg0, fun_container arg1)
: m_guard(std::move(arg0)), m_funs(std::move(arg1)) {
}
template<typename NewGuard>
rvalue_builder<
guard_expr<
logical_and_op,
guard_expr<exec_xfun_op, Guard, util::void_type>,
NewGuard>,
Transformers,
Pattern>
when(NewGuard ng,
typename std::enable_if<
std::is_same<NewGuard, NewGuard>::value
&& !std::is_same<Guard, empty_value_guard>::value
>::type* = 0 ) const {
return {(ge_sub_function(m_guard) && ng), std::move(m_funs)};
}
template<typename NewGuard>
rvalue_builder<NewGuard, Transformers, Pattern>
when(NewGuard ng,
typename std::enable_if<
std::is_same<NewGuard, NewGuard>::value
&& std::is_same<Guard, empty_value_guard>::value
>::type* = 0 ) const {
return {std::move(ng), std::move(m_funs)};
}
template<typename Expr>
match_expr<typename get_case<is_complete, Expr, Guard, Transformers, Pattern>::type>
operator>>(Expr expr) const {
typedef typename get_case<
is_complete,
Expr,
Guard,
Transformers,
Pattern
>::type
tpair;
return tpair{typename tpair::first_type{m_funs},
typename tpair::second_type{std::move(expr),
std::move(m_guard)}};
}
template<class G, class T, class P>
disjunct_rvalue_builders<rvalue_builder, rvalue_builder<G, T, P> >
operator||(rvalue_builder<G, T, P> other) const {
return {*this, std::move(other)};
}
};
template<bool IsCallable, typename T>
struct pattern_type_ {
typedef util::get_callable_trait<T> ctrait;
typedef typename ctrait::arg_types args;
static_assert(util::tl_size<args>::value == 1, "only unary functions allowed");
typedef typename util::rm_ref<typename util::tl_head<args>::type>::type type;
};
template<typename T>
struct pattern_type_<false, T> {
typedef typename implicit_conversions<
typename util::rm_ref<
typename detail::unboxed<T>::type
>::type
>::type
type;
};
template<typename T>
struct pattern_type : pattern_type_<util::is_callable<T>::value && !detail::is_boxed<T>::value, T> {
};
} } // cppa::detail
namespace cppa {
/**
* @brief A wildcard that matches any number of any values.
*/
constexpr anything any_vals = anything{};
#ifdef CPPA_DOCUMENTATION
/**
* @brief A wildcard that matches the argument types
* of a given callback. Must be the last argument to {@link on()}.
* @see {@link math_actor_example.cpp Math Actor Example}
*/
constexpr __unspecified__ arg_match;
/**
* @brief Left-hand side of a partial function expression.
*
* Equal to <tt>on(arg_match)</tt>.
*/
constexpr __unspecified__ on_arg_match;
/**
* @brief Right-hand side expression to *not* match a particular pattern.
*/
constexpr __unspecified__ skip_message;
/**
* @brief A wildcard that matches any value of type @p T.
* @see {@link math_actor_example.cpp Math Actor Example}
*/
template<typename T>
__unspecified__ val();
/**
* @brief Left-hand side of a partial function expression that matches values.
*
* This overload can be used with the wildcards {@link cppa::val val},
* {@link cppa::any_vals any_vals} and {@link cppa::arg_match arg_match}.
*/
template<typename T, typename... Ts>
__unspecified__ on(const T& arg, const Ts&... args);
/**
* @brief Left-hand side of a partial function expression that matches types.
*
* This overload matches types only. The type {@link cppa::anything anything}
* can be used as wildcard to match any number of elements of any types.
*/
template<typename... Ts>
__unspecified__ on();
/**
* @brief Left-hand side of a partial function expression that matches types.
*
* This overload matches up to four leading atoms.
* The type {@link cppa::anything anything}
* can be used as wildcard to match any number of elements of any types.
*/
template<atom_value... Atoms, typename... Ts>
__unspecified__ on();
#else
bool skip_message();
template<typename T>
constexpr typename detail::boxed<T>::type val() {
return typename detail::boxed<T>::type();
}
typedef typename detail::boxed<util::arg_match_t>::type boxed_arg_match_t;
constexpr boxed_arg_match_t arg_match = boxed_arg_match_t();
template<typename T, typename... Ts>
detail::rvalue_builder<
detail::value_guard<
typename util::tl_filter_not<
typename util::tl_trim<
typename util::tl_map<
util::type_list<T,Ts...>,
detail::boxed_and_callable_to_void,
detail::implicit_conversions
>::type
>::type,
is_anything
>::type
>,
typename util::tl_map<
util::type_list<T,Ts...>,
detail::boxed_and_not_callable_to_void
>::type,
util::type_list<typename detail::pattern_type<T>::type,
typename detail::pattern_type<Ts>::type...> >
on(const T& arg, const Ts&... args) {
return {detail::rvalue_builder_args_ctor{}, arg, args...};
}
inline detail::rvalue_builder<detail::empty_value_guard,
util::empty_type_list,
util::empty_type_list > on() {
return {};
}
template<typename T0, typename... Ts>
detail::rvalue_builder<detail::empty_value_guard,
util::empty_type_list,
util::type_list<T0, Ts...> >
on() {
return {};
}
template<atom_value A0, typename... Ts>
decltype(on(A0, val<Ts>()...)) on() {
return on(A0, val<Ts>()...);
}
template<atom_value A0, atom_value A1, typename... Ts>
decltype(on(A0, A1, val<Ts>()...)) on() {
return on(A0, A1, val<Ts>()...);
}
template<atom_value A0, atom_value A1, atom_value A2, typename... Ts>
decltype(on(A0, A1, A2, val<Ts>()...)) on() {
return on(A0, A1, A2, val<Ts>()...);
}
template<atom_value A0, atom_value A1, atom_value A2, atom_value A3,
typename... Ts>
decltype(on(A0, A1, A2, A3, val<Ts>()...)) on() {
return on(A0, A1, A2, A3, val<Ts>()...);
}
template<class Rep, class Period>
constexpr detail::behavior_rvalue_builder
after(const std::chrono::duration<Rep, Period>& d) {
return { util::duration(d) };
}
inline decltype(on<anything>()) others() {
return on<anything>();
}
// some more convenience
namespace detail {
class on_the_fly_rvalue_builder {
public:
constexpr on_the_fly_rvalue_builder() { }
template<typename Guard>
auto when(Guard g) const -> decltype(on(arg_match).when(g)) {
return on(arg_match).when(g);
}
template<typename Expr>
match_expr<
typename get_case<
false,
Expr,
empty_value_guard,
util::empty_type_list,
util::empty_type_list
>::type>
operator>>(Expr expr) const {
typedef typename get_case<
false,
Expr,
empty_value_guard,
util::empty_type_list,
util::empty_type_list
>::type
result_type;
return result_type{typename result_type::first_type{},
typename result_type::second_type{
std::move(expr),
empty_value_guard{}}};
}
};
} // namespace detail
constexpr detail::on_the_fly_rvalue_builder on_arg_match;
#endif // CPPA_DOCUMENTATION
} // namespace cppa
#endif // CPPA_ON_HPP