while.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <html>
  2. <head>
  3. <title>BOOST_PP_WHILE</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_WHILE</b> macro represents a looping construct.
  9. </div>
  10. <h4>Usage</h4>
  11. <div class="code">
  12. <b>BOOST_PP_WHILE</b>(<i>pred</i>, <i>op</i>, <i>state</i>)
  13. </div>
  14. <h4>Arguments</h4>
  15. <dl>
  16. <dt>pred</dt>
  17. <dd>
  18. A binary predicate of the form <i>pred</i>(<i>d</i>, <i>state</i>).&nbsp;
  19. This predicate is expanded by <b>BOOST_PP_WHILE</b> with the next available
  20. iteration <i>d</i> and the current <i>state</i>.&nbsp;
  21. This predicate must expand to value in the range of <i>0</i> to <b>BOOST_PP_LIMIT_MAG</b>.
  22. The construct continues to loop until this predicate returns <i>0</i>.&nbsp;
  23. When this predicate returns <i>0</i>, <b>BOOST_PP_WHILE</b> returns the current <i>state</i>.
  24. </dd>
  25. <dt>op</dt>
  26. <dd>
  27. A binary operation of the form <i>op</i>(<i>d</i>, <i>state</i>).&nbsp;
  28. This operation is expanded by <b>BOOST_PP_WHILE</b> with the next available
  29. iteration <i>d</i> and the current <i>state</i>.&nbsp;
  30. 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>.
  31. </dd>
  32. <dt>state</dt>
  33. <dd>
  34. The initial state.&nbsp;
  35. Often this argument is a <i>tuple</i>.
  36. </dd>
  37. </dl>
  38. <h4>Remarks</h4>
  39. <div>
  40. This macro iterates <i>op</i>(<i>d</i>, <i>state</i>) while <i>pred</i>(<i>d</i>, <i>state</i>) is non-zero.&nbsp;
  41. In other words expands to:
  42. <div>
  43. <i>op</i>(<i>d</i>, ... <i>op</i>(<i>d</i>, <i>op</i>(<i>d</i>, <i>state</i>)) ... ).
  44. </div>
  45. </div>
  46. <div>
  47. The <i>d</i> value that is passed to both <i>pred</i> and <i>op</i> represents the next available iteration.&nbsp;
  48. Other macros that have <b>_D</b> suffix variants internally use <b>BOOST_PP_WHILE</b>--for example, <b>BOOST_PP_ADD</b> and <b>BOOST_PP_ADD_D</b>.&nbsp;
  49. Using these <b>_D</b> versions is not strictly necessary, but passing the <i>d</i> value (that passed to <i>pred</i> or <i>op</i>) to these macros allows them to reenter <b>BOOST_PP_WHILE</b> with maximum efficiency.
  50. </div>
  51. <div>
  52. To directly use this <i>d</i> value, rather than simply passing it to another macro, see <b>BOOST_PP_WHILE_<i>d</i></b>.
  53. </div>
  54. <div>
  55. Previously, this macro could not be used recursively inside <b>BOOST_PP_WHILE</b>.&nbsp;
  56. This limitation no longer exists, as the library can automatically detect the next available iteration.
  57. </div>
  58. <h4>See Also</h4>
  59. <ul>
  60. <li><a href="limit_mag.html">BOOST_PP_LIMIT_MAG</a></li>
  61. <li><a href="while_d.html">BOOST_PP_WHILE_<i>d</i></a></li>
  62. </ul>
  63. <h4>Requirements</h4>
  64. <div>
  65. <b>Header:</b> &nbsp;<a href="../headers/control/while.html">&lt;boost/preprocessor/control/while.hpp&gt;</a>
  66. </div>
  67. <h4>Sample Code</h4>
  68. <div><pre>
  69. #include &lt;<a href="../headers/arithmetic/add.html">boost/preprocessor/arithmetic/add.hpp</a>&gt;
  70. #include &lt;<a href="../headers/control/while.html">boost/preprocessor/control/while.hpp</a>&gt;
  71. #include &lt;<a href="../headers/tuple/elem.html">boost/preprocessor/tuple/elem.hpp</a>&gt;
  72. #define PRED(n, state) <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(2, 1, state)
  73. #define OP(d, state) \
  74. OP_D( \
  75. d, \
  76. <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(2, 0, state), \
  77. <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(2, 1, state) \
  78. ) \
  79. /**/
  80. #define OP_D(d, res, c) \
  81. ( \
  82. <a href="add_d.html">BOOST_PP_ADD_D</a>( \
  83. d, \
  84. res, \
  85. <a href="dec.html">BOOST_PP_DEC</a>(c) \
  86. ), \
  87. <a href="dec.html">BOOST_PP_DEC</a>(c) \
  88. ) \
  89. /**/
  90. #define SUMMATION(n) \
  91. <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>( \
  92. 2, 0, \
  93. <a href="while.html">BOOST_PP_WHILE</a>(PRED, OP, (n, n)) \
  94. ) \
  95. /**/
  96. SUMMATION(3) // expands to 6
  97. SUMMATION(4) // expands to 10
  98. </pre></div>
  99. <hr size="1">
  100. <div style="margin-left: 0px;">
  101. <i>© Copyright <a href="http://www.housemarque.com" target="_top">Housemarque Oy</a> 2002</i>
  102. </br><i>© Copyright Paul Mensonides 2002</i>
  103. </div>
  104. <div style="margin-left: 0px;">
  105. <p><small>Distributed under the Boost Software License, Version 1.0. (See
  106. accompanying file <a href="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
  107. copy at <a href=
  108. "http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)</small></p>
  109. </div>
  110. </body>
  111. </html>