-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathlambda.hpp
More file actions
1730 lines (1523 loc) · 63.1 KB
/
Copy pathlambda.hpp
File metadata and controls
1730 lines (1523 loc) · 63.1 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
#ifndef BOOST_PP_IS_ITERATING
///////////////////////////////////////////////////////////////////////////////
// Copyright 2008 Eric Niebler. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// This example contains a full-featured reimplementation of the old,
// now-deprecated Boost Lambda Library (BLL) on top of Boost.Proto. It
// is necessarily complex to accomodate all the quirks and inconsistencies
// of that old library, but it is a good example of how to build a
// complete and full-featured EDLS using Proto.
#ifndef BOOST_LAMBDA_HPP_EAN_04_19_2008
#define BOOST_LAMBDA_HPP_EAN_04_19_2008
#include <iosfwd>
#include <typeinfo>
#include <algorithm>
#include <boost/ref.hpp>
#include <boost/assert.hpp>
#include <boost/mpl/or.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/void.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/next_prior.hpp>
#include <boost/mpl/min_max.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/preprocessor.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/utility/result_of.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/type_traits/add_reference.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/proto/proto.hpp>
#ifndef BOOST_LAMBDA_MAX_ARITY
# define BOOST_LAMBDA_MAX_ARITY 3
#endif
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable: 4355) // 'this' : used in base member initializer list
# pragma warning(disable: 4065) // switch statement contains 'default' but no 'case' labels
#endif
namespace boost { namespace lambda
{
namespace tag
{
struct if_ {};
struct if_else_ {};
struct for_ {};
struct while_ {};
struct do_while_ {};
struct protect {};
struct try_ {};
struct throw_ {};
struct rethrow_ {};
struct switch_ {};
struct default_ {};
template<int I> struct case_ { static const int value = I; };
template<typename E> struct catch_ { typedef E exception_type; };
struct catch_all_ { typedef catch_all_ exception_type; };
};
template<typename Int>
struct placeholder
{
typedef typename Int::tag tag;
typedef typename Int::value_type value_type;
typedef placeholder<Int> type;
typedef placeholder<typename Int::next> next;
typedef placeholder<typename Int::prior> prior;
static const value_type value = Int::value;
friend std::ostream &operator<<(std::ostream &sout, placeholder)
{
return sout << "boost::lambda::_" << (Int::value+1);
}
};
struct exception_placeholder
{};
struct no_exception_type {};
no_exception_type const no_exception = {};
// Calculate the arity of a lambda expression
struct Arity
: proto::or_<
proto::when<proto::terminal<placeholder<proto::_> >, mpl::next<proto::_value>()>
, proto::when<proto::terminal<proto::_>, mpl::int_<0>()>
, proto::otherwise<proto::fold<proto::_, mpl::int_<0>(), mpl::max<proto::_state, Arity>()> >
>
{};
// True when a lambda expression can be applied with no arguments and
// without an active exception object
struct IsNullary
: proto::or_<
proto::when<proto::terminal<placeholder<proto::_> >, mpl::false_()>
, proto::when<proto::terminal<exception_placeholder>, mpl::false_()>
, proto::when<proto::terminal<proto::_>, mpl::true_()>
, proto::otherwise<proto::fold<proto::_, mpl::true_(), mpl::and_<proto::_state, IsNullary>()> >
>
{};
struct Eval;
template<typename Expr, typename State, typename Data>
typename boost::result_of<Eval(Expr&, State&, Data&)>::type
eval_lambda(Expr& e, State& s, Data& d);
struct EvalWhile : proto::transform<EvalWhile>
{
template<typename Expr, typename State, typename Data>
struct impl : proto::transform_impl<Expr, State, Data>
{
typedef mpl::void_ result_type;
result_type operator()(
typename impl::expr_param expr
, typename impl::state_param state
, typename impl::data_param data
) const
{
while(eval_lambda(proto::left(expr), state, data))
{
eval_lambda(proto::right(expr), state, data);
}
return result_type();
}
};
};
struct EvalDoWhile : proto::transform<EvalDoWhile>
{
template<typename Expr, typename State, typename Data>
struct impl : proto::transform_impl<Expr, State, Data>
{
typedef mpl::void_ result_type;
result_type operator()(
typename impl::expr_param expr
, typename impl::state_param state
, typename impl::data_param data
) const
{
do
{
eval_lambda(proto::child_c<0>(expr), state, data);
}
while(eval_lambda(proto::child_c<1>(expr), state, data));
return result_type();
}
};
};
struct EvalFor : proto::transform<EvalFor>
{
template<typename Expr, typename State, typename Data>
struct impl : proto::transform_impl<Expr, State, Data>
{
typedef mpl::void_ result_type;
result_type operator()(
typename impl::expr_param expr
, typename impl::state_param state
, typename impl::data_param data
) const
{
for(eval_lambda(proto::child_c<0>(expr), state, data)
; eval_lambda(proto::child_c<1>(expr), state, data)
; eval_lambda(proto::child_c<2>(expr), state, data))
{
eval_lambda(proto::child_c<3>(expr), state, data);
}
return result_type();
}
};
};
struct EvalIf : proto::transform<EvalIf>
{
template<typename Expr, typename State, typename Data>
struct impl : proto::transform_impl<Expr, State, Data>
{
typedef mpl::void_ result_type;
result_type operator()(
typename impl::expr_param expr
, typename impl::state_param state
, typename impl::data_param data
) const
{
if(eval_lambda(proto::left(expr), state, data))
{
eval_lambda(proto::right(expr), state, data);
}
return result_type();
}
};
};
struct EvalIfElse : proto::transform<EvalIfElse>
{
template<typename Expr, typename State, typename Data>
struct impl : proto::transform_impl<Expr, State, Data>
{
typedef mpl::void_ result_type;
result_type operator()(
typename impl::expr_param expr
, typename impl::state_param state
, typename impl::data_param data
) const
{
if(eval_lambda(proto::child_c<0>(expr), state, data))
{
eval_lambda(proto::child_c<1>(expr), state, data);
}
else
{
eval_lambda(proto::child_c<2>(expr), state, data);
}
return result_type();
}
};
};
struct EvalException : proto::transform<EvalException>
{
template<typename Expr, typename State, typename Data>
struct impl : proto::transform_impl<Expr, State, Data>
{
typedef typename remove_const<typename impl::state>::type result_type;
BOOST_MPL_ASSERT_NOT((is_same<result_type, no_exception_type>));
BOOST_MPL_ASSERT_NOT((is_same<result_type, tag::catch_all_>));
typename impl::state_param operator()(
typename impl::expr_param
, typename impl::state_param state
, typename impl::data_param
) const
{
return state;
}
};
};
struct EvalSwitch : proto::transform<EvalSwitch>
{
template<typename Expr, typename State, typename Data, long Arity, typename BackTag>
struct impl2;
#define M0(Z, N, DATA) \
case proto::tag_of<typename proto::result_of::child_c<Expr, N>::type>::type::value: \
eval_lambda(proto::child_c<N>(expr), state, data); \
break; \
/**/
#define M1(Z, N, DATA) \
template<typename Expr, typename State, typename Data, typename BackTag> \
struct impl2<Expr, State, Data, N, BackTag> \
: proto::transform_impl<Expr, State, Data> \
{ \
typedef void result_type; \
\
void operator()( \
typename impl2::expr_param expr \
, typename impl2::state_param state \
, typename impl2::data_param data \
) const \
{ \
switch(eval_lambda(proto::child_c<0>(expr), state, data)) \
{ \
BOOST_PP_REPEAT_FROM_TO_ ## Z(1, N, M0, ~) \
default: \
break; \
} \
} \
}; \
\
template<typename Expr, typename State, typename Data> \
struct impl2<Expr, State, Data, N, tag::default_> \
: proto::transform_impl<Expr, State, Data> \
{ \
typedef void result_type; \
\
void operator()( \
typename impl2::expr_param expr \
, typename impl2::state_param state \
, typename impl2::data_param data \
) const \
{ \
switch(eval_lambda(proto::child_c<0>(expr), state, data)) \
{ \
BOOST_PP_REPEAT_FROM_TO_ ## Z(1, BOOST_PP_DEC(N), M0, ~) \
default:; \
eval_lambda(proto::child_c<BOOST_PP_DEC(N)>(expr), state, data); \
break; \
} \
} \
}; \
/**/
BOOST_PP_REPEAT_FROM_TO(2, BOOST_PP_INC(BOOST_PROTO_MAX_ARITY), M1, ~)
#undef M0
#undef M1
template<typename Expr, typename State, typename Data>
struct impl
: impl2<
Expr
, State
, Data
, proto::arity_of<Expr>::value
, typename proto::tag_of<
typename proto::result_of::child_c<
Expr
, proto::arity_of<Expr>::value-1
>::type
>::type
>
{};
};
struct throw_fun
{
BOOST_PROTO_CALLABLE()
typedef void result_type;
template<typename Expr>
void operator()(Expr const &e) const
{
throw e;
}
};
struct unwrap_ref : proto::callable
{
template<typename Sig>
struct result;
template<typename This, typename T>
struct result<This(reference_wrapper<T>)>
{
typedef T &type;
};
template<typename This, typename T>
struct result<This(T &)>
: result<This(T)>
{};
template<typename T>
T &operator()(reference_wrapper<T> const &ref) const
{
return ref;
}
};
struct anytype
{
template<typename T>
anytype(T &) { BOOST_ASSERT(false); }
template<typename T>
operator T &() const { BOOST_ASSERT(false); throw; }
private:
anytype();
};
struct rethrow_fun
{
BOOST_PROTO_CALLABLE()
typedef anytype result_type;
template<typename State>
anytype operator()(State const &) const
{
BOOST_MPL_ASSERT_NOT((is_same<State, no_exception_type>));
throw;
}
};
struct Cases
{
template<typename Tag>
struct case_
: proto::otherwise<proto::_default<Eval> >
{};
template<typename E>
struct case_<tag::catch_<E> >
: proto::otherwise<Eval(proto::_child)>
{};
template<int I>
struct case_<tag::case_<I> >
: proto::otherwise<Eval(proto::_child)>
{};
};
template<> struct Cases::case_<tag::while_> : proto::otherwise<EvalWhile> {};
template<> struct Cases::case_<tag::for_> : proto::otherwise<EvalFor> {};
template<> struct Cases::case_<tag::if_> : proto::otherwise<EvalIf> {};
template<> struct Cases::case_<tag::if_else_> : proto::otherwise<EvalIfElse> {};
template<> struct Cases::case_<tag::do_while_> : proto::otherwise<EvalDoWhile> {};
template<> struct Cases::case_<tag::switch_> : proto::otherwise<EvalSwitch> {};
template<> struct Cases::case_<tag::protect> : proto::otherwise<proto::_child> {};
template<> struct Cases::case_<tag::default_> : proto::otherwise<Eval(proto::_child)> {};
template<> struct Cases::case_<tag::catch_all_> : proto::otherwise<Eval(proto::_child)> {};
template<>
struct Cases::case_<proto::tag::terminal>
: proto::or_<
proto::when<
proto::terminal<placeholder<proto::_> >
, proto::functional::at(proto::_data, proto::_value)
>
, proto::when<
proto::terminal<exception_placeholder>
, EvalException
>
, proto::when<
proto::terminal<reference_wrapper<proto::_> >
, unwrap_ref(proto::_value)
>
, proto::otherwise<proto::_default<Eval> >
>
{};
template<>
struct Cases::case_<proto::tag::function>
: proto::or_<
proto::when<
proto::function<proto::terminal<rethrow_fun> >
, rethrow_fun(proto::_state)
>
, proto::otherwise<proto::_default<Eval> >
>
{};
struct Eval
: proto::switch_<Cases>
{};
template<typename Expr, typename State, typename Data>
typename boost::result_of<Eval(Expr&, State&, Data&)>::type
eval_lambda(Expr& e, State& s, Data& d)
{
return Eval()(e, s, d);
}
// Use a grammar to disable Proto's assignment operator overloads.
// We'll define our own because we want (x+=_1) to store x by
// reference. (In all other cases, variables are stored by value
// within lambda expressions.)
struct Grammar
: proto::switch_<struct AssignOps>
{};
struct AssignOps
{
template<typename Tag> struct case_ : proto::_ {};
};
template<> struct AssignOps::case_<proto::tag::shift_left_assign> : proto::not_<proto::_> {};
template<> struct AssignOps::case_<proto::tag::shift_right_assign> : proto::not_<proto::_> {};
template<> struct AssignOps::case_<proto::tag::multiplies_assign> : proto::not_<proto::_> {};
template<> struct AssignOps::case_<proto::tag::divides_assign> : proto::not_<proto::_> {};
template<> struct AssignOps::case_<proto::tag::modulus_assign> : proto::not_<proto::_> {};
template<> struct AssignOps::case_<proto::tag::plus_assign> : proto::not_<proto::_> {};
template<> struct AssignOps::case_<proto::tag::minus_assign> : proto::not_<proto::_> {};
template<> struct AssignOps::case_<proto::tag::bitwise_and_assign> : proto::not_<proto::_> {};
template<> struct AssignOps::case_<proto::tag::bitwise_or_assign> : proto::not_<proto::_> {};
template<> struct AssignOps::case_<proto::tag::bitwise_xor_assign> : proto::not_<proto::_> {};
template<typename Expr>
struct llexpr;
// Wrap expressions in lambda::llexpr<>.
struct Generator
: proto::pod_generator<llexpr>
{};
// The domain for the lambda library.
struct lldomain
: proto::domain<Generator, Grammar, proto::default_domain>
{
// Make all terminals and children held by value instead of by reference.
// Proto::domain<>::as_expr<> holds everything it can by value; the only
// exceptions are function types, abstract types, and iostreams.
template<typename T>
struct as_child
: proto_base_domain::as_expr<T>
{};
// The exception is arrays, which should still be held by reference
template<typename T, std::size_t N>
struct as_child<T[N]>
: proto_base_domain::as_child<T[N]>
{};
};
template<typename Sig>
struct llresult;
template<typename This>
struct llresult<This()>
: mpl::if_c<
result_of<IsNullary(This &)>::type::value
, result_of<Eval(This &, no_exception_type const &, fusion::vector0<> &)>
, mpl::identity<void>
>::type
{};
#define M0(Z, N, DATA) \
template<typename This BOOST_PP_ENUM_TRAILING_PARAMS_Z(Z, N, typename A)> \
struct llresult<This(BOOST_PP_ENUM_PARAMS_Z(Z, N, A))> \
: result_of< \
Eval( \
This & \
, no_exception_type const & \
, BOOST_PP_CAT(fusion::vector, N)<BOOST_PP_ENUM_PARAMS_Z(Z, N, A)> & \
) \
> \
{}; \
/**/
BOOST_PP_REPEAT_FROM_TO(1, BOOST_PP_INC(BOOST_LAMBDA_MAX_ARITY), M0, ~)
#undef M0
template<typename Expr>
struct llexpr
{
BOOST_PROTO_BASIC_EXTENDS(Expr, llexpr<Expr>, lldomain)
BOOST_PROTO_EXTENDS_ASSIGN()
BOOST_PROTO_EXTENDS_SUBSCRIPT()
template<typename Sig>
struct result
: llresult<Sig>
{};
typename result<llexpr const()>::type
operator()() const
{
fusion::vector0<> args;
return eval_lambda(*this, no_exception, args);
}
#define M1(Z, N, _) ((0)(1))
#define M2(R, PRODUCT) M3(R, BOOST_PP_SEQ_SIZE(PRODUCT), PRODUCT)
#define M3(R, SIZE, PRODUCT) \
template<BOOST_PP_ENUM_PARAMS(SIZE, typename A)> \
typename result<llexpr const(BOOST_PP_SEQ_FOR_EACH_I_R(R, M5, ~, PRODUCT))>::type \
operator ()(BOOST_PP_SEQ_FOR_EACH_I_R(R, M4, ~, PRODUCT)) const \
{ \
BOOST_MPL_ASSERT_RELATION(result_of<Arity(Expr const &)>::type::value, <=, SIZE); \
BOOST_PP_CAT(fusion::vector, SIZE)<BOOST_PP_SEQ_FOR_EACH_I_R(R, M5, ~, PRODUCT)> args \
(BOOST_PP_SEQ_FOR_EACH_I_R(R, M6, ~, PRODUCT)); \
return eval_lambda(*this, no_exception, args); \
} \
/**/
#define M4(R, _, I, ELEM) \
BOOST_PP_COMMA_IF(I) BOOST_PP_CAT(A, I) BOOST_PP_CAT(C, ELEM) &BOOST_PP_CAT(a, I) \
/**/
#define M5(R, _, I, ELEM) \
BOOST_PP_COMMA_IF(I) BOOST_PP_CAT(A, I) BOOST_PP_CAT(C, ELEM)& \
/**/
#define M6(R, _, I, ELEM) \
BOOST_PP_COMMA_IF(I) BOOST_PP_CAT(a, I) \
/**/
#define C0
#define C1 const
#define BOOST_PP_ITERATION_PARAMS_1 (3, (1, BOOST_LAMBDA_MAX_ARITY, "lambda.hpp"))
#include BOOST_PP_ITERATE()
#undef C0
#undef C1
#undef M1
#undef M2
#undef M3
#undef M4
#undef M5
#undef M6
};
typedef llexpr<proto::terminal<placeholder<mpl::int_<0> > >::type> placeholder1_type;
typedef llexpr<proto::terminal<placeholder<mpl::int_<1> > >::type> placeholder2_type;
typedef llexpr<proto::terminal<placeholder<mpl::int_<2> > >::type> placeholder3_type;
placeholder1_type const _1 = {{{}}};
placeholder2_type const _2 = {{{}}};
placeholder3_type const _3 = {{{}}};
placeholder1_type const free1 = {{{}}};
placeholder2_type const free2 = {{{}}};
placeholder3_type const free3 = {{{}}};
typedef llexpr<proto::terminal<exception_placeholder>::type> placeholderE_type;
placeholderE_type const _e = {{{}}};
struct byref
{
template<typename Sig>
struct result;
template<typename This, typename T>
struct result<This(T &)>
{
typedef llexpr<typename proto::terminal<T &>::type> type;
};
template<typename This, typename T>
struct result<This(llexpr<T> &)>
{
typedef boost::reference_wrapper<llexpr<T> > type;
};
template<typename This, typename T>
struct result<This(llexpr<T> const &)>
{
typedef boost::reference_wrapper<llexpr<T> const> type;
};
template<typename T>
typename result<byref(T &)>::type operator()(T &t) const
{
typename result<byref(T &)>::type that = {{t}};
return that;
}
template<typename T>
typename result<byref(T const &)>::type operator()(T const &t) const
{
typename result<byref(T const &)>::type that = {{t}};
return that;
}
template<typename T>
boost::reference_wrapper<llexpr<T> > operator()(llexpr<T> &t) const
{
return boost::ref(t);
}
template<typename T>
boost::reference_wrapper<llexpr<T> const> operator()(llexpr<T> const &t) const
{
return boost::ref(t);
}
};
namespace exprns_
{
// Ugh, the assign operators (and only the assign operators) store
// their left terminals by reference. That requires this special handling.
#define BOOST_LAMBDA_DEFINE_ASSIGN_OP(OP, TAG) \
template<typename T, typename U> \
typename proto::result_of::make_expr< \
TAG \
, lldomain \
, typename boost::result_of<byref(T &)>::type \
, U & \
>::type const \
operator OP(T &t, U &u) \
{ \
return proto::make_expr<TAG, lldomain>(byref()(t), boost::ref(u)); \
} \
template<typename T, typename U> \
typename proto::result_of::make_expr< \
TAG \
, lldomain \
, typename boost::result_of<byref(T &)>::type \
, U const & \
>::type const \
operator OP(T &t, U const &u) \
{ \
return proto::make_expr<TAG, lldomain>(byref()(t), boost::ref(u)); \
} \
/**/
BOOST_LAMBDA_DEFINE_ASSIGN_OP(<<=, boost::proto::tag::shift_left_assign)
BOOST_LAMBDA_DEFINE_ASSIGN_OP(>>=, boost::proto::tag::shift_right_assign)
BOOST_LAMBDA_DEFINE_ASSIGN_OP(*= , boost::proto::tag::multiplies_assign)
BOOST_LAMBDA_DEFINE_ASSIGN_OP(/= , boost::proto::tag::divides_assign)
BOOST_LAMBDA_DEFINE_ASSIGN_OP(%= , boost::proto::tag::modulus_assign)
BOOST_LAMBDA_DEFINE_ASSIGN_OP(+= , boost::proto::tag::plus_assign)
BOOST_LAMBDA_DEFINE_ASSIGN_OP(-= , boost::proto::tag::minus_assign)
BOOST_LAMBDA_DEFINE_ASSIGN_OP(&= , boost::proto::tag::bitwise_and_assign)
BOOST_LAMBDA_DEFINE_ASSIGN_OP(|= , boost::proto::tag::bitwise_or_assign)
BOOST_LAMBDA_DEFINE_ASSIGN_OP(^= , boost::proto::tag::bitwise_xor_assign)
}
template<typename T>
struct var_type
{
typedef llexpr<typename proto::terminal<T &>::type> type;
};
template<typename T>
llexpr<typename proto::terminal<T &>::type> const
var(T &t)
{
llexpr<typename proto::terminal<T &>::type> that = {{t}};
return that;
}
template<typename T>
struct constant_type
: proto::result_of::make_expr<
proto::tag::terminal
, lldomain
, T const &
>
{};
template<typename T>
typename constant_type<T>::type const
constant(T const &t)
{
typename constant_type<T>::type that = {{t}};
return that;
}
template<typename T>
struct constant_ref_type
{
typedef llexpr<typename proto::terminal<T const &>::type> type;
};
template<typename T>
llexpr<typename proto::terminal<T const &>::type> const
constant_ref(T const &t)
{
llexpr<typename proto::terminal<T const &>::type> that = {{t}};
return that;
}
template<typename Cond>
struct while_generator
{
explicit while_generator(Cond const &c)
: cond(c)
{}
template<typename Body>
typename proto::result_of::make_expr<
tag::while_
, lldomain
, Cond const &
, Body const &
>::type const
operator[](Body const &body) const
{
return proto::make_expr<tag::while_, lldomain>(
boost::ref(this->cond)
, boost::ref(body)
);
}
private:
Cond const &cond;
};
template<typename Expr>
while_generator<Expr> while_(Expr const &expr)
{
return while_generator<Expr>(expr);
}
template<typename Expr>
struct else_generator
{
typedef typename proto::result_of::left<Expr const &>::type condition_type;
typedef typename proto::result_of::right<Expr const &>::type body1_type;
explicit else_generator(Expr const &expr)
: if_(expr)
{}
template<typename Body2>
typename proto::result_of::make_expr<
tag::if_else_
, lldomain
, condition_type
, body1_type
, Body2 const &
>::type const
operator[](Body2 const &body2) const
{
return proto::make_expr<tag::if_else_, lldomain>(
boost::ref(proto::left(this->if_))
, boost::ref(proto::right(this->if_))
, boost::ref(body2)
);
}
private:
Expr const &if_;
};
template<typename Expr>
struct with_else : Expr
{
template<typename T>
with_else(T const &expr)
: Expr(expr)
, else_(*this)
{}
else_generator<Expr> else_;
};
template<typename Cond>
struct if_generator
{
explicit if_generator(Cond const &c)
: cond(c)
{}
template<typename Body>
with_else<
typename proto::result_of::make_expr<
tag::if_
, lldomain
, Cond const &
, Body const &
>::type
> const
operator[](Body const &body) const
{
return proto::make_expr<tag::if_, lldomain>(
boost::ref(this->cond)
, boost::ref(body)
);
}
private:
Cond const &cond;
};
template<typename Expr>
if_generator<Expr> if_(Expr const &expr)
{
return if_generator<Expr>(expr);
}
template<typename Init, typename Cond, typename Oper>
struct for_generator
{
explicit for_generator(Init const &i, Cond const &c, Oper const &o)
: init(i)
, cond(c)
, oper(o)
{}
template<typename Body>
typename proto::result_of::make_expr<
tag::for_
, lldomain
, Init const &
, Cond const &
, Oper const &
, Body const &
>::type const
operator[](Body const &body) const
{
return proto::make_expr<tag::for_, lldomain>(
boost::ref(this->init)
, boost::ref(this->cond)
, boost::ref(this->oper)
, boost::ref(body)
);
}
private:
Init const &init;
Cond const &cond;
Oper const &oper;
};
template<typename Init, typename Cond, typename Oper>
for_generator<Init, Cond, Oper> for_(Init const &i, Cond const &c, Oper const &o)
{
return for_generator<Init, Cond, Oper>(i, c, o);
}
template<typename Body>
struct do_while_generator
{
explicit do_while_generator(Body const &b)
: body(b)
{}
template<typename Cond>
typename proto::result_of::make_expr<
tag::do_while_
, lldomain
, Body const &
, Cond const &
>::type const
operator()(Cond const &cond) const
{
return proto::make_expr<tag::do_while_, lldomain>(
boost::ref(this->body)
, boost::ref(cond)
);
}
private:
Body const &body;
};
template<typename Body>
struct do_body
{
explicit do_body(Body const &body)
: while_(body)
{}
do_while_generator<Body> while_;
};
struct do_generator
{
template<typename Body>
do_body<Body> operator[](Body const &body) const
{
return do_body<Body>(body);
}
};
do_generator const do_ = {};
struct noop_fun
{
typedef void result_type;
void operator()() const {}
};
typedef llexpr<proto::function<llexpr<proto::terminal<noop_fun>::type> >::type> noop_type;
noop_type const noop = {{{{{}}}}};
template<typename Init, typename Cond, typename Oper>
typename proto::result_of::make_expr<
tag::for_
, lldomain
, Init const &
, Cond const &
, Oper const &
, noop_type const &
>::type const
for_loop(Init const &init, Cond const &cond, Oper const &oper)
{
return proto::make_expr<tag::for_, lldomain>(
boost::ref(init)
, boost::ref(cond)
, boost::ref(oper)
, boost::ref(noop)
);
}
template<typename Init, typename Cond, typename Oper, typename Body>
typename proto::result_of::make_expr<
tag::for_
, lldomain
, Init const &
, Cond const &
, Oper const &
, Body const &
>::type const
for_loop(Init const &init, Cond const &cond, Oper const &oper, Body const &body)
{
return proto::make_expr<tag::for_>(
boost::ref(init)
, boost::ref(cond)
, boost::ref(oper)
, boost::ref(body)
);
}
template<typename Cond, typename Body>
typename proto::result_of::make_expr<
tag::while_
, lldomain
, Cond const &
, Body const &
>::type const
while_loop(Cond const &cond, Body const &body)
{
return proto::make_expr<tag::while_, lldomain>(
boost::ref(cond)