]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [/ |
2 | (C) Copyright Edward Diener 2011,2012 | |
3 | Distributed under the Boost Software License, Version 1.0. | |
4 | (See accompanying file LICENSE_1_0.txt or copy at | |
5 | http://www.boost.org/LICENSE_1_0.txt). | |
6 | ] | |
7 | ||
8 | [section:tti_detail_has_member_data Introspecting member data] | |
9 | ||
10 | The TTI macro [macroref BOOST_TTI_HAS_MEMBER_DATA] introspects | |
11 | member data of a class. | |
12 | ||
13 | BOOST_TTI_HAS_MEMBER_DATA macro takes a single | |
14 | parameter which is the name of an inner member data whose existence | |
15 | the programmer wants to check. The macro generates a metafunction | |
16 | called 'has_member_data_'name_of_inner_member_data'. | |
17 | ||
18 | The metafunction can be invoked in two different ways. | |
19 | ||
20 | The first way is by passing it two parameters. The first parameter | |
21 | is the enclosing type to introspect and the second parameter is the | |
22 | type of the member data. | |
23 | ||
24 | The second way is by passing it a single parameter, which is a pointer | |
25 | to member type. This type has the form of: | |
26 | ||
27 | MemberData_Type Enclosing_Type::* | |
28 | ||
29 | The metafunction returns a single type called 'type', which is a | |
30 | boost::mpl::bool_. As a convenience the metafunction | |
31 | returns the value of this type directly as a compile time bool constant | |
32 | called 'value'. This value is true or false depending on whether the | |
33 | inner member data, of the specified type, exists or not. | |
34 | ||
35 | [heading Generating the metafunction] | |
36 | ||
37 | You generate the metafunction by invoking the macro with the name | |
38 | of an inner member data: | |
39 | ||
40 | BOOST_TTI_HAS_MEMBER_DATA(AMemberData) | |
41 | ||
42 | generates a metafunction called 'has_member_data_AMemberData' in the current scope. | |
43 | ||
44 | [heading Invoking the metafunction] | |
45 | ||
46 | You invoke the metafunction by instantiating the template with an enclosing | |
47 | type to introspect and the type of the member data, or by instantiating the | |
48 | template with a pointer to member data type. The return value called 'value' | |
49 | is a compile time bool constant telling you whether or not the member data . | |
50 | exists. | |
51 | ||
52 | has_member_data_AMemberData<Enclosing_Type,MemberData_Type>::value | |
53 | ||
54 | OR | |
55 | ||
56 | has_member_data_AMemberData<MemberData_Type Enclosing_Type::*>::value | |
57 | ||
58 | [heading Examples] | |
59 | ||
60 | First we generate metafunctions for various inner member data names: | |
61 | ||
62 | #include <boost/tti/has_member_data.hpp> | |
63 | ||
64 | BOOST_TTI_HAS_MEMBER_DATA(data1) | |
65 | BOOST_TTI_HAS_MEMBER_DATA(data2) | |
66 | BOOST_TTI_HAS_MEMBER_DATA(data3) | |
67 | ||
68 | Next let us create some user-defined types we want to introspect. | |
69 | ||
70 | struct AClass | |
71 | { | |
72 | }; | |
73 | struct Top | |
74 | { | |
75 | int data1; | |
76 | AClass * data2; | |
77 | }; | |
78 | struct Top2 | |
79 | { | |
80 | long data1; | |
81 | Top data3; | |
82 | }; | |
83 | ||
84 | Finally we invoke our metafunction and return our value. | |
85 | This all happens at compile time, and can be used by | |
86 | programmers doing compile time template metaprogramming. | |
87 | ||
88 | We will show both forms in the following examples. | |
89 | Both forms are completely interchangeable as to the result | |
90 | desired. | |
91 | ||
92 | has_member_data_data1<Top,int>::value; // true | |
93 | has_member_data_data1<Top,long>::value; // false | |
94 | ||
95 | has_member_data_data1<Top2,int>::value; // false | |
96 | has_member_data_data1<long Top2::*>::value; // true | |
97 | ||
98 | has_member_data_data2<AClass * Top::*>::value; // true | |
99 | has_member_data_data2<Top,int *>::value; // false | |
100 | ||
101 | has_member_data_data3<int Top2::*>::value; // false | |
102 | has_member_data_data3<Top Top2::*>::value; // true; | |
103 | ||
104 | [heading Metafunction re-use] | |
105 | ||
106 | The macro encodes only the name of the member data for which | |
107 | we are searching and the fact that we are introspecting for | |
108 | member data within an enclosing type. | |
109 | ||
110 | Because of this, once we create our metafunction for | |
111 | introspecting an inner member data by name, we can reuse the | |
112 | metafunction for introspecting any enclosing type, having any | |
113 | inner member data type, for that name. | |
114 | ||
115 | [endsect] |