This macro iterates
op(
d,
state) while
pred(
d,
state) is non-zero.
In other words expands to:
op(d, ... op(d, op(d, state)) ... ).
At certain times, it may be necessary to perform the concatenation with
BOOST_PP_CAT rather than the preprocessor token-pasting operator.
This happens when the
d value is a macro invocation itself.
It needs a delay to allow it to expand.
The syntax in such a scenario becomes:
BOOST_PP_CAT(BOOST_PP_WHILE_, d)(pred, op, state).
#include <boost/preprocessor/arithmetic/add.hpp>
#include <boost/preprocessor/arithmetic/dec.hpp>
#include <boost/preprocessor/array/elem.hpp>
#include <boost/preprocessor/array/size.hpp>
#include <boost/preprocessor/control/while.hpp>
#include <boost/preprocessor/tuple/elem.hpp>
#define PRED(d, data) BOOST_PP_TUPLE_ELEM(3, 1, data)
#define OP(d, data) /* ............ */ \
OP_D( \
d, \
BOOST_PP_TUPLE_ELEM(3, 0, data), \
BOOST_PP_TUPLE_ELEM(3, 1, data), \
BOOST_PP_TUPLE_ELEM(3, 2, data) \
) \
/**/
#define OP_D(d, res, i, array) /* ................. */ \
( \
BOOST_PP_ADD_D( \
d, res, \
BOOST_PP_ARRAY_ELEM(BOOST_PP_DEC(i), array)), \
BOOST_PP_DEC(i), \
array \
) \
/**/
#define ACCUMULATE_D(d, array) /* .......... */ \
BOOST_PP_TUPLE_ELEM( \
3, 0, \
BOOST_PP_WHILE_ ## d( \
PRED, OP, \
(0, BOOST_PP_ARRAY_SIZE(array), array) \
) \
) \
/**/
#define ARRAY (4, (1, 2, 3, 4))
ACCUMULATE_D(1, ARRAY)// expands to 10