123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
- <title>Introspecting member data</title>
- <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
- <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
- <link rel="home" href="../index.html" title="Chapter 1. The Type Traits Introspection Library">
- <link rel="up" href="../index.html" title="Chapter 1. The Type Traits Introspection Library">
- <link rel="prev" href="tti_detail_has_template/tti_detail_has_template_metafunction.html" title="Using the has_template_(xxx) metafunction">
- <link rel="next" href="tti_detail_has_member_function.html" title="Introspecting member function">
- </head>
- <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
- <table cellpadding="2" width="100%"><tr>
- <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
- <td align="center"><a href="../../../../../index.html">Home</a></td>
- <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
- <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
- <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
- <td align="center"><a href="../../../../../more/index.htm">More</a></td>
- </tr></table>
- <hr>
- <div class="spirit-nav">
- <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>
- </div>
- <div class="section">
- <div class="titlepage"><div><div><h2 class="title" style="clear: both">
- <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
- member data</a>
- </h2></div></div></div>
- <p>
- 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>
- introspects member data of a class.
- </p>
- <p>
- BOOST_TTI_HAS_MEMBER_DATA macro takes a single parameter which is the name
- of an inner member data whose existence the programmer wants to check. The
- macro generates a metafunction called "has_member_data_'name_of_inner_member_data'".
- </p>
- <p>
- The metafunction can be invoked in two different ways.
- </p>
- <p>
- The first way is by passing it two parameters. The first parameter is the enclosing
- type to introspect and the second parameter is the type of the member data.
- </p>
- <p>
- The second way is by passing it a single parameter, which is a pointer to member
- type. This type has the form of:
- </p>
- <pre class="programlisting"><span class="identifier">MemberData_Type</span> <span class="identifier">Enclosing_Type</span><span class="special">::*</span>
- </pre>
- <p>
- The metafunction returns a single type called 'type', which is a boost::mpl::bool_.
- As a convenience the metafunction returns the value of this type directly as
- a compile time bool constant called 'value'. This value is true or false depending
- on whether the inner member data, of the specified type, exists or not.
- </p>
- <h4>
- <a name="the_type_traits_introspection_library.tti_detail_has_member_data.h0"></a>
- <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
- the metafunction</a>
- </h4>
- <p>
- You generate the metafunction by invoking the macro with the name of an inner
- member data:
- </p>
- <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>
- </pre>
- <p>
- generates a metafunction called 'has_member_data_AMemberData' in the current
- scope.
- </p>
- <h4>
- <a name="the_type_traits_introspection_library.tti_detail_has_member_data.h1"></a>
- <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
- the metafunction</a>
- </h4>
- <p>
- You invoke the metafunction by instantiating the template with an enclosing
- type to introspect and the type of the member data, or by instantiating the
- template with a pointer to member data type. The return value called 'value'
- is a compile time bool constant telling you whether or not the member data
- . exists.
- </p>
- <pre class="programlisting"><span class="identifier">has_member_data_AMemberData</span><span class="special"><</span><span class="identifier">Enclosing_Type</span><span class="special">,</span><span class="identifier">MemberData_Type</span><span class="special">>::</span><span class="identifier">value</span>
- <span class="identifier">OR</span>
- <span class="identifier">has_member_data_AMemberData</span><span class="special"><</span><span class="identifier">MemberData_Type</span> <span class="identifier">Enclosing_Type</span><span class="special">::*>::</span><span class="identifier">value</span>
- </pre>
- <h4>
- <a name="the_type_traits_introspection_library.tti_detail_has_member_data.h2"></a>
- <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>
- </h4>
- <p>
- First we generate metafunctions for various inner member data names:
- </p>
- <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</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">></span>
- <span class="identifier">BOOST_TTI_HAS_MEMBER_DATA</span><span class="special">(</span><span class="identifier">data1</span><span class="special">)</span>
- <span class="identifier">BOOST_TTI_HAS_MEMBER_DATA</span><span class="special">(</span><span class="identifier">data2</span><span class="special">)</span>
- <span class="identifier">BOOST_TTI_HAS_MEMBER_DATA</span><span class="special">(</span><span class="identifier">data3</span><span class="special">)</span>
- </pre>
- <p>
- Next let us create some user-defined types we want to introspect.
- </p>
- <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">AClass</span>
- <span class="special">{</span>
- <span class="special">};</span>
- <span class="keyword">struct</span> <span class="identifier">Top</span>
- <span class="special">{</span>
- <span class="keyword">int</span> <span class="identifier">data1</span><span class="special">;</span>
- <span class="identifier">AClass</span> <span class="special">*</span> <span class="identifier">data2</span><span class="special">;</span>
- <span class="special">};</span>
- <span class="keyword">struct</span> <span class="identifier">Top2</span>
- <span class="special">{</span>
- <span class="keyword">long</span> <span class="identifier">data1</span><span class="special">;</span>
- <span class="identifier">Top</span> <span class="identifier">data3</span><span class="special">;</span>
- <span class="special">};</span>
- </pre>
- <p>
- Finally we invoke our metafunction and return our value. This all happens at
- compile time, and can be used by programmers doing compile time template metaprogramming.
- </p>
- <p>
- We will show both forms in the following examples. Both forms are completely
- interchangeable as to the result desired.
- </p>
- <pre class="programlisting"><span class="identifier">has_member_data_data1</span><span class="special"><</span><span class="identifier">Top</span><span class="special">,</span><span class="keyword">int</span><span class="special">>::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
- <span class="identifier">has_member_data_data1</span><span class="special"><</span><span class="identifier">Top</span><span class="special">,</span><span class="keyword">long</span><span class="special">>::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
- <span class="identifier">has_member_data_data1</span><span class="special"><</span><span class="identifier">Top2</span><span class="special">,</span><span class="keyword">int</span><span class="special">>::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
- <span class="identifier">has_member_data_data1</span><span class="special"><</span><span class="keyword">long</span> <span class="identifier">Top2</span><span class="special">::*>::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
- <span class="identifier">has_member_data_data2</span><span class="special"><</span><span class="identifier">AClass</span> <span class="special">*</span> <span class="identifier">Top</span><span class="special">::*>::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
- <span class="identifier">has_member_data_data2</span><span class="special"><</span><span class="identifier">Top</span><span class="special">,</span><span class="keyword">int</span> <span class="special">*>::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
- <span class="identifier">has_member_data_data3</span><span class="special"><</span><span class="keyword">int</span> <span class="identifier">Top2</span><span class="special">::*>::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
- <span class="identifier">has_member_data_data3</span><span class="special"><</span><span class="identifier">Top</span> <span class="identifier">Top2</span><span class="special">::*>::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true;</span>
- </pre>
- <h4>
- <a name="the_type_traits_introspection_library.tti_detail_has_member_data.h3"></a>
- <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
- re-use</a>
- </h4>
- <p>
- The macro encodes only the name of the member data for which we are searching
- and the fact that we are introspecting for member data within an enclosing
- type.
- </p>
- <p>
- Because of this, once we create our metafunction for introspecting an inner
- member data by name, we can reuse the metafunction for introspecting any enclosing
- type, having any inner member data type, for that name.
- </p>
- </div>
- <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
- <td align="left"></td>
- <td align="right"><div class="copyright-footer">Copyright © 2011-2013 Tropic Software
- East Inc<p>
- Distributed under the Boost Software License, Version 1.0. (See accompanying
- 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>)
- </p>
- </div></td>
- </tr></table>
- <hr>
- <div class="spirit-nav">
- <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>
- </div>
- </body>
- </html>
|