1c23bb4aeb050c99ead321c714fed74ed17ed902
[platform/upstream/boost.git] / libs / preprocessor / doc / ref / for_r.html
1 <html>
2 <head>
3         <title>BOOST_PP_FOR_r</title>
4         <link rel="stylesheet" type="text/css" href="../styles.css">
5 </head>
6 <body>
7         <div style="margin-left:  0px;">
8                 The <b>BOOST_PP_FOR_<i>r</i></b> macro represents a reentry into the <b>BOOST_PP_FOR</b> repetition construct.
9         </div>
10         <h4>Usage</h4>
11                 <div class="code">
12                         <b>BOOST_PP_FOR_</b> ## <i>r</i>(<i>state</i>, <i>pred</i>, <i>op</i>, <i>macro</i>)
13                 </div>
14         <h4>Arguments</h4>
15                 <dl>
16                         <dt>r</dt>
17                         <dd>
18                                 The next available <b>BOOST_PP_FOR</b> repetition.
19                         </dd>
20                         <dt>state</dt>
21                         <dd>
22                                 The initial state.
23                         </dd>
24                         <dt>pred</dt>
25                         <dd>
26                                 A binary predicate of the form <i>pred</i>(<i>r</i>, <i>state</i>).&nbsp;
27                                 This macro must expand to an integer in the range of <i>0</i> to <b>BOOST_PP_LIMIT_MAG</b>.&nbsp;
28                                 <b>BOOST_PP_FOR</b> repeatedly expands <i>macro</i> while this predicate returns non-zero.&nbsp;
29                                 This macro is called with the next available <b>BOOST_PP_FOR</b> repetition and the current <i>state</i>.
30                         </dd>
31                         <dt>op</dt>
32                         <dd>
33                                 A binary operation of the form <i>op</i>(<i>r</i>, <i>state</i>).&nbsp;
34                                 This operation is expanded by <b>BOOST_PP_FOR</b> with the next available <b>BOOST_PP_FOR</b> repetition and the current <i>state</i>.&nbsp;
35                                 This macro is repeatedly applied to the <i>state</i>, each time producing a new <i>state</i>, until <i>pred</i> returns <i>0</i>.
36                         </dd>
37                         <dt>macro</dt>
38                         <dd>
39                                 A binary macro of the form <i>macro</i>(<i>r</i>, <i>state</i>).&nbsp;
40                                 This macro is expanded by <b>BOOST_PP_FOR</b> with the next available <b>BOOST_PP_FOR</b> repetition and the current <i>state</i>.&nbsp;
41                                 This macro is is repeated by <b>BOOST_PP_FOR</b> until <i>pred</i> returns <i>0</i>.
42                         </dd>
43                 </dl>
44         <h4>Remarks</h4>
45                 <div>
46                         This macro expands to the sequence:
47                         <div>
48                                 <i>macro</i>(<i>r</i>, <i>state</i>) <i>macro</i>(<i>r</i>, <i>op</i>(<i>r</i>, <i>state</i>)) ... <i>macro</i>(<i>r</i>, <i>op</i>(<i>r</i>, ... <i>op</i>(<i>r</i>, <i>state</i>) ... ))
49                         </div>
50                 </div>
51                 <div>
52                         At certain times, it may be necessary to perform the concatenation with <b>BOOST_PP_CAT</b> rather than the preprocessor token-pasting operator.&nbsp;
53                         This happens when the <i>r</i> value is a macro invocation itself.&nbsp;
54                         It needs a delay to allow it to expand.&nbsp;
55                         The syntax in such a scenario becomes:
56                         <div>
57                                 <b>BOOST_PP_CAT</b>(<b>BOOST_PP_FOR_</b>, <i>r</i>)(<i>state</i>, <i>pred</i>, <i>op</i>, <i>macro</i>)
58                         </div>
59                 </div>
60         <h4>See Also</h4>
61                 <ul>
62                         <li><a href="cat.html">BOOST_PP_CAT</a></li>
63                         <li><a href="for.html">BOOST_PP_FOR</a></li>
64                         <li><a href="limit_mag.html">BOOST_PP_LIMIT_MAG</a></li>
65                 </ul>
66         <h4>Requirements</h4>
67                 <div>
68                         <b>Header:</b> &nbsp;<a href="../headers/repetition/for.html">&lt;boost/preprocessor/repetition/for.hpp&gt;</a>
69                 </div>
70         <h4>Sample Code</h4>
71 <div><pre>
72 #include &lt;<a href="../headers/arithmetic/dec.html">boost/preprocessor/arithmetic/dec.hpp</a>&gt;
73 #include &lt;<a href="../headers/arithmetic/inc.html">boost/preprocessor/arithmetic/inc.hpp</a>&gt;
74 #include &lt;<a href="../headers/comparison/not_equal.html">boost/preprocessor/comparison/not_equal.hpp</a>&gt;
75 #include &lt;<a href="../headers/punctuation/comma_if.html">boost/preprocessor/punctuation/comma_if.hpp</a>&gt;
76 #include &lt;<a href="../headers/repetition/for.html">boost/preprocessor/repetition/for.hpp</a>&gt;
77 #include &lt;<a href="../headers/tuple/elem.html">boost/preprocessor/tuple/elem.hpp</a>&gt;
78
79 #define PRED(r, state) \
80    <a href="not_equal.html">BOOST_PP_NOT_EQUAL</a>( \
81       <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(4, 0, state), \
82       <a href="inc.html">BOOST_PP_INC</a>( \
83          <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(4, 1, state) \
84       ) \
85    ) \
86    /**/
87
88 #define OP(r, state) \
89    ( \
90       <a href="inc.html">BOOST_PP_INC</a>( \
91          <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(4, 0, state) \
92       ), \
93       <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(4, 1, state), \
94       <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(4, 2, state), \
95       <a href="inc.html">BOOST_PP_INC</a>( \
96          <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(4, 3, state) \
97       ) \
98    ) \
99    /**/
100
101 #define MACRO(r, state) \
102    <a href="comma_if.html">BOOST_PP_COMMA_IF</a>( \
103       <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(4, 3, state) \
104    ) template&lt; \
105       <a href="for_r.html">BOOST_PP_FOR_</a> ## r( \
106          (0, <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(4, 0, state), _, 0), \
107          PRED_2, OP, MACRO_2 \
108       ) \
109    &gt; class <a href="cat.html">BOOST_PP_CAT</a>( \
110       <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(4, 2, state), \
111       <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(4, 0, state) \
112    ) \
113    /**/
114
115 #define PRED_2(r, state) \
116    <a href="not_equal.html">BOOST_PP_NOT_EQUAL</a>( \
117       <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(4, 0, state), \
118       <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(4, 1, state) \
119    ) \
120    /**/
121
122 #define MACRO_2(r, state) \
123    <a href="comma_if.html">BOOST_PP_COMMA_IF</a>( \
124       <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(4, 0, state) \
125    ) class \
126    /**/
127
128 #define TEMPLATE_TEMPLATE(low, high, name) \
129    <a href="for.html">BOOST_PP_FOR</a>( \
130       (low, high, name, 0), \
131       PRED, OP, MACRO \
132    ) \
133    /**/
134
135 TEMPLATE_TEMPLATE(2, 4, T)
136 /*
137    expands to...
138    template&lt;class, class&gt; class T2,
139    template&lt;class, class, class&gt; class T3,
140    template&lt;class, class, class, class&gt; class T4
141 */
142 </pre></div>
143         <hr size="1">
144         <div style="margin-left: 0px;">
145                 <i>© Copyright <a href="http://www.housemarque.com" target="_top">Housemarque Oy</a> 2002</i>
146                 </br><i>© Copyright Paul Mensonides 2002</i>
147         </div>
148         <div style="margin-left: 0px;">
149                 <p><small>Distributed under the Boost Software License, Version 1.0. (See
150                 accompanying file <a href="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
151                 copy at <a href=
152                 "http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)</small></p>
153         </div>
154 </body>
155 </html>