File: repeat_2d.c

package info (click to toggle)
boost 1.27.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,908 kB
  • ctags: 26,546
  • sloc: cpp: 122,225; ansic: 10,956; python: 4,412; sh: 855; yacc: 803; makefile: 257; perl: 165; lex: 90; csh: 6
file content (74 lines) | stat: -rw-r--r-- 2,171 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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 <boost/preprocessor/repeat.hpp>
#include <boost/preprocessor/repeat_2nd.hpp>

/** Repeats the macro M(X,Y,P) for X = [0,W[ and Y = [0,H[.

In other words, expands to the sequence:

<PRE>\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</PRE>

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 <boost/preprocessor/logical/or.hpp>
#include <boost/preprocessor/comma_if.hpp>
#include <boost/preprocessor/list/cat.hpp>
#include <boost/preprocessor/tuple/to_list.hpp>

/* 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 };
 */