1 2 // Copyright (C) 2009-2012 Lorenzo Caminiti 3 // Distributed under the Boost Software License, Version 1.0 4 // (see accompanying file LICENSE_1_0.txt or a copy at 5 // http://www.boost.org/LICENSE_1_0.txt) 6 // Home at http://www.boost.org/libs/local_function 7 8 #ifndef BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_HPP_ 9 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_HPP_ 10 11 #include <boost/local_function/aux_/symbol.hpp> 12 #include <boost/local_function/aux_/macro/decl.hpp> 13 #include <boost/local_function/aux_/preprocessor/traits/bind.hpp> 14 #include <boost/local_function/aux_/preprocessor/traits/decl_binds.hpp> 15 #include <boost/local_function/aux_/preprocessor/traits/decl_const_binds.hpp> 16 #include <boost/utility/identity_type.hpp> 17 #include <boost/scope_exit.hpp> 18 #include <boost/type_traits/remove_reference.hpp> 19 #include <boost/type_traits/function_traits.hpp> 20 #include <boost/preprocessor/control/expr_iif.hpp> 21 #include <boost/preprocessor/control/iif.hpp> 22 #include <boost/preprocessor/facilities/empty.hpp> 23 #include <boost/preprocessor/facilities/is_empty.hpp> 24 #include <boost/preprocessor/facilities/identity.hpp> 25 #include <boost/preprocessor/logical/bitand.hpp> 26 #include <boost/preprocessor/punctuation/comma_if.hpp> 27 #include <boost/preprocessor/tuple/elem.hpp> 28 #include <boost/preprocessor/tuple/eat.hpp> 29 #include <boost/preprocessor/list/adt.hpp> 30 #include <boost/preprocessor/list/for_each_i.hpp> 31 #include <boost/preprocessor/list/append.hpp> 32 33 // PRIVATE // 34 35 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_PARAMS_VAR_(id) \ 36 BOOST_LOCAL_FUNCTION_AUX_SYMBOL( (params)(id) ) 37 38 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_TAG_DECL_(r, id, i, bind_traits) \ 39 BOOST_SCOPE_EXIT_DETAIL_TAG_DECL(r, id, i, \ 40 BOOST_LOCAL_FUNCTION_AUX_PP_BIND_TRAITS_VAR_WITHOUT_TYPE( \ 41 bind_traits)) 42 43 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_CAPTURE_DECL_TYPED_( \ 44 r, id, typename01, i, bind_traits) \ 45 typedef BOOST_PP_EXPR_IIF(typename01, typename) \ 46 /* remove ref because typed var can have & prefix */ \ 47 ::boost::remove_reference< BOOST_PP_EXPR_IIF(typename01, typename) \ 48 ::boost::function_traits< \ 49 /* instead of using Boost.Typeof, get bind type as 1st */ \ 50 /* argument type of func type `void (type_ [&] var_)` */ \ 51 void ( BOOST_LOCAL_FUNCTION_AUX_PP_BIND_TRAITS_VAR_WITH_TYPE( \ 52 bind_traits) ) \ 53 >::arg1_type \ 54 >::type \ 55 BOOST_SCOPE_EXIT_DETAIL_CAPTURE_T(id, i, \ 56 BOOST_LOCAL_FUNCTION_AUX_PP_BIND_TRAITS_VAR_WITHOUT_TYPE( \ 57 bind_traits)) \ 58 ; /* close typedef */ 59 60 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_CAPTURE_DECL_DEDUCED_( \ 61 r, id, typename01, i, bind_traits) \ 62 BOOST_SCOPE_EXIT_DETAIL_CAPTURE_DECL(r, \ 63 ( \ 64 id \ 65 , \ 66 /* ScopeExit expects typename or EMPTY() here */ \ 67 BOOST_PP_EXPR_IIF(typename01, typename) \ 68 ), \ 69 i, BOOST_LOCAL_FUNCTION_AUX_PP_BIND_TRAITS_VAR_WITHOUT_TYPE( \ 70 bind_traits)) 71 72 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_CAPTURE_DECL_( \ 73 r, id_typename, i, bind_traits) \ 74 BOOST_PP_IIF(BOOST_PP_IS_EMPTY( \ 75 BOOST_LOCAL_FUNCTION_AUX_PP_BIND_TRAITS_VAR_WITH_TYPE(bind_traits)), \ 76 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_CAPTURE_DECL_DEDUCED_ \ 77 , \ 78 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_CAPTURE_DECL_TYPED_ \ 79 )(r, BOOST_PP_TUPLE_ELEM(2, 0, id_typename), \ 80 BOOST_PP_TUPLE_ELEM(2, 1, id_typename), i, bind_traits) 81 82 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_PARAM_DECL_( \ 83 r, id_typename, i, bind_traits) \ 84 BOOST_SCOPE_EXIT_DETAIL_PARAM_DECL(r, \ 85 ( \ 86 BOOST_PP_TUPLE_ELEM(2, 0, id_typename) \ 87 , \ 88 /* ScopeExit expects typename or EMPTY() here */ \ 89 BOOST_PP_EXPR_IIF(BOOST_PP_TUPLE_ELEM(2, 1, id_typename), \ 90 typename \ 91 ) \ 92 ), \ 93 i, BOOST_LOCAL_FUNCTION_AUX_PP_BIND_TRAITS_VAR_WITHOUT_TYPE( \ 94 bind_traits)) 95 96 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_MEMBER_DECL_VAR_( \ 97 r, id, typename01, i, var) \ 98 BOOST_PP_EXPR_IIF(typename01, typename) \ 99 BOOST_IDENTITY_TYPE(( /* must use IDENTITY because of tparam comma */ \ 100 ::boost::scope_exit::detail::member< \ 101 BOOST_SCOPE_EXIT_DETAIL_PARAM_T(id, i, var) \ 102 , BOOST_SCOPE_EXIT_DETAIL_TAG(id, i) \ 103 > \ 104 )) \ 105 BOOST_SCOPE_EXIT_DETAIL_PARAM(id, i, var); 106 107 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_MEMBER_DECL_( \ 108 r, id_typename, i, bind_traits) \ 109 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_MEMBER_DECL_VAR_(r, \ 110 BOOST_PP_TUPLE_ELEM(2, 0, id_typename), \ 111 BOOST_PP_TUPLE_ELEM(2, 1, id_typename), \ 112 i, BOOST_LOCAL_FUNCTION_AUX_PP_BIND_TRAITS_VAR_WITHOUT_TYPE( \ 113 bind_traits)) 114 115 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_PARAM_INIT_(r, id, i, bind_traits) \ 116 BOOST_SCOPE_EXIT_DETAIL_PARAM_INIT(r, id, i, \ 117 BOOST_LOCAL_FUNCTION_AUX_PP_BIND_TRAITS_VAR_WITHOUT_TYPE( \ 118 bind_traits)) 119 120 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_CAPTURE_TYPE_(id) \ 121 BOOST_LOCAL_FUNCTION_AUX_SYMBOL( (this_capture_type)(id) ) 122 123 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_TYPE_(id) \ 124 BOOST_LOCAL_FUNCTION_AUX_SYMBOL( (this_type)(id) ) 125 126 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_TYPEDEF_DEDUCED_( \ 127 id, typename01, all_bind_this_types) \ 128 BOOST_SCOPE_EXIT_DETAIL_TYPEDEF_TYPEOF_THIS(id, \ 129 BOOST_PP_EXPR_IIF(typename01, typename), /* otherwise EMPTY() */ \ 130 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_CAPTURE_TYPE_(id)) 131 132 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_TYPEDEF_TYPED_( \ 133 id, typename01, all_bind_this_types) \ 134 typedef \ 135 BOOST_LOCAL_FUNCTION_AUX_PP_BIND_TRAITS_THIS_TYPE(BOOST_PP_LIST_FIRST( \ 136 all_bind_this_types)) \ 137 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_CAPTURE_TYPE_(id) \ 138 ; 139 140 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_TYPEDEF_( \ 141 id, typename01, all_bind_this_types) \ 142 /* typedef type_ */ \ 143 BOOST_PP_IIF(BOOST_PP_IS_EMPTY( \ 144 /* all_bind_this_type is list with 1 elem (possibly PP_EMPTY), */ \ 145 /* otherwise got a pp-parsing error before getting here */ \ 146 BOOST_LOCAL_FUNCTION_AUX_PP_BIND_TRAITS_THIS_TYPE( \ 147 BOOST_PP_LIST_FIRST(all_bind_this_types))), \ 148 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_TYPEDEF_DEDUCED_ \ 149 , \ 150 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_TYPEDEF_TYPED_ \ 151 )(id, typename01, all_bind_this_types) 152 153 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_ALL_( \ 154 all_binds, all_bind_this_types, id, typename01) \ 155 /* binding tags */ \ 156 BOOST_PP_IIF(BOOST_PP_LIST_IS_CONS(all_bind_this_types), \ 157 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_TYPEDEF_ \ 158 , \ 159 BOOST_PP_TUPLE_EAT(3) \ 160 )(id, typename01, all_bind_this_types) \ 161 BOOST_PP_LIST_FOR_EACH_I(BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_TAG_DECL_, id, \ 162 all_binds) \ 163 BOOST_PP_LIST_FOR_EACH_I(BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_CAPTURE_DECL_, \ 164 (id, typename01), all_binds) \ 165 /* binding class */ \ 166 struct BOOST_SCOPE_EXIT_DETAIL_PARAMS_T(id) { \ 167 /* interim capture types to workaround internal error on old GCC */ \ 168 BOOST_PP_EXPR_IIF(BOOST_PP_LIST_IS_CONS(all_bind_this_types), \ 169 typedef BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_CAPTURE_TYPE_(id) \ 170 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_TYPE_(id) ; \ 171 ) \ 172 BOOST_PP_EXPR_IIF(BOOST_PP_LIST_IS_CONS(all_bind_this_types), \ 173 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_TYPE_(id) \ 174 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_VAR; \ 175 ) \ 176 BOOST_PP_LIST_FOR_EACH_I( \ 177 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_PARAM_DECL_, \ 178 (id, typename01), all_binds) \ 179 BOOST_PP_LIST_FOR_EACH_I( \ 180 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_MEMBER_DECL_, \ 181 (id, typename01), all_binds) \ 182 } BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_PARAMS_VAR_(id) = \ 183 /* NOTE: there is no way to wrap member initializer commas within */ \ 184 /* parenthesis so you must handle these commas manually if expanding */ \ 185 /* this macro within another macro */ \ 186 { \ 187 /* initialize the struct with param values to bind */ \ 188 BOOST_PP_EXPR_IIF(BOOST_PP_LIST_IS_CONS(all_bind_this_types), \ 189 this) /* here name `this` access object at enclosing scope */ \ 190 BOOST_PP_COMMA_IF(BOOST_PP_BITAND( \ 191 BOOST_PP_LIST_IS_CONS(all_bind_this_types) \ 192 , BOOST_PP_LIST_IS_CONS(all_binds) \ 193 )) \ 194 BOOST_PP_LIST_FOR_EACH_I( \ 195 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_PARAM_INIT_, id, all_binds) \ 196 }; 197 198 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_(id, typename01, decl_traits) \ 199 /* IMPORTANT: the order of these appends is important, it must follow */ \ 200 /* the indexing order used by the functor code which starts */ \ 201 /* enumerating const binds and then non-const binds */ \ 202 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_ALL_( \ 203 BOOST_PP_LIST_APPEND( \ 204 BOOST_LOCAL_FUNCTION_AUX_PP_DECL_TRAITS_CONST_BINDS( \ 205 decl_traits),\ 206 BOOST_LOCAL_FUNCTION_AUX_PP_DECL_TRAITS_BINDS( \ 207 decl_traits)), \ 208 BOOST_PP_LIST_APPEND( \ 209 BOOST_LOCAL_FUNCTION_AUX_PP_DECL_TRAITS_CONST_BIND_THIS_TYPES(decl_traits), \ 210 BOOST_LOCAL_FUNCTION_AUX_PP_DECL_TRAITS_BIND_THIS_TYPES( \ 211 decl_traits)), \ 212 id, typename01) 213 214 // PUBLIC // 215 216 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_TYPE(id, typename01) \ 217 BOOST_PP_EXPR_IIF(typename01, typename) \ 218 BOOST_SCOPE_EXIT_DETAIL_PARAMS_T(id):: \ 219 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_TYPE_(id) 220 221 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_THIS_VAR \ 222 BOOST_LOCAL_FUNCTION_AUX_SYMBOL( (this_var) ) 223 224 #define BOOST_LOCAL_FUNCTION_AUX_CODE_BIND(id, typename01, decl_traits) \ 225 /* the binding data structures must be declared and initialized (to */ \ 226 /* empty structs, so hopefully the compiler will optimize away the */ \ 227 /* no-op code) even when there is no bound param because these structs */ \ 228 /* are used to init `...args.value` which is always used by the `END` */ \ 229 /* macro later because this macro does not know if there are bound */ \ 230 /* params or not */ \ 231 BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_(id, typename01, decl_traits) \ 232 /* this code takes advantage of the template argument list/comparison */ \ 233 /* operator ambiguity to declare a variable iff it hasn't already been */ \ 234 /* declared in that scope; the second occurrence is parsed as: */ \ 235 /* (declared<(resolve<sizeof(boost_local_auxXargs)>::cmp1<0)>::cmp2> */ \ 236 /* ...Xargs); */ \ 237 /* which is a no-op */ \ 238 ::boost::scope_exit::detail::declared< boost::scope_exit::detail::resolve< \ 239 /* cannot prefix with `::` as in `sizeof(:: ...` because the name */ \ 240 /* must refer to the local variable name to allow multiple local */ \ 241 /* functions (and exits) within the same scope (however this */ \ 242 /* does not allow for nesting because local variables cannot be */ \ 243 /* used in nested code blocks) */ \ 244 sizeof(BOOST_LOCAL_FUNCTION_AUX_DECL_ARGS_VAR) \ 245 >::cmp1<0>::cmp2 > BOOST_LOCAL_FUNCTION_AUX_DECL_ARGS_VAR; \ 246 /* stores bound types/values into `...args` variable (args variable */ \ 247 /* can be accessed by `NAME` macro because doesn't use __LINE__ id) */ \ 248 BOOST_LOCAL_FUNCTION_AUX_DECL_ARGS_VAR.value = \ 249 &BOOST_LOCAL_FUNCTION_AUX_CODE_BIND_PARAMS_VAR_(id); 250 251 #endif // #include guard 252 253