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 10The TTI macro [macroref BOOST_TTI_HAS_MEMBER_DATA] introspects 11member data of a class. 12 13BOOST_TTI_HAS_MEMBER_DATA macro takes a single 14parameter which is the name of an inner member data whose existence 15the programmer wants to check. The macro generates a metafunction 16called 'has_member_data_'name_of_inner_member_data'. 17 18The metafunction can be invoked in two different ways. 19 20The first way is by passing it two parameters. The first parameter 21is the enclosing type to introspect and the second parameter is the 22type of the member data. 23 24The second way is by passing it a single parameter, which is a pointer 25to member type. This type has the form of: 26 27 MemberData_Type Enclosing_Type::* 28 29The metafunction returns a single type called 'type', which is a 30boost::mpl::bool_. As a convenience the metafunction 31returns the value of this type directly as a compile time bool constant 32called 'value'. This value is true or false depending on whether the 33inner member data, of the specified type, exists or not. 34 35[heading Generating the metafunction] 36 37You generate the metafunction by invoking the macro with the name 38of an inner member data: 39 40 BOOST_TTI_HAS_MEMBER_DATA(AMemberData) 41 42generates a metafunction called 'has_member_data_AMemberData' in the current scope. 43 44[heading Invoking the metafunction] 45 46You invoke the metafunction by instantiating the template with an enclosing 47type to introspect and the type of the member data, or by instantiating the 48template with a pointer to member data type. The return value called 'value' 49is a compile time bool constant telling you whether or not the member data . 50exists. 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 60First 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 68Next 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 84Finally we invoke our metafunction and return our value. 85This all happens at compile time, and can be used by 86programmers doing compile time template metaprogramming. 87 88We will show both forms in the following examples. 89Both forms are completely interchangeable as to the result 90desired. 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 106The macro encodes only the name of the member data for which 107we are searching and the fact that we are introspecting for 108member data within an enclosing type. 109 110Because of this, once we create our metafunction for 111introspecting an inner member data by name, we can reuse the 112metafunction for introspecting any enclosing type, having any 113inner member data type, for that name. 114 115[endsect] 116