has_type.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // (C) Copyright Edward Diener 2011,2012,2013
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt).
  5. #if !defined(BOOST_TTI_HAS_TYPE_HPP)
  6. #define BOOST_TTI_HAS_TYPE_HPP
  7. #include <boost/config.hpp>
  8. #include <boost/preprocessor/cat.hpp>
  9. #include <boost/tti/gen/has_type_gen.hpp>
  10. #include <boost/tti/gen/namespace_gen.hpp>
  11. #include <boost/tti/detail/dtype.hpp>
  12. #include <boost/tti/detail/ddeftype.hpp>
  13. /*
  14. The succeeding comments in this file are in doxygen format.
  15. */
  16. /** \file
  17. */
  18. /**
  19. BOOST_TTI_TRAIT_HAS_TYPE is a macro which expands to a metafunction.
  20. The metafunction tests whether an inner type with a particular name exists
  21. and, optionally, whether a lambda expression invoked with the inner type
  22. is true or not.
  23. trait = the name of the metafunction within the tti namespace.
  24. name = the name of the inner type.
  25. generates a metafunction called "trait" where 'trait' is the macro parameter.
  26. template<class BOOST_TTI_TP_T,class BOOST_TTI_TP_U>
  27. struct trait
  28. {
  29. static const value = unspecified;
  30. typedef mpl::bool_<true-or-false> type;
  31. };
  32. The metafunction types and return:
  33. BOOST_TTI_TP_T = the enclosing type in which to look for our 'name'.
  34. BOOST_TTI_TP_U = (optional) An optional template parameter, defaulting to a marker type.
  35. If specified it is an MPL lambda expression which is invoked
  36. with the inner type found and must return a constant boolean
  37. value.
  38. returns = 'value' depends on whether or not the optional BOOST_TTI_TP_U is specified.
  39. If BOOST_TTI_TP_U is not specified, then 'value' is true if the 'name' type
  40. exists within the enclosing type BOOST_TTI_TP_T; otherwise 'value' is false.
  41. If BOOST_TTI_TP_U is specified , then 'value' is true if the 'name' type exists
  42. within the enclosing type BOOST_TTI_TP_T and the lambda expression as specified
  43. by BOOST_TTI_TP_U, invoked by passing the actual inner type of 'name', returns
  44. a 'value' of true; otherwise 'value' is false.
  45. The action taken with BOOST_TTI_TP_U occurs only when the 'name' type exists
  46. within the enclosing type BOOST_TTI_TP_T.
  47. Example usage:
  48. BOOST_TTI_TRAIT_HAS_TYPE(LookFor,MyType) generates the metafunction LookFor in the current scope
  49. to look for an inner type called MyType.
  50. LookFor<EnclosingType>::value is true if MyType is an inner type of EnclosingType, otherwise false.
  51. LookFor<EnclosingType,ALambdaExpression>::value is true if MyType is an inner type of EnclosingType
  52. and invoking ALambdaExpression with the inner type returns a value of true, otherwise false.
  53. A popular use of the optional MPL lambda expression is to check whether the type found is the same
  54. as another type, when the type found is a typedef. In that case our example would be:
  55. LookFor<EnclosingType,boost::is_same<_,SomeOtherType> >::value is true if MyType is an inner type
  56. of EnclosingType and is the same type as SomeOtherType.
  57. */
  58. #define BOOST_TTI_TRAIT_HAS_TYPE(trait,name) \
  59. BOOST_TTI_DETAIL_TRAIT_HAS_TYPE(trait,name) \
  60. template \
  61. < \
  62. class BOOST_TTI_TP_T, \
  63. class BOOST_TTI_TP_U = BOOST_TTI_NAMESPACE::detail::deftype \
  64. > \
  65. struct trait \
  66. { \
  67. typedef typename \
  68. BOOST_PP_CAT(trait,_detail_type)<BOOST_TTI_TP_T,BOOST_TTI_TP_U>::type type; \
  69. BOOST_STATIC_CONSTANT(bool,value=type::value); \
  70. }; \
  71. /**/
  72. /**
  73. BOOST_TTI_HAS_TYPE is a macro which expands to a metafunction.
  74. The metafunction tests whether an inner type with a particular name exists
  75. and, optionally, whether a lambda expression invoked with the inner type
  76. is true or not.
  77. name = the name of the inner type.
  78. generates a metafunction called "has_type_'name'" where 'name' is the macro parameter.
  79. template<class BOOST_TTI_TP_T,class BOOST_TTI_TP_U>
  80. struct has_type_'name'
  81. {
  82. static const value = unspecified;
  83. typedef mpl::bool_<true-or-false> type;
  84. };
  85. The metafunction types and return:
  86. BOOST_TTI_TP_T = the enclosing type in which to look for our 'name'.
  87. BOOST_TTI_TP_U = (optional) An optional template parameter, defaulting to a marker type.
  88. If specified it is an MPL lambda expression which is invoked
  89. with the inner type found and must return a constant boolean
  90. value.
  91. returns = 'value' depends on whether or not the optional BOOST_TTI_TP_U is specified.
  92. If BOOST_TTI_TP_U is not specified, then 'value' is true if the 'name' type
  93. exists within the enclosing type BOOST_TTI_TP_T; otherwise 'value' is false.
  94. If BOOST_TTI_TP_U is specified , then 'value' is true if the 'name' type exists
  95. within the enclosing type BOOST_TTI_TP_T and the lambda expression as specified
  96. by BOOST_TTI_TP_U, invoked by passing the actual inner type of 'name', returns
  97. a 'value' of true; otherwise 'value' is false.
  98. The action taken with BOOST_TTI_TP_U occurs only when the 'name' type exists
  99. within the enclosing type BOOST_TTI_TP_T.
  100. Example usage:
  101. BOOST_TTI_HAS_TYPE(MyType) generates the metafunction has_type_MyType in the current scope
  102. to look for an inner type called MyType.
  103. has_type_MyType<EnclosingType>::value is true if MyType is an inner type of EnclosingType, otherwise false.
  104. has_type_MyType<EnclosingType,ALambdaExpression>::value is true if MyType is an inner type of EnclosingType
  105. and invoking ALambdaExpression with the inner type returns a value of true, otherwise false.
  106. A popular use of the optional MPL lambda expression is to check whether the type found is the same
  107. as another type, when the type found is a typedef. In that case our example would be:
  108. has_type_MyType<EnclosingType,boost::is_same<_,SomeOtherType> >::value is true if MyType is an inner type
  109. of EnclosingType and is the same type as SomeOtherType.
  110. */
  111. #define BOOST_TTI_HAS_TYPE(name) \
  112. BOOST_TTI_TRAIT_HAS_TYPE \
  113. ( \
  114. BOOST_TTI_HAS_TYPE_GEN(name), \
  115. name \
  116. ) \
  117. /**/
  118. #endif // BOOST_TTI_HAS_TYPE_HPP