diff --git a/example/catch_builtin.cpp b/example/catch_builtin.cpp new file mode 100644 index 0000000..852c3a1 --- /dev/null +++ b/example/catch_builtin.cpp @@ -0,0 +1,50 @@ +/* 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 demonstrates the usage of lists and BOOST_PP_LIST_FOR_EACH(). */ + +#include +#include +#include +#include + +/* List of built-in types. (Strictly speaking wchar_t should be on the list.) */ +#define BUILTIN_TYPES\ + BOOST_PP_TUPLE_TO_LIST\ + ( 13\ + , ( bool\ + , char\ + , signed char\ + , unsigned char\ + , unsigned short\ + , short\ + , int\ + , unsigned\ + , long\ + , unsigned long\ + , float\ + , double\ + , long double\ + )\ + ) + +/* This is the macro we pass to BOOST_PP_LIST_FOR_EACH(). */ +#define CATCH(R,_,T)\ + catch (T t) { std::cerr << "Caught an " << typeid(t).name() << " = " << t; } + +int main() +{ + try { throw 10; } + BOOST_PP_LIST_FOR_EACH(CATCH,_,BUILTIN_TYPES) + + return 0; +} diff --git a/example/count_down.c b/example/count_down.c new file mode 100644 index 0000000..e9162d0 --- /dev/null +++ b/example/count_down.c @@ -0,0 +1,30 @@ +/* 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 is a trivial example of using BOOST_PP_WHILE() that simply + * counts down from N to 0 ultimately expanding to a 0. + */ + +#include +#include + +#define COUNT_DOWN(N) BOOST_PP_WHILE(COUNT_DOWN_C,COUNT_DOWN_F,N) +/* Above is the macro we are implementing using BOOST_PP_WHILE(). */ + +#define COUNT_DOWN_C(D,N) N +/* Above is the condition. It expands to the current N. */ + +#define COUNT_DOWN_F(D,N) BOOST_PP_DEC(N) +/* Above is the iteration macro. It decrements N. */ + +enum { x = COUNT_DOWN(50) }; +/* The above expands to 0. */ diff --git a/example/repeat_2d.c b/example/repeat_2d.c new file mode 100644 index 0000000..e5a66bc --- /dev/null +++ b/example/repeat_2d.c @@ -0,0 +1,74 @@ +/* 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 +#include + +/** Repeats the macro M(X,Y,P) for X = [0,W[ and Y = [0,H[. + +In other words, expands to the sequence: + +
\verbatim
+  M(  0,  0,  P) M(  1,  0,  P) ... M(W-1,  0,  P)
+  M(  0,  1,  P) M(  1,  1,  P) ... M(W-1,  1,  P)
+        ...            ...      ...       ...
+  M(  0,H-1,  P) M(  1,H-1,  P) ... M(W-1,H-1,  P)
+\endverbatim
+ +Uses BOOST_PP_REPEAT() and BOOST_PP_REPEAT_2ND(). +*/ +#define REPEAT_2D(W,H,M,P)\ + /* Here we must use BOOST_PP_REPEAT_2ND(), because\ + * we use BOOST_PP_REPEAT() in REPEAT_2D_ROW().\ + */\ + BOOST_PP_REPEAT_2ND\ + ( H\ + , REPEAT_2D_ROW\ + , (W,M,P)\ + ) + +#ifndef DOXYGEN_SHOULD_SKIP_THIS +#define REPEAT_2D_ROW(Y,WMP)\ + BOOST_PP_REPEAT\ + ( BOOST_PP_TUPLE_ELEM(3,0,WMP)\ + , REPEAT_2D_ELEM\ + , (Y, BOOST_PP_TUPLE_ELEM(3,1,WMP), BOOST_PP_TUPLE_ELEM(3,2,WMP))\ + ) +#define REPEAT_2D_ELEM(X,YMP)\ + BOOST_PP_TUPLE_ELEM(3,1,YMP)\ + ( X\ + , BOOST_PP_TUPLE_ELEM(3,0,YMP)\ + , BOOST_PP_TUPLE_ELEM(3,2,YMP)\ + ) +#endif + +#include +#include +#include +#include + +/* 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 }; + */