tti_detail_has_static_member_data.qbk 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_static_member_data Introspecting static member data]
  8. The TTI macro [macroref BOOST_TTI_HAS_STATIC_MEMBER_DATA] introspects
  9. static member data of a class.
  10. BOOST_TTI_HAS_STATIC_MEMBER_DATA macro takes a single
  11. parameter which is the name of an inner static member data whose existence
  12. the programmer wants to check. The macro generates a metafunction
  13. called "has_static_member_data_'name_of_inner_static_member_data'".
  14. The metafunction can be invoked by passing it the enclosing type
  15. to introspect and the type of the static member 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. static member 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 static member data:
  24. BOOST_TTI_HAS_STATIC_MEMBER_DATA(AStaticMemberData)
  25. generates a metafunction called 'has_static_member_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 static member data. A return value called
  29. 'value' is a compile time bool constant.
  30. has_static_member_data_AStaticMemberData<Enclosing_Type,StaticMemberData_Type>::value
  31. [heading Examples]
  32. First we generate metafunctions for various inner member data names:
  33. #include <boost/tti/has_static_member_data.hpp>
  34. BOOST_TTI_HAS_STATIC_MEMBER_DATA(data1)
  35. BOOST_TTI_HAS_STATIC_MEMBER_DATA(data2)
  36. BOOST_TTI_HAS_STATIC_MEMBER_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. static int data1;
  44. static AClass * data2;
  45. };
  46. struct Top2
  47. {
  48. static long data1;
  49. static 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_static_member_data_data1<Top,int>::value; // true
  55. has_static_member_data_data1<Top,long>::value; // false
  56. has_static_member_data_data1<Top2,int>::value; // false
  57. has_static_member_data_data1<Top2,long>::value; // true
  58. has_static_member_data_data2<Top,AClass *>::value; // true
  59. has_static_member_data_data2<Top,int *>::value; // false
  60. has_static_member_data_data3<Top2,int>::value; // false
  61. has_static_member_data_data3<Top2,Top>::value; // true;
  62. [heading Metafunction re-use]
  63. The macro encodes only the name of the static member data for
  64. which we are searching and the fact that we are introspecting
  65. for static member data within an enclosing type.
  66. Because of this, once we create our metafunction for
  67. introspecting an inner static member data by name, we can reuse
  68. the metafunction for introspecting any enclosing type, having
  69. any inner static member data type, for that name.
  70. [endsect]