has_union.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // (C) Copyright Edward Diener 2019
  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_UNION_HPP)
  6. #define BOOST_TTI_HAS_UNION_HPP
  7. #include <boost/config.hpp>
  8. #include <boost/preprocessor/cat.hpp>
  9. #include <boost/tti/gen/has_union_gen.hpp>
  10. #include <boost/tti/gen/namespace_gen.hpp>
  11. #include <boost/tti/detail/dunion.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. /// A macro which expands to a metafunction which tests whether an inner union with a particular name exists.
  19. /**
  20. BOOST_TTI_TRAIT_HAS_UNION is a macro which expands to a metafunction.
  21. The metafunction tests whether an inner union with a particular name exists
  22. and, optionally, whether an MPL lambda expression invoked with the inner union
  23. is true or not. The macro takes the form of BOOST_TTI_TRAIT_HAS_UNION(trait,name) where
  24. trait = the name of the metafunction <br/>
  25. name = the name of the inner union.
  26. BOOST_TTI_TRAIT_HAS_UNION generates a metafunction called "trait" where 'trait' is the macro parameter.
  27. @code
  28. template<class BOOST_TTI_TP_T,class BOOST_TTI_TP_U>
  29. struct trait
  30. {
  31. static const value = unspecified;
  32. typedef mpl::bool_<true-or-false> type;
  33. };
  34. The metafunction types and return:
  35. BOOST_TTI_TP_T = the enclosing type in which to look for our 'name'.
  36. The enclosing type can be a class, struct, or union.
  37. BOOST_TTI_TP_U = (optional) An optional template parameter, defaulting to a marker type.
  38. If specified it is an MPL lambda expression which is invoked
  39. with the inner union found and must return a constant boolean
  40. value.
  41. returns = 'value' depends on whether or not the optional BOOST_TTI_TP_U is specified.
  42. If BOOST_TTI_TP_U is not specified, then 'value' is true if the 'name' union
  43. exists within the enclosing type BOOST_TTI_TP_T; otherwise 'value' is false.
  44. If BOOST_TTI_TP_U is specified , then 'value' is true if the 'name' union exists
  45. within the enclosing type BOOST_TTI_TP_T and the MPL lambda expression as specified
  46. by BOOST_TTI_TP_U, invoked by passing the actual inner union of 'name', returns
  47. a 'value' of true; otherwise 'value' is false.
  48. The action taken with BOOST_TTI_TP_U occurs only when the 'name' union exists
  49. within the enclosing type BOOST_TTI_TP_T.
  50. @endcode
  51. Example usage:
  52. @code
  53. BOOST_TTI_TRAIT_HAS_UNION(LookFor,MyType) generates the metafunction LookFor in the current scope
  54. to look for an inner union called MyType.
  55. LookFor<EnclosingType>::value is true if MyType is an inner union of EnclosingType, otherwise false.
  56. LookFor<EnclosingType,ALambdaExpression>::value is true if MyType is an inner union of EnclosingType
  57. and invoking ALambdaExpression with the inner union returns a value of true, otherwise false.
  58. A popular use of the optional MPL lambda expression is to check whether the union found is the same
  59. as another type, when the union found is a typedef. In that case our example would be:
  60. LookFor<EnclosingType,boost::is_same<_,SomeOtherType> >::value is true if MyType is an inner union
  61. of EnclosingType and is the same type as SomeOtherType.
  62. @endcode
  63. */
  64. #define BOOST_TTI_TRAIT_HAS_UNION(trait,name) \
  65. BOOST_TTI_DETAIL_TRAIT_HAS_UNION(trait,name) \
  66. template \
  67. < \
  68. class BOOST_TTI_TP_T, \
  69. class BOOST_TTI_TP_U = BOOST_TTI_NAMESPACE::detail::deftype \
  70. > \
  71. struct trait \
  72. { \
  73. typedef typename \
  74. BOOST_PP_CAT(trait,_detail_union)<BOOST_TTI_TP_T,BOOST_TTI_TP_U>::type type; \
  75. BOOST_STATIC_CONSTANT(bool,value=type::value); \
  76. }; \
  77. /**/
  78. /// A macro which expands to a metafunction which tests whether an inner union with a particular name exists.
  79. /**
  80. BOOST_TTI_HAS_UNION is a macro which expands to a metafunction.
  81. The metafunction tests whether an inner union with a particular name exists
  82. and, optionally, whether an MPL lambda expression invoked with the inner union
  83. is true or not. The macro takes the form of BOOST_TTI_HAS_UNION(name) where
  84. name = the name of the inner union.
  85. BOOST_TTI_HAS_UNION generates a metafunction called "has_union_'name'" where 'name' is the macro parameter.
  86. @code
  87. template<class BOOST_TTI_TP_T,class BOOST_TTI_TP_U>
  88. struct has_union_'name'
  89. {
  90. static const value = unspecified;
  91. typedef mpl::bool_<true-or-false> type;
  92. };
  93. The metafunction types and return:
  94. BOOST_TTI_TP_T = the enclosing type in which to look for our 'name'.
  95. BOOST_TTI_TP_U = (optional) An optional template parameter, defaulting to a marker type.
  96. If specified it is an MPL lambda expression which is invoked
  97. with the inner union found and must return a constant boolean
  98. value.
  99. returns = 'value' depends on whether or not the optional BOOST_TTI_TP_U is specified.
  100. If BOOST_TTI_TP_U is not specified, then 'value' is true if the 'name' union
  101. exists within the enclosing type BOOST_TTI_TP_T; otherwise 'value' is false.
  102. If BOOST_TTI_TP_U is specified, then 'value' is true if the 'name' union exists
  103. within the enclosing type BOOST_TTI_TP_T and the MPL lambda expression as specified
  104. by BOOST_TTI_TP_U, invoked by passing the actual inner union of 'name', returns
  105. a 'value' of true; otherwise 'value' is false.
  106. The action taken with BOOST_TTI_TP_U occurs only when the 'name' union exists
  107. within the enclosing type BOOST_TTI_TP_T.
  108. @endcode
  109. Example usage:
  110. @code
  111. BOOST_TTI_HAS_UNION(MyType) generates the metafunction has_union_MyType in the current scope
  112. to look for an inner union called MyType.
  113. has_union_MyType<EnclosingType>::value is true if MyType is an inner union of EnclosingType, otherwise false.
  114. has_class_MyType<EnclosingType,ALambdaExpression>::value is true if MyType is an inner union of EnclosingType
  115. and invoking ALambdaExpression with the inner union returns a value of true, otherwise false.
  116. A popular use of the optional MPL lambda expression is to check whether the union found is the same
  117. as another type, when the union found is a typedef. In that case our example would be:
  118. has_union_MyType<EnclosingType,boost::is_same<_,SomeOtherType> >::value is true if MyType is an inner union
  119. of EnclosingType and is the same type as SomeOtherType.
  120. @endcode
  121. */
  122. #define BOOST_TTI_HAS_UNION(name) \
  123. BOOST_TTI_TRAIT_HAS_UNION \
  124. ( \
  125. BOOST_TTI_HAS_UNION_GEN(name), \
  126. name \
  127. ) \
  128. /**/
  129. #endif // BOOST_TTI_HAS_UNION_HPP