tti_detail_has_function.qbk 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_function Introspecting an inner function]
  8. The TTI macro [macroref BOOST_TTI_HAS_FUNCTION] introspects
  9. an inner function of a class. The function can be either a member
  10. function or a static member function.
  11. BOOST_TTI_HAS_FUNCTION takes a single
  12. parameter which is the name of an inner function whose existence
  13. the programmer wants to check. The macro generates a metafunction
  14. called "has_function_'name_of_inner_function'".
  15. The metafunction can be invoked by passing it the enclosing type to introspect and a
  16. signature for the function as separate template arguments. The signature for the
  17. function consists of a return type, optional parameter types in the form of a boost::mpl
  18. forward sequence of types, and an optional Boost FunctionTypes tag type. A
  19. typical boost::mpl forward sequence of types is a boost::mpl::vector<>.
  20. The metafunction returns a single type called 'type', which is a
  21. boost::mpl::bool_. As a convenience the metafunction
  22. returns the value of this type directly as a compile time bool constant
  23. called 'value'. This is true or false depending on whether the inner
  24. function, of the specified signature, exists or not.
  25. [heading Generating the metafunction]
  26. You generate the metafunction by invoking the macro with the name
  27. of an inner function:
  28. BOOST_TTI_HAS_FUNCTION(AnInnerFunction)
  29. generates a metafunction called 'has_function_AnInnerFunction' in the current scope.
  30. [heading Invoking the metafunction]
  31. You invoke the metafunction by instantiating the template with an enclosing
  32. type to introspect and the signature of the function as a series of template
  33. parameters.
  34. A return value called 'value' is a compile time bool constant.
  35. has_function_AnInnerFunction
  36. <
  37. Enclosing_Type,
  38. Function_ReturnType,
  39. boost::mpl::vector<Function_ParameterTypes>, // optional, can be any mpl forward sequence
  40. boost::function_types::SomeTagType // optional, can be any FunctionTypes tag type
  41. >::value
  42. [heading Examples]
  43. First we generate metafunctions for various inner function names:
  44. #include <boost/tti/has_function.hpp>
  45. BOOST_TTI_HAS_FUNCTION(function1)
  46. BOOST_TTI_HAS_FUNCTION(function2)
  47. BOOST_TTI_HAS_FUNCTION(function3)
  48. Next let us create some user-defined types we want to introspect.
  49. struct AClass { };
  50. struct Top
  51. {
  52. static int function1();
  53. AClass function2(double,short *);
  54. };
  55. struct Top2
  56. {
  57. long function2(Top &,int,bool,short,float);
  58. static Top * function3(long,int,AClass &);
  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. has_function_function1<Top,int>::value; // true
  64. has_function_function1<Top2,int>::value; // false
  65. has_function_function2<Top,AClass,boost::mpl::vector<double,short *> >::value; // true
  66. has_function_function2<Top2,AClass,boost::mpl::vector<double,short *> >::value; // false
  67. has_function_function3<Top2,int>::value; // false
  68. has_function_function3<Top2,Top *,boost::mpl::vector<long,int,AClass &> >::value; // true;
  69. [heading Metafunction re-use]
  70. The macro encodes only the name of the function
  71. for which we are searching and the fact that we are
  72. introspecting for a function within an enclosing type.
  73. Because of this, once we create our metafunction for
  74. introspecting a function by name, we can reuse the
  75. metafunction for introspecting any enclosing type, having any
  76. function, for that name.
  77. [endsect]