Consider the below code.
int value1 = BOOST_BINARY( 100 111000 01 1 110 );
Can somebody explain how BOOST_BINARY calculates the int value there?
It is very, very complicated. This BOOST library uses macros constructed by others macros.
I am going to present you some simplified example of preprocessor programming:
The simplified question:
How to make macro adding 0 or 1 at the beginning of rest - like in this examples:
CAT ( 0 1 ) --> 01
CAT ( 1 100 ) --> 1100
So - somehow ,
must be put between them both arguments in sequence. Well I did this:
// start point - this is our desired format
#define CATS(a,b) a##b
// helper macro to invoke preprocessor on result of a
#define CALLP(a) a
// some helper symbols to build CATS()
#define CATB (
#define CATE )
#define CATSN CATS
// I use that only 0 or 1 is expected at the beginning:
// CATS ( 0,
#define CAT_B0 CATSN CATB 0,
// CATS ( 1,
#define CAT_B1 CATSN CATB 1,
// the final macro:
// Example:
// CAT(0 XXX) --> CALLP( CAT_B ## 0 XXX ) )
// --> CALLP(CAT_B0 XXX ) --> CALLP(CATSN ( 0, XXX ) )
// --> CALLP( CATS(0, XXX) ) --> CATS(0,XXX) --> 0##XXX -> 0XXX
#define CAT(tt) CALLP (CAT_B ## tt CATE)
It really works (at least at gcc): http://ideone.com/EKlTGt
Now imagine how complicated is work done by BOOST folks in their library? Yes, it is very complicated - but use such preprocessor tricks I presented, and maybe many others - I looked at the source of this BOOST library http://www.boost.org/doc/libs/1_46_1/boost/utility/binary.hpp today for the first time (believe or not).
Use the source, Luke!
The following code works by converting the input bit pattern into a
Boost.Preprocessor sequence, then converting groupings of 3 bits each into
the corresponding octal digit, and finally concatenating all of the digits
together along with a leading zero. This yields a standard octal literal
with the desired value as specified in bits.
And that's pretty easy. We just need to define a few macros. Hold on tight.
#define BOOST_BINARY( bit_groupings ) \
BOOST_BINARY_LITERAL_D( BOOST_PP_DEDUCE_D(), bit_groupings )
// ...
#define BOOST_BINARY_LITERAL_D( d, bit_groupings ) \
BOOST_PP_SEQ_CAT \
( (0) BOOST_DETAIL_CREATE_BINARY_LITERAL_OCTAL_SEQUENCE( d, bit_groupings ) \
)
#define BOOST_DETAIL_CREATE_BINARY_LITERAL_OCTAL_SEQUENCE( d, bit_groupings ) \
BOOST_PP_SEQ_TRANSFORM \
( BOOST_DETAIL_TRIPLE_TO_OCTAL_OPERATION \
, BOOST_PP_NIL \
, BOOST_PP_IDENTITY( BOOST_DETAIL_CONVERT_BIT_SEQUENCE_TO_TRIPLE_SEQUENCE )()\
( BOOST_DETAIL_COMPLETE_TRIPLE_SEQUENCE \
( \
d \
, BOOST_DETAIL_CREATE_BINARY_LITERAL_BIT_SEQUENCE( d, bit_groupings ) \
) \
) \
)
#define BOOST_DETAIL_CONVERT_BIT_SEQUENCE_TO_TRIPLE_SEQUENCE( bit_sequence ) \
BOOST_PP_CAT \
( BOOST_DETAIL_CONVERT_BIT_SEQUENCE_TO_PARENTHETIC_TUPLE_1 bit_sequence \
, END_BIT \
)
#define BOOST_DETAIL_BITS_PER_OCTIT 3
#define BOOST_DETAIL_COMPLETE_TRIPLE_SEQUENCE( d, incomplete_nibble_sequence ) \
BOOST_PP_CAT \
( BOOST_DETAIL_CREATE_TRIPLE_COMPLETION_SEQUENCE_ \
, BOOST_PP_MOD_D( d \
, BOOST_PP_SEQ_SIZE( incomplete_nibble_sequence ) \
, BOOST_DETAIL_BITS_PER_OCTIT \
) \
) \
incomplete_nibble_sequence
#define BOOST_DETAIL_FIXED_COMPL( bit ) \
BOOST_PP_CAT( BOOST_DETAIL_FIXED_COMPL_, bit )
#define BOOST_DETAIL_FIXED_COMPL_0 1
#define BOOST_DETAIL_FIXED_COMPL_1 0
#define BOOST_DETAIL_CREATE_BINARY_LITERAL_BIT_SEQUENCE( d, bit_groupings ) \
BOOST_PP_EMPTY \
BOOST_PP_CAT( BOOST_PP_WHILE_, d ) \
( BOOST_DETAIL_BINARY_LITERAL_PREDICATE \
, BOOST_DETAIL_BINARY_LITERAL_OPERATION \
, bit_groupings () \
)
#define BOOST_DETAIL_BINARY_LITERAL_PREDICATE( d, state ) \
BOOST_DETAIL_FIXED_COMPL( BOOST_DETAIL_IS_NULLARY_ARGS( state ) )
#define BOOST_DETAIL_BINARY_LITERAL_OPERATION( d, state ) \
BOOST_DETAIL_SPLIT_AND_SWAP \
( BOOST_PP_CAT( BOOST_DETAIL_BINARY_LITERAL_ELEMENT_, state ) )
#define BOOST_DETAIL_TRIPLE_TO_OCTAL_OPERATION( s, dummy_param, tuple ) \
BOOST_DETAIL_TERNARY_TRIPLE_TO_OCTAL tuple
#define BOOST_DETAIL_TERNARY_TRIPLE_TO_OCTAL( bit2, bit1, bit0 ) \
BOOST_DETAIL_TRIPLE_TO_OCTAL_ ## bit2 ## bit1 ## bit0
#define BOOST_DETAIL_CREATE_TRIPLE_COMPLETION_SEQUENCE_1 (0)(0)
#define BOOST_DETAIL_CREATE_TRIPLE_COMPLETION_SEQUENCE_2 (0)
#define BOOST_DETAIL_CREATE_TRIPLE_COMPLETION_SEQUENCE_0
#define BOOST_DETAIL_CONVERT_BIT_SEQUENCE_TO_PARENTHETIC_TUPLE_1END_BIT
#define BOOST_DETAIL_CONVERT_BIT_SEQUENCE_TO_PARENTHETIC_TUPLE_1( bit ) \
( ( bit, BOOST_DETAIL_CONVERT_BIT_SEQUENCE_TO_PARENTHETIC_TUPLE_2
#define BOOST_DETAIL_CONVERT_BIT_SEQUENCE_TO_PARENTHETIC_TUPLE_2( bit ) \
bit, BOOST_DETAIL_CONVERT_BIT_SEQUENCE_TO_PARENTHETIC_TUPLE_3
#define BOOST_DETAIL_CONVERT_BIT_SEQUENCE_TO_PARENTHETIC_TUPLE_3( bit ) \
bit ) ) BOOST_DETAIL_CONVERT_BIT_SEQUENCE_TO_PARENTHETIC_TUPLE_1
#define BOOST_DETAIL_SPLIT_AND_SWAP( params ) \
BOOST_PP_IDENTITY( BOOST_DETAIL_SPLIT_AND_SWAP_PARAMS )()( params )
#define BOOST_DETAIL_SPLIT_AND_SWAP_PARAMS( first_param, second_param ) \
second_param first_param
#define BOOST_DETAIL_LEFT_OF_COMMA( params ) \
BOOST_PP_IDENTITY( BOOST_DETAIL_FIRST_MACRO_PARAM )()( params )
#define BOOST_DETAIL_FIRST_MACRO_PARAM( first_param, second_param ) \
first_param
/* Begin derived concepts from Chaos by Paul Mensonides */
#define BOOST_DETAIL_IS_NULLARY_ARGS( param ) \
BOOST_DETAIL_LEFT_OF_COMMA \
( BOOST_PP_CAT( BOOST_DETAIL_IS_NULLARY_ARGS_R_ \
, BOOST_DETAIL_IS_NULLARY_ARGS_C param \
) \
)
#define BOOST_DETAIL_IS_NULLARY_ARGS_C() \
1
#define BOOST_DETAIL_IS_NULLARY_ARGS_R_1 \
1, BOOST_PP_NIL
#define BOOST_DETAIL_IS_NULLARY_ARGS_R_BOOST_DETAIL_IS_NULLARY_ARGS_C \
0, BOOST_PP_NIL
/* End derived concepts from Chaos by Paul Mensonides */
#define BOOST_DETAIL_TRIPLE_TO_OCTAL_000 0
#define BOOST_DETAIL_TRIPLE_TO_OCTAL_001 1
#define BOOST_DETAIL_TRIPLE_TO_OCTAL_010 2
#define BOOST_DETAIL_TRIPLE_TO_OCTAL_011 3
#define BOOST_DETAIL_TRIPLE_TO_OCTAL_100 4
#define BOOST_DETAIL_TRIPLE_TO_OCTAL_101 5
#define BOOST_DETAIL_TRIPLE_TO_OCTAL_110 6
#define BOOST_DETAIL_TRIPLE_TO_OCTAL_111 7
#define BOOST_DETAIL_BINARY_LITERAL_ELEMENT_0 (0),
#define BOOST_DETAIL_BINARY_LITERAL_ELEMENT_1 (1),
#define BOOST_DETAIL_BINARY_LITERAL_ELEMENT_00 (0)(0),
#define BOOST_DETAIL_BINARY_LITERAL_ELEMENT_01 (0)(1),
// ... and so on, until ...
#define BOOST_DETAIL_BINARY_LITERAL_ELEMENT_11111110 (1)(1)(1)(1)(1)(1)(1)(0),
#define BOOST_DETAIL_BINARY_LITERAL_ELEMENT_11111111 (1)(1)(1)(1)(1)(1)(1)(1),
This makes heavy use of the macros in boost/preprocessor/
, but I won't repeat those here. It's thousands of lines of code all together.
I know this is not a very helpful, in-depth answer, but I'm going to post it anyway because it's better than nothing. Hopefully someone will have the courage to wade through this and provide something more insightful!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With