1[/ 2 (C) Copyright Edward Diener 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_data Introspecting data] 9 10The TTI macro [macroref BOOST_TTI_HAS_DATA] introspects the data of a class. 11The data can be member data or static member data. 12 13BOOST_TTI_HAS_DATA macro takes a single parameter, which is the name 14of an inner member data or static member data, whose existence 15the programmer wants to check. The macro generates a metafunction 16called "has_data_'name_of_inner_data'". 17 18The metafunction can be invoked by passing it the enclosing type 19to introspect and the type of the data. 20 21The metafunction returns a single type called 'type', which is a 22boost::mpl::bool_. As a convenience the metafunction 23returns the value of this type directly as a compile time bool constant 24called 'value'. This is true or false depending on whether the inner 25data, of the specified type, exists or not. 26 27[heading Generating the metafunction] 28 29You generate the metafunction by invoking the macro with the name 30of an inner data member: 31 32 BOOST_TTI_HAS_DATA(AData) 33 34generates a metafunction called 'has_data_AStaticMemberData' in the current scope. 35 36[heading Invoking the metafunction] 37 38You invoke the metafunction by instantiating the template with an enclosing 39type to introspect and the type of the data. A return value called 40'value' is a compile time bool constant. 41 42 has_data_AData<Enclosing_Type,Data_Type>::value 43 44[heading Examples] 45 46First we generate metafunctions for various inner data names: 47 48 #include <boost/tti/has_data.hpp> 49 50 BOOST_TTI_HAS_DATA(data1) 51 BOOST_TTI_HAS_DATA(data2) 52 BOOST_TTI_HAS_DATA(data3) 53 54Next let us create some user-defined types we want to introspect. 55 56 struct AClass 57 { 58 }; 59 struct Top 60 { 61 int data1; 62 static AClass * data2; 63 }; 64 struct Top2 65 { 66 static long data1; 67 Top data3; 68 }; 69 70Finally we invoke our metafunction and return our value. 71This all happens at compile time, and can be used by 72programmers doing compile time template metaprogramming. 73 74 has_data_data1<Top,int>::value; // true 75 has_data_data1<Top,long>::value; // false 76 has_data_data1<Top2,int>::value; // false 77 has_data_data1<Top2,long>::value; // true 78 79 has_data_data2<Top,AClass *>::value; // true 80 has_data_data2<Top,int *>::value; // false 81 82 has_data_data3<Top2,int>::value; // false 83 has_data_data3<Top2,Top>::value; // true; 84 85[heading Metafunction re-use] 86 87The macro encodes only the name of the data for 88which we are searching and the fact that we are introspecting 89for data within an enclosing type. 90 91Because of this, once we create our metafunction for 92introspecting an inner data by name, we can reuse 93the metafunction for introspecting any enclosing type, having 94any inner data type, for that name. 95 96[endsect] 97