tti_detail_has_member_data.qbk 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. [section:tti_detail_has_member_data Introspecting member data]
  8. The TTI macro [macroref BOOST_TTI_HAS_MEMBER_DATA] introspects
  9. member data of a class.
  10. BOOST_TTI_HAS_MEMBER_DATA macro takes a single
  11. parameter which is the name of an inner member data whose existence
  12. the programmer wants to check. The macro generates a metafunction
  13. called "has_member_data_'name_of_inner_member_data'".
  14. The metafunction can be invoked in two different ways.
  15. The first way is by passing it two parameters. The first parameter
  16. is the enclosing type to introspect and the second parameter is the
  17. type of the member data.
  18. The second way is by passing it a single parameter, which is a pointer
  19. to member type. This type has the form of:
  20. MemberData_Type Enclosing_Type::*
  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 value is true or false depending on whether the
  25. inner member data, of the specified type, exists or not.
  26. [heading Generating the metafunction]
  27. You generate the metafunction by invoking the macro with the name
  28. of an inner member data:
  29. BOOST_TTI_HAS_MEMBER_DATA(AMemberData)
  30. generates a metafunction called 'has_member_data_AMemberData' in the current scope.
  31. [heading Invoking the metafunction]
  32. You invoke the metafunction by instantiating the template with an enclosing
  33. type to introspect and the type of the member data, or by instantiating the
  34. template with a pointer to member data type. The return value called 'value'
  35. is a compile time bool constant telling you whether or not the member data .
  36. exists.
  37. has_member_data_AMemberData<Enclosing_Type,MemberData_Type>::value
  38. OR
  39. has_member_data_AMemberData<MemberData_Type Enclosing_Type::*>::value
  40. [heading Examples]
  41. First we generate metafunctions for various inner member data names:
  42. #include <boost/tti/has_member_data.hpp>
  43. BOOST_TTI_HAS_MEMBER_DATA(data1)
  44. BOOST_TTI_HAS_MEMBER_DATA(data2)
  45. BOOST_TTI_HAS_MEMBER_DATA(data3)
  46. Next let us create some user-defined types we want to introspect.
  47. struct AClass
  48. {
  49. };
  50. struct Top
  51. {
  52. int data1;
  53. AClass * data2;
  54. };
  55. struct Top2
  56. {
  57. long data1;
  58. Top data3;
  59. };
  60. Finally we invoke our metafunction and return our value.
  61. This all happens at compile time, and can be used by
  62. programmers doing compile time template metaprogramming.
  63. We will show both forms in the following examples.
  64. Both forms are completely interchangeable as to the result
  65. desired.
  66. has_member_data_data1<Top,int>::value; // true
  67. has_member_data_data1<Top,long>::value; // false
  68. has_member_data_data1<Top2,int>::value; // false
  69. has_member_data_data1<long Top2::*>::value; // true
  70. has_member_data_data2<AClass * Top::*>::value; // true
  71. has_member_data_data2<Top,int *>::value; // false
  72. has_member_data_data3<int Top2::*>::value; // false
  73. has_member_data_data3<Top Top2::*>::value; // true;
  74. [heading Metafunction re-use]
  75. The macro encodes only the name of the member data for which
  76. we are searching and the fact that we are introspecting for
  77. member data within an enclosing type.
  78. Because of this, once we create our metafunction for
  79. introspecting an inner member data by name, we can reuse the
  80. metafunction for introspecting any enclosing type, having any
  81. inner member data type, for that name.
  82. [endsect]