tti_detail_has_static_member_function.qbk 4.9 KB

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