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