使用boost的preprocessor
在线文档:http://www.boost.org/doc/libs/1_48_0/libs/preprocessor/doc/index.html
http://www.boost.org/doc/libs/1_48_0/libs/preprocessor/doc/topics/techniques.html
http://www.boost.org/doc/libs/1_48_0/libs/preprocessor/doc/ref.html
几篇不错的博文:http://blog.csdn.net/zx77/article/details/2270991
一个关于2D的repeat的实现 :
/* Copyright (C) 2002
* Housemarque Oy
* http://www.housemarque.com
*
* Permission to copy, use, modify, sell and distribute this software is
* granted provided this copyright notice appears in all copies. This
* software is provided "as is" without express or implied warranty, and
* with no claim as to its suitability for any purpose.
*
* See http://www.boost.org for most recent version.
*/
/* This example implements a generalized macro for 2D repetition using
* the simple repetition primitives of the preprocessor library.
*/
#include "preprocessor/repeat.hpp"
#include "preprocessor/logical/or.hpp"
#include "preprocessor/comma_if.hpp"
#include "preprocessor/list/cat.hpp"
#include "preprocessor/tuple/elem.hpp"
#include "preprocessor/tuple/to_list.hpp"
/** <p>Repeats the macro <code>M(X,Y,DATA)</code> for <code>X = [0,W)</code> and <code>Y = [0,H)</code>.</p>
<p>In other words, expands to the sequence:</p>
<pre>
M( 0, 0, DATA) M( 1, 0, DATA) ... M(W-1, 0, DATA)
M( 0, 1, DATA) M( 1, 1, DATA) ... M(W-1, 1, DATA)
... ... ... ...
M( 0,H-1, DATA) M( 1,H-1, DATA) ... M(W-1,H-1, DATA)
</pre>
*/
#define REPEAT_2D(W,H,M,DATA)\
/* Here we can simply use BOOST_PP_REPEAT(), because\
* it implements automatic recursion.\
*/\
BOOST_PP_REPEAT\
( H\
, REPEAT_2D_ROW\
, (W,M,DATA)\
)
#define REPEAT_2D_ROW(z,Y,WMD)\
BOOST_PP_REPEAT\
( BOOST_PP_TUPLE_ELEM(3,0,WMD)\
, REPEAT_2D_ELEM\
, (Y, BOOST_PP_TUPLE_ELEM(3,1,WMD), BOOST_PP_TUPLE_ELEM(3,2,WMD))\
)
#define REPEAT_2D_ELEM(z,X,YMD)\
BOOST_PP_TUPLE_ELEM(3,1,YMD)\
( X\
, BOOST_PP_TUPLE_ELEM(3,0,YMD)\
, BOOST_PP_TUPLE_ELEM(3,2,YMD)\
)
/* Here we use the above macro to generate something. */
#define ELEM(X,Y,E) BOOST_PP_COMMA_IF(BOOST_PP_OR(X,Y)) BOOST_PP_LIST_CAT(BOOST_PP_TUPLE_TO_LIST(5,(E,_,X,_,Y)))
enum { REPEAT_2D(3,4,ELEM,elem) };
/* The above expands to:
*
* enum { elem_0_0, elem_1_0, elem_2_0,
* elem_0_1, elem_1_1, elem_2_1,
* elem_0_2, elem_1_2, elem_2_2,
* elem_0_3, elem_1_3, elem_2_3 };
*/
http://www.boost.org/doc/libs/1_48_0/libs/preprocessor/doc/topics/techniques.html
http://www.boost.org/doc/libs/1_48_0/libs/preprocessor/doc/ref.html
几篇不错的博文:http://blog.csdn.net/zx77/article/details/2270991
一个关于2D的repeat的实现 :
/* Copyright (C) 2002
* Housemarque Oy
* http://www.housemarque.com
*
* Permission to copy, use, modify, sell and distribute this software is
* granted provided this copyright notice appears in all copies. This
* software is provided "as is" without express or implied warranty, and
* with no claim as to its suitability for any purpose.
*
* See http://www.boost.org for most recent version.
*/
/* This example implements a generalized macro for 2D repetition using
* the simple repetition primitives of the preprocessor library.
*/
#include "preprocessor/repeat.hpp"
#include "preprocessor/logical/or.hpp"
#include "preprocessor/comma_if.hpp"
#include "preprocessor/list/cat.hpp"
#include "preprocessor/tuple/elem.hpp"
#include "preprocessor/tuple/to_list.hpp"
/** <p>Repeats the macro <code>M(X,Y,DATA)</code> for <code>X = [0,W)</code> and <code>Y = [0,H)</code>.</p>
<p>In other words, expands to the sequence:</p>
<pre>
M( 0, 0, DATA) M( 1, 0, DATA) ... M(W-1, 0, DATA)
M( 0, 1, DATA) M( 1, 1, DATA) ... M(W-1, 1, DATA)
... ... ... ...
M( 0,H-1, DATA) M( 1,H-1, DATA) ... M(W-1,H-1, DATA)
</pre>
*/
#define REPEAT_2D(W,H,M,DATA)\
/* Here we can simply use BOOST_PP_REPEAT(), because\
* it implements automatic recursion.\
*/\
BOOST_PP_REPEAT\
( H\
, REPEAT_2D_ROW\
, (W,M,DATA)\
)
#define REPEAT_2D_ROW(z,Y,WMD)\
BOOST_PP_REPEAT\
( BOOST_PP_TUPLE_ELEM(3,0,WMD)\
, REPEAT_2D_ELEM\
, (Y, BOOST_PP_TUPLE_ELEM(3,1,WMD), BOOST_PP_TUPLE_ELEM(3,2,WMD))\
)
#define REPEAT_2D_ELEM(z,X,YMD)\
BOOST_PP_TUPLE_ELEM(3,1,YMD)\
( X\
, BOOST_PP_TUPLE_ELEM(3,0,YMD)\
, BOOST_PP_TUPLE_ELEM(3,2,YMD)\
)
/* Here we use the above macro to generate something. */
#define ELEM(X,Y,E) BOOST_PP_COMMA_IF(BOOST_PP_OR(X,Y)) BOOST_PP_LIST_CAT(BOOST_PP_TUPLE_TO_LIST(5,(E,_,X,_,Y)))
enum { REPEAT_2D(3,4,ELEM,elem) };
/* The above expands to:
*
* enum { elem_0_0, elem_1_0, elem_2_0,
* elem_0_1, elem_1_1, elem_2_1,
* elem_0_2, elem_1_2, elem_2_2,
* elem_0_3, elem_1_3, elem_2_3 };
*/