vmd_generic.html 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Generic macros for working with data types</title>
  5. <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
  7. <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;The Variadic Macro Data Library 1.9">
  8. <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;The Variadic Macro Data Library 1.9">
  9. <link rel="prev" href="vmd_specific/vmd_identifying.html" title="Identifying data types">
  10. <link rel="next" href="vmd_generic/vmd_convert_sequence.html" title="Getting the type of data">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr>
  14. <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
  15. <td align="center"><a href="../../../../../index.html">Home</a></td>
  16. <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
  17. <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
  18. <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
  19. <td align="center"><a href="../../../../../more/index.htm">More</a></td>
  20. </tr></table>
  21. <hr>
  22. <div class="spirit-nav">
  23. <a accesskey="p" href="vmd_specific/vmd_identifying.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="vmd_generic/vmd_convert_sequence.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  27. <a name="variadic_macro_data.vmd_generic"></a><a class="link" href="vmd_generic.html" title="Generic macros for working with data types">Generic macros for working
  28. with data types</a>
  29. </h2></div></div></div>
  30. <div class="toc"><dl class="toc">
  31. <dt><span class="section"><a href="vmd_generic.html#variadic_macro_data.vmd_generic.vmd_sequence">Parsing
  32. sequences</a></span></dt>
  33. <dd><dl>
  34. <dt><span class="section"><a href="vmd_generic.html#variadic_macro_data.vmd_generic.vmd_sequence.vmd_sequence_convert">Converting
  35. sequences</a></span></dt>
  36. <dt><span class="section"><a href="vmd_generic.html#variadic_macro_data.vmd_generic.vmd_sequence.vmd_sequence_access">Accessing
  37. a sequence element</a></span></dt>
  38. </dl></dd>
  39. <dt><span class="section"><a href="vmd_generic/vmd_convert_sequence.html">Getting
  40. the type of data</a></span></dt>
  41. <dt><span class="section"><a href="vmd_generic/vmd_assert.html">Testing for
  42. equality and inequality</a></span></dt>
  43. </dl></div>
  44. <p>
  45. Besides the specific macros for working with data types VMD has a number of
  46. generic macros for parsing sequences.
  47. </p>
  48. <div class="section">
  49. <div class="titlepage"><div><div><h3 class="title">
  50. <a name="variadic_macro_data.vmd_generic.vmd_sequence"></a><a class="link" href="vmd_generic.html#variadic_macro_data.vmd_generic.vmd_sequence" title="Parsing sequences">Parsing
  51. sequences</a>
  52. </h3></div></div></div>
  53. <div class="toc"><dl class="toc">
  54. <dt><span class="section"><a href="vmd_generic.html#variadic_macro_data.vmd_generic.vmd_sequence.vmd_sequence_convert">Converting
  55. sequences</a></span></dt>
  56. <dt><span class="section"><a href="vmd_generic.html#variadic_macro_data.vmd_generic.vmd_sequence.vmd_sequence_access">Accessing
  57. a sequence element</a></span></dt>
  58. </dl></div>
  59. <p>
  60. In the normal use of Boost PP data is passed as arguments to a macro in discrete
  61. units so that each parameter expects a single data type. A typical macro
  62. might be:
  63. </p>
  64. <pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">AMACRO</span><span class="special">(</span><span class="identifier">anumber</span><span class="special">,</span><span class="identifier">atuple</span><span class="special">,</span><span class="identifier">anidentifier</span><span class="special">)</span> <span class="identifier">someoutput</span>
  65. </pre>
  66. <p>
  67. where the 'atuple', having the form of ( data1, data2, data3 ), itself may
  68. contain different data types of elements.
  69. </p>
  70. <p>
  71. This is the standard macro design and internally it is the easiest way to
  72. pass macro data back and forth. The Boost PP library has a rich set of functionality
  73. to deal with all of its high-level data types, and variadic data, with its
  74. own simpler functionality, also offers another alternative to representing
  75. data.
  76. </p>
  77. <p>
  78. Occasionally designers of macros, especially for the use of others programmers
  79. within a particular library, have expressed the need for a macro parameter
  80. to allow a more C/C++ like syntax where a single parameter might mimic a
  81. C++ function-call or a C-like type modification syntax, or some other more
  82. complicated construct. Something along the lines of:
  83. </p>
  84. <pre class="programlisting"><span class="identifier">areturn</span> <span class="identifier">afunction</span> <span class="special">(</span> <span class="identifier">aparameter1</span><span class="special">,</span> <span class="identifier">aparameter2</span><span class="special">,</span> <span class="identifier">aparameter3</span> <span class="special">)</span>
  85. </pre>
  86. <p>
  87. or
  88. </p>
  89. <pre class="programlisting"><span class="special">(</span> <span class="identifier">type</span> <span class="special">)</span> <span class="identifier">data</span>
  90. </pre>
  91. <p>
  92. etc. etc.
  93. </p>
  94. <p>
  95. In other words, from a syntactical level when designing possible macro input,
  96. is it possible to design parameter data to look more like C/C++ when macros
  97. are used in a library and still do a certain amount of preprocessor metaprogramming
  98. with such mixed token input ?
  99. </p>
  100. <p>
  101. VMD has functionality which allows more than one type of preprocessing token,
  102. excluding an 'empty' token which always refers to some entire input, to be
  103. part of a single parameter of input data. These preprocessing tokens as a
  104. single parameter are syntactically a consecutive series of data. The single
  105. limitation of this consecutive series of data is that each top-level part
  106. of the data of this series is of some VMD data type. What this means is that
  107. if some input consists of a series of data types it is possible to extract
  108. the data for each data type in that series.
  109. </p>
  110. <p>
  111. In practicality what this means is that, given the examples just above, if
  112. 'areturn', 'afunction', and 'data' are identifiers it would be possible to
  113. parse either of the two inputs above so that one could identify the different
  114. data types involved and do preprocessor metaprogramming based on those results.
  115. </p>
  116. <h5>
  117. <a name="variadic_macro_data.vmd_generic.vmd_sequence.h0"></a>
  118. <span class="phrase"><a name="variadic_macro_data.vmd_generic.vmd_sequence.sequence_definition"></a></span><a class="link" href="vmd_generic.html#variadic_macro_data.vmd_generic.vmd_sequence.sequence_definition">Sequence
  119. definition</a>
  120. </h5>
  121. <p>
  122. I will be calling such input data, which consists of all top-level data types
  123. in a series, by the term of a 'sequence'. Each separate data type in the
  124. sequence is called an 'element'. In this definition of a 'sequence' we can
  125. have 0 or more elements, so that a sequence is a general name for any VMD
  126. input. A sequence is therefore any input VMD can parse, whether it is emptiness,
  127. a single element, or more than one element in a series. Therefore when we
  128. speak of VMD macros parsing input data we are really speaking of VMD macros
  129. parsing a sequence. A sequence can therefore also be part of a Boost PP composite
  130. data type, or variadic data, and VMD can still parse such an embedded sequence
  131. if asked to do so.
  132. </p>
  133. <h5>
  134. <a name="variadic_macro_data.vmd_generic.vmd_sequence.h1"></a>
  135. <span class="phrase"><a name="variadic_macro_data.vmd_generic.vmd_sequence.sequence_parsing"></a></span><a class="link" href="vmd_generic.html#variadic_macro_data.vmd_generic.vmd_sequence.sequence_parsing">Sequence
  136. parsing</a>
  137. </h5>
  138. <p>
  139. Parsing a sequence means that VMD can step through each element of a sequence
  140. sequentially, determine the type and data of each element, then move on to
  141. the next element. Parsing is sequential and can only be done in a forward
  142. direction, but it can be done any number of times. In C++ iterator terms
  143. parsing of a sequence is a forward iterator.
  144. </p>
  145. <p>
  146. Working with a sequence is equivalent to using VMD macros 'generically'.
  147. </p>
  148. <p>
  149. Before I give an explanation of how to use a sequence using VMD generic functionality
  150. I would like to make two points:
  151. </p>
  152. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  153. <li class="listitem">
  154. The possibility of working with a sequence which contains more than one
  155. data type can be easily abused. In general keeping things simple is usually
  156. better than making things overly complicated when it comes to the syntactical
  157. side of things in a computer language. A macro parameter syntactical
  158. possibility has to be understandable to be used.
  159. </li>
  160. <li class="listitem">
  161. Using VMD to parse the individual data types of a sequence takes more
  162. preprocessing time than functionality offered with Boost PP data types,
  163. because it is based on forward access through each top-level type of
  164. the sequence.
  165. </li>
  166. </ul></div>
  167. <p>
  168. The one constraint in a sequence is that the top-level must consist of VMD
  169. data types, in other words preprocessor tokens which VMD understands. By
  170. top-level it is meant that a Boost PP composite data may have elements which
  171. VMD cannot parse but as long as the input consists of the composite data
  172. types and not the inner unparsable elements, VMD can parse the input. Therefore
  173. if preprocessor data is one of the examples above, you will be successful
  174. in using VMD. However if your preprocessor data takes the form of:
  175. </p>
  176. <pre class="programlisting"><span class="special">&amp;</span><span class="identifier">name</span> <span class="identifier">identifier</span> <span class="special">(</span> <span class="identifier">param</span> <span class="special">)</span>
  177. </pre>
  178. <p>
  179. or
  180. </p>
  181. <pre class="programlisting"><span class="identifier">identifier</span> <span class="string">"string literal"</span>
  182. </pre>
  183. <p>
  184. or
  185. </p>
  186. <pre class="programlisting"><span class="identifier">identifier</span> <span class="special">+</span> <span class="identifier">number</span>
  187. </pre>
  188. <p>
  189. or
  190. </p>
  191. <pre class="programlisting"><span class="identifier">identifier</span> <span class="special">+=</span> <span class="number">4.3</span>
  192. </pre>
  193. <p>
  194. etc. etc.
  195. </p>
  196. <p>
  197. you will not be able to parse the data using VMD since '&amp;', "string
  198. literal", '+', '+=', and "4.3" are preprocessor tokens which
  199. are not VMD top-level data types and therefore VMD cannot handle them at
  200. the parsing level. You can still of course pass such data as preprocessing
  201. input to macros but you cannot use VMD to recognize the parts of such data.
  202. </p>
  203. <p>
  204. This is similar to the fact that VMD cannot tell you what type preprocessor
  205. data is as a whole, using any of the VMD identifying macros already discussed,
  206. if the type is not one that VMD can handle.
  207. </p>
  208. <p>
  209. On the other hand you can still use VMD to parse such tokens in the input
  210. if you use Boost PP data types as top-level data types to do so. Such as:
  211. </p>
  212. <pre class="programlisting"><span class="special">(</span> <span class="special">&amp;</span><span class="identifier">name</span> <span class="special">)</span> <span class="identifier">identifier</span> <span class="special">(</span> <span class="identifier">param</span> <span class="special">)</span>
  213. </pre>
  214. <p>
  215. or
  216. </p>
  217. <pre class="programlisting"><span class="identifier">identifier</span> <span class="special">(</span> <span class="string">"string literal"</span> <span class="special">)</span>
  218. </pre>
  219. <p>
  220. or
  221. </p>
  222. <pre class="programlisting"><span class="identifier">identifier</span> <span class="special">(</span> <span class="special">+</span> <span class="special">)</span> <span class="identifier">number</span>
  223. </pre>
  224. <p>
  225. or
  226. </p>
  227. <pre class="programlisting"><span class="identifier">identifier</span> <span class="special">(</span> <span class="special">+=</span> <span class="special">)</span> <span class="number">4</span> <span class="special">(</span> <span class="special">.</span> <span class="special">)</span> <span class="number">3</span>
  228. </pre>
  229. <p>
  230. The succeeding topics explain the VMD functionality for parsing a sequence
  231. for each individual VMD data type in that sequence.
  232. </p>
  233. <h5>
  234. <a name="variadic_macro_data.vmd_generic.vmd_sequence.h2"></a>
  235. <span class="phrase"><a name="variadic_macro_data.vmd_generic.vmd_sequence.sequence_types"></a></span><a class="link" href="vmd_generic.html#variadic_macro_data.vmd_generic.vmd_sequence.sequence_types">Sequence
  236. types</a>
  237. </h5>
  238. <p>
  239. A VMD sequence can be seen as one of either three general types:
  240. </p>
  241. <div class="orderedlist"><ol class="orderedlist" type="1">
  242. <li class="listitem">
  243. An empty sequence
  244. </li>
  245. <li class="listitem">
  246. A single element sequence
  247. </li>
  248. <li class="listitem">
  249. A multi-element sequence
  250. </li>
  251. </ol></div>
  252. <p>
  253. An empty sequence is merely input that is empty, what VMD calls "emptiness".
  254. Use the previously explained BOOST_VMD_IS_EMPTY macro to test for an empty
  255. sequence.
  256. </p>
  257. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="identifier">is_empty</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  258. <span class="preprocessor">#define</span> <span class="identifier">AN_EMPTY_SEQUENCE</span>
  259. <span class="identifier">BOOST_VMD_IS_EMPTY</span><span class="special">(</span><span class="identifier">AN_EMPTY_SEQUENCE</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="number">1</span>
  260. </pre>
  261. <p>
  262. The type of an empty sequence is BOOST_VMD_TYPE_EMPTY.
  263. </p>
  264. <p>
  265. A single element sequence is a single VMD data type. This is what we have
  266. been previously discussing as data which VMD can parse in this documentation
  267. with our identifying macros. You can use the BOOST_VMD_IS_UNARY macro to
  268. test for a single element sequence.
  269. </p>
  270. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="identifier">is_unary</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  271. <span class="preprocessor">#define</span> <span class="identifier">A_SINGLE_ELEMENT_SEQUENCE</span> <span class="special">(</span><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">)</span>
  272. <span class="identifier">BOOST_VMD_IS_UNARY</span><span class="special">(</span><span class="identifier">A_SINGLE_ELEMENT_SEQUENCE</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="number">1</span>
  273. </pre>
  274. <p>
  275. The type of a single element sequence is the type of the individual data
  276. type. In our example above the type of A_SINGLE_ELEMENT_SEQUENCE is BOOST_VMD_TYPE_TUPLE.
  277. </p>
  278. <p>
  279. A multi-element sequence consists of more than one data type. This is the
  280. "new" type which VMD can parse. You can use the BOOST_VMD_IS_MULTI
  281. macro to test for a multi-element sequence.
  282. </p>
  283. <pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">A_MULTI_ELEMENT_SEQUENCE</span> <span class="special">(</span><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">)</span> <span class="special">(</span><span class="number">1</span><span class="special">)(</span><span class="number">2</span><span class="special">)</span> <span class="number">45</span>
  284. </pre>
  285. <p>
  286. The A_MULTI_ELEMENT_SEQUENCE consists of a tuple followed by a seq followed
  287. by a number.
  288. </p>
  289. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="identifier">is_multi</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  290. <span class="identifier">BOOST_VMD_IS_MULTI</span><span class="special">(</span><span class="identifier">A_MULTI_ELEMENT_SEQUENCE</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="number">1</span>
  291. </pre>
  292. <p>
  293. The type of a multi-element sequence is always BOOST_VMD_TYPE_SEQUENCE.
  294. </p>
  295. <p>
  296. The type of a sequence can be obtained generically with the BOOST_VMD_GET_TYPE
  297. macro. We will be explaining this further in the documentation.
  298. </p>
  299. <h5>
  300. <a name="variadic_macro_data.vmd_generic.vmd_sequence.h3"></a>
  301. <span class="phrase"><a name="variadic_macro_data.vmd_generic.vmd_sequence.sequence_size"></a></span><a class="link" href="vmd_generic.html#variadic_macro_data.vmd_generic.vmd_sequence.sequence_size">Sequence
  302. size</a>
  303. </h5>
  304. <p>
  305. The size of any sequence can be accessed using the BOOST_VMD_SIZE macro.
  306. For an empty sequence the size is always 0. For a single element sequence
  307. the size is always 1. For a multi-element sequence the size is the number
  308. of individual top-level data types in the sequence.
  309. </p>
  310. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="identifier">size</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  311. <span class="identifier">BOOST_VMD_SIZE</span><span class="special">(</span><span class="identifier">AN_EMPTY_SEQUENCE</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="number">0</span>
  312. <span class="identifier">BOOST_VMD_SIZE</span><span class="special">(</span><span class="identifier">A_SINGLE_ELEMENT_SEQUENCE</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="number">1</span>
  313. <span class="identifier">BOOST_VMD_SIZE</span><span class="special">(</span><span class="identifier">A_MULTI_ELEMENT_SEQUENCE</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="number">3</span>
  314. </pre>
  315. <h5>
  316. <a name="variadic_macro_data.vmd_generic.vmd_sequence.h4"></a>
  317. <span class="phrase"><a name="variadic_macro_data.vmd_generic.vmd_sequence.using_vmd_to_parse_sequence_inpu"></a></span><a class="link" href="vmd_generic.html#variadic_macro_data.vmd_generic.vmd_sequence.using_vmd_to_parse_sequence_inpu">Using
  318. VMD to parse sequence input</a>
  319. </h5>
  320. <p>
  321. For a VMD sequence essentially two ways of parsing into individual data types
  322. are offered by the VMD library:
  323. </p>
  324. <div class="orderedlist"><ol class="orderedlist" type="1">
  325. <li class="listitem">
  326. The sequence can be converted to any of the Boost PP data types, or to
  327. variadic data, where each individual data type in the sequence becomes
  328. a separate element of the particular composite data type chosen. The
  329. conversion to a particular Boost PP data type or variadic data is slow,
  330. because it is based on forward access through each top-level type of
  331. the sequence, but afterwards accessing any individual element is as fast
  332. as accessing any element in the Boost PP data type or among variadic
  333. data.
  334. </li>
  335. <li class="listitem">
  336. The sequence can be accessed directly through its individual elements.
  337. This is slower than accessing an element of a Boost PP data type or variadic
  338. data but offers conceptual access to the original sequence as a series
  339. of elements.
  340. </li>
  341. </ol></div>
  342. <p>
  343. These two techniques will be discussed in succeeding topics.
  344. </p>
  345. <div class="section">
  346. <div class="titlepage"><div><div><h4 class="title">
  347. <a name="variadic_macro_data.vmd_generic.vmd_sequence.vmd_sequence_convert"></a><a class="link" href="vmd_generic.html#variadic_macro_data.vmd_generic.vmd_sequence.vmd_sequence_convert" title="Converting sequences">Converting
  348. sequences</a>
  349. </h4></div></div></div>
  350. <p>
  351. The easiest way to work with a sequence is to convert it to a Boost PP
  352. data type. Likewise you can also convert a sequence to variadic data even
  353. though the Boost PP data types have much greater functionality than variadic
  354. data in Boost PP.
  355. </p>
  356. <p>
  357. To convert a sequence to a Boost PP data type or variadic data the macros
  358. to be used are:
  359. </p>
  360. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  361. <li class="listitem">
  362. BOOST_VMD_TO_ARRAY(sequence) to convert the sequence to an array
  363. </li>
  364. <li class="listitem">
  365. BOOST_VMD_TO_LIST(sequence) to convert the sequence to a list
  366. </li>
  367. <li class="listitem">
  368. BOOST_VMD_TO_SEQ(sequence) to convert the sequence to a seq
  369. </li>
  370. <li class="listitem">
  371. BOOST_VMD_TO_TUPLE(sequence) to convert the sequence to a tuple
  372. </li>
  373. <li class="listitem">
  374. BOOST_VMD_ENUM(sequence) to convert the sequence to variadic data
  375. </li>
  376. </ul></div>
  377. <p>
  378. After the conversion the elements of a sequence become the elements of
  379. the corresponding composite data type.
  380. </p>
  381. <p>
  382. Once the elements of the sequence have been converted to the elements of
  383. the composite data type the full power of that composite data type can
  384. be used to process each element. Furthermore the programmer can use VMD
  385. to discover the type of an individual element for further processing.
  386. </p>
  387. <p>
  388. For single element sequences the result is always a single element composite
  389. data type. For multi-element sequences the result is always a composite
  390. data type of more than one element.
  391. </p>
  392. <p>
  393. For a sequence that is empty the result is emptiness when converting to
  394. a seq, tuple, or variadic data; the result is an empty array or list when
  395. converting to each of those composite data types respectively.
  396. </p>
  397. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="keyword">enum</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  398. <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="identifier">to_array</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  399. <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="identifier">to_list</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  400. <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="identifier">to_seq</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  401. <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="identifier">to_tuple</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  402. <span class="preprocessor">#define</span> <span class="identifier">BOOST_VMD_REGISTER_ANID</span> <span class="special">(</span><span class="identifier">ANID</span><span class="special">)</span>
  403. <span class="preprocessor">#define</span> <span class="identifier">SEQUENCE_EMPTY</span>
  404. <span class="preprocessor">#define</span> <span class="identifier">SEQUENCE_SINGLE</span> <span class="number">35</span>
  405. <span class="preprocessor">#define</span> <span class="identifier">SEQUENCE_SINGLE_2</span> <span class="identifier">ANID</span>
  406. <span class="preprocessor">#define</span> <span class="identifier">SEQUENCE_MULTI</span> <span class="special">(</span><span class="number">0</span><span class="special">,</span><span class="number">1</span><span class="special">)</span> <span class="special">(</span><span class="number">2</span><span class="special">)(</span><span class="number">3</span><span class="special">)(</span><span class="number">4</span><span class="special">)</span>
  407. <span class="preprocessor">#define</span> <span class="identifier">SEQUENCE_MULTI_2</span> <span class="identifier">BOOST_VMD_TYPE_SEQ</span> <span class="special">(</span><span class="number">2</span><span class="special">,(</span><span class="number">5</span><span class="special">,</span><span class="number">6</span><span class="special">))</span>
  408. <span class="identifier">BOOST_VMD_TO_ARRAY</span><span class="special">(</span><span class="identifier">SEQUENCE_EMPTY</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="identifier">an</span> <span class="identifier">empty</span> <span class="identifier">array</span> <span class="char">'(0,())'</span>
  409. <span class="identifier">BOOST_VMD_TO_LIST</span><span class="special">(</span><span class="identifier">SEQUENCE_SINGLE</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="identifier">a</span> <span class="identifier">one</span><span class="special">-</span><span class="identifier">element</span> <span class="identifier">list</span> <span class="char">'(35,BOOST_PP_NIL)'</span>
  410. <span class="identifier">BOOST_VMD_TO_SEQ</span><span class="special">(</span><span class="identifier">SEQUENCE_SINGLE_2</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="identifier">a</span> <span class="identifier">one</span><span class="special">-</span><span class="identifier">element</span> <span class="identifier">seq</span> <span class="char">'(ANID)'</span>
  411. <span class="identifier">BOOST_VMD_TO_TUPLE</span><span class="special">(</span><span class="identifier">SEQUENCE_MULTI</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="identifier">a</span> <span class="identifier">multi</span><span class="special">-</span><span class="identifier">element</span> <span class="identifier">tuple</span> <span class="char">'((0,1),(2)(3)(4))'</span>
  412. <span class="identifier">BOOST_VMD_ENUM</span><span class="special">(</span><span class="identifier">SEQUENCE_MULTI_2</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="identifier">multi</span><span class="special">-</span><span class="identifier">element</span> <span class="identifier">variadic</span> <span class="identifier">data</span> <span class="char">'BOOST_VMD_TYPE_SEQ,(2,(5,6))'</span>
  413. </pre>
  414. <h6>
  415. <a name="variadic_macro_data.vmd_generic.vmd_sequence.vmd_sequence_convert.h0"></a>
  416. <span class="phrase"><a name="variadic_macro_data.vmd_generic.vmd_sequence.vmd_sequence_convert.usage"></a></span><a class="link" href="vmd_generic.html#variadic_macro_data.vmd_generic.vmd_sequence.vmd_sequence_convert.usage">Usage</a>
  417. </h6>
  418. <p>
  419. You can use the general header file:
  420. </p>
  421. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  422. </pre>
  423. <p>
  424. or you can use individual header files for each of these macros. The individual
  425. header files are:
  426. </p>
  427. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="identifier">to_array</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="comment">// for the BOOST_VMD_TO_ARRAY macro</span>
  428. <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="identifier">to_list</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="comment">// for the BOOST_VMD_TO_LIST macro</span>
  429. <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="identifier">to_seq</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="comment">// for the BOOST_VMD_TO_SEQ macro</span>
  430. <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="identifier">to_tuple</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="comment">// for the BOOST_VMD_TO_TUPLE macro.</span>
  431. <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="keyword">enum</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="comment">// for the BOOST_VMD_ENUM macro.</span>
  432. </pre>
  433. </div>
  434. <div class="section">
  435. <div class="titlepage"><div><div><h4 class="title">
  436. <a name="variadic_macro_data.vmd_generic.vmd_sequence.vmd_sequence_access"></a><a class="link" href="vmd_generic.html#variadic_macro_data.vmd_generic.vmd_sequence.vmd_sequence_access" title="Accessing a sequence element">Accessing
  437. a sequence element</a>
  438. </h4></div></div></div>
  439. <p>
  440. It is possible to access an individual element of a sequence. The macro
  441. to do this is called BOOST_VMD_ELEM. The macro takes two required parameters.
  442. The required parameters are the element number to access and the sequence,
  443. in that order. The element number is a 0-based number and its maximum value
  444. should be one less than the size of the sequence.
  445. </p>
  446. <p>
  447. The BOOST_VMD_ELEM macro returns the actual sequence element. If the first
  448. required parameter is greater or equal to the size of the sequence the
  449. macro returns emptiness. Because of this using BOOST_VMD_ELEM on an empty
  450. sequence, whose size is 0, always returns emptiness.
  451. </p>
  452. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="identifier">elem</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  453. <span class="preprocessor">#define</span> <span class="identifier">BOOST_VMD_REGISTER_ANAME</span> <span class="special">(</span><span class="identifier">ANAME</span><span class="special">)</span>
  454. <span class="preprocessor">#define</span> <span class="identifier">A_SEQUENCE</span> <span class="special">(</span><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">,</span><span class="number">3</span><span class="special">)</span> <span class="number">46</span> <span class="special">(</span><span class="identifier">list_data1</span><span class="special">,(</span><span class="identifier">list_data2</span><span class="special">,</span><span class="identifier">BOOST_PP_NIL</span><span class="special">))</span> <span class="identifier">BOOST_VMD_TYPE_SEQ</span> <span class="identifier">ANAME</span>
  455. <span class="preprocessor">#define</span> <span class="identifier">AN_EMPTY_SEQUENCE</span>
  456. <span class="identifier">BOOST_VMD_ELEM</span><span class="special">(</span><span class="number">0</span><span class="special">,</span><span class="identifier">A_SEQUENCE</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="special">(</span><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">,</span><span class="number">3</span><span class="special">)</span>
  457. <span class="identifier">BOOST_VMD_ELEM</span><span class="special">(</span><span class="number">1</span><span class="special">,</span><span class="identifier">A_SEQUENCE</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="number">46</span>
  458. <span class="identifier">BOOST_VMD_ELEM</span><span class="special">(</span><span class="number">2</span><span class="special">,</span><span class="identifier">A_SEQUENCE</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="special">(</span><span class="identifier">list_data1</span><span class="special">,(</span><span class="identifier">list_data2</span><span class="special">,</span><span class="identifier">BOOST_PP_NIL</span><span class="special">))</span>
  459. <span class="identifier">BOOST_VMD_ELEM</span><span class="special">(</span><span class="number">3</span><span class="special">,</span><span class="identifier">A_SEQUENCE</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="identifier">BOOST_VMD_TYPE_SEQ</span>
  460. <span class="identifier">BOOST_VMD_ELEM</span><span class="special">(</span><span class="number">4</span><span class="special">,</span><span class="identifier">A_SEQUENCE</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="identifier">ANAME</span>
  461. <span class="identifier">BOOST_VMD_ELEM</span><span class="special">(</span><span class="number">5</span><span class="special">,</span><span class="identifier">A_SEQUENCE</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="identifier">emptiness</span>
  462. <span class="identifier">BOOST_VMD_ELEM</span><span class="special">(</span><span class="number">0</span><span class="special">,</span><span class="identifier">AN_EMPTY_SEQUENCE</span><span class="special">)</span> <span class="identifier">will</span> <span class="keyword">return</span> <span class="identifier">emptiness</span>
  463. </pre>
  464. <p>
  465. Accessing an element of a sequence directly is slower than accessing an
  466. element of a Boost PP data type or even variadic data, since each access
  467. has to directly cycle through each element of the sequence to get to the
  468. one being accessed. The process of sequentially parsing each element again
  469. each time is slower than accessing a Boost PP data type element.
  470. </p>
  471. <h6>
  472. <a name="variadic_macro_data.vmd_generic.vmd_sequence.vmd_sequence_access.h0"></a>
  473. <span class="phrase"><a name="variadic_macro_data.vmd_generic.vmd_sequence.vmd_sequence_access.usage"></a></span><a class="link" href="vmd_generic.html#variadic_macro_data.vmd_generic.vmd_sequence.vmd_sequence_access.usage">Usage</a>
  474. </h6>
  475. <p>
  476. You can use the general header file:
  477. </p>
  478. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  479. </pre>
  480. <p>
  481. or you can use the individual header file:
  482. </p>
  483. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">/</span><span class="identifier">elem</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  484. </pre>
  485. </div>
  486. </div>
  487. </div>
  488. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  489. <td align="left"></td>
  490. <td align="right"><div class="copyright-footer">Copyright &#169; 2010-2017 Tropic Software
  491. East Inc</div></td>
  492. </tr></table>
  493. <hr>
  494. <div class="spirit-nav">
  495. <a accesskey="p" href="vmd_specific/vmd_identifying.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="vmd_generic/vmd_convert_sequence.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  496. </div>
  497. </body>
  498. </html>