tti_detail_has_member_function.qbk 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. [/
  2. (C) Copyright Edward Diener 2011,2012,2014
  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_function Introspecting member function]
  8. The TTI macro [macroref BOOST_TTI_HAS_MEMBER_FUNCTION] introspects
  9. a member function of a class.
  10. BOOST_TTI_HAS_MEMBER_FUNCTION takes a single
  11. parameter which is the name of an inner member function whose existence
  12. the programmer wants to check. The macro generates a metafunction
  13. called "has_member_function_'name_of_inner_member_function'".
  14. The metafunction can be invoked in two different ways.
  15. The first way of invoking the metafunction is by passing it the enclosing
  16. type to introspect and a signature for the member function as a series of
  17. separate template arguments. The signature for the member function consists
  18. of the template arguments of a return type, of optional parameter types in
  19. the form of a boost::mpl forward sequence of types, and of an optional Boost
  20. FunctionTypes tag type. A typical boost::mpl forward sequence of types is
  21. a boost::mpl::vector<>.
  22. The optional Boost FunctionTypes tag type may be used to specify
  23. cv-qualification. This means you can add 'const', 'volatile', or both by
  24. specifying an appropriate tag type. An alternate to using the tag type
  25. is to specify the enclosing type as 'const', 'volatile', or both.
  26. As an example if you specify the tag type as
  27. 'boost::function_types::const_qualified' or if you specify the enclosing
  28. type as 'const T', the member function which you are introspecting
  29. must be a const function.
  30. The second way of invoking the metafunction is by passing it a single
  31. parameter, which is a pointer to member function. This type has the form of:
  32. Return_Type ( Enclosing_Type::* ) ( Parameter_Types ) cv_qualifier(s)
  33. where the Parameter_Types may be empty, or a comma-separated
  34. list of parameter types if there are more than one parameter type.
  35. The cv-qualifier may be 'const', 'volatile', or 'const volatile'.
  36. The metafunction returns a single type called 'type', which is a
  37. boost::mpl::bool_. As a convenience the metafunction
  38. returns the value of this type directly as a compile time bool constant
  39. called 'value'. This 'value' is true or false depending on whether the inner
  40. member function, of the specified signature, exists or not.
  41. [heading Generating the metafunction]
  42. You generate the metafunction by invoking the macro with the name
  43. of an inner member function:
  44. BOOST_TTI_HAS_MEMBER_FUNCTION(AMemberFunction)
  45. generates a metafunction called 'has_member_function_AMemberFunction' in the current scope.
  46. [heading Invoking the metafunction]
  47. You invoke the metafunction by instantiating the template with an enclosing
  48. type to introspect and the signature of the member function as a series of template
  49. parameters. Alternatively you can invoke the metafunction by passing it a single
  50. type which is a pointer to member function.
  51. A return value called 'value' is a compile time bool constant.
  52. has_member_function_AMemberFunction
  53. <
  54. Enclosing_Type,
  55. MemberFunction_ReturnType,
  56. boost::mpl::vector<MemberFunction_ParameterTypes>, // optional, can be any mpl forward sequence
  57. boost::function_types::SomeTagType // optional, can be any FunctionTypes tag type
  58. >::value
  59. OR
  60. has_member_function_AMemberFunction
  61. <
  62. MemberFunction_ReturnType (Enclosing_Type::*) (MemberFunction_ParameterTypes) optional_cv_qualification
  63. >::value
  64. [heading Examples]
  65. First we generate metafunctions for various inner member function names:
  66. #include <boost/tti/has_member_function.hpp>
  67. BOOST_TTI_HAS_MEMBER_FUNCTION(function1)
  68. BOOST_TTI_HAS_MEMBER_FUNCTION(function2)
  69. BOOST_TTI_HAS_MEMBER_FUNCTION(function3)
  70. Next let us create some user-defined types we want to introspect.
  71. struct AClass
  72. {
  73. };
  74. struct Top
  75. {
  76. int function1();
  77. AClass function2(double,short *);
  78. };
  79. struct Top2
  80. {
  81. long function2(Top &,int,bool,short,float);
  82. Top * function3(long,int,AClass &);
  83. };
  84. Finally we invoke our metafunction and return our value.
  85. This all happens at compile time, and can be used by
  86. programmers doing compile time template metaprogramming.
  87. We will show both forms in the following examples.
  88. Both forms are completely interchangeable as to the result
  89. desired.
  90. has_member_function_function1<Top,int>::value; // true
  91. has_member_function_function1<Top,int,boost::mpl::vector<> >::value; // true
  92. has_member_function_function1<Top2,int>::value; // false
  93. has_member_function_function2<AClass (Top::*) (double,short *)>::value; // true
  94. has_member_function_function2<AClass (Top2::*) (double,short *)>::value; // false
  95. has_member_function_function2<long (Top2::*) (Top &,int,bool,short,float)>::value; // true
  96. has_member_function_function3<int (Top2::*) ()>::value; // false
  97. has_member_function_function3<Top2,Top *,boost::mpl::vector<long,int,AClass &> >::value; // true;
  98. [heading Metafunction re-use]
  99. The macro encodes only the name of the member function for which
  100. we are searching and the fact that we are introspecting for a
  101. member function within an enclosing type.
  102. Because of this, once we create our metafunction for
  103. introspecting a member function by name, we can reuse the
  104. metafunction for introspecting any enclosing type, having any
  105. member function, for that name.
  106. [endsect]