9
3

tti_detail_has_member_data.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Introspecting member data</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 Type Traits Introspection Library">
  8. <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;The Type Traits Introspection Library">
  9. <link rel="prev" href="tti_detail_has_template/tti_detail_has_template_metafunction.html" title="Using the has_template_(xxx) metafunction">
  10. <link rel="next" href="tti_detail_has_member_function.html" title="Introspecting member function">
  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="tti_detail_has_template/tti_detail_has_template_metafunction.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="tti_detail_has_member_function.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="the_type_traits_introspection_library.tti_detail_has_member_data"></a><a class="link" href="tti_detail_has_member_data.html" title="Introspecting member data">Introspecting
  28. member data</a>
  29. </h2></div></div></div>
  30. <p>
  31. The TTI macro <code class="computeroutput"><a class="link" href="../BOOST_TTI_HAS_MEMBER_DATA.html" title="Macro BOOST_TTI_HAS_MEMBER_DATA">BOOST_TTI_HAS_MEMBER_DATA</a></code>
  32. introspects member data of a class.
  33. </p>
  34. <p>
  35. BOOST_TTI_HAS_MEMBER_DATA macro takes a single parameter which is the name
  36. of an inner member data whose existence the programmer wants to check. The
  37. macro generates a metafunction called "has_member_data_'name_of_inner_member_data'".
  38. </p>
  39. <p>
  40. The metafunction can be invoked in two different ways.
  41. </p>
  42. <p>
  43. The first way is by passing it two parameters. The first parameter is the enclosing
  44. type to introspect and the second parameter is the type of the member data.
  45. </p>
  46. <p>
  47. The second way is by passing it a single parameter, which is a pointer to member
  48. type. This type has the form of:
  49. </p>
  50. <pre class="programlisting"><span class="identifier">MemberData_Type</span> <span class="identifier">Enclosing_Type</span><span class="special">::*</span>
  51. </pre>
  52. <p>
  53. The metafunction returns a single type called 'type', which is a boost::mpl::bool_.
  54. As a convenience the metafunction returns the value of this type directly as
  55. a compile time bool constant called 'value'. This value is true or false depending
  56. on whether the inner member data, of the specified type, exists or not.
  57. </p>
  58. <h4>
  59. <a name="the_type_traits_introspection_library.tti_detail_has_member_data.h0"></a>
  60. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_member_data.generating_the_metafunction"></a></span><a class="link" href="tti_detail_has_member_data.html#the_type_traits_introspection_library.tti_detail_has_member_data.generating_the_metafunction">Generating
  61. the metafunction</a>
  62. </h4>
  63. <p>
  64. You generate the metafunction by invoking the macro with the name of an inner
  65. member data:
  66. </p>
  67. <pre class="programlisting"><span class="identifier">BOOST_TTI_HAS_MEMBER_DATA</span><span class="special">(</span><span class="identifier">AMemberData</span><span class="special">)</span>
  68. </pre>
  69. <p>
  70. generates a metafunction called 'has_member_data_AMemberData' in the current
  71. scope.
  72. </p>
  73. <h4>
  74. <a name="the_type_traits_introspection_library.tti_detail_has_member_data.h1"></a>
  75. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_member_data.invoking_the_metafunction"></a></span><a class="link" href="tti_detail_has_member_data.html#the_type_traits_introspection_library.tti_detail_has_member_data.invoking_the_metafunction">Invoking
  76. the metafunction</a>
  77. </h4>
  78. <p>
  79. You invoke the metafunction by instantiating the template with an enclosing
  80. type to introspect and the type of the member data, or by instantiating the
  81. template with a pointer to member data type. The return value called 'value'
  82. is a compile time bool constant telling you whether or not the member data
  83. . exists.
  84. </p>
  85. <pre class="programlisting"><span class="identifier">has_member_data_AMemberData</span><span class="special">&lt;</span><span class="identifier">Enclosing_Type</span><span class="special">,</span><span class="identifier">MemberData_Type</span><span class="special">&gt;::</span><span class="identifier">value</span>
  86. <span class="identifier">OR</span>
  87. <span class="identifier">has_member_data_AMemberData</span><span class="special">&lt;</span><span class="identifier">MemberData_Type</span> <span class="identifier">Enclosing_Type</span><span class="special">::*&gt;::</span><span class="identifier">value</span>
  88. </pre>
  89. <h4>
  90. <a name="the_type_traits_introspection_library.tti_detail_has_member_data.h2"></a>
  91. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_member_data.examples"></a></span><a class="link" href="tti_detail_has_member_data.html#the_type_traits_introspection_library.tti_detail_has_member_data.examples">Examples</a>
  92. </h4>
  93. <p>
  94. First we generate metafunctions for various inner member data names:
  95. </p>
  96. <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">tti</span><span class="special">/</span><span class="identifier">has_member_data</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  97. <span class="identifier">BOOST_TTI_HAS_MEMBER_DATA</span><span class="special">(</span><span class="identifier">data1</span><span class="special">)</span>
  98. <span class="identifier">BOOST_TTI_HAS_MEMBER_DATA</span><span class="special">(</span><span class="identifier">data2</span><span class="special">)</span>
  99. <span class="identifier">BOOST_TTI_HAS_MEMBER_DATA</span><span class="special">(</span><span class="identifier">data3</span><span class="special">)</span>
  100. </pre>
  101. <p>
  102. Next let us create some user-defined types we want to introspect.
  103. </p>
  104. <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">AClass</span>
  105. <span class="special">{</span>
  106. <span class="special">};</span>
  107. <span class="keyword">struct</span> <span class="identifier">Top</span>
  108. <span class="special">{</span>
  109. <span class="keyword">int</span> <span class="identifier">data1</span><span class="special">;</span>
  110. <span class="identifier">AClass</span> <span class="special">*</span> <span class="identifier">data2</span><span class="special">;</span>
  111. <span class="special">};</span>
  112. <span class="keyword">struct</span> <span class="identifier">Top2</span>
  113. <span class="special">{</span>
  114. <span class="keyword">long</span> <span class="identifier">data1</span><span class="special">;</span>
  115. <span class="identifier">Top</span> <span class="identifier">data3</span><span class="special">;</span>
  116. <span class="special">};</span>
  117. </pre>
  118. <p>
  119. Finally we invoke our metafunction and return our value. This all happens at
  120. compile time, and can be used by programmers doing compile time template metaprogramming.
  121. </p>
  122. <p>
  123. We will show both forms in the following examples. Both forms are completely
  124. interchangeable as to the result desired.
  125. </p>
  126. <pre class="programlisting"><span class="identifier">has_member_data_data1</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">,</span><span class="keyword">int</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
  127. <span class="identifier">has_member_data_data1</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">,</span><span class="keyword">long</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
  128. <span class="identifier">has_member_data_data1</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">,</span><span class="keyword">int</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
  129. <span class="identifier">has_member_data_data1</span><span class="special">&lt;</span><span class="keyword">long</span> <span class="identifier">Top2</span><span class="special">::*&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
  130. <span class="identifier">has_member_data_data2</span><span class="special">&lt;</span><span class="identifier">AClass</span> <span class="special">*</span> <span class="identifier">Top</span><span class="special">::*&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
  131. <span class="identifier">has_member_data_data2</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">,</span><span class="keyword">int</span> <span class="special">*&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
  132. <span class="identifier">has_member_data_data3</span><span class="special">&lt;</span><span class="keyword">int</span> <span class="identifier">Top2</span><span class="special">::*&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
  133. <span class="identifier">has_member_data_data3</span><span class="special">&lt;</span><span class="identifier">Top</span> <span class="identifier">Top2</span><span class="special">::*&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true;</span>
  134. </pre>
  135. <h4>
  136. <a name="the_type_traits_introspection_library.tti_detail_has_member_data.h3"></a>
  137. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_member_data.metafunction_re_use"></a></span><a class="link" href="tti_detail_has_member_data.html#the_type_traits_introspection_library.tti_detail_has_member_data.metafunction_re_use">Metafunction
  138. re-use</a>
  139. </h4>
  140. <p>
  141. The macro encodes only the name of the member data for which we are searching
  142. and the fact that we are introspecting for member data within an enclosing
  143. type.
  144. </p>
  145. <p>
  146. Because of this, once we create our metafunction for introspecting an inner
  147. member data by name, we can reuse the metafunction for introspecting any enclosing
  148. type, having any inner member data type, for that name.
  149. </p>
  150. </div>
  151. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  152. <td align="left"></td>
  153. <td align="right"><div class="copyright-footer">Copyright &#169; 2011-2013 Tropic Software
  154. East Inc<p>
  155. Distributed under the Boost Software License, Version 1.0. (See accompanying
  156. file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
  157. </p>
  158. </div></td>
  159. </tr></table>
  160. <hr>
  161. <div class="spirit-nav">
  162. <a accesskey="p" href="tti_detail_has_template/tti_detail_has_template_metafunction.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="tti_detail_has_member_function.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  163. </div>
  164. </body>
  165. </html>