1 
2 //  (C) Copyright Edward Diener 2011-2015
3 //  Use, modification and distribution are subject to the Boost Software License,
4 //  Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 //  http://www.boost.org/LICENSE_1_0.txt).
6 
7 #if !defined(BOOST_VMD_DETAIL_SEQ_HPP)
8 #define BOOST_VMD_DETAIL_SEQ_HPP
9 
10 #include <boost/preprocessor/comparison/equal.hpp>
11 #include <boost/preprocessor/control/iif.hpp>
12 #include <boost/preprocessor/control/while.hpp>
13 #include <boost/preprocessor/facilities/empty.hpp>
14 #include <boost/preprocessor/seq/push_back.hpp>
15 #include <boost/preprocessor/tuple/elem.hpp>
16 #include <boost/preprocessor/tuple/push_back.hpp>
17 #include <boost/preprocessor/tuple/size.hpp>
18 #include <boost/preprocessor/variadic/elem.hpp>
19 #include <boost/vmd/is_empty.hpp>
20 #include <boost/vmd/detail/empty_result.hpp>
21 #include <boost/vmd/detail/mods.hpp>
22 #include <boost/vmd/detail/not_empty.hpp>
23 #include <boost/vmd/detail/parens.hpp>
24 
25 #define BOOST_VMD_DETAIL_SEQ_STATE_INIT(seq) \
26     BOOST_PP_TUPLE_PUSH_BACK \
27         ( \
28         BOOST_PP_TUPLE_PUSH_BACK \
29             ( \
30             BOOST_VMD_DETAIL_PARENS(seq,BOOST_VMD_RETURN_AFTER), \
31             BOOST_PP_EMPTY() \
32             ), \
33         BOOST_PP_EMPTY() \
34         ) \
35 /**/
36 
37 #define BOOST_VMD_DETAIL_SEQ_STATE_INIT_D(d,seq) \
38     BOOST_PP_TUPLE_PUSH_BACK \
39         ( \
40         BOOST_PP_TUPLE_PUSH_BACK \
41             ( \
42             BOOST_VMD_DETAIL_PARENS_D(d,seq,BOOST_VMD_RETURN_AFTER), \
43             BOOST_PP_EMPTY() \
44             ), \
45         BOOST_PP_EMPTY() \
46         ) \
47 /**/
48 
49 #define BOOST_VMD_DETAIL_SEQ_STATE_PRED(d,state) \
50     BOOST_VMD_DETAIL_NOT_EMPTY \
51         ( \
52         BOOST_PP_TUPLE_ELEM(0,state) \
53         ) \
54 /**/
55 
56 #define BOOST_VMD_DETAIL_SEQ_STATE_OP_SET_FAILURE(d,state) \
57     ( \
58     BOOST_PP_EMPTY(), \
59     BOOST_PP_EMPTY(), \
60     BOOST_PP_TUPLE_ELEM(2,state), \
61     BOOST_PP_TUPLE_ELEM(3,state) \
62     ) \
63 /**/
64 
65 #define BOOST_VMD_DETAIL_SEQ_STATE_OP_MAKE_SEQ(d,state) \
66     BOOST_PP_TUPLE_ELEM(0,state) \
67 /**/
68 
69 #define BOOST_VMD_DETAIL_SEQ_STATE_OP_UPGRADE_SEQ(d,state) \
70     BOOST_PP_SEQ_PUSH_BACK \
71         ( \
72         BOOST_PP_TUPLE_ELEM(2,state), \
73         BOOST_PP_TUPLE_ELEM \
74             ( \
75             0, \
76             BOOST_PP_TUPLE_ELEM(0,state) \
77             ) \
78         ) \
79 /**/
80 
81 #define BOOST_VMD_DETAIL_SEQ_STATE_OP_SET_SUCCESS(d,state) \
82     ( \
83     BOOST_PP_EMPTY(), \
84     BOOST_PP_EMPTY(), \
85     BOOST_PP_IIF \
86         ( \
87         BOOST_VMD_IS_EMPTY \
88             ( \
89               BOOST_PP_TUPLE_ELEM(2,state) \
90             ), \
91         BOOST_VMD_DETAIL_SEQ_STATE_OP_MAKE_SEQ, \
92         BOOST_VMD_DETAIL_SEQ_STATE_OP_UPGRADE_SEQ \
93         ) \
94     (d,state), \
95     BOOST_PP_EMPTY() \
96     ) \
97 /**/
98 
99 #define BOOST_VMD_DETAIL_SEQ_STATE_OP_SET_CONTINUE(d,state) \
100     BOOST_PP_TUPLE_PUSH_BACK \
101         ( \
102         BOOST_PP_TUPLE_PUSH_BACK \
103             ( \
104             BOOST_VMD_DETAIL_PARENS_D(d,BOOST_PP_TUPLE_ELEM(1,state),BOOST_VMD_RETURN_AFTER), \
105             BOOST_PP_IIF \
106                 ( \
107                 BOOST_VMD_IS_EMPTY \
108                     ( \
109                     BOOST_PP_TUPLE_ELEM(2,state) \
110                     ), \
111                 BOOST_VMD_DETAIL_SEQ_STATE_OP_MAKE_SEQ, \
112                 BOOST_VMD_DETAIL_SEQ_STATE_OP_UPGRADE_SEQ \
113                 ) \
114             (d,state) \
115             ), \
116         BOOST_PP_TUPLE_ELEM(1,state) \
117         ) \
118 /**/
119 
120 #define BOOST_VMD_DETAIL_SEQ_STATE_OP_CHECK_REST(d,state) \
121     BOOST_PP_IIF \
122         ( \
123         BOOST_VMD_IS_EMPTY \
124             ( \
125               BOOST_PP_TUPLE_ELEM(1,state) \
126             ), \
127         BOOST_VMD_DETAIL_SEQ_STATE_OP_SET_SUCCESS, \
128         BOOST_VMD_DETAIL_SEQ_STATE_OP_SET_CONTINUE \
129         ) \
130     (d,state) \
131 /**/
132 
133 #define BOOST_VMD_DETAIL_SEQ_STATE_OP(d,state) \
134     BOOST_PP_IIF \
135         ( \
136         BOOST_PP_EQUAL_D \
137             ( \
138             d, \
139             1, \
140             BOOST_PP_TUPLE_SIZE \
141                 ( \
142                   BOOST_PP_TUPLE_ELEM(0,state) \
143                 ) \
144             ), \
145         BOOST_VMD_DETAIL_SEQ_STATE_OP_CHECK_REST, \
146         BOOST_VMD_DETAIL_SEQ_STATE_OP_SET_FAILURE \
147         ) \
148     (d,state) \
149 /**/
150 
151 #define BOOST_VMD_DETAIL_SEQ_STATE_RESULT(state) \
152     ( \
153     BOOST_PP_TUPLE_ELEM(2,state), \
154     BOOST_PP_TUPLE_ELEM(3,state) \
155     ) \
156 /**/
157 
158 #define BOOST_VMD_DETAIL_SEQ_PROCESS(seq) \
159     BOOST_VMD_DETAIL_SEQ_STATE_RESULT \
160       ( \
161       BOOST_PP_WHILE \
162         ( \
163         BOOST_VMD_DETAIL_SEQ_STATE_PRED, \
164         BOOST_VMD_DETAIL_SEQ_STATE_OP, \
165         BOOST_VMD_DETAIL_SEQ_STATE_INIT(seq) \
166         ) \
167       ) \
168 /**/
169 
170 #define BOOST_VMD_DETAIL_SEQ_SPLIT(seq) \
171     BOOST_PP_IIF \
172         ( \
173         BOOST_VMD_IS_EMPTY(seq), \
174         BOOST_VMD_DETAIL_EMPTY_RESULT, \
175         BOOST_VMD_DETAIL_SEQ_PROCESS \
176         ) \
177     (seq) \
178 /**/
179 
180 #define BOOST_VMD_DETAIL_SEQ_BEGIN(seq) \
181     BOOST_PP_TUPLE_ELEM(0,BOOST_VMD_DETAIL_SEQ_SPLIT(seq)) \
182 /**/
183 
184 #define BOOST_VMD_DETAIL_SEQ_PROCESS_D(d,seq) \
185     BOOST_VMD_DETAIL_SEQ_STATE_RESULT \
186       ( \
187       BOOST_PP_WHILE_ ## d \
188         ( \
189         BOOST_VMD_DETAIL_SEQ_STATE_PRED, \
190         BOOST_VMD_DETAIL_SEQ_STATE_OP, \
191         BOOST_VMD_DETAIL_SEQ_STATE_INIT_D(d,seq) \
192         ) \
193       ) \
194 /**/
195 
196 #define BOOST_VMD_DETAIL_SEQ_SPLIT_D(d,seq) \
197     BOOST_PP_IIF \
198         ( \
199         BOOST_VMD_IS_EMPTY(seq), \
200         BOOST_VMD_DETAIL_EMPTY_RESULT, \
201         BOOST_VMD_DETAIL_SEQ_PROCESS_D \
202         ) \
203     (d,seq) \
204 /**/
205 
206 #define BOOST_VMD_DETAIL_SEQ_BEGIN_D(d,seq) \
207     BOOST_PP_TUPLE_ELEM(0,BOOST_VMD_DETAIL_SEQ_SPLIT_D(d,seq)) \
208 /**/
209 
210 #define BOOST_VMD_DETAIL_SEQ_D(d,...) \
211     BOOST_PP_IIF \
212         ( \
213         BOOST_VMD_DETAIL_MODS_IS_RESULT_AFTER \
214             ( \
215             BOOST_VMD_DETAIL_NEW_MODS_D(d,BOOST_VMD_ALLOW_AFTER,__VA_ARGS__) \
216             ), \
217         BOOST_VMD_DETAIL_SEQ_SPLIT_D, \
218         BOOST_VMD_DETAIL_SEQ_BEGIN_D \
219         ) \
220     (d,BOOST_PP_VARIADIC_ELEM(0,__VA_ARGS__)) \
221 /**/
222 
223 #define BOOST_VMD_DETAIL_SEQ(...) \
224     BOOST_PP_IIF \
225         ( \
226         BOOST_VMD_DETAIL_MODS_IS_RESULT_AFTER \
227             ( \
228             BOOST_VMD_DETAIL_NEW_MODS(BOOST_VMD_ALLOW_AFTER,__VA_ARGS__) \
229             ), \
230         BOOST_VMD_DETAIL_SEQ_SPLIT, \
231         BOOST_VMD_DETAIL_SEQ_BEGIN \
232         ) \
233     (BOOST_PP_VARIADIC_ELEM(0,__VA_ARGS__)) \
234 /**/
235 
236 #endif /* BOOST_VMD_DETAIL_SEQ_HPP */
237