has_enum.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_ENUM_HPP)
  6. #define BOOST_TTI_HAS_ENUM_HPP
  7. #include <boost/config.hpp>
  8. #include <boost/preprocessor/cat.hpp>
  9. #include <boost/tti/gen/has_enum_gen.hpp>
  10. #include <boost/tti/gen/namespace_gen.hpp>
  11. #include <boost/tti/detail/denum.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 enum with a particular name exists.
  19. /**
  20. BOOST_TTI_TRAIT_HAS_ENUM is a macro which expands to a metafunction.
  21. The metafunction tests whether an inner enum with a particular name exists
  22. and, optionally, whether an MPL lambda expression invoked with the inner enum
  23. is true or not. The macro takes the form of BOOST_TTI_TRAIT_HAS_ENUM(trait,name) where
  24. trait = the name of the metafunction <br/>
  25. name = the name of the inner enum. The name can be that of an enum or, in C++11 on up, an enum class.
  26. BOOST_TTI_TRAIT_HAS_ENUM 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 enum 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' enum
  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' enum 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 enum 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' enum exists
  49. within the enclosing type BOOST_TTI_TP_T.
  50. @endcode
  51. Example usage:
  52. @code
  53. BOOST_TTI_TRAIT_HAS_ENUM(LookFor,MyType) generates the metafunction LookFor in the current scope
  54. to look for an inner enum called MyType.
  55. LookFor<EnclosingType>::value is true if MyType is an inner enum of EnclosingType, otherwise false.
  56. LookFor<EnclosingType,ALambdaExpression>::value is true if MyType is an inner enum of EnclosingType
  57. and invoking ALambdaExpression with the inner enum returns a value of true, otherwise false.
  58. A popular use of the optional MPL lambda expression is to check whether the enum found is the same
  59. as another type, when the enum 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 enum
  61. of EnclosingType and is the same type as SomeOtherType.
  62. @endcode
  63. */
  64. #define BOOST_TTI_TRAIT_HAS_ENUM(trait,name) \
  65. BOOST_TTI_DETAIL_TRAIT_HAS_ENUM(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_enum)<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 enum with a particular name exists.
  79. /**
  80. BOOST_TTI_HAS_ENUM is a macro which expands to a metafunction.
  81. The metafunction tests whether an inner enum with a particular name exists
  82. and, optionally, whether an MPL lambda expression invoked with the inner enum
  83. is true or not. The macro takes the form of BOOST_TTI_HAS_ENUM(name) where
  84. name = the name of the inner enum. The name can be that of an enum or, in C++11 on up, an enum class.
  85. BOOST_TTI_HAS_ENUM generates a metafunction called "has_enum_'name'" where 'name' is the macro parameter.
  86. @code
  87. template<class BOOST_TTI_TP_T,class BOOST_TTI_TP_U>
  88. struct has_enum_'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. The enclosing type can be a class, struct, or union.
  96. BOOST_TTI_TP_U = (optional) An optional template parameter, defaulting to a marker type.
  97. If specified it is an MPL lambda expression which is invoked
  98. with the inner enum found and must return a constant boolean
  99. value.
  100. returns = 'value' depends on whether or not the optional BOOST_TTI_TP_U is specified.
  101. If BOOST_TTI_TP_U is not specified, then 'value' is true if the 'name' enum
  102. exists within the enclosing type BOOST_TTI_TP_T; otherwise 'value' is false.
  103. If BOOST_TTI_TP_U is specified, then 'value' is true if the 'name' enum exists
  104. within the enclosing type BOOST_TTI_TP_T and the MPL lambda expression as specified
  105. by BOOST_TTI_TP_U, invoked by passing the actual inner enum of 'name', returns
  106. a 'value' of true; otherwise 'value' is false.
  107. The action taken with BOOST_TTI_TP_U occurs only when the 'name' enum exists
  108. within the enclosing type BOOST_TTI_TP_T.
  109. @endcode
  110. Example usage:
  111. @code
  112. BOOST_TTI_HAS_ENUM(MyType) generates the metafunction has_enum_MyType in the current scope
  113. to look for an inner enum called MyType.
  114. has_enum_MyType<EnclosingType>::value is true if MyType is an inner enum of EnclosingType, otherwise false.
  115. has_class_MyType<EnclosingType,ALambdaExpression>::value is true if MyType is an inner enum of EnclosingType
  116. and invoking ALambdaExpression with the inner enum returns a value of true, otherwise false.
  117. A popular use of the optional MPL lambda expression is to check whether the enum found is the same
  118. as another type, when the enum found is a typedef. In that case our example would be:
  119. has_enum_MyType<EnclosingType,boost::is_same<_,SomeOtherType> >::value is true if MyType is an inner enum
  120. of EnclosingType and is the same type as SomeOtherType.
  121. @endcode
  122. */
  123. #define BOOST_TTI_HAS_ENUM(name) \
  124. BOOST_TTI_TRAIT_HAS_ENUM \
  125. ( \
  126. BOOST_TTI_HAS_ENUM_GEN(name), \
  127. name \
  128. ) \
  129. /**/
  130. #endif // BOOST_TTI_HAS_ENUM_HPP