| 1 | /* |
| 2 | * Copyright 2003 - 2016, The libsigc++ Development Team |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | #ifndef SIGC_ADAPTORS_BIND_H |
| 20 | #define SIGC_ADAPTORS_BIND_Hcxx . . $ `sigc++/adaptors/bind.h:20:9`! |
| 21 | |
| 22 | #include <sigc++/adaptors/adapts.h>cxx . . $ `<file>/sigc++/adaptors/adapts.h`/ |
| 23 | #include <sigc++/adaptors/bound_argument.h>cxx . . $ `<file>/sigc++/adaptors/bound_argument.h`/ |
| 24 | #include <sigc++/adaptors/tuple_visitor_visit_each.h>cxx . . $ `<file>/sigc++/adaptors/tuple_visitor_visit_each.h`/ |
| 25 | #include <sigc++/tuple-utils/tuple_for_each.h>cxx . . $ `<file>/sigc++/tuple-utils/tuple_for_each.h`/ |
| 26 | #include <sigc++/tuple-utils/tuple_start.h>cxx . . $ `<file>/sigc++/tuple-utils/tuple_start.h`/ |
| 27 | #include <sigc++/tuple-utils/tuple_end.h>cxx . . $ `<file>/sigc++/tuple-utils/tuple_end.h`/ |
| 28 | #include <sigc++/tuple-utils/tuple_transform_each.h>cxx . . $ `<file>/sigc++/tuple-utils/tuple_transform_each.h`/ |
| 29 | #include <functional> |
| 30 | |
| 31 | namespace sigccxx . . $ sigc/ |
| 32 | { |
| 33 | |
| 34 | /** @defgroup bind bind(), bind_return() |
| 35 | * sigc::bind() alters an arbitrary functor by fixing arguments to certain values. |
| 36 | * For single argument binding, overloads of sigc::bind() are provided that let you |
| 37 | * specify the zero-based position of the argument to fix with the first template parameter. |
| 38 | * (A value of @p -1 fixes the last argument so sigc::bind<-1>() gives the same result as |
| 39 | * sigc::bind().) |
| 40 | * The types of the arguments can optionally be specified if not deduced. |
| 41 | * |
| 42 | * @par Examples: |
| 43 | * @code |
| 44 | * void foo(int, int, int); |
| 45 | * // single argument binding ... |
| 46 | * sigc::bind(&foo,1)(2,3); //fixes the last (third) argument and calls foo(2,3,1) |
| 47 | * sigc::bind<-1>(&foo,1)(2,3); //same as bind(&foo,1)(2,3) (calls foo(2,3,1)) |
| 48 | * sigc::bind<0>(&foo,1)(2,3); //fixes the first argument and calls foo(1,2,3) |
| 49 | * sigc::bind<1>(&foo,1)(2,3); //fixes the second argument and calls foo(2,1,3) |
| 50 | * sigc::bind<2>(&foo,1)(2,3); //fixes the third argument and calls foo(2,3,1) |
| 51 | * // multi argument binding ... |
| 52 | * sigc::bind(&foo,1,2)(3); //fixes the last two arguments and calls foo(3,1,2) |
| 53 | * sigc::bind(&foo,1,2,3)(); //fixes all three arguments and calls foo(1,2,3) |
| 54 | * @endcode |
| 55 | * |
| 56 | * The functor that sigc::bind() returns can be passed directly into |
| 57 | * @ref sigc::signal_with_accumulator::connect() "sigc::signal::connect()" or |
| 58 | * @ref sigc::signal_with_accumulator::connect_first() "sigc::signal::connect_first()". |
| 59 | * |
| 60 | * @par Example: |
| 61 | * @code |
| 62 | * sigc::signal<void()> some_signal; |
| 63 | * void foo(int); |
| 64 | * some_signal.connect(sigc::bind(&foo,1)); |
| 65 | * @endcode |
| 66 | * |
| 67 | * sigc::bind_return() alters an arbitrary functor by |
| 68 | * fixing its return value to a certain value. |
| 69 | * |
| 70 | * @par Example: |
| 71 | * @code |
| 72 | * void foo(); |
| 73 | * std::cout << sigc::bind_return(&foo, 5)(); // calls foo() and returns 5 |
| 74 | * @endcode |
| 75 | * |
| 76 | * You can bind references to functors by passing the objects through |
| 77 | * the std::ref() helper function. |
| 78 | * |
| 79 | * @par Example: |
| 80 | * @code |
| 81 | * int some_int; |
| 82 | * sigc::signal<void()> some_signal; |
| 83 | * void foo(int&); |
| 84 | * some_signal.connect(sigc::bind(&foo, std::ref(some_int))); |
| 85 | * @endcode |
| 86 | * |
| 87 | * If you bind an object of a sigc::trackable derived type to a functor |
| 88 | * by reference, a slot assigned to the bind adaptor is cleared automatically |
| 89 | * when the object goes out of scope. |
| 90 | * |
| 91 | * @par Example: |
| 92 | * @code |
| 93 | * struct bar : public sigc::trackable {} some_bar; |
| 94 | * sigc::signal<void()> some_signal; |
| 95 | * void foo(bar&); |
| 96 | * some_signal.connect(sigc::bind(&foo, std::ref(some_bar))); |
| 97 | * // disconnected automatically if some_bar goes out of scope |
| 98 | * @endcode |
| 99 | * |
| 100 | * @ingroup adaptors |
| 101 | */ |
| 102 | |
| 103 | namespace internalcxx . . $ sigc/internal/ |
| 104 | { |
| 105 | |
| 106 | template<typename T_element> |
| 107 | struct TransformEachInvokercxx . . $ sigc/internal/TransformEachInvoker# |
| 108 | { |
| 109 | // We take T_element as non-const because invoke() is not const. |
| 110 | // TODO: Take element as T_element&& ? |
| 111 | constexpr static decltype(auto) transformcxx . . $ sigc/internal/TransformEachInvoker#transform(f4228a0b465c78e).(T_element& element) { return element.invoke(); } |
| 112 | }; |
| 113 | |
| 114 | } // namespace internal |
| 115 | |
| 116 | /** Adaptor that binds arguments to the wrapped functor. |
| 117 | * Use the convenience function sigc::bind() to create an instance of sigc::bind_functor. |
| 118 | * |
| 119 | * The following template arguments are used: |
| 120 | * - @e I_location Zero-based position of the argument to fix (@p -1 for the last argument). |
| 121 | * - @e T_bound Types of the bound argument. |
| 122 | * - @e T_functor Type of the functor to wrap. |
| 123 | * |
| 124 | * @ingroup bind |
| 125 | */ |
| 126 | template<int I_location, typename T_functor, typename... T_bound> |
| 127 | struct bind_functorcxx . . $ sigc/bind_functor# : public adaptscxx . . $ sigc/adapts#<T_functor> |
| 128 | { |
| 129 | /** Invokes the wrapped functor passing on the arguments. |
| 130 | * bound_ is passed as the next argument. |
| 131 | * @param arg Arguments to be passed on to the functor. |
| 132 | * @return The return value of the functor invocation. |
| 133 | */ |
| 134 | template<typename... T_arg> |
| 135 | decltype(auto) operatorcxx . . $ sigc/bind_functor#`operator()`(56e6a2751545b43b).()(T_arg&&... arg) |
| 136 | { |
| 137 | // For instance, if I_location is 1, and arg has 4 arguments, |
| 138 | // we would want to call operator() with (arg0, bound, arg1, arg2). |
| 139 | |
| 140 | using tuple_type_argscxx . . $ sigc/bind_functor#`operator()`(56e6a2751545b43b).tuple_type_args# = stdcxx . . $ std/::tuplecxx . . $ std/tuple#<type_trait_pass_tcxx . . $ sigc/type_trait_pass_t#<T_arg>...>; |
| 141 | const auto t_args = stdcxx . . $ std/::tuplecxx . . $ std/tuple#<T_arg...>(stdcxx . . $ std/::forwardcxx . . $ std/forward(f56228ea88711827).<T_arg>(arg)...); |
| 142 | constexpr auto t_args_size = stdcxx . . $ std/::tuple_sizecxx . . $ std/tuple_size#<tuple_type_args>::value; |
| 143 | |
| 144 | // Prevent calling tuple_start<> with values that will cause a compilation error. |
| 145 | static_assert(I_location <= t_args_size, |
| 146 | "I_location must be less than or equal to the number of arguments."); |
| 147 | |
| 148 | const auto t_start = internalcxx . . $ sigc/internal/::tuple_startcxx . . $ sigc/internal/tuple_start(fb2f2aa7f1f50df6).<I_location>(t_args); |
| 149 | const auto t_bound = internalcxx . . $ sigc/internal/::tuple_transform_eachcxx . . $ sigc/internal/tuple_transform_each(fb2f2aa7f1f50df6).<internalcxx . . $ sigc/internal/::TransformEachInvoker>(bound_cxx . . $ sigc/bind_functor#bound_.); |
| 150 | const auto t_end = internalcxx . . $ sigc/internal/::tuple_endcxx . . $ sigc/internal/tuple_end(fb2f2aa7f1f50df6).<t_args_size - I_location>(t_args); |
| 151 | const auto t_with_bound = stdcxx . . $ std/::tuple_catcxx . . $ std/tuple_cat(e4b7fdbc2fd7b4b4).(t_start, t_bound, t_end); |
| 152 | |
| 153 | return stdcxx . . $ std/::applycxx . . $ std/apply(a33bb6fa5bc61f12).(this->functor_cxx . . $ sigc/adapts#functor_., t_with_bound); |
| 154 | } |
| 155 | |
| 156 | /** Constructs a bind_functor object that binds an argument to the passed functor. |
| 157 | * @param func Functor to invoke from operator()(). |
| 158 | * @param bound Argument to bind to the functor. |
| 159 | */ |
| 160 | bind_functorcxx . . $ sigc/bind_functor#`bind_functor<I_location, T_functor, T_bound...>`(3681483430992e9e).(type_trait_take_tcxx . . $ sigc/type_trait_take_t#<T_functor> func, type_trait_take_tcxx . . $ sigc/type_trait_take_t#<T_bound>... bound) |
| 161 | : adaptscxx . . $ sigc/adapts#<T_functor>(func), bound_cxx . . $ sigc/bind_functor#bound_.(bound...) |
| 162 | { |
| 163 | } |
| 164 | |
| 165 | // public to avoid template friend declarations (used by visitor::do_visit_each()) |
| 166 | /// The arguments bound to the functor. |
| 167 | stdcxx . . $ std/::tuplecxx . . $ std/tuple#<bound_argumentcxx . . $ sigc/bound_argument#<T_bound>...> bound_cxx . . $ sigc/bind_functor#bound_.; |
| 168 | }; |
| 169 | |
| 170 | /** Adaptor that binds argument(s) to the wrapped functor. |
| 171 | * This template specialization fixes the last argument(s) of the wrapped functor. |
| 172 | * |
| 173 | * @ingroup bind |
| 174 | */ |
| 175 | template<typename T_functor, typename... T_type> |
| 176 | struct bind_functorcxx . . $ sigc/bind_functor#<-1, T_functor, T_type...> : public adaptscxx . . $ sigc/adapts#<T_functor> |
| 177 | { |
| 178 | public: |
| 179 | /** Invokes the wrapped functor passing on the arguments. |
| 180 | * bound_ is passed as the next argument. |
| 181 | * @param arg Arguments to be passed on to the functor. |
| 182 | * @return The return value of the functor invocation. |
| 183 | */ |
| 184 | template<typename... T_arg> |
| 185 | decltype(auto) operatorcxx . . $ sigc/bind_functor#`operator()`(56e6a2751545b43b).()(T_arg&&... arg) |
| 186 | { |
| 187 | // For instance, if arg has 4 arguments, |
| 188 | // we would want to call operator() with (arg0, arg1, arg2, bound). |
| 189 | |
| 190 | const auto t_args = stdcxx . . $ std/::tuplecxx . . $ std/tuple#<T_arg...>(stdcxx . . $ std/::forwardcxx . . $ std/forward(f56228ea88711827).<T_arg>(arg)...); |
| 191 | const auto t_bound = internalcxx . . $ sigc/internal/::tuple_transform_eachcxx . . $ sigc/internal/tuple_transform_each(fb2f2aa7f1f50df6).<internalcxx . . $ sigc/internal/::TransformEachInvoker>(bound_cxx . . $ sigc/bind_functor#bound_.); |
| 192 | const auto t_with_bound = stdcxx . . $ std/::tuple_catcxx . . $ std/tuple_cat(e4b7fdbc2fd7b4b4).(t_args, t_bound); |
| 193 | |
| 194 | return stdcxx . . $ std/::applycxx . . $ std/apply(a33bb6fa5bc61f12).(this->functor_cxx . . $ sigc/adapts#functor_., t_with_bound); |
| 195 | } |
| 196 | |
| 197 | /** Constructs a bind_functor object that binds an argument to the passed functor. |
| 198 | * @param func Functor to invoke from operator()(). |
| 199 | * @param bound Arguments to bind to the functor. |
| 200 | */ |
| 201 | bind_functorcxx . . $ sigc/bind_functor#`bind_functor<-1, T_functor, T_type...>`(fd09d0d7bdb8cb2a).(type_trait_take_tcxx . . $ sigc/type_trait_take_t#<T_functor> func, type_trait_take_tcxx . . $ sigc/type_trait_take_t#<T_type>... bound) |
| 202 | : adaptscxx . . $ sigc/adapts#<T_functor>(func), bound_cxx . . $ sigc/bind_functor#bound_.(bound...) |
| 203 | { |
| 204 | } |
| 205 | |
| 206 | // public to avoid template friend declarations (used by visitor::do_visit_each()) |
| 207 | /// The argument bound to the functor. |
| 208 | stdcxx . . $ std/::tuplecxx . . $ std/tuple#<bound_argumentcxx . . $ sigc/bound_argument#<T_type>...> bound_cxx . . $ sigc/bind_functor#bound_.; |
| 209 | }; |
| 210 | |
| 211 | #ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 212 | // template specialization of visitor<>::do_visit_each<>(action, functor): |
| 213 | /** Performs a functor on each of the targets of a functor. |
| 214 | * The function overload for sigc::bind_functor performs a functor on the |
| 215 | * functor and on the object instances stored in the sigc::bind_functor object. |
| 216 | * |
| 217 | * @ingroup bind |
| 218 | */ |
| 219 | template<int T_loc, typename T_functor, typename... T_bound> |
| 220 | struct visitorcxx . . $ sigc/visitor#<bind_functorcxx . . $ sigc/bind_functor#<T_loc, T_functor, T_bound...>> |
| 221 | { |
| 222 | template<typename T_action> |
| 223 | static void do_visit_eachcxx . . $ sigc/visitor#do_visit_each(945e9fff1cca170c).(const T_action& action, |
| 224 | const bind_functorcxx . . $ sigc/bind_functor#<T_loc, T_functor, T_bound...>& target) |
| 225 | { |
| 226 | sigccxx . . $ sigc/::visit_eachcxx . . $ sigc/visit_each(13c11f7bd0e7e3d2).(action, target.functor_cxx . . $ sigc/adapts#functor_.); |
| 227 | sigccxx . . $ sigc/::visit_eachcxx . . $ sigc/visit_each(13c11f7bd0e7e3d2).(action, stdcxx . . $ std/::getcxx . . $ std/get(5aabc72a970aa7ce).<0>(target.bound_cxx . . $ sigc/bind_functor#bound_.)); |
| 228 | } |
| 229 | }; |
| 230 | |
| 231 | // template specialization of visitor<>::do_visit_each<>(action, functor): |
| 232 | /** Performs a functor on each of the targets of a functor. |
| 233 | * The function overload for sigc::bind_functor performs a functor on the |
| 234 | * functor and on the object instances stored in the sigc::bind_functor object. |
| 235 | * |
| 236 | * @ingroup bind |
| 237 | */ |
| 238 | template<typename T_functor, typename... T_type> |
| 239 | struct visitorcxx . . $ sigc/visitor#<bind_functorcxx . . $ sigc/bind_functor#<-1, T_functor, T_type...>> |
| 240 | { |
| 241 | template<typename T_action> |
| 242 | static void do_visit_eachcxx . . $ sigc/visitor#do_visit_each(30e5cbc8f3c26469).(const T_action& action, |
| 243 | const bind_functorcxx . . $ sigc/bind_functor#<-1, T_functor, T_type...>& target) |
| 244 | { |
| 245 | sigccxx . . $ sigc/::visit_eachcxx . . $ sigc/visit_each(13c11f7bd0e7e3d2).(action, target.functor_cxx . . $ sigc/adapts#functor_.); |
| 246 | |
| 247 | sigccxx . . $ sigc/::internalcxx . . $ sigc/internal/::tuple_for_eachcxx . . $ sigc/internal/tuple_for_each(a7bd5da4e6b724ef).<internalcxx . . $ sigc/internal/::TupleVisitorVisitEach>(target.bound_cxx . . $ sigc/bind_functor#bound_., action); |
| 248 | } |
| 249 | }; |
| 250 | |
| 251 | #endif // DOXYGEN_SHOULD_SKIP_THIS |
| 252 | |
| 253 | /** Creates an adaptor of type sigc::bind_functor which binds the passed argument to the passed |
| 254 | * functor. |
| 255 | * The optional template argument @e I_location specifies the zero-based |
| 256 | * position of the argument to be fixed (@p -1 stands for the last argument). |
| 257 | * |
| 258 | * @param func Functor that should be wrapped. |
| 259 | * @param b Arguments to bind to @e func. |
| 260 | * @return Adaptor that executes @e func with the bound argument on invocation. |
| 261 | * |
| 262 | * @ingroup bind |
| 263 | */ |
| 264 | template<int I_location, typename T_functor, typename... T_bound> |
| 265 | inline decltype(auto) |
| 266 | bindcxx . . $ sigc/bind(308ebd3d357202eb).(const T_functor& func, T_bound... b) |
| 267 | { |
| 268 | return bind_functorcxx . . $ sigc/bind_functor#<I_location, T_functor, T_bound...>(func, b...); |
| 269 | } |
| 270 | |
| 271 | /** Creates an adaptor of type sigc::bind_functor which fixes the last arguments of the passed |
| 272 | * functor. |
| 273 | * This function overload fixes the last arguments of @e func. |
| 274 | * |
| 275 | * @param func Functor that should be wrapped. |
| 276 | * @param b Arguments to bind to @e func. |
| 277 | * @return Adaptor that executes func with the bound argument on invocation. |
| 278 | * |
| 279 | * @ingroup bind |
| 280 | */ |
| 281 | template<typename T_functor, typename... T_type> |
| 282 | inline decltype(auto) |
| 283 | bindcxx . . $ sigc/bind(d0af3ca68815daea).(const T_functor& func, T_type... b) |
| 284 | { |
| 285 | return bind_functorcxx . . $ sigc/bind_functor#<-1, T_functor, T_type...>(func, b...); |
| 286 | } |
| 287 | |
| 288 | } /* namespace sigc */ |
| 289 | |
| 290 | #endif /* SIGC_ADAPTORS_BIND_H */ |