1 2 // (C) Copyright Edward Diener 2011,2012,2013 3 // Use, modification and distribution are subject to the Boost Software License, 4 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 5 // http://www.boost.org/LICENSE_1_0.txt). 6 7 #if !defined(BOOST_TTI_HAS_TYPE_HPP) 8 #define BOOST_TTI_HAS_TYPE_HPP 9 10 #include <boost/config.hpp> 11 #include <boost/preprocessor/cat.hpp> 12 #include <boost/tti/gen/has_type_gen.hpp> 13 #include <boost/tti/gen/namespace_gen.hpp> 14 #include <boost/tti/detail/dtype.hpp> 15 #include <boost/tti/detail/ddeftype.hpp> 16 17 /* 18 19 The succeeding comments in this file are in doxygen format. 20 21 */ 22 23 /** \file 24 */ 25 26 /// A macro which expands to a metafunction which tests whether an inner type with a particular name exists. 27 /** 28 29 BOOST_TTI_TRAIT_HAS_TYPE is a macro which expands to a metafunction. 30 The metafunction tests whether an inner type with a particular name exists 31 and, optionally, whether an MPL lambda expression invoked with the inner type 32 is true or not. The macro takes the form of BOOST_TTI_TRAIT_HAS_TYPE(trait,name) where 33 34 trait = the name of the metafunction <br/> 35 name = the name of the inner type. 36 37 BOOST_TTI_TRAIT_HAS_TYPE generates a metafunction called "trait" where 'trait' is the macro parameter. 38 39 @code 40 41 template<class BOOST_TTI_TP_T,class BOOST_TTI_TP_U> 42 struct trait 43 { 44 static const value = unspecified; 45 typedef mpl::bool_<true-or-false> type; 46 }; 47 48 The metafunction types and return: 49 50 BOOST_TTI_TP_T = the enclosing type in which to look for our 'name'. 51 The enclosing type can be a class, struct, or union. 52 53 BOOST_TTI_TP_U = (optional) An optional template parameter, defaulting to a marker type. 54 If specified it is an MPL lambda expression which is invoked 55 with the inner type found and must return a constant boolean 56 value. 57 58 returns = 'value' depends on whether or not the optional BOOST_TTI_TP_U is specified. 59 60 If BOOST_TTI_TP_U is not specified, then 'value' is true if the 'name' type 61 exists within the enclosing type BOOST_TTI_TP_T; otherwise 'value' is false. 62 63 If BOOST_TTI_TP_U is specified , then 'value' is true if the 'name' type exists 64 within the enclosing type BOOST_TTI_TP_T and the MPL lambda expression as specified 65 by BOOST_TTI_TP_U, invoked by passing the actual inner type of 'name', returns 66 a 'value' of true; otherwise 'value' is false. 67 68 The action taken with BOOST_TTI_TP_U occurs only when the 'name' type exists 69 within the enclosing type BOOST_TTI_TP_T. 70 71 @endcode 72 73 Example usage: 74 75 @code 76 77 BOOST_TTI_TRAIT_HAS_TYPE(LookFor,MyType) generates the metafunction LookFor in the current scope 78 to look for an inner type called MyType. 79 80 LookFor<EnclosingType>::value is true if MyType is an inner type of EnclosingType, otherwise false. 81 82 LookFor<EnclosingType,ALambdaExpression>::value is true if MyType is an inner type of EnclosingType 83 and invoking ALambdaExpression with the inner type returns a value of true, otherwise false. 84 85 A popular use of the optional MPL lambda expression is to check whether the type found is the same 86 as another type, when the type found is a typedef. In that case our example would be: 87 88 LookFor<EnclosingType,boost::is_same<_,SomeOtherType> >::value is true if MyType is an inner type 89 of EnclosingType and is the same type as SomeOtherType. 90 91 @endcode 92 93 */ 94 #define BOOST_TTI_TRAIT_HAS_TYPE(trait,name) \ 95 BOOST_TTI_DETAIL_TRAIT_HAS_TYPE(trait,name) \ 96 template \ 97 < \ 98 class BOOST_TTI_TP_T, \ 99 class BOOST_TTI_TP_U = BOOST_TTI_NAMESPACE::detail::deftype \ 100 > \ 101 struct trait \ 102 { \ 103 typedef typename \ 104 BOOST_PP_CAT(trait,_detail_type)<BOOST_TTI_TP_T,BOOST_TTI_TP_U>::type type; \ 105 BOOST_STATIC_CONSTANT(bool,value=type::value); \ 106 }; \ 107 /**/ 108 109 /// A macro which expands to a metafunction which tests whether an inner type with a particular name exists. 110 /** 111 112 BOOST_TTI_HAS_TYPE is a macro which expands to a metafunction. 113 The metafunction tests whether an inner type with a particular name exists 114 and, optionally, whether an MPL lambda expression invoked with the inner type 115 is true or not. The macro takes the form of BOOST_TTI_HAS_TYPE(name) where 116 117 name = the name of the inner type. 118 119 BOOST_TTI_HAS_TYPE generates a metafunction called "has_type_'name'" where 'name' is the macro parameter. 120 121 @code 122 123 template<class BOOST_TTI_TP_T,class BOOST_TTI_TP_U> 124 struct has_type_'name' 125 { 126 static const value = unspecified; 127 typedef mpl::bool_<true-or-false> type; 128 }; 129 130 The metafunction types and return: 131 132 BOOST_TTI_TP_T = the enclosing type in which to look for our 'name'. 133 The enclosing type can be a class, struct, or union. 134 135 BOOST_TTI_TP_U = (optional) An optional template parameter, defaulting to a marker type. 136 If specified it is an MPL lambda expression which is invoked 137 with the inner type found and must return a constant boolean 138 value. 139 140 returns = 'value' depends on whether or not the optional BOOST_TTI_TP_U is specified. 141 142 If BOOST_TTI_TP_U is not specified, then 'value' is true if the 'name' type 143 exists within the enclosing type BOOST_TTI_TP_T; otherwise 'value' is false. 144 145 If BOOST_TTI_TP_U is specified , then 'value' is true if the 'name' type exists 146 within the enclosing type BOOST_TTI_TP_T and the MPL lambda expression as specified 147 by BOOST_TTI_TP_U, invoked by passing the actual inner type of 'name', returns 148 a 'value' of true; otherwise 'value' is false. 149 150 The action taken with BOOST_TTI_TP_U occurs only when the 'name' type exists 151 within the enclosing type BOOST_TTI_TP_T. 152 153 @endcode 154 155 Example usage: 156 157 @code 158 159 BOOST_TTI_HAS_TYPE(MyType) generates the metafunction has_type_MyType in the current scope 160 to look for an inner type called MyType. 161 162 has_type_MyType<EnclosingType>::value is true if MyType is an inner type of EnclosingType, otherwise false. 163 164 has_type_MyType<EnclosingType,ALambdaExpression>::value is true if MyType is an inner type of EnclosingType 165 and invoking ALambdaExpression with the inner type returns a value of true, otherwise false. 166 167 A popular use of the optional MPL lambda expression is to check whether the type found is the same 168 as another type, when the type found is a typedef. In that case our example would be: 169 170 has_type_MyType<EnclosingType,boost::is_same<_,SomeOtherType> >::value is true if MyType is an inner type 171 of EnclosingType and is the same type as SomeOtherType. 172 173 @endcode 174 175 */ 176 #define BOOST_TTI_HAS_TYPE(name) \ 177 BOOST_TTI_TRAIT_HAS_TYPE \ 178 ( \ 179 BOOST_TTI_HAS_TYPE_GEN(name), \ 180 name \ 181 ) \ 182 /**/ 183 184 #endif // BOOST_TTI_HAS_TYPE_HPP 185