tti_detail_has_data.qbk 2.8 KB

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