1 // Copyright Cromwell D. Enage 2013. 2 // Distributed under the Boost Software License, Version 1.0. 3 // (See accompanying file LICENSE_1_0.txt or copy at 4 // http://www.boost.org/LICENSE_1_0.txt) 5 6 #ifndef BOOST_PARAMETER_AUX_PREPROCESSOR_INC_BINARY_SEQ_HPP 7 #define BOOST_PARAMETER_AUX_PREPROCESSOR_INC_BINARY_SEQ_HPP 8 9 #include <boost/preprocessor/seq/push_back.hpp> 10 11 // This macro keeps the rest of the sequence if carry == 0. 12 #define BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_0(seq, element) \ 13 (BOOST_PP_SEQ_PUSH_BACK(seq, element), 0) 14 /**/ 15 16 #include <boost/preprocessor/control/iif.hpp> 17 18 // This macro updates the rest of the sequence if carry == 1. 19 #define BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_1(seq, element) \ 20 (BOOST_PP_SEQ_PUSH_BACK(seq, BOOST_PP_IIF(element, 0, 1)), element) 21 /**/ 22 23 #include <boost/preprocessor/tuple/elem.hpp> 24 #include <boost/preprocessor/cat.hpp> 25 26 // This macro maintains a tuple (seq, carry), where seq is the intermediate 27 // result and carry is a flag that will unset upon finding an element == 0. 28 #define BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_OP(s, result_tuple, element) \ 29 BOOST_PP_CAT( \ 30 BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_ \ 31 , BOOST_PP_TUPLE_ELEM(2, 1, result_tuple) \ 32 )(BOOST_PP_TUPLE_ELEM(2, 0, result_tuple), element) 33 /**/ 34 35 // This macro keeps the sequence at its original length if carry == 0. 36 #define BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_IMPL_0(seq) seq 37 /**/ 38 39 // This macro appends a zero to seq if carry == 1. 40 #define BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_IMPL_1(seq) \ 41 BOOST_PP_SEQ_PUSH_BACK(seq, 0) 42 /**/ 43 44 // This macro takes in the tuple (seq, carry), with carry indicating whether 45 // or not seq originally contained all 1s. If so, then seq now contains all 46 // 0s, and this macro pushes an extra 0 before expanding to the new sequence. 47 // Otherwise, this macro expands to seq as is. 48 #define BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_IMPL(seq_and_carry) \ 49 BOOST_PP_CAT( \ 50 BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_IMPL_ \ 51 , BOOST_PP_TUPLE_ELEM(2, 1, seq_and_carry) \ 52 )(BOOST_PP_TUPLE_ELEM(2, 0, seq_and_carry)) 53 /**/ 54 55 #include <boost/preprocessor/seq/seq.hpp> 56 #include <boost/preprocessor/seq/fold_left.hpp> 57 58 // This macro treats the specified sequence of 1s and 0s like a binary number 59 // in reverse and expands to a sequence representing the next value up. 60 // However, if the input sequence contains all 1s, then the output sequence 61 // will contain one more element but all 0s. 62 // 63 // Examples: 64 // seq = (1)(0)(1)(0) --> return (0)(1)(1)(0) 65 // seq = (1)(1)(1)(0) --> return (0)(0)(0)(1) 66 // seq = (1)(1)(1)(1) --> return (0)(0)(0)(0)(0) 67 #define BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ(seq) \ 68 BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_IMPL( \ 69 BOOST_PP_SEQ_FOLD_LEFT( \ 70 BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_OP \ 71 , (BOOST_PP_SEQ_NIL, 1) \ 72 , seq \ 73 ) \ 74 ) 75 /**/ 76 77 #endif // include guard 78 79