Skip to content
Navigation Menu
{{ message }}
forked from actor-framework/actor-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatch_expr.hpp
More file actions
1000 lines (857 loc) · 33.5 KB
/
Copy pathmatch_expr.hpp
File metadata and controls
1000 lines (857 loc) · 33.5 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \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_MATCH_EXPR_HPP
#define CPPA_MATCH_EXPR_HPP
#include "cppa/option.hpp"
#include "cppa/guard_expr.hpp"
#include "cppa/tpartial_function.hpp"
#include "cppa/util/call.hpp"
#include "cppa/util/rm_ref.hpp"
#include "cppa/util/int_list.hpp"
#include "cppa/util/type_list.hpp"
#include "cppa/util/rm_option.hpp"
#include "cppa/util/purge_refs.hpp"
#include "cppa/util/disjunction.hpp"
#include "cppa/util/left_or_right.hpp"
#include "cppa/util/deduce_ref_type.hpp"
#include "cppa/util/get_result_type.hpp"
#include "cppa/util/rebindable_reference.hpp"
#include "cppa/detail/matches.hpp"
#include "cppa/detail/projection.hpp"
#include "cppa/detail/value_guard.hpp"
#include "cppa/detail/pseudo_tuple.hpp"
#include "cppa/detail/behavior_impl.hpp"
namespace cppa { namespace detail {
template<long N>
struct long_constant { static constexpr long value = N; };
typedef long_constant<-1l> minus1l;
template<typename T1, typename T2>
inline T2& deduce_const(T1&, T2& rhs) { return rhs; }
template<typename T1, typename T2>
inline const T2& deduce_const(const T1&, T2& rhs) { return rhs; }
template<class FilteredPattern>
struct invoke_policy_base {
typedef FilteredPattern filtered_pattern;
typedef typename pseudo_tuple_from_type_list<filtered_pattern>::type
tuple_type;
};
// covers wildcard_position::multiple and wildcard_position::in_between
template<wildcard_position, class Pattern, class FilteredPattern>
struct invoke_policy_impl : invoke_policy_base<FilteredPattern> {
typedef invoke_policy_base<FilteredPattern> super;
template<class Tuple>
static bool can_invoke(const std::type_info& type_token,
const Tuple& tup) {
typedef typename match_impl_from_type_list<Tuple,Pattern>::type mimpl;
return type_token == typeid(FilteredPattern) || mimpl::_(tup);
}
template<typename PtrType, class Tuple>
static bool prepare_invoke(typename super::tuple_type& result,
const std::type_info& type_token,
bool,
PtrType*,
Tuple& tup) {
typedef typename match_impl_from_type_list<
typename std::remove_const<Tuple>::type,
Pattern
>::type
mimpl;
util::limited_vector<size_t,util::tl_size<FilteredPattern>::value> mv;
if (type_token == typeid(FilteredPattern) || mimpl::_(tup, mv)) {
for (size_t i = 0; i < util::tl_size<FilteredPattern>::value; ++i) {
result[i] = const_cast<void*>(tup.at(mv[i]));
}
return true;
}
return false;
}
};
template<>
struct invoke_policy_impl<wildcard_position::nil,
util::empty_type_list,
util::empty_type_list >
: invoke_policy_base<util::empty_type_list> {
typedef invoke_policy_base<util::empty_type_list> super;
template<typename PtrType, class Tuple>
static bool prepare_invoke(typename super::tuple_type&,
const std::type_info& type_token,
bool,
PtrType*,
Tuple& tup) {
return can_invoke(type_token, tup);
}
template<class Tuple>
static bool can_invoke(const std::type_info& arg_types, const Tuple&) {
return arg_types == typeid(util::empty_type_list);
}
};
template<class Pattern, typename... Ts>
struct invoke_policy_impl<wildcard_position::nil,
Pattern,
util::type_list<Ts...>>
: invoke_policy_base<util::type_list<Ts...>> {
typedef invoke_policy_base<util::type_list<Ts...>> super;
typedef typename super::tuple_type tuple_type;
typedef detail::tdata<Ts...> native_data_type;
typedef typename detail::static_types_array<Ts...> arr_type;
template<class Tup>
static bool prepare_invoke(std::false_type, tuple_type&, Tup&) {
return false;
}
template<class Tup>
static bool prepare_invoke(std::true_type, tuple_type& result, Tup& tup) {
for (size_t i = 0; i < sizeof...(Ts); ++i) {
result[i] = const_cast<void*>(tup.at(i));
}
return true;
}
template<typename PtrType, class Tuple>
static bool prepare_invoke(tuple_type& result,
const std::type_info&,
bool,
PtrType*,
Tuple& tup,
typename std::enable_if<
std::is_same<
typename std::remove_const<Tuple>::type,
detail::abstract_tuple
>::value == false
>::type* = 0) {
std::integral_constant<bool,
util::tl_binary_forall<
typename util::tl_map<
typename Tuple::types,
util::purge_refs
>::type,
util::type_list<Ts...>,
std::is_same
>::value > token;
return prepare_invoke(token, result, tup);
}
template<typename PtrType, class Tuple>
static bool prepare_invoke(typename super::tuple_type& result,
const std::type_info& arg_types,
bool dynamically_typed,
PtrType* native_arg,
Tuple& tup,
typename std::enable_if<
std::is_same<
typename std::remove_const<Tuple>::type,
detail::abstract_tuple
>::value == true
>::type* = 0) {
if (arg_types == typeid(util::type_list<Ts...>)) {
if (native_arg) {
typedef typename std::conditional<
std::is_const<PtrType>::value,
const native_data_type*,
native_data_type*
>::type
cast_type;
auto arg = reinterpret_cast<cast_type>(native_arg);
for (size_t i = 0; i < sizeof...(Ts); ++i) {
result[i] = const_cast<void*>(arg->at(i));
}
return true;
}
// 'fall through'
}
else if (dynamically_typed) {
auto& arr = arr_type::arr;
if (tup.size() != sizeof...(Ts)) {
return false;
}
for (size_t i = 0; i < sizeof...(Ts); ++i) {
if (arr[i] != tup.type_at(i)) {
return false;
}
}
// 'fall through'
}
else return false;
for (size_t i = 0; i < sizeof...(Ts); ++i) {
result[i] = const_cast<void*>(tup.at(i));
}
return true;
}
template<class Tuple>
static bool can_invoke(const std::type_info& arg_types, const Tuple&) {
return arg_types == typeid(util::type_list<Ts...>);
}
};
template<>
struct invoke_policy_impl<wildcard_position::leading,
util::type_list<anything>,
util::empty_type_list>
: invoke_policy_base<util::empty_type_list> {
typedef invoke_policy_base<util::empty_type_list> super;
template<class Tuple>
static inline bool can_invoke(const std::type_info&, const Tuple&) {
return true;
}
template<typename PtrType, typename Tuple>
static inline bool prepare_invoke(typename super::tuple_type&,
const std::type_info&,
bool,
PtrType*,
Tuple&) {
return true;
}
};
template<class Pattern, typename... Ts>
struct invoke_policy_impl<wildcard_position::trailing,
Pattern,
util::type_list<Ts...>>
: invoke_policy_base<util::type_list<Ts...>> {
typedef invoke_policy_base<util::type_list<Ts...>> super;
template<class Tuple>
static bool can_invoke(const std::type_info& arg_types,
const Tuple& tup) {
if (arg_types == typeid(util::type_list<Ts...>)) {
return true;
}
typedef detail::static_types_array<Ts...> arr_type;
auto& arr = arr_type::arr;
if (tup.size() < sizeof...(Ts)) {
return false;
}
for (size_t i = 0; i < sizeof...(Ts); ++i) {
if (arr[i] != tup.type_at(i)) {
return false;
}
}
return true;
}
template<typename PtrType, class Tuple>
static bool prepare_invoke(typename super::tuple_type& result,
const std::type_info& arg_types,
bool,
PtrType*,
Tuple& tup) {
if (!can_invoke(arg_types, tup)) return false;
for (size_t i = 0; i < sizeof...(Ts); ++i) {
result[i] = const_cast<void*>(tup.at(i));
}
return true;
}
};
template<class Pattern, typename... Ts>
struct invoke_policy_impl<wildcard_position::leading,
Pattern,
util::type_list<Ts...>>
: invoke_policy_base<util::type_list<Ts...>> {
typedef invoke_policy_base<util::type_list<Ts...>> super;
template<class Tuple>
static bool can_invoke(const std::type_info& arg_types,
const Tuple& tup) {
if (arg_types == typeid(util::type_list<Ts...>)) {
return true;
}
typedef detail::static_types_array<Ts...> arr_type;
auto& arr = arr_type::arr;
if (tup.size() < sizeof...(Ts)) return false;
size_t i = tup.size() - sizeof...(Ts);
size_t j = 0;
while (j < sizeof...(Ts)) {
if (arr[i++] != tup.type_at(j++)) return false;
}
return true;
}
template<typename PtrType, class Tuple>
static bool prepare_invoke(typename super::tuple_type& result,
const std::type_info& arg_types,
bool,
PtrType*,
Tuple& tup) {
if (!can_invoke(arg_types, tup)) return false;
size_t i = tup.size() - sizeof...(Ts);
size_t j = 0;
while (j < sizeof...(Ts)) {
result[j++] = const_cast<void*>(tup.at(i++));
}
return true;
}
};
template<class Pattern>
struct invoke_policy
: invoke_policy_impl<
get_wildcard_position<Pattern>(),
Pattern,
typename util::tl_filter_not_type<Pattern,anything>::type> {
};
template<class Pattern, class Projection, class PartialFun>
struct projection_partial_function_pair : std::pair<Projection,PartialFun> {
template<typename... Ts>
projection_partial_function_pair(Ts&&... args)
: std::pair<Projection,PartialFun>(std::forward<Ts>(args)...) { }
typedef Pattern pattern_type;
};
template<class Expr, class Guard, class Transformers, class Pattern>
struct get_case_ {
typedef typename util::get_callable_trait<Expr>::type ctrait;
typedef typename util::tl_filter_not_type<
Pattern,
anything
>::type
filtered_pattern;
typedef typename util::tl_pad_right<
Transformers,
util::tl_size<filtered_pattern>::value
>::type
padded_transformers;
typedef typename util::tl_map<
filtered_pattern,
std::add_const,
std::add_lvalue_reference
>::type
base_signature;
typedef typename util::tl_map_conditional<
typename util::tl_pad_left<
typename ctrait::arg_types,
util::tl_size<filtered_pattern>::value
>::type,
std::is_lvalue_reference,
false,
std::add_const,
std::add_lvalue_reference
>::type
padded_expr_args;
// override base signature with required argument types of Expr
// and result types of transformation
typedef typename util::tl_zip<
typename util::tl_map<
padded_transformers,
util::get_result_type,
util::rm_option,
std::add_lvalue_reference
>::type,
typename util::tl_zip<
padded_expr_args,
base_signature,
util::left_or_right
>::type,
util::left_or_right
>::type
partial_fun_signature;
// 'inherit' mutable references from partial_fun_signature
// for arguments without transformation
typedef typename util::tl_zip<
typename util::tl_zip<
padded_transformers,
partial_fun_signature,
util::if_not_left
>::type,
base_signature,
util::deduce_ref_type
>::type
projection_signature;
typedef typename projection_from_type_list<
padded_transformers,
projection_signature
>::type
type1;
typedef typename get_tpartial_function<
Expr,
Guard,
partial_fun_signature
>::type
type2;
typedef projection_partial_function_pair<Pattern,type1,type2> type;
};
template<bool Complete, class Expr, class Guard, class Trans, class Pattern>
struct get_case {
typedef typename get_case_<Expr,Guard,Trans,Pattern>::type type;
};
template<class Expr, class Guard, class Trans, class Pattern>
struct get_case<false,Expr,Guard,Trans,Pattern> {
typedef typename util::tl_pop_back<Pattern>::type lhs_pattern;
typedef typename util::tl_map<
typename util::get_arg_types<Expr>::types,
util::rm_ref
>::type
rhs_pattern;
typedef typename get_case_<
Expr,
Guard,
Trans,
typename util::tl_concat<lhs_pattern,rhs_pattern>::type
>::type
type;
};
template<typename Fun>
struct has_bool_result {
typedef typename Fun::result_type result_type;
static constexpr bool value = std::is_same<bool,result_type>::value;
typedef std::integral_constant<bool,value> token_type;
};
template<typename T1, typename T2>
T1& select_if(std::true_type, T1& lhs, T2&) { return lhs; }
template<typename T1, typename T2>
T2& select_if(std::false_type, T1&, T2& rhs) { return rhs; }
template<class PPFPs, typename PtrType, class Tuple>
inline bool unroll_expr(PPFPs&,
bool&,
std::uint64_t,
minus1l,
const std::type_info&,
bool,
PtrType*,
Tuple&) {
return false;
}
template<class PPFPs, long N, typename PtrType, class Tuple>
bool unroll_expr(PPFPs& fs,
bool& invoke_res,
std::uint64_t bitmask,
long_constant<N>,
const std::type_info& type_token,
bool is_dynamic,
PtrType* ptr,
Tuple& tup) {
if (unroll_expr(fs, invoke_res, bitmask, long_constant<N-1>{},
type_token, is_dynamic, ptr, tup)) {
return true;
}
if ((bitmask & (0x01 << N)) == 0) return false;
auto& f = get<N>(fs);
typedef typename util::rm_ref<decltype(f)>::type Fun;
typedef typename Fun::pattern_type pattern_type;
typedef detail::invoke_policy<pattern_type> policy;
typename policy::tuple_type targs;
if (policy::prepare_invoke(targs, type_token, is_dynamic, ptr, tup)) {
auto is = util::get_indices(targs);
util::void_type dummy;
typename has_bool_result<typename Fun::second_type>::token_type stoken;
return util::apply_args_prefixed(f.first,
deduce_const(tup, targs),
is,
f.second,
select_if(stoken, invoke_res, dummy));
}
return false;
}
// PPFP = projection_partial_function_pair
template<class PPFPs, class Tuple>
inline bool can_unroll_expr(PPFPs&, minus1l, const std::type_info&, const Tuple&) {
return false;
}
template<class PPFPs, long N, class Tuple>
inline bool can_unroll_expr(PPFPs& fs, long_constant<N>, const std::type_info& arg_types, const Tuple& tup) {
if (can_unroll_expr(fs, long_constant<N-1l>(), arg_types, tup)) {
return true;
}
auto& f = get<N>(fs);
typedef typename util::rm_ref<decltype(f)>::type Fun;
typedef typename Fun::pattern_type pattern_type;
typedef detail::invoke_policy<pattern_type> policy;
return policy::can_invoke(arg_types, tup);
}
template<class PPFPs, class Tuple>
inline std::uint64_t calc_bitmask(PPFPs&, minus1l, const std::type_info&, const Tuple&) {
return 0x00;
}
template<class PPFPs, long N, class Tuple>
inline std::uint64_t calc_bitmask(PPFPs& fs, long_constant<N>, const std::type_info& tinf, const Tuple& tup) {
auto& f = get<N>(fs);
typedef typename util::rm_ref<decltype(f)>::type Fun;
typedef typename Fun::pattern_type pattern_type;
typedef detail::invoke_policy<pattern_type> policy;
std::uint64_t result = policy::can_invoke(tinf, tup) ? (0x01 << N) : 0x00;
return result | calc_bitmask(fs, long_constant<N-1l>(), tinf, tup);
}
template<bool IsManipulator, typename T0, typename T1>
struct mexpr_fwd_ {
typedef T1 type;
};
template<typename T>
struct mexpr_fwd_<false,const T&,T> {
typedef std::reference_wrapper<const T> type;
};
template<typename T>
struct mexpr_fwd_<true,T&,T> {
typedef std::reference_wrapper<T> type;
};
template<bool IsManipulator, typename T>
struct mexpr_fwd {
typedef typename mexpr_fwd_<
IsManipulator,
T,
typename detail::implicit_conversions<
typename util::rm_ref<T>::type
>::type
>::type
type;
};
// detach_if_needed(any_tuple tup, bool do_detach)
inline any_tuple& detach_if_needed(any_tuple& tup, std::true_type) {
tup.force_detach();
return tup;
}
inline any_tuple detach_if_needed(const any_tuple& tup, std::true_type) {
any_tuple cpy{tup};
cpy.force_detach();
return std::move(cpy);
}
inline const any_tuple& detach_if_needed(const any_tuple& tup, std::false_type) {
return tup;
}
template<typename Ptr>
inline void* fetch_native_data(Ptr& ptr, std::true_type) {
return ptr->mutable_native_data();
}
template<typename Ptr>
inline const void* fetch_native_data(const Ptr& ptr, std::false_type) {
return ptr->native_data();
}
} } // namespace cppa::detail
namespace cppa {
/** @cond PRIVATE */
template<typename T>
struct is_manipulator_case {
static constexpr bool value = T::second_type::manipulates_args;
};
/** @endcond */
/**
* @brief A match expression encapsulating cases <tt>Cs...</tt>.
*/
template<class... Cs>
class match_expr {
static_assert(sizeof...(Cs) < 64, "too many functions");
public:
static constexpr bool may_have_timeout = false;
typedef util::type_list<Cs...> cases_list;
static constexpr bool has_manipulator = util::tl_exists<cases_list,is_manipulator_case>::value;
typedef detail::long_constant<sizeof...(Cs)-1l> idx_token_type;
static constexpr idx_token_type idx_token = idx_token_type{};
template<typename... Ts>
match_expr(Ts&&... args) : m_cases(std::forward<Ts>(args)...) {
init();
}
match_expr(match_expr&& other) : m_cases(std::move(other.m_cases)) {
init();
}
match_expr(const match_expr& other) : m_cases(other.m_cases) {
init();
}
inline bool invoke(const any_tuple& tup) {
return invoke_impl(tup);
}
inline bool invoke(any_tuple& tup) {
return invoke_impl(tup);
}
inline bool invoke(any_tuple&& tup) {
any_tuple tmp{tup};
return invoke_impl(tmp);
}
bool can_invoke(const any_tuple& tup) {
auto type_token = tup.type_token();
if (not tup.dynamically_typed()) {
auto bitmask = get_cache_entry(type_token, tup);
return bitmask != 0;
}
return can_unroll_expr(m_cases,
idx_token,
*type_token,
tup);
}
inline bool operator()(const any_tuple& tup) {
return invoke_impl(tup);
}
inline bool operator()(any_tuple& tup) {
return invoke_impl(tup);
}
inline bool operator()(any_tuple&& tup) {
any_tuple tmp{tup};
return invoke_impl(tmp);
}
template<typename... Ts>
bool operator()(Ts&&... args) {
// wraps and applies implicit conversions to args
typedef detail::tdata<
typename detail::mexpr_fwd<
has_manipulator,
Ts
>::type...
>
tuple_type;
tuple_type tup{std::forward<Ts>(args)...};
auto& type_token = typeid(typename tuple_type::types);
auto bitmask = get_cache_entry(&type_token, tup);
// ref_type keeps track of whether this match_expr is a mutator
typedef typename std::conditional<
has_manipulator,
tuple_type&,
const tuple_type&
>::type
ref_type;
// same here
typedef typename std::conditional<
has_manipulator,
void*,
const void*
>::type
ptr_type;
// iterate over cases and return if any case was invoked
bool invoke_result = true;
bool unroll_result = unroll_expr(m_cases,
invoke_result,
bitmask,
idx_token,
type_token,
false, // not dynamically_typed
static_cast<ptr_type>(nullptr),
static_cast<ref_type>(tup));
return unroll_result && invoke_result;
}
template<class... Ds>
match_expr<Cs...,Ds...> or_else(const match_expr<Ds...>& other) const {
detail::tdata<util::rebindable_reference<const Cs>...,
util::rebindable_reference<const Ds>... > all_cases;
rebind_tdata(all_cases, m_cases, other.cases());
return {all_cases};
}
inline const detail::tdata<Cs...>& cases() const {
return m_cases;
}
struct pfun_impl : detail::behavior_impl {
match_expr pfun;
template<typename Arg>
pfun_impl(const Arg& from) : pfun(from) { }
bool invoke(any_tuple& tup) {
return pfun.invoke(tup);
}
bool invoke(const any_tuple& tup) {
return pfun.invoke(tup);
}
bool defined_at(const any_tuple& tup) {
return pfun.can_invoke(tup);
}
typedef typename detail::behavior_impl::pointer pointer;
pointer copy(const generic_timeout_definition& tdef) const {
return new_default_behavior_impl(pfun, tdef.timeout, tdef.handler);
}
};
intrusive_ptr<detail::behavior_impl> as_behavior_impl() const {
return new pfun_impl(*this);
}
private:
// structure: tdata< tdata<type_list<...>, ...>,
// tdata<type_list<...>, ...>,
// ...>
detail::tdata<Cs...> m_cases;
static constexpr size_t cache_size = 10;
typedef std::pair<const std::type_info*,std::uint64_t> cache_element;
util::limited_vector<cache_element,cache_size> m_cache;
// ring buffer like access to m_cache
size_t m_cache_begin;
size_t m_cache_end;
cache_element m_dummy;
static inline void advance_(size_t& i) {
i = (i + 1) % cache_size;
}
inline size_t find_token_pos(const std::type_info* type_token) {
for (size_t i = m_cache_begin ; i != m_cache_end; advance_(i)) {
if (m_cache[i].first == type_token) return i;
}
return m_cache_end;
}
template<class Tuple>
std::uint64_t get_cache_entry(const std::type_info* type_token,
const Tuple& value) {
CPPA_REQUIRE(type_token != nullptr);
if (value.dynamically_typed()) {
return m_dummy.second; // all groups enabled
}
size_t i = find_token_pos(type_token);
// if we didn't found a cache entry ...
if (i == m_cache_end) {
// ... 'create' one (override oldest element in cache if full)
advance_(m_cache_end);
if (m_cache_end == m_cache_begin) advance_(m_cache_begin);
m_cache[i].first = type_token;
m_cache[i].second = calc_bitmask(m_cases,
idx_token,
*type_token,
value);
}
return m_cache[i].second;
}
void init() {
m_dummy.second = std::numeric_limits<std::uint64_t>::max();
m_cache.resize(cache_size);
for (auto& entry : m_cache) { entry.first = nullptr; }
m_cache_begin = m_cache_end = 0;
}
template<class Tuple>
bool invoke_impl(Tuple& tup) {
std::integral_constant<bool,has_manipulator> mutator_token;
// returns either a reference or a new object
typedef decltype(detail::detach_if_needed(tup, mutator_token)) detached;
detached tref = detail::detach_if_needed(tup, mutator_token);
auto& vals = tref.vals();
auto ndp = fetch_native_data(vals, mutator_token);
auto token_ptr = vals->type_token();
auto bitmask = get_cache_entry(token_ptr, *vals);
auto dynamically_typed = vals->dynamically_typed();
bool invoke_result = true; // may be set to false by an invoked functor
bool unroll_result = unroll_expr(m_cases,
invoke_result,
bitmask,
idx_token,
*token_ptr,
dynamically_typed,
ndp,
*vals);
return invoke_result && unroll_result;
}
};
template<class List>
struct match_expr_from_type_list;
template<typename... Ts>
struct match_expr_from_type_list<util::type_list<Ts...> > {
typedef match_expr<Ts...> type;
};
template<typename... Lhs, typename... Rhs>
inline match_expr<Lhs...,Rhs...> operator,(const match_expr<Lhs...>& lhs,
const match_expr<Rhs...>& rhs) {
return lhs.or_else(rhs);
}
template<typename... Cs>
match_expr<Cs...>& match_expr_collect(match_expr<Cs...>& arg) {
return arg;
}
template<typename... Cs>
match_expr<Cs...>&& match_expr_collect(match_expr<Cs...>&& arg) {
return std::move(arg);
}
template<typename... Cs>
const match_expr<Cs...>& match_expr_collect(const match_expr<Cs...>& arg) {
return arg;
}
template<typename T, typename... Ts>
typename match_expr_from_type_list<
typename util::tl_concat<
typename T::cases_list,
typename Ts::cases_list...
>::type
>::type
match_expr_collect(const T& arg, const Ts&... args) {
typename detail::tdata_from_type_list<
typename util::tl_map<
typename util::tl_concat<
typename T::cases_list,
typename Ts::cases_list...
>::type,
gref_wrapped
>::type
>::type
all_cases;
detail::rebind_tdata(all_cases, arg.cases(), args.cases()...);
return {all_cases};
}
namespace detail {
typedef std::true_type with_timeout;
typedef std::false_type without_timeout;
// with timeout
// end of recursion
template<class Data, class Token, typename F>
behavior_impl* concat_rec(const Data& data, Token, const timeout_definition<F>& arg) {
typedef typename match_expr_from_type_list<Token>::type combined_type;
return new default_behavior_impl<combined_type,F>{data, arg};
}
// recursive concatenation function
template<class Data, class Token, typename T, typename... Ts>
behavior_impl* concat_rec(const Data& data, Token, const T& arg, const Ts&... args) {
typedef typename util::tl_concat<
Token,
typename T::cases_list
>::type
next_token_type;
typename tdata_from_type_list<
typename util::tl_map<
next_token_type,
gref_wrapped
>::type
>::type
next_data;
next_token_type next_token;
rebind_tdata(next_data, data, arg.cases());
return concat_rec(next_data, next_token, args...);
}
template<typename F>
behavior_impl* concat_expr(with_timeout, const timeout_definition<F>& arg) {
typedef default_behavior_impl<dummy_match_expr,F> impl_type;
return new impl_type(dummy_match_expr{}, arg);
}
template<typename T, typename... Ts>
behavior_impl* concat_expr(with_timeout, const T& arg, const Ts&... args) {
typename tdata_from_type_list<
typename util::tl_map<
typename T::cases_list,
gref_wrapped
>::type
>::type
wrapper;
detail::rebind_tdata(wrapper, arg.cases());
return concat_rec(wrapper, typename T::cases_list{}, args...);
}
// without timeout
template<typename T, typename... Ts>
behavior_impl* concat_expr(without_timeout, const T& arg, const Ts&... args) {
typename tdata_from_type_list<
typename util::tl_map<
typename util::tl_concat<
typename T::cases_list,
typename Ts::cases_list...
>::type,
gref_wrapped
>::type
>::type
all_cases;
typedef typename match_expr_from_type_list<
typename util::tl_concat<
typename T::cases_list,
typename Ts::cases_list...
>::type
>::type
combined_type;
auto lvoid = []() { };
typedef default_behavior_impl<combined_type,decltype(lvoid)> impl_type;
rebind_tdata(all_cases, arg.cases(), args.cases()...);
return new impl_type(all_cases, util::duration{}, lvoid);
}
template<typename T, typename... Ts>
behavior_impl_ptr match_expr_concat(const T& arg, const Ts&... args) {
std::integral_constant<bool,util::disjunction<T::may_have_timeout,Ts::may_have_timeout...>::value> token;
// use static call dispatch to select correct function
return concat_expr(token, arg, args...);
}
} // namespace detail
} // namespace cppa
#endif // CPPA_MATCH_EXPR_HPP
You can’t perform that action at this time.
