1 | <html> |
---|
2 | <head> |
---|
3 | <!-- Generated by the Spirit (http://spirit.sf.net) QuickDoc --> |
---|
4 | <title>Tuples</title> |
---|
5 | <link rel="stylesheet" href="theme/style.css" type="text/css"> |
---|
6 | <link rel="prev" href="inside_phoenix.html"> |
---|
7 | <link rel="next" href="actors_revisited.html"> |
---|
8 | </head> |
---|
9 | <body> |
---|
10 | <table width="100%" height="48" border="0" background="theme/bkd2.gif" cellspacing="2"> |
---|
11 | <tr> |
---|
12 | <td width="10"> |
---|
13 | </td> |
---|
14 | <td width="85%"> |
---|
15 | <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>Tuples</b></font> |
---|
16 | </td> |
---|
17 | <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" align="right" border="0"></a></td> |
---|
18 | </tr> |
---|
19 | </table> |
---|
20 | <br> |
---|
21 | <table border="0"> |
---|
22 | <tr> |
---|
23 | <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td> |
---|
24 | <td width="30"><a href="inside_phoenix.html"><img src="theme/l_arr.gif" border="0"></a></td> |
---|
25 | <td width="20"><a href="actors_revisited.html"><img src="theme/r_arr.gif" border="0"></a></td> |
---|
26 | </tr> |
---|
27 | </table> |
---|
28 | <p> |
---|
29 | Tuples are the most basic infrastructure that the framework builds with. This sub-library provides a mechanism to bundle objects of arbitrary types in a single structure. Tuples hold heterogeneous types up to a predefined maximum.</p> |
---|
30 | <p> |
---|
31 | Only the most basic functionality needed are provided. This is a straight-forward and extremely lean and mean library. Unlike other recursive list-like tuple implementations, this tuple library implementation uses simple structs similar to std::pair with specialization for 0 to N tuple elements, where N is a predefined constant. There are only 4 tuple operations to learn:</p> |
---|
32 | <p> |
---|
33 | 1) Construction</p> |
---|
34 | <p> |
---|
35 | Here are examples on how to construct tuples:</p> |
---|
36 | <code><pre> |
---|
37 | <span class=keyword>typedef </span><span class=identifier>tuple</span><span class=special><</span><span class=keyword>int</span><span class=special>, </span><span class=keyword>char</span><span class=special>> </span><span class=identifier>t1_t</span><span class=special>; |
---|
38 | </span><span class=keyword>typedef </span><span class=identifier>tuple</span><span class=special><</span><span class=keyword>int</span><span class=special>, </span><span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span><span class=special>, </span><span class=keyword>double</span><span class=special>> </span><span class=identifier>t2_t</span><span class=special>; |
---|
39 | |
---|
40 | // </span><span class=keyword>this </span><span class=identifier>tuple </span><span class=identifier>has </span><span class=identifier>an </span><span class=keyword>int </span><span class=keyword>and </span><span class=keyword>char </span><span class=identifier>members |
---|
41 | </span><span class=identifier>t1_t </span><span class=identifier>t1</span><span class=special>(</span><span class=number>3</span><span class=special>, </span><span class=literal>'c'</span><span class=special>); |
---|
42 | |
---|
43 | // </span><span class=keyword>this </span><span class=identifier>tuple </span><span class=identifier>has </span><span class=identifier>an </span><span class=keyword>int</span><span class=special>, </span><span class=identifier>std</span><span class=special>::</span><span class=identifier>string </span><span class=keyword>and </span><span class=keyword>double </span><span class=identifier>members |
---|
44 | </span><span class=identifier>t2_t </span><span class=identifier>t2</span><span class=special>(</span><span class=number>3</span><span class=special>, </span><span class=string>"hello"</span><span class=special>, </span><span class=number>3.14</span><span class=special>); |
---|
45 | </span></pre></code> |
---|
46 | <p> |
---|
47 | 2) Member access</p> |
---|
48 | <p> |
---|
49 | A member in a tuple can be accessed using the tuple's operator by specifying the Nth tuple_index. Here are some examples:</p> |
---|
50 | <code><pre> |
---|
51 | <span class=identifier>tuple_index</span><span class=special><</span><span class=number>0</span><span class=special>> </span><span class=identifier>ix0</span><span class=special>; // </span><span class=number>0</span><span class=identifier>th </span><span class=identifier>index </span><span class=special>== </span><span class=number>1</span><span class=identifier>st </span><span class=identifier>item |
---|
52 | </span><span class=identifier>tuple_index</span><span class=special><</span><span class=number>1</span><span class=special>> </span><span class=identifier>ix1</span><span class=special>; // </span><span class=number>1</span><span class=identifier>st </span><span class=identifier>index </span><span class=special>== </span><span class=number>2</span><span class=identifier>nd </span><span class=identifier>item |
---|
53 | </span><span class=identifier>tuple_index</span><span class=special><</span><span class=number>2</span><span class=special>> </span><span class=identifier>ix2</span><span class=special>; // </span><span class=number>2</span><span class=identifier>nd </span><span class=identifier>index </span><span class=special>== </span><span class=number>3</span><span class=identifier>rd </span><span class=identifier>item |
---|
54 | |
---|
55 | </span><span class=comment>// Note zero based indexing. 0 = 1st item, 1 = 2nd item |
---|
56 | |
---|
57 | </span><span class=identifier>t1</span><span class=special>[</span><span class=identifier>ix0</span><span class=special>] = </span><span class=number>33</span><span class=special>; // </span><span class=identifier>sets </span><span class=identifier>the </span><span class=keyword>int </span><span class=identifier>member </span><span class=identifier>of </span><span class=identifier>the </span><span class=identifier>tuple </span><span class=identifier>t1 |
---|
58 | </span><span class=identifier>t2</span><span class=special>[</span><span class=identifier>ix2</span><span class=special>] = </span><span class=number>6e6</span><span class=special>; // </span><span class=identifier>sets </span><span class=identifier>the </span><span class=keyword>double </span><span class=identifier>member </span><span class=identifier>of </span><span class=identifier>the </span><span class=identifier>tuple </span><span class=identifier>t2 |
---|
59 | </span><span class=identifier>t1</span><span class=special>[</span><span class=identifier>ix1</span><span class=special>] = </span><span class=literal>'a'</span><span class=special>; // </span><span class=identifier>sets </span><span class=identifier>the </span><span class=keyword>char </span><span class=identifier>member </span><span class=identifier>of </span><span class=identifier>the </span><span class=identifier>tuple </span><span class=identifier>t1 |
---|
60 | </span></pre></code> |
---|
61 | <p> |
---|
62 | Access to out of bound indexes returns a nil_t value.</p> |
---|
63 | <p> |
---|
64 | 3) Member type inquiry</p> |
---|
65 | <p> |
---|
66 | The type of an individual member can be queried. Example:</p> |
---|
67 | <code><pre> |
---|
68 | <span class=identifier>tuple_element</span><span class=special><</span><span class=number>1</span><span class=special>, </span><span class=identifier>t2_t</span><span class=special>>::</span><span class=identifier>type |
---|
69 | </span></pre></code> |
---|
70 | <p> |
---|
71 | Refers to the type of the second member (again note zero based indexing, hence 0 = 1st item, 1 = 2nd item) of the tuple.</p> |
---|
72 | <p> |
---|
73 | Access to out of bound indexes returns a nil_t type.</p> |
---|
74 | <p> |
---|
75 | 4) Tuple length</p> |
---|
76 | <p> |
---|
77 | The number of elements in a tuple can be queried. Example:</p> |
---|
78 | <code><pre> |
---|
79 | <span class=keyword>int </span><span class=identifier>n </span><span class=special>= </span><span class=identifier>t1</span><span class=special>.</span><span class=identifier>length</span><span class=special>; |
---|
80 | </span></pre></code> |
---|
81 | <p> |
---|
82 | gets the number of elements in tuple t1.</p> |
---|
83 | <p> |
---|
84 | length is a static constant. Thus, TupleT::length also works. Example:</p> |
---|
85 | <code><pre> |
---|
86 | <span class=keyword>int </span><span class=identifier>n </span><span class=special>= </span><span class=identifier>t1_t</span><span class=special>::</span><span class=identifier>length</span><span class=special>; |
---|
87 | </span></pre></code> |
---|
88 | <table border="0"> |
---|
89 | <tr> |
---|
90 | <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td> |
---|
91 | <td width="30"><a href="inside_phoenix.html"><img src="theme/l_arr.gif" border="0"></a></td> |
---|
92 | <td width="20"><a href="actors_revisited.html"><img src="theme/r_arr.gif" border="0"></a></td> |
---|
93 | </tr> |
---|
94 | </table> |
---|
95 | <br> |
---|
96 | <hr size="1"> |
---|
97 | <p class="copyright">Copyright © 2001-2002 Joel de Guzman<br> |
---|
98 | <br> |
---|
99 | <font size="2">Use, modification and distribution is subject to the Boost Software |
---|
100 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
101 | http://www.boost.org/LICENSE_1_0.txt) </font> </p> |
---|
102 | </body> |
---|
103 | </html> |
---|