file_iteration.html 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. <html>
  2. <head>
  3. <title>file_iteration.html</title>
  4. <link rel="stylesheet" type="text/css" href="../styles.css">
  5. </head>
  6. <body>
  7. <h4>
  8. File Iteration
  9. </h4>
  10. <div>
  11. File iteration is a complex, but powerful, vertical repetition construct.&nbsp;
  12. It repeatedly includes a <i>file</i> for each number in a user-specified range.
  13. </div>
  14. <h4>
  15. Tutorial
  16. </h4>
  17. <div>
  18. This mechanism requires two pieces of information to operate:&nbsp; a range to
  19. iterate over and a file to include on each iteration.&nbsp; It can optionally
  20. take a third piece of information that represents flags used to discriminate
  21. between different iterations of the same file.&nbsp; This information is
  22. obtained by the mechanism through one or two <i>named external arguments</i>.&nbsp;
  23. These arguments are specified as user-defined macros named <b>BOOST_PP_ITERATION_PARAMS_<i>x</i></b>
  24. or the combination of <b>BOOST_PP_FILENAME_<i>x</i></b> and <b>BOOST_PP_ITERATION_LIMITS</b>.
  25. </div>
  26. <div>
  27. <b>BOOST_PP_ITERATION_LIMITS</b> specifies the range of values to iterate
  28. over.&nbsp; It <i>must</i> expand to a <i>tuple</i> containing two elements--a
  29. lower and upper bound.&nbsp; Both the upper and lower bounds must be numeric
  30. values in the range of <i>0</i> to <b>BOOST_PP_LIMIT_ITERATION</b>.&nbsp; For
  31. example, if the user wishes a file to be included for numbers ranging from <i>0</i>
  32. to <i>10</i>, <b>BOOST_PP_ITERATION_LIMITS</b> would be defined like this:
  33. </div>
  34. <div class="code">
  35. <pre>
  36. #define BOOST_PP_ITERATION_LIMITS (0, 10)
  37. </pre>
  38. </div>
  39. <div>
  40. Note that there is whitespace after the name of the macro.&nbsp; The macro <i>does
  41. not</i> take <i>two</i> arguments.&nbsp; In the case above, if there was
  42. no whitespace, a preprocessing error would occur because <i>0</i> and <i>10</i>
  43. are invalid identifiers.
  44. </div>
  45. <div>
  46. Both the upper and lower bounds specified in the <b>BOOST_PP_ITERATION_LIMITS</b>
  47. macro are <i>evaluated parameters</i>.&nbsp; This implies that they can include
  48. simple arithmetic or logical expressions.&nbsp; For instance, the above
  49. definition could easily have been written like this:
  50. </div>
  51. <div class="code">
  52. <pre>
  53. #define N() 5
  54. #define BOOST_PP_ITERATION_LIMITS (0, N() + 5)
  55. </pre>
  56. </div>
  57. <div>
  58. Because of this, if the whitespace after the macro name is elided, it is
  59. possible for the definition to be syntactically valid:
  60. </div>
  61. <div class="code">
  62. <pre>
  63. #define A 0
  64. #define B 10
  65. #define BOOST_PP_ITERATION_LIMITS(A, B)
  66. // note: no whitespace ^
  67. </pre>
  68. </div>
  69. <div>
  70. If this happens, an error will occur inside the mechanism when it attempts to
  71. use this macro.&nbsp; The error messages that result may be obscure, so always
  72. remember to include the whitespace.&nbsp; A <i>correct</i> version of the above
  73. looks like this:
  74. </div>
  75. <div class="code">
  76. <pre>
  77. #define A 0
  78. #define B 10
  79. #define BOOST_PP_ITERATION_LIMITS (A, B)
  80. // note: has whitespace ^
  81. </pre>
  82. </div>
  83. <div>
  84. <b>BOOST_PP_FILENAME_<i>x</i></b> specifies the file to iterate over.&nbsp; The <i>x</i>
  85. is a placeholder for the dimension of iteration.&nbsp; (For now, we'll assume
  86. this is <i>1</i>--i.e. the first dimension, so we are actually dealing with <b>BOOST_PP_FILENAME_1</b>.)&nbsp;
  87. This macro must expand to a valid filename--in quotes or in angle brackets
  88. depending on how the file is accessed:
  89. </div>
  90. <div class="code">
  91. <pre>
  92. #define BOOST_PP_FILENAME_1 "file.h"
  93. // -or-
  94. #define BOOST_PP_FILENAME_1 &lt;file.h&gt;
  95. </pre>
  96. </div>
  97. <div>
  98. All that we need now to perform a simple file iteration is to invoke the
  99. mechanism:
  100. </div>
  101. <div class="code">
  102. <pre>
  103. ??=include BOOST_PP_ITERATE()
  104. </pre>
  105. </div>
  106. <div>
  107. (The <code>??=</code> token is a trigraph for <code>#</code>.&nbsp; I use the
  108. trigraph to make it clear that I am <i>including</i> a file rather than
  109. defining or expanding a macro, but it is not necessary.&nbsp; Even the digraph
  110. version, <code>%:</code>, could be used.&nbsp; Some compilers do not readily
  111. accept trigraphs and digraphs, so keep that in mind.&nbsp; Other than that, use
  112. whichever one you prefer.)
  113. </div>
  114. <div>
  115. So, if we wish to iterate "file.h" from <i>1</i> to <i>10</i>, we just need to
  116. put the pieces together:
  117. </div>
  118. <div class="code">
  119. <pre>
  120. #define BOOST_PP_ITERATION_LIMITS (1, 10)
  121. #define BOOST_PP_FILENAME_1 "file.h"
  122. ??=include BOOST_PP_ITERATE()
  123. </pre>
  124. </div>
  125. <div>
  126. The above code has the effect of including "file.h" ten times in
  127. succession.&nbsp;
  128. </div>
  129. <div>
  130. Alternately, both the range and the file to iterate over can be expressed in
  131. one macro, <b>BOOST_PP_ITERATION_PARAMS_<i>x</i></b>.&nbsp; Once again, the <i>x</i>
  132. is a placeholder for the dimension of iteration--which we'll assume is <i>1</i>.&nbsp;
  133. This macro must expand to an <i>array</i> that includes the lower bound, upper
  134. bound, filename, and optional flags (in that order).
  135. </div>
  136. <div class="code">
  137. <pre>
  138. #define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 10, "file.h"))
  139. ??=include BOOST_PP_ITERATE()
  140. </pre>
  141. </div>
  142. <div>
  143. This has the same effect as the previous version.&nbsp; Only one of these two
  144. ways to specify the parameters can be used at a time.&nbsp; (The reason that
  145. there are two different methods has to do with dimensional abstraction which
  146. I'll get to later.)
  147. </div>
  148. <div>
  149. There is nothing particularly useful about including a file ten times.&nbsp;
  150. The difference is that the current macro state changes each time.&nbsp; For
  151. example, the current "iteration value" is available with <b>BOOST_PP_ITERATION</b>().&nbsp;
  152. If "file.h" is defined like this...
  153. </div>
  154. <div class="code">
  155. <pre>
  156. // file.h
  157. template&lt;&gt; struct sample&lt;BOOST_PP_ITERATION()&gt; { };
  158. </pre>
  159. </div>
  160. <div>
  161. ...and it is iterated as follows...
  162. </div>
  163. <div class="code">
  164. <pre>
  165. template&lt;int&gt; struct sample;
  166. #define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 5, "file.h"))
  167. ??=include BOOST_PP_ITERATE()
  168. </pre>
  169. </div>
  170. <div>
  171. ...the result is different each time:
  172. </div>
  173. <div>
  174. <pre>
  175. template&lt;&gt; struct sample&lt;1&gt; { };
  176. template&lt;&gt; struct sample&lt;2&gt; { };
  177. template&lt;&gt; struct sample&lt;3&gt; { };
  178. template&lt;&gt; struct sample&lt;4&gt; { };
  179. template&lt;&gt; struct sample&lt;5&gt; { };
  180. </pre>
  181. </div>
  182. <div>
  183. There is no reason that a file can't iterate over itself.&nbsp; This has the
  184. advantage of keeping the code together.&nbsp; The problem is that you have to
  185. discriminate the "regular" section of the file from the iterated section of the
  186. file.&nbsp; The library provides the <b>BOOST_PP_IS_ITERATING</b> macro to help
  187. in this regard.&nbsp; This macro is defined as <i>1</i> if an iteration is in
  188. progress.&nbsp; For example, to merge the contents of "file.h" into the file
  189. that iterates it:
  190. </div>
  191. <div class="code">
  192. <pre>
  193. // sample.h
  194. #if !BOOST_PP_IS_ITERATING
  195. #ifndef SAMPLE_H
  196. #define SAMPLE_H
  197. #include &lt;boost/preprocessor/iteration/iterate.hpp&gt;
  198. template&lt;int&gt; struct sample;
  199. #define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 5, "sample.h"))
  200. ??=include BOOST_PP_ITERATE()
  201. #endif // SAMPLE_H
  202. #else
  203. template&lt;&gt; struct sample&lt;BOOST_PP_ITERATION()&gt; { };
  204. #endif
  205. </pre>
  206. </div>
  207. <div>
  208. Using the same file like this raises another issue.&nbsp; What happens when a
  209. file performs two separate file iterations over itself?&nbsp; This is the
  210. purpose of the optional flags parameter.&nbsp; It is used to discriminate
  211. between separate iterations.
  212. </div>
  213. <div class="code">
  214. <pre>
  215. // sample.h
  216. #if !BOOST_PP_IS_ITERATING
  217. #ifndef SAMPLE_H
  218. #define SAMPLE_H
  219. #include &lt;boost/preprocessor/iteration/iterate.hpp&gt;
  220. #include &lt;boost/preprocessor/repetition/enum_params.hpp&gt;
  221. #include &lt;boost/preprocessor/repetition/enum_shifted_params.hpp&gt;
  222. template&lt;int&gt; struct sample;
  223. #define BOOST_PP_ITERATION_PARAMS_1 (4, (1, 5, "sample.h", 1))
  224. ??=include BOOST_PP_ITERATE()
  225. template&lt;class T, class U&gt; struct typelist_t {
  226. typedef T head;
  227. typedef U tail;
  228. };
  229. template&lt;int&gt; struct typelist;
  230. struct null_t;
  231. template&lt;&gt; struct typelist&lt;1&gt; {
  232. template&lt;class T0&gt; struct args {
  233. typedef typelist_t&lt;T0, null_t&gt; type;
  234. };
  235. };
  236. #ifndef TYPELIST_MAX
  237. #define TYPELIST_MAX 50
  238. #endif
  239. #define BOOST_PP_ITERATION_PARAMS_1 (4, (2, TYPELIST_MAX, "sample.h", 2))
  240. ??=include BOOST_PP_ITERATE()
  241. #endif // SAMPLE_H
  242. #elif BOOST_PP_ITERATION_FLAGS() == 1
  243. template&lt;&gt; struct sample&lt;BOOST_PP_ITERATION()&gt; { };
  244. #elif BOOST_PP_ITERATION_FLAGS() == 2
  245. #define N BOOST_PP_ITERATION()
  246. template&lt;&gt; struct typelist&lt;N&gt; {
  247. template&lt;BOOST_PP_ENUM_PARAMS(N, class T)&gt; struct args {
  248. typedef typelist_t&lt;
  249. T0,
  250. typename typelist&lt;N - 1&gt;::args&lt;BOOST_PP_ENUM_SHIFTED_PARAMS(N, T)&gt;::type
  251. &gt; type;
  252. };
  253. };
  254. #undef N
  255. #endif
  256. </pre>
  257. </div>
  258. <div>
  259. Notice the use of the "flags" parameter (which is accessed through <b>BOOST_PP_ITERATION_FLAGS</b>()).&nbsp;
  260. It discriminates between our recurring <code>sample</code> iteration and a
  261. typelist linearization iteration.&nbsp;
  262. </div>
  263. <div>
  264. The second iteration illustrates the power of the file iteration
  265. mechanism.&nbsp; It generates typelist linearizations of the form <code>typelist&lt;3&gt;::args&lt;int,
  266. double, char&gt;::type</code>.
  267. </div>
  268. <div>
  269. Actually, to continue the typelist example, with the help of another iteration
  270. we can <i>fully</i> linearize typelist creation....
  271. </div>
  272. <div class="code">
  273. <pre>
  274. // extract.h
  275. #if !BOOST_PP_IS_ITERATING
  276. #ifndef EXTRACT_H
  277. #define EXTRACT_H
  278. #include &lt;boost/preprocessor/iteration/iterate.hpp&gt;
  279. #include &lt;boost/preprocessor/repetition/enum.hpp&gt;
  280. #include &lt;boost/preprocessor/repetition/enum_params.hpp&gt;
  281. #include &lt;boost/preprocessor/repetition/enum_trailing_params.hpp&gt;
  282. // certain types such as "void" can't be function argument types
  283. template&lt;class T&gt; struct incomplete {
  284. typedef T type;
  285. };
  286. template&lt;class T&gt; struct strip_incomplete {
  287. typedef T type;
  288. };
  289. template&lt;class T&gt; struct strip_incomplete&lt;incomplete&lt;T&gt; &gt; {
  290. typedef T type;
  291. };
  292. template&lt;template&lt;int&gt; class output, class func_t&gt; struct extract;
  293. #ifndef EXTRACT_MAX
  294. #define EXTRACT_MAX 50
  295. #endif
  296. #define BOOST_PP_ITERATION_PARAMS_1 (3, (1, EXTRACT_MAX, "extract.h"))
  297. ??=include BOOST_PP_ITERATE()
  298. #endif // EXTRACT_H
  299. #else
  300. #define N BOOST_PP_ITERATION()
  301. #define STRIP(z, n, _) \
  302. typename strip_incomplete&lt;T ## n&gt;::type \
  303. /**/
  304. template&lt;template&lt;int&gt; class output, class R BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)&gt;
  305. struct extract&lt;R (BOOST_PP_ENUM_PARAMS(N, T))&gt; {
  306. typedef typename output&lt;N&gt;::template args&lt;BOOST_PP_ENUM(N, STRIP, nil)&gt;::type type;
  307. };
  308. #undef STRIP
  309. #undef N
  310. #endif
  311. </pre>
  312. </div>
  313. <div>
  314. Now we can define a helper macro to finish the job:
  315. </div>
  316. <div class="code">
  317. <pre>
  318. #define TYPELIST(args) extract&lt;typelist, void args&gt;::type
  319. typedef TYPELIST((int, double, incomplete&lt;void&gt;)) xyz;
  320. </pre>
  321. </div>
  322. <div>
  323. There are two minor caveats with this result.&nbsp; First, certain types like <code>void</code>
  324. can't be the type of an argument, so they have to be wrapped with <code>incomplete&lt;T&gt;</code>.&nbsp;
  325. Second, the necessary double parenthesis is annoying.&nbsp; If and when C++
  326. gets C99's variadic macros, <code>TYPELIST</code> can be redefined:
  327. </div>
  328. <div class="code">
  329. <pre>
  330. #define TYPELIST(...) extract&lt;typelist, void (__VA_ARGS__)&gt;::type
  331. typedef TYPELIST(int, double, short) xyz;
  332. </pre>
  333. </div>
  334. <div>
  335. Note also that both the lower and upper bounds of an iteration are also
  336. accessible inside an iteration with <b>BOOST_PP_ITERATION_START</b>() and <b>BOOST_PP_ITERATION_FINISH</b>().
  337. </div>
  338. <div>
  339. It is my hope that the explanation and examples presented here demonstrate the
  340. power of file iteration.&nbsp; Even so, this is just the beginning.&nbsp; The
  341. file iteration mechanism also defines a full suite of facilities to support
  342. multidimensional iteration.
  343. </div>
  344. <h4>
  345. Multiple Dimensions
  346. </h4>
  347. <div>
  348. The file iteration mechanism supports up to <b>BOOST_PP_LIMIT_ITERATION_DIM</b>
  349. dimensions.&nbsp; The first dimension (i.e. the outermost) we have already used
  350. above.&nbsp; In order to use the second dimension (inside the first), we simply
  351. have to replace the placeholder <i>x</i> with <i>2</i> instead of <i>1</i>.
  352. </div>
  353. <div class="code">
  354. <pre>
  355. #define BOOST_PP_ITERATION_PARAMS_2 /* ... */
  356. ^
  357. </pre>
  358. </div>
  359. <div>
  360. ...or...
  361. </div>
  362. <div class="code">
  363. <pre>
  364. #define BOOST_PP_FILENAME_2 /* ... */
  365. ^
  366. </pre>
  367. </div>
  368. <div>
  369. Each dimension must be used <i>in order</i> starting with <i>1</i>.&nbsp;
  370. Therefore, the above can <i>only</i> be valid immediately inside the first
  371. dimension.&nbsp;
  372. </div>
  373. <div>
  374. At this point, further explanation is necessary regarding <b>BOOST_PP_ITERATION</b>,
  375. <b>BOOST_PP_ITERATION_START</b>, and <b>BOOST_PP_ITERATION_FINISH</b>.&nbsp; <b>BOOST_PP_ITERATION</b>()
  376. expands to the iteration value of the <i>current</i> dimension--regardless of
  377. what dimension that is.&nbsp; Likewise, <b>BOOST_PP_ITERATION_START</b>() and <b>BOOST_PP_ITERATION_FINISH</b>()
  378. expand to the lower and upper bounds of the <i>current</i> dimension.&nbsp;
  379. Using the following pseudo-code as reference:
  380. </div>
  381. <div class="code">
  382. <pre>
  383. for (int i = start(1); i <= finish(1); ++i) {
  384. // A
  385. for (int j = start(2); j <= finish(2); ++j) {
  386. // B
  387. }
  388. // C
  389. }
  390. </pre>
  391. </div>
  392. <div>
  393. At point <i>A</i>, <b>BOOST_PP_ITERATION</b>() refers to <code>i</code>.&nbsp; <b>BOOST_PP_ITERATION_START</b>()
  394. and <b>BOOST_PP_ITERATION_FINISH</b>() refer to <code>start(1)</code> and <code>finish(1)</code>
  395. respectively.&nbsp; At point <i>B</i>, however, <b>BOOST_PP_ITERATION</b>()
  396. refers to <code>j</code>--the <i>current</i> iteration value at point <i>B</i>.&nbsp;
  397. The same is true for <b>BOOST_PP_ITERATION_START</b>() which refers to <code>start(2)</code>,
  398. etc..
  399. </div>
  400. <div>
  401. If separate files are used for each dimension, then there are no major
  402. problems, and using multiple dimensions is straightforward.&nbsp; However, if
  403. more than one dimension is located in the same file, they need to be
  404. distinguished from one another.&nbsp; The file iteration mechanism provides the
  405. macro <b>BOOST_PP_ITERATION_DEPTH</b> for this purpose:
  406. </div>
  407. <div class="code">
  408. <pre>
  409. // file.h
  410. #if !BOOST_PP_IS_ITERATING
  411. #ifndef FILE_H
  412. #define FILE_H
  413. #include &lt;boost/preprocessor/iteration/iterate.hpp&gt;
  414. #define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 2, "file.h"))
  415. ??=include BOOST_PP_ITERATE()
  416. #endif // FILE_H
  417. #elif BOOST_PP_ITERATION_DEPTH() == 1
  418. // A
  419. + BOOST_PP_ITERATION()
  420. #define BOOST_PP_ITERATION_PARAMS_2 (3, (1, 2, "file.h"))
  421. ??=include BOOST_PP_ITERATE()
  422. // C
  423. #elif BOOST_PP_ITERATION_DEPTH() == 2
  424. // B
  425. - BOOST_PP_ITERATION()
  426. #endif
  427. </pre>
  428. </div>
  429. <div>
  430. This will result to the following:
  431. </div>
  432. <div>
  433. <pre>
  434. + 1
  435. - 1
  436. - 2
  437. + 2
  438. - 1
  439. - 2
  440. </pre>
  441. </div>
  442. <div>
  443. Multiple dimensions raise another question.&nbsp; How does one access the state
  444. of dimensions <i>other</i> than the current dimension?&nbsp; In other words,
  445. how does one access <code>i</code> at point <i>A</i>?&nbsp; Because of the
  446. preprocessor's lazy evaluation, this <i>doesn't</i> work....
  447. </div>
  448. <div class="code">
  449. <pre>
  450. // ...
  451. #elif BOOST_PP_ITERATION_DEPTH() == 1
  452. #define I BOOST_PP_ITERATION()
  453. #define BOOST_PP_ITERATION_PARAMS_2 (3, (1, 2, "file.h"))
  454. ??=include BOOST_PP_ITERATE()
  455. #undef I
  456. #elif BOOST_PP_ITERATION_DEPTH() == 2
  457. #define J BOOST_PP_ITERATION()
  458. // use I and J
  459. #undef I
  460. #endif
  461. </pre>
  462. </div>
  463. <div>
  464. The problem here is that <code>I</code> refers to <b>BOOST_PP_ITERATION</b>(),
  465. not to the <i>value</i> of <b>BOOST_PP_ITERATION</b>() at the point of <code>I</code>'s
  466. definition.
  467. </div>
  468. <div>
  469. The library provides macros to access these values in two ways--absolutely or
  470. relatively.&nbsp; The first variety accesses a value of a specific iteration
  471. frame (i.e. dimension).&nbsp; To access the iteration value of the first
  472. dimension--from <i>any</i> dimension--<b>BOOST_PP_FRAME_ITERATION</b>(<i>1</i>)
  473. is used.&nbsp; To access the iteration value of the second dimension, <b>BOOST_PP_FRAME_ITERATION</b>(<i>2</i>)
  474. is used, and so on.&nbsp;
  475. </div>
  476. <div>
  477. There are also frame versions to access the lower bound, the upper bound, and
  478. the flags of a dimension:&nbsp; <b>BOOST_PP_FRAME_START</b>, <b>BOOST_PP_FRAME_FINISH</b>,
  479. and <b>BOOST_PP_FRAME_FLAGS</b>.
  480. </div>
  481. <div>
  482. So, to fix the last example, we modify the definition of <code>I</code>....
  483. </div>
  484. <div class="code">
  485. <pre>
  486. // ...
  487. #elif BOOST_PP_ITERATION_DEPTH() == 1
  488. #define I BOOST_PP_FRAME_ITERATION(1)
  489. // ...
  490. </pre>
  491. </div>
  492. <div>
  493. The library also provides macros to access values in dimensions <i>relative</i>
  494. to the current dimension (e.g. the <i>previous</i> dimension).&nbsp; These
  495. macros take an argument that is interpreted as an offset from the current
  496. frame.&nbsp; For example, <b>BOOST_PP_RELATIVE_ITERATION</b>(<i>1</i>) always
  497. refers to the outer dimension immediately previous to the current
  498. dimension.&nbsp; An argument of <i>0</i> is interpreted as an offset of <i>0</i>
  499. which causes <b>BOOST_PP_RELATIVE_ITERATION</b>(<i>0</i>) to be equivalent to <b>BOOST_PP_ITERATION</b>().&nbsp;
  500. <b>BOOST_PP_RELATIVE_ITERATION</b>(<i>2</i>) refers to the iteration value of
  501. the dimension immediately preceding the dimension that precedes the current
  502. dimension.&nbsp;
  503. </div>
  504. <div>
  505. The lower and upper bounds of a dimension can be accessed in this fashion as
  506. well with <b>BOOST_PP_RELATIVE_START</b> and <b>BOOST_PP_RELATIVE_FINISH</b>.&nbsp;
  507. The flags of a relative dimension can be accessed with <b>BOOST_PP_RELATIVE_FLAGS</b>.
  508. </div>
  509. <h4>
  510. Relativity
  511. </h4>
  512. <div>
  513. I mentioned earlier that there is a reason that there are two ways to
  514. parametize the mechanism.&nbsp; The reason is dimensional abstraction.&nbsp; In
  515. certain situations the dimension is unknown by the code that is being
  516. iterated--possibly because the code is reused at multiple, different
  517. dimensions.&nbsp; If that code needs to iterate again, it has to define the
  518. right parameters (based on the dimension) for the mechanism to consume.&nbsp;
  519. </div>
  520. <div>
  521. All of the macro state maintained by the mechanism can be referred to in an
  522. indirect way relative to a dimension.&nbsp; This is the purpose of the <b>BOOST_PP_RELATIVE_</b>
  523. accessors.&nbsp;
  524. </div>
  525. <div>
  526. Likewise, the user-defined <i>named external arguments</i> can be defined this
  527. way as well--<i>except</i> the name of the file to iterate.&nbsp; Because the
  528. lower and upper boundaries are <i>evaluated</i> by the mechanism, the
  529. implementation no longer needs the macro <b>BOOST_PP_ITERATION_LIMITS</b>, and
  530. the identifier can be reused for each dimension of iteration.&nbsp;
  531. </div>
  532. <div>
  533. Unfortunately, the filename is a different story.&nbsp; The library has no way
  534. to evaluate the quoted (or angle-bracketed) text.&nbsp; Therefore, it has to
  535. use a different macro for each dimension.&nbsp; That is the purpose of the <b>BOOST_PP_FILENAME_<i>x</i></b>
  536. macros.&nbsp; They exist to isolate the only non-abstractable piece of data
  537. required by the mechanism.&nbsp;
  538. </div>
  539. <div>
  540. In order to define the filename in an abstract fashion, you need to do
  541. something like this:
  542. </div>
  543. <div class="code">
  544. <pre>
  545. #define UNIQUE_TO_FILE "some_file.h"
  546. #if BOOST_PP_ITERATION_DEPTH() == 0
  547. #define BOOST_PP_FILENAME_1 UNIQUE_TO_FILE
  548. #elif BOOST_PP_ITERATION_DEPTH() == 1
  549. #define BOOST_PP_FILENAME_2 UNIQUE_TO_FILE
  550. #elif BOOST_PP_ITERATION_DEPTH() == 2
  551. #define BOOST_PP_FILENAME_3 UNIQUE_TO_FILE
  552. // ... up to BOOST_PP_LIMIT_ITERATION_DIM
  553. #endif
  554. </pre>
  555. </div>
  556. <div>
  557. The intent is to avoid having to do this for anything but the filename.&nbsp;
  558. If this needs to be done more than once in a file (<b>BOOST_PP_FILENAME_<i>x</i></b>
  559. is undefined by the mechanism after it is used.), consider using a separate
  560. file to make the proper definition:
  561. </div>
  562. <div class="code">
  563. <pre>
  564. # // detail/define_file_h.h
  565. # ifndef FILE_H
  566. # error FILE_H is not defined
  567. # endif
  568. #
  569. # if BOOST_PP_ITERATION_DEPTH() == 0
  570. # define BOOST_PP_FILENAME_1 FILE_H
  571. # elif BOOST_PP_ITERATION_DEPTH() == 1
  572. # define BOOST_PP_FILENAME_2 FILE_H
  573. # elif BOOST_PP_ITERATION_DEPTH() == 2
  574. # define BOOST_PP_FILENAME_3 FILE_H
  575. # elif BOOST_PP_ITERATION_DEPTH() == 3
  576. # define BOOST_PP_FILENAME_4 FILE_H
  577. # elif BOOST_PP_ITERATION_DEPTH() == 4
  578. # define BOOST_PP_FILENAME_5 FILE_H
  579. # else
  580. # error unsupported iteration dimension
  581. # endif
  582. </pre>
  583. </div>
  584. <div>
  585. And then use it like this....
  586. </div>
  587. <div class="code">
  588. <pre>
  589. // file.h
  590. #if !BOOST_PP_IS_ITERATING
  591. #ifndef FILE_H
  592. #define FILE_H "file.h"
  593. #define BOOST_PP_ITERATION_LIMITS (1, 10)
  594. #include "detail/define_file_h.h"
  595. ??=include BOOST_PP_ITERATE()
  596. #endif // FILE_H
  597. #else
  598. // iterated portion
  599. #endif
  600. </pre>
  601. </div>
  602. <div>
  603. With a little effort like this, it is possible to maintain the abstraction
  604. without the code bloat that would otherwise be required.&nbsp; Unfortunately,
  605. this is not a completely general solution as it would need to be done for each
  606. unique filename, but it is better than nothing.
  607. </div>
  608. <h4>
  609. Conclusion
  610. </h4>
  611. <div>
  612. That about covers the facilities that are available from the mechanism.&nbsp;
  613. Using these facilities, let's implement a <code>function_traits</code> template
  614. to demonstrate a full-fledge use of the mechanism.
  615. </div>
  616. <h4>
  617. Function Traits - An Involved Example
  618. </h4>
  619. <div>
  620. Implementing a comprehensive <code>function_traits</code> template metafunction
  621. requires the use of every major part of the file iteration mechanism.&nbsp;
  622. </div>
  623. <div>
  624. (This example makes no attempt of work around compiler deficiencies and exists
  625. only to illustrate the mechanism.)
  626. </div>
  627. <div>
  628. The result should have the following features:
  629. </div>
  630. <ul>
  631. <li>
  632. return type</li>
  633. <li>
  634. number and types of parameters</li>
  635. <li>
  636. whether or not the type is a pointer-to-function, reference-to-function,
  637. pointer-to-member-function, or a plain function type</li>
  638. <li>
  639. whether the type has an ellipsis</li>
  640. <li>
  641. if not a pointer-to-member-function, the equivalent pointer-to-function,
  642. reference-to-function, and function type</li>
  643. <li>
  644. otherwise, the pointer-to-member type, the class type to which it refers, and
  645. whether it is const and/or volatile qualified</li>
  646. </ul>
  647. <div>
  648. There are a myriad of ways that this can be implemented.&nbsp; I'll give a
  649. brief summary here of what is happening in the implementation below.&nbsp;
  650. </div>
  651. <div>
  652. The implementation inherently has to deal with function arity.&nbsp; Therefore,
  653. at minimum, we need to iterate over function arities and define partial
  654. specializations of the primary template <code>function_traits</code>.&nbsp; The
  655. situation is further complicated by variadic functions (i.e. functions with an
  656. ellipsis).&nbsp; Therefore, for every arity, we need a variadic version as
  657. well.
  658. </div>
  659. <div>
  660. We also need to handle pointers-to-member-functions.&nbsp; This implies that we
  661. have to handle not just arity and variadics, but also cv-qualifications.&nbsp;
  662. </div>
  663. <div>
  664. For the sake of clarity, the implementation below handles function types and
  665. pointers-to-member-functions separately.&nbsp; They could be merged, but the
  666. result would be significantly messier.
  667. </div>
  668. <div>
  669. To handle function types, the implementation below iterates over function
  670. arities.&nbsp; For each arity, it iterates over each parameter to provide
  671. access to each individually.&nbsp; It then re-includes itself to define a
  672. variadic specialization of the same arity.&nbsp; It performs the rough
  673. equivalent of the following pseudo-code:
  674. </div>
  675. <div class="code">
  676. <pre>
  677. void make_spec(int i, bool variadic) {
  678. :open function_traits&lt;i, variadic&gt;
  679. for (int j = 0; j < i; ++j) {
  680. :parameter&lt;j&gt;
  681. }
  682. :close
  683. if (!variadic) {
  684. make_spec(i, true);
  685. }
  686. return;
  687. }
  688. void function_types(int max_arity) {
  689. for (int i = 0; i <= max_arity; ++i) {
  690. make_spec(i, false);
  691. }
  692. return;
  693. }
  694. </pre>
  695. </div>
  696. <div>
  697. The implementation of pointers-to-member-functions is a bit different.&nbsp;
  698. First, it iterates over cv-qualifiers.&nbsp; For each cv-qualifier, it iterates
  699. over function arities.&nbsp; For each function arity, it iterates again over
  700. each parameter.&nbsp; It then re-includes itself to define a variadic
  701. specialization of the same arity....
  702. </div>
  703. <div class="code">
  704. <pre>
  705. void make_spec(int j, const char* cv, bool variadic) {
  706. :open function_traits&lt;j, cv, variadic&gt;
  707. for (int k = 0; k < j; ++k) {
  708. parameter&lt;k&gt;
  709. }
  710. :close
  711. if (!variadic) {
  712. make_spec(j, cv, true);
  713. }
  714. return;
  715. }
  716. void gen_arities(const char* cv, int max_arity) {
  717. for (int j = 0; j <= max_arity; ++j) {
  718. make_spec(j, cv, false);
  719. }
  720. return;
  721. }
  722. void pointers_to_members(int max_arity) {
  723. static const char* cv_qualifiers[] = { "", "const", "volatile", "const volatile" };
  724. for (int i = 0; i < 4; ++i) {
  725. gen_arities(cv_qualifiers[i], max_arity);
  726. }
  727. return;
  728. }
  729. </pre>
  730. </div>
  731. <div>
  732. Here is the complete implementation.&nbsp; This example represents the power of
  733. the file iteration mechanism as well as the library in general, so follow it
  734. carefully if you wish to fully understand what the mechanism does....
  735. </div>
  736. <div class="code">
  737. <pre>
  738. // function_traits.hpp
  739. #if !BOOST_PP_IS_ITERATING
  740. #ifndef FUNCTION_TRAITS_HPP
  741. #define FUNCTION_TRAITS_HPP
  742. #include &lt;boost/preprocessor/cat.hpp&gt;
  743. #include &lt;boost/preprocessor/facilities/apply.hpp&gt;
  744. #include &lt;boost/preprocessor/iteration/iterate.hpp&gt;
  745. #include &lt;boost/preprocessor/iteration/self.hpp&gt;
  746. #include &lt;boost/preprocessor/repetition/enum_params.hpp&gt;
  747. #include &lt;boost/preprocessor/repetition/enum_trailing_params.hpp&gt;
  748. #include &lt;boost/preprocessor/tuple/elem.hpp&gt;
  749. // enable user-expansion
  750. #ifndef FUNCTION_TRAITS_MAX_ARITY
  751. #define FUNCTION_TRAITS_MAX_ARITY 15
  752. #endif
  753. namespace detail {
  754. // avoid replication of "default" values
  755. struct function_traits_base {
  756. static const bool is_plain = false;
  757. static const bool is_pointer = false;
  758. static const bool is_reference = false;
  759. static const bool is_member = false;
  760. };
  761. } // detail
  762. // no definition
  763. template&lt;class&gt; struct function_traits;
  764. // extract ellipsis state
  765. #define ELLIPSIS(n) \
  766. BOOST_PP_APPLY( \
  767. BOOST_PP_TUPLE_ELEM(2, n, ELLIPSIS_I) \
  768. ) \
  769. /**/
  770. // iterate over function arities for function types
  771. #define BOOST_PP_ITERATION_PARAMS_1 \
  772. (4, (0, FUNCTION_TRAITS_MAX_ARITY, "function_traits.hpp", 0)) \
  773. /**/
  774. ??=include BOOST_PP_ITERATE()
  775. // obtain a cv-qualifier by index
  776. #define QUALIFIER(n) \
  777. BOOST_PP_APPLY( \
  778. BOOST_PP_TUPLE_ELEM( \
  779. 4, n, \
  780. (BOOST_PP_NIL, (const), (volatile), (const volatile)) \
  781. ) \
  782. ) \
  783. /**/
  784. // iterate over cv-qualifiers for pointers-to-members
  785. #define BOOST_PP_ITERATION_PARAMS_1 \
  786. (4, (0, 3, "function_traits.hpp", 1)) \
  787. /**/
  788. ??=include BOOST_PP_ITERATE()
  789. // remove temporary macros
  790. #undef QUALIFIER
  791. #undef ELLIPSIS
  792. // overriding jumper for pointers-to-functions
  793. template&lt;class T&gt; struct function_traits&lt;T*&gt; : function_traits&lt;T&gt; {
  794. static const bool is_plain = false;
  795. static const bool is_pointer = true;
  796. };
  797. // overriding jumper for references-to-functions
  798. template&lt;class T&gt; struct function_traits&lt;T&amp;&gt; : function_traits&lt;T&gt; {
  799. static const bool is_plain = false;
  800. static const bool is_reference = true;
  801. };
  802. // eof
  803. #endif // FUNCTION_TRAITS_HPP
  804. // specializations for function types
  805. #elif BOOST_PP_ITERATION_DEPTH() == 1 \
  806. &amp;&amp; BOOST_PP_ITERATION_FLAGS() == 0 \
  807. /**/
  808. // define ellipsis state
  809. #if BOOST_PP_IS_SELFISH
  810. #define ELLIPSIS_I ((true), (...))
  811. #else
  812. #define ELLIPSIS_I ((false), BOOST_PP_NIL)
  813. #endif
  814. #define N BOOST_PP_ITERATION()
  815. template&lt;class R BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)&gt;
  816. struct function_traits&lt;R (BOOST_PP_ENUM_PARAMS(N, T) ELLIPSIS(1))&gt;
  817. : detail::function_traits_base {
  818. static const bool is_plain = true;
  819. typedef R function_type(BOOST_PP_ENUM_PARAMS(N, T) ELLIPSIS(1));
  820. typedef function_type* pointer_type;
  821. typedef function_type&amp; reference_type;
  822. static const bool has_ellipsis = ELLIPSIS(0);
  823. typedef R return_type;
  824. static const int parameter_count = N;
  825. template&lt;int, class D = int&gt; struct parameter;
  826. #if N
  827. // iterate over parameters
  828. #define BOOST_PP_ITERATION_PARAMS_2 \
  829. (3, (0, N - 1, "function_traits.hpp")) \
  830. /**/
  831. ??=include BOOST_PP_ITERATE()
  832. #endif
  833. };
  834. #undef N
  835. #undef ELLIPSIS_I
  836. // re-include this section for an ellipsis variant
  837. #if !BOOST_PP_IS_SELFISH
  838. #define BOOST_PP_INDIRECT_SELF "function_traits.hpp"
  839. ??=include BOOST_PP_INCLUDE_SELF()
  840. #endif
  841. // iteration over cv-qualifiers
  842. #elif BOOST_PP_ITERATION_DEPTH() == 1 \
  843. &amp;&amp; BOOST_PP_ITERATION_FLAGS() == 1 \
  844. /**/
  845. #define BOOST_PP_ITERATION_PARAMS_2 \
  846. (3, (0, FUNCTION_TRAITS_MAX_ARITY, "function_traits.hpp")) \
  847. /**/
  848. ??=include BOOST_PP_ITERATE()
  849. // generate specializations for pointers-to-members
  850. #elif BOOST_PP_ITERATION_DEPTH() == 2 \
  851. &amp;&amp; BOOST_PP_FRAME_FLAGS(1) == 1 \
  852. // define ellipsis state
  853. #if BOOST_PP_IS_SELFISH
  854. #define ELLIPSIS_I ((true), (...))
  855. #else
  856. #define ELLIPSIS_I ((false), BOOST_PP_NIL)
  857. #endif
  858. #define N BOOST_PP_ITERATION()
  859. #define Q QUALIFIER(BOOST_PP_FRAME_ITERATION(1))
  860. template&lt;class R, class O BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)&gt;
  861. struct function_traits&lt;R (O::*)(BOOST_PP_ENUM_PARAMS(N, T) ELLIPSIS(1)) Q&gt;
  862. : detail::function_traits_base {
  863. static const bool is_member = true;
  864. typedef R (O::* pointer_to_member_type)(BOOST_PP_ENUM_PARAMS(N, T) ELLIPSIS(1)) Q;
  865. typedef O class_type;
  866. typedef Q O qualified_class_type;
  867. static const bool has_ellipsis = ELLIPSIS(0);
  868. static const bool is_const =
  869. BOOST_PP_FRAME_ITERATION(1) == 1 || BOOST_PP_FRAME_ITERATION(1) == 3;
  870. static const bool is_volatile =
  871. BOOST_PP_FRAME_ITERATION(1) == 2 || BOOST_PP_FRAME_ITERATION(1) == 3;
  872. typedef R return_type;
  873. static const int parameter_count = N;
  874. template&lt;int, class D = int&gt; struct parameter;
  875. #if N
  876. // iterate over parameters
  877. #define BOOST_PP_ITERATION_PARAMS_3 \
  878. (3, (0, N - 1, "function_traits.hpp")) \
  879. /**/
  880. ??=include BOOST_PP_ITERATE()
  881. #endif
  882. };
  883. #undef Q
  884. #undef N
  885. #undef ELLIPSIS_I
  886. // re-include this section for an ellipsis variant
  887. #if !BOOST_PP_IS_SELFISH
  888. #define BOOST_PP_INDIRECT_SELF "function_traits.hpp"
  889. ??=include BOOST_PP_INCLUDE_SELF()
  890. #endif
  891. // parameter specializations
  892. #else
  893. #define X BOOST_PP_ITERATION()
  894. template&lt;class D&gt; struct parameter&lt;X, D&gt; {
  895. typedef BOOST_PP_CAT(T, X) type;
  896. };
  897. #undef X
  898. #endif
  899. </pre>
  900. </div>
  901. <div>
  902. One problem that still exists is the lack of support for <code>throw</code> specifications.&nbsp;
  903. There is no way that we can completely handle it anyway because we cannot
  904. partially specialize on <code>throw</code> specifications.&nbsp; However, we
  905. could accurately report the "actual" function type, etc., including the <code>throw</code>
  906. specification (which the above implementation doesn't do, as it reconstructs
  907. those types).&nbsp; If you like, you can figure out how to do that on your own
  908. as an exercise.&nbsp;
  909. </div>
  910. <h4>
  911. See Also
  912. </h4>
  913. <ul>
  914. <li>
  915. <a href="../ref/iterate.html">BOOST_PP_ITERATE</a></li>
  916. </ul>
  917. <div class="sig">
  918. - Paul Mensonides
  919. </div>
  920. <hr size="1">
  921. <div style="margin-left: 0px;">
  922. <i>© Copyright <a href="http://www.housemarque.com" target="_top">Housemarque Oy</a> 2002</i>
  923. </br><i>© Copyright Paul Mensonides 2002</i>
  924. </div>
  925. <div style="margin-left: 0px;">
  926. <p><small>Distributed under the Boost Software License, Version 1.0. (See
  927. accompanying file <a href="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
  928. copy at <a href=
  929. "http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)</small></p>
  930. </div>
  931. </body>
  932. </html>