tti_using_mm.qbk 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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_usingMM An example using the Macro Metafunctions]
  8. [#sectti_usingMM]
  9. Using the macro metafunctions can be illustrated by first creating some hypothetical
  10. user-defined type with corresponding nested types and other inner elements.
  11. With this type we can illustrate the use of the macro metafunctions. This is
  12. just meant to serve as a model for what a type T might entail from within
  13. a class or function template where 'T' is a type passed to the template.
  14. // An enclosing type
  15. struct AType
  16. {
  17. // Type
  18. typedef int AnIntType; // as a typedef
  19. struct BType // as a nested type
  20. {
  21. struct CType
  22. {
  23. };
  24. };
  25. // Template
  26. template <class> struct AMemberTemplate { };
  27. template <class,class,class> struct AnotherMemberTemplate { };
  28. template <class,class,int,class,template <class> class,class,long> struct ManyParameters { };
  29. template <class,class,int,short,class,template <class,int> class,class> struct MoreParameters { };
  30. // Data
  31. BType IntBT;
  32. // Function
  33. int IntFunction(short) { return 0; }
  34. // Static Data
  35. static short DSMember;
  36. // Static Function
  37. static int SIntFunction(long,double) { return 2; }
  38. };
  39. I will be using the type above just to illustrate the sort of
  40. metaprogramming questions we can ask of some type T which is passed
  41. to the template programmer in a class template. Here is what the
  42. class template might look like:
  43. #include <boost/tti/tti.hpp>
  44. template<class T>
  45. struct OurTemplateClass
  46. {
  47. // compile-time template code regarding T
  48. };
  49. Now let us create and invoke the macro metafunctions for each of our inner element types,
  50. to see if type T above corresponds to our hypothetical type above. Imagine this being
  51. within 'OurTemplateClass' above. In the examples below the same macro is invoked just once
  52. to avoid ODR violations.
  53. [heading Type]
  54. Does T have a nested type called 'AnIntType' ?
  55. BOOST_TTI_HAS_TYPE(AnIntType)
  56. has_type_AnIntType
  57. <
  58. T
  59. >
  60. Does T have a nested type called 'BType' ?
  61. BOOST_TTI_HAS_TYPE(BType)
  62. has_type_BType
  63. <
  64. T
  65. >
  66. [heading Type checking the typedef using a lambda expression]
  67. Does T have a nested typedef called 'AnIntType' whose type is an 'int' ?
  68. #include <boost/mpl/placeholders.hpp
  69. #include <boost/type_traits/is_same.hpp
  70. using namespace boost::mpl::placeholders;
  71. has_type_AnIntType
  72. <
  73. T,
  74. boost::is_same<_1,int>
  75. >
  76. [heading Template]
  77. Does T have a nested class template called 'AMemberTemplate' whose template
  78. parameters are all types ('class' or 'typename') ?
  79. BOOST_TTI_HAS_TEMPLATE(AMemberTemplate,BOOST_PP_NIL)
  80. has_template_AMemberTemplate
  81. <
  82. T
  83. >
  84. [heading Template using variadic macros]
  85. Does T have a nested class template called 'AMemberTemplate' whose template
  86. parameters are all types ('class' or 'typename') ?
  87. BOOST_TTI_HAS_TEMPLATE(AnotherMemberTemplate)
  88. has_template_AnotherMemberTemplate
  89. <
  90. T
  91. >
  92. [heading Template with params]
  93. Does T have a nested class template called 'MoreParameters' whose template
  94. parameters are specified exactly ?
  95. BOOST_TTI_HAS_TEMPLATE(MoreParameters,(8,(class,class,int,short,class,template <class,int> class,class)))
  96. has_template_MoreParameters
  97. <
  98. T
  99. >
  100. [heading Template with params using variadic macros]
  101. Does T have a nested class template called 'ManyParameters' whose template
  102. parameters are specified exactly ?
  103. BOOST_TTI_HAS_TEMPLATE(ManyParameters,class,class,int,class,template <class> class,class,long)
  104. has_template_ManyParameters
  105. <
  106. T
  107. >
  108. [heading Member data]
  109. Does T have a member data called 'IntBT' whose type is 'AType::BType' ?
  110. BOOST_TTI_HAS_MEMBER_DATA(IntBT)
  111. has_member_data_IntBT
  112. <
  113. T,
  114. AType::BType
  115. >
  116. [heading Member data with composite type]
  117. Does T have a member data called 'IntBT' whose type is 'AType::BType' ?
  118. BOOST_TTI_HAS_MEMBER_DATA(IntBT)
  119. has_member_data_IntBT
  120. <
  121. AType::BType T::*
  122. >
  123. [heading Member function with individual types]
  124. Does T have a member function called 'IntFunction' whose type is
  125. 'int (short)' ?
  126. BOOST_TTI_HAS_MEMBER_FUNCTION(IntFunction)
  127. has_member_function_IntFunction
  128. <
  129. T,
  130. int,
  131. boost::mpl::vector<short>
  132. >
  133. [heading Member function with composite type]
  134. Does T have a member function called 'IntFunction' whose type is
  135. 'int (short)' ?
  136. BOOST_TTI_HAS_MEMBER_FUNCTION(IntFunction)
  137. has_member_function_IntFunction
  138. <
  139. int (T::*)(short)
  140. >
  141. [heading Static member data]
  142. Does T have a static member data called 'DSMember' whose type is 'short' ?
  143. BOOST_TTI_HAS_STATIC_MEMBER_DATA(DSMember)
  144. has_static_member_data_DSMember
  145. <
  146. T,
  147. short
  148. >
  149. [heading Static member function with individual types]
  150. Does T have a static member function called 'SIntFunction' whose type
  151. is 'int (long,double)' ?
  152. BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION(SIntFunction)
  153. has_static_member_function_SIntFunction
  154. <
  155. T,
  156. int,
  157. boost::mpl::vector<long,double>
  158. >
  159. [heading Static member function with composite type]
  160. Does T have a static member function called 'SIntFunction' whose type
  161. is 'int (long,double)' ?
  162. BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION(SIntFunction)
  163. has_static_member_function_SIntFunction
  164. <
  165. T,
  166. int (long,double)
  167. >
  168. [heading Data]
  169. Does T have a member data or static member data called 'DSMember' whose type is 'short' ?
  170. BOOST_TTI_HAS_DATA(DSMember)
  171. has_static_member_data_DSMember
  172. <
  173. T,
  174. short
  175. >
  176. [heading Function]
  177. Does T have a member function or a static member function called 'IntFunction' whose type is
  178. 'int (short)' ?
  179. BOOST_TTI_HAS_FUNCTION(IntFunction)
  180. has_function_IntFunction
  181. <
  182. T,
  183. int,
  184. boost::mpl::vector<short>
  185. >
  186. [heading Member type]
  187. Create a nested type T::BType::CType without creating a compiler error
  188. if T does not have the nested type BType::CType ?
  189. BOOST_TTI_MEMBER_TYPE(BType)
  190. BOOST_TTI_MEMBER_TYPE(CType)
  191. typename
  192. member_type_CType
  193. <
  194. typename
  195. member_type_BType
  196. <
  197. T
  198. >::type
  199. >::type
  200. [heading Member type existence]
  201. Does a nested type T::BType::CType, created without creating a compiler error
  202. if T does not have the nested type BType::CType, actually exist ?
  203. BOOST_TTI_MEMBER_TYPE(BType)
  204. BOOST_TTI_MEMBER_TYPE(CType)
  205. typedef typename
  206. member_type_CType
  207. <
  208. typename
  209. member_type_BType
  210. <
  211. T
  212. >::type
  213. >::type
  214. AType;
  215. boost::tti::valid_member_type
  216. <
  217. AType
  218. >
  219. [endsect]