| 1 | #ifndef GI_CALLBACK_HPP |
| 2 | #define GI_CALLBACK_HPP |
| 3 | |
| 4 | #include "base.hpp" |
| 5 | #include "exception.hpp" |
| 6 | #include "wrap.hpp" |
| 7 | |
| 8 | GI_MODULE_EXPORT |
| 9 | namespace gi |
| 10 | { |
| 11 | namespace detail |
| 12 | { |
| 13 | #if GI_CONFIG_EXCEPTIONS |
| 14 | |
| 15 | inline ::GError ** |
| 16 | find_gerror(bool &has_gerror) |
| 17 | { |
| 18 | has_gerror = false; |
| 19 | return nullptr; |
| 20 | } |
| 21 | |
| 22 | inline ::GError ** |
| 23 | find_gerror(bool &has_gerror, ::GError **error) |
| 24 | { |
| 25 | has_gerror = true; |
| 26 | return error; |
| 27 | } |
| 28 | |
| 29 | template<typename CT, typename... CType> |
| 30 | inline ::GError ** |
| 31 | find_gerror(bool &has_gerror, CT /*arg*/, CType... args) |
| 32 | { |
| 33 | return find_gerror(has_gerror, args...); |
| 34 | } |
| 35 | |
| 36 | inline ::GError * |
| 37 | exception_error(const repository::GLib::Error &exc) |
| 38 | { |
| 39 | return g_error_copy(exc.gobj_()); |
| 40 | } |
| 41 | |
| 42 | inline ::GError * |
| 43 | exception_error(const repository::GLib::Error_Ref &exc) |
| 44 | { |
| 45 | return g_error_copy(exc.gobj_()); |
| 46 | } |
| 47 | |
| 48 | template<typename E> |
| 49 | inline ::GError * |
| 50 | exception_error(const E &exc) |
| 51 | { |
| 52 | static auto quark = g_quark_from_static_string("gi-error-quark"); |
| 53 | return g_error_new(quark, 0, "%s", exception_desc(exc).c_str()); |
| 54 | } |
| 55 | |
| 56 | template<bool SILENT = FALSE, typename E, typename... CType> |
| 57 | void |
| 58 | report_exception(const E &exc, CType... args) |
| 59 | { |
| 60 | // see if we can really report error somewhere |
| 61 | bool has_gerror = false; |
| 62 | GError **error = find_gerror(has_gerror, args...); |
| 63 | if (has_gerror) { |
| 64 | g_return_if_fail(error == NULL || *error == NULL); |
| 65 | if (error) |
| 66 | *error = exception_error(exc); |
| 67 | // if caller does not need/want error, exception disappears here |
| 68 | } else { |
| 69 | // simply report the hard and simple way |
| 70 | // otherwise catch internally if something else/more is desired |
| 71 | if (!SILENT) { |
| 72 | auto msg = std::string("handler exception; ") + exception_desc(exc); |
| 73 | g_critical("%s", msg.c_str()); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | #endif |
| 78 | |
| 79 | // (re)float only applies to GObject |
| 80 | template<typename CppType, typename Transfer, |
| 81 | typename std::enable_if<!traits::is_object<CppType>::value>::type * = |
| 82 | nullptr> |
| 83 | auto |
| 84 | unwrap_maybe_float(CppType &&v, const Transfer &t) |
| 85 | { |
| 86 | return unwrap(std::forward<CppType>(v), t); |
| 87 | } |
| 88 | |
| 89 | // (re) float only for transfer none/floating |
| 90 | template<typename CppType, |
| 91 | typename std::enable_if<traits::is_object<CppType>::value>::type * = |
| 92 | nullptr> |
| 93 | inline typename traits::ctype<CppType>::type |
| 94 | unwrap_maybe_float(CppType &&v, const transfer_full_t &t) |
| 95 | { |
| 96 | return unwrap(std::forward<CppType>(v), t); |
| 97 | } |
| 98 | |
| 99 | template<typename CppType, |
| 100 | typename std::enable_if<traits::is_object<CppType>::value>::type * = |
| 101 | nullptr> |
| 102 | inline typename traits::ctype<CppType>::type |
| 103 | unwrap_maybe_float(CppType &&v, const transfer_none_t &) |
| 104 | { |
| 105 | // expected called with r-value |
| 106 | static_assert(!std::is_reference<CppType>::value, ""); |
| 107 | // steal/take from wrapper |
| 108 | auto result = (typename traits::ctype<CppType>::type)v.release_(); |
| 109 | // the following is essentially a bit of a hack as mentioned in |
| 110 | // https://bugzilla.gnome.org/show_bug.cgi?id=693393) |
| 111 | // that is, we are about to return an object to C (from binding/callback) |
| 112 | // and this should be done with none transfer |
| 113 | // this none may actually mean floating (e.g. a factory-like callback) |
| 114 | // but no way to know from annotations |
| 115 | // so if at runtime the wrapper actually holds the only reference, |
| 116 | // then it is about to be destroyed (when wrapper goes away) |
| 117 | // *before* the object can make it back to caller |
| 118 | // so turn that ref into a floating one (as only that makes sense) |
| 119 | // if it is not the only ref, it is kept alive elsewhere |
| 120 | // (as typically so for a "getter" callback) |
| 121 | // so it is really treated as none |
| 122 | auto obj = (GObject *)result; |
| 123 | // theoretically not MT safe, but if == 1, only 1 thread should be involved |
| 124 | if (obj->ref_count == 1) { |
| 125 | g_object_force_floating(obj); |
| 126 | } else { |
| 127 | // otherwise unref as wrapper would have |
| 128 | g_object_unref(obj); |
| 129 | } |
| 130 | return result; |
| 131 | } |
| 132 | |
| 133 | template<typename T> |
| 134 | constexpr T |
| 135 | unconst(T t) |
| 136 | { |
| 137 | return t; |
| 138 | } |
| 139 | |
| 140 | inline char * |
| 141 | unconst(const char *t) |
| 142 | { |
| 143 | return (char *)t; |
| 144 | } |
| 145 | |
| 146 | // helper types to provide additional argument info beyond Transfer |
| 147 | template<std::size_t... index> |
| 148 | struct args_index |
| 149 | { |
| 150 | static constexpr auto value = std::make_tuple(index...); |
| 151 | using value_type = decltype(value); |
| 152 | }; |
| 153 | |
| 154 | template<typename Transfer, bool _inout, typename CustomTraits = void, |
| 155 | typename ArgsIndex = args_index<>> |
| 156 | struct arg_info |
| 157 | { |
| 158 | using transfer_type = Transfer; |
| 159 | static constexpr bool inout = _inout; |
| 160 | // tuple of index; used to selects the C arguments (to assemble C++ argument) |
| 161 | using args_type = ArgsIndex; |
| 162 | // additional info as used by corresponding cb_arg_handler |
| 163 | using custom_type = CustomTraits; |
| 164 | }; |
| 165 | |
| 166 | // access above info by forwarding types |
| 167 | template<typename T, typename Enable = void> |
| 168 | struct arg_traits |
| 169 | { |
| 170 | using transfer_type = typename T::transfer_type; |
| 171 | static constexpr bool inout = T::inout; |
| 172 | using args_type = typename T::args_type; |
| 173 | using custom_type = typename T::custom_type; |
| 174 | }; |
| 175 | |
| 176 | // legacy case; only transfer type |
| 177 | template<typename T> |
| 178 | struct arg_traits<T, |
| 179 | typename std::enable_if<std::is_base_of<transfer_t, T>::value>::type> |
| 180 | { |
| 181 | using transfer_type = T; |
| 182 | static constexpr bool inout = false; |
| 183 | using args_type = args_index<>; |
| 184 | using custom_type = void; |
| 185 | }; |
| 186 | |
| 187 | // IndexTuple is essentially an args_index<...> |
| 188 | template<typename IndexTuple, std::size_t... Index, typename F, |
| 189 | typename ArgTuple> |
| 190 | decltype(auto) |
| 191 | apply_with_args(std::index_sequence<Index...>, F &&f, ArgTuple &&args) |
| 192 | { |
| 193 | return f(std::get<std::get<Index>(IndexTuple::value)>(args)...); |
| 194 | } |
| 195 | |
| 196 | template<typename IndexTuple, typename F, typename... Args> |
| 197 | decltype(auto) |
| 198 | apply_with_args(F &&f, Args &&...args) |
| 199 | { |
| 200 | return apply_with_args<IndexTuple>( |
| 201 | std::make_index_sequence< |
| 202 | std::tuple_size<typename IndexTuple::value_type>::value>(), |
| 203 | std::forward<F>(f), std::forward_as_tuple(args...)); |
| 204 | } |
| 205 | |
| 206 | // a simple callback has no (need for) args_index |
| 207 | template<typename T> |
| 208 | struct is_simple_cb : public std::true_type |
| 209 | {}; |
| 210 | |
| 211 | template<typename Transfer, typename... Transfers> |
| 212 | struct is_simple_cb<std::tuple<Transfer, Transfers...>> |
| 213 | { |
| 214 | static constexpr bool value = |
| 215 | std::tuple_size< |
| 216 | typename arg_traits<Transfer>::args_type::value_type>::value == 0 || |
| 217 | (sizeof...(Transfers) > 0 && |
| 218 | is_simple_cb<std::tuple<Transfers...>>::value); |
| 219 | }; |
| 220 | |
| 221 | template<typename T, typename CT = void> |
| 222 | struct map_cpp_function; |
| 223 | |
| 224 | // handles all calls C -> C++ (callbacks, virtual method calls) |
| 225 | // restrictions though on types supported (enforced by code generation) |
| 226 | template<typename T, typename RetTransfer, typename ArgTransfers, |
| 227 | typename CT = typename map_cpp_function<T>::type, |
| 228 | bool SIMPLE = is_simple_cb<ArgTransfers>::value> |
| 229 | struct transform_caller; |
| 230 | |
| 231 | // helper used below that provides pre-call and post-call steps |
| 232 | // to handle each argument's conversion to and from C++ |
| 233 | // in so-called (most) simple cases, there is one-to-one mapping between |
| 234 | // C and C++ arguments and C++ argument type that allows to deduce context |
| 235 | // (in particular, no callbacks or sized array) |
| 236 | // as such, proper steps can be obtained by specialization on Cpp argument type |
| 237 | template<typename CppArg, typename Enable = void> |
| 238 | struct cb_arg_handler; |
| 239 | |
| 240 | // in simple cases, C signature can be derived from C++ signature |
| 241 | template<typename T, typename CT> |
| 242 | struct map_cpp_function |
| 243 | { |
| 244 | using type = CT; |
| 245 | }; |
| 246 | |
| 247 | template<typename R, typename... Args> |
| 248 | struct map_cpp_function<R(Args...), void> |
| 249 | { |
| 250 | using type = typename traits::ctype<R>::type( |
| 251 | typename cb_arg_handler<Args>::c_type...); |
| 252 | }; |
| 253 | |
| 254 | // signature used in virtual method handling |
| 255 | template<typename R, typename... Args> |
| 256 | struct map_cpp_function<R (*)(Args...), void> |
| 257 | { |
| 258 | using type = typename traits::ctype<R>::type( |
| 259 | typename cb_arg_handler<Args>::c_type...); |
| 260 | }; |
| 261 | |
| 262 | // NOTE function type R(const A) is identical to R(A) |
| 263 | // so no deduced Args below will retain const (if such) |
| 264 | // in simple cases, *Transfer* is simply a transfer type |
| 265 | // but it may also be a more elaborate argument trait |
| 266 | |
| 267 | template<typename R, typename... Args, typename RetTransfer, |
| 268 | typename... Transfers, typename CR, typename... CArgs, bool SIMPLE> |
| 269 | struct transform_caller<R(Args...), RetTransfer, std::tuple<Transfers...>, |
| 270 | CR(CArgs...), SIMPLE> |
| 271 | { |
| 272 | static_assert(sizeof...(Args) == sizeof...(Transfers), ""); |
| 273 | static_assert(!SIMPLE || sizeof...(Args) == sizeof...(CArgs), ""); |
| 274 | |
| 275 | typedef transform_caller self_type; |
| 276 | typedef R (*caller_type)(Args &&..., void *d); |
| 277 | |
| 278 | private: |
| 279 | static R do_call(Args &&...args, caller_type func, void *d) |
| 280 | { |
| 281 | return func(std::forward<Args>(args)..., d); |
| 282 | } |
| 283 | |
| 284 | // helper that provides context for pack expansion below |
| 285 | static void dummy_call(...){}; |
| 286 | |
| 287 | template<typename T> |
| 288 | static auto _tt(const T &) |
| 289 | { |
| 290 | return typename arg_traits<T>::transfer_type(); |
| 291 | } |
| 292 | |
| 293 | // non-void return |
| 294 | template<typename T, std::size_t... Index, |
| 295 | typename std::enable_if<SIMPLE && !std::is_void<T>::value>::type * = |
| 296 | nullptr> |
| 297 | static CR _wrapper( |
| 298 | CArgs... args, caller_type func, void *d, std::index_sequence<Index...>) |
| 299 | { |
| 300 | std::tuple<cb_arg_handler<Args>...> handlers; |
| 301 | auto ret = do_call( |
| 302 | std::get<Index>(handlers).arg(args, _tt(Transfers()), Transfers())..., |
| 303 | func, d); |
| 304 | dummy_call((std::get<Index>(handlers).post(args, _tt(Transfers())), 0)...); |
| 305 | return unconst(unwrap_maybe_float(std::move(ret), RetTransfer())); |
| 306 | } |
| 307 | |
| 308 | // void return |
| 309 | template<typename T, std::size_t... Index, |
| 310 | typename std::enable_if<SIMPLE && std::is_void<T>::value>::type * = |
| 311 | nullptr> |
| 312 | static CR _wrapper( |
| 313 | CArgs... args, caller_type func, void *d, std::index_sequence<Index...>) |
| 314 | { |
| 315 | std::tuple<cb_arg_handler<Args>...> handlers; |
| 316 | do_call( |
| 317 | std::get<Index>(handlers).arg(args, _tt(Transfers()), Transfers())..., |
| 318 | func, d); |
| 319 | dummy_call((std::get<Index>(handlers).post(args, _tt(Transfers())), 0)...); |
| 320 | } |
| 321 | |
| 322 | // complex; non-void return |
| 323 | template<typename T, std::size_t... Index, |
| 324 | typename std::enable_if<!SIMPLE && !std::is_void<T>::value>::type * = |
| 325 | nullptr> |
| 326 | static CR _wrapper( |
| 327 | CArgs... args, caller_type func, void *d, std::index_sequence<Index...>) |
| 328 | { |
| 329 | std::tuple<cb_arg_handler<Args>...> handlers; |
| 330 | auto ret = do_call( |
| 331 | apply_with_args<typename arg_traits<Transfers>::args_type>( |
| 332 | [&handlers](auto... selargs) mutable -> decltype(auto) { |
| 333 | return std::get<Index>(handlers).arg(selargs..., |
| 334 | typename arg_traits<Transfers>::transfer_type(), Transfers()); |
| 335 | }, |
| 336 | args...)..., |
| 337 | func, d); |
| 338 | dummy_call((apply_with_args<typename arg_traits<Transfers>::args_type>( |
| 339 | [&handlers](auto... selargs) mutable -> decltype(auto) { |
| 340 | return std::get<Index>(handlers).post(selargs..., |
| 341 | typename arg_traits<Transfers>::transfer_type()); |
| 342 | }, |
| 343 | args...), |
| 344 | 0)...); |
| 345 | return unconst(unwrap_maybe_float(std::move(ret), RetTransfer())); |
| 346 | } |
| 347 | |
| 348 | // complex; void return |
| 349 | template<typename T, std::size_t... Index, |
| 350 | typename std::enable_if<!SIMPLE && std::is_void<T>::value>::type * = |
| 351 | nullptr> |
| 352 | static CR _wrapper( |
| 353 | CArgs... args, caller_type func, void *d, std::index_sequence<Index...>) |
| 354 | { |
| 355 | std::tuple<cb_arg_handler<Args>...> handlers; |
| 356 | do_call(apply_with_args<typename arg_traits<Transfers>::args_type>( |
| 357 | [&handlers](auto... selargs) mutable -> decltype(auto) { |
| 358 | return std::get<Index>(handlers).arg(selargs..., |
| 359 | typename arg_traits<Transfers>::transfer_type(), |
| 360 | Transfers()); |
| 361 | }, |
| 362 | args...)..., |
| 363 | func, d); |
| 364 | dummy_call((apply_with_args<typename arg_traits<Transfers>::args_type>( |
| 365 | [&handlers](auto... selargs) mutable -> decltype(auto) { |
| 366 | return std::get<Index>(handlers).post(selargs..., |
| 367 | typename arg_traits<Transfers>::transfer_type()); |
| 368 | }, |
| 369 | args...), |
| 370 | 0)...); |
| 371 | } |
| 372 | |
| 373 | public: |
| 374 | static CR wrapper(CArgs... args, caller_type func, void *d) |
| 375 | { |
| 376 | // exceptions should not escape into plain C |
| 377 | #if GI_CONFIG_EXCEPTIONS |
| 378 | try { |
| 379 | #endif |
| 380 | return self_type::template _wrapper<R>( |
| 381 | args..., func, d, std::make_index_sequence<sizeof...(Args)>()); |
| 382 | #if GI_CONFIG_EXCEPTIONS |
| 383 | } catch (const repository::GLib::Error &exc) { |
| 384 | report_exception(exc, args...); |
| 385 | } catch (const repository::GLib::Error_Ref &exc) { |
| 386 | report_exception(exc, args...); |
| 387 | } catch (const std::exception &exc) { |
| 388 | report_exception(exc, args...); |
| 389 | } catch (...) { |
| 390 | report_exception(nullptr, args...); |
| 391 | } |
| 392 | return typename traits::ctype<R>::type(); |
| 393 | #endif |
| 394 | } |
| 395 | }; |
| 396 | |
| 397 | // minor helper traits used below |
| 398 | namespace _traits |
| 399 | { |
| 400 | template<typename T> |
| 401 | struct remove_all_pointers |
| 402 | { |
| 403 | using type = T; |
| 404 | }; |
| 405 | |
| 406 | template<typename T> |
| 407 | struct remove_all_pointers<T *> |
| 408 | { |
| 409 | using type = typename remove_all_pointers<T>::type; |
| 410 | }; |
| 411 | |
| 412 | template<typename T> |
| 413 | struct remove_all_pointers<T *const> |
| 414 | { |
| 415 | using type = typename remove_all_pointers<T>::type; |
| 416 | }; |
| 417 | |
| 418 | template<typename T> |
| 419 | using is_basic_or_void = typename std::conditional<traits::is_basic<T>::value || |
| 420 | std::is_void<T>::value, |
| 421 | std::true_type, std::false_type>::type; |
| 422 | |
| 423 | template<typename T> |
| 424 | using is_basic_argument = typename std::conditional< |
| 425 | is_basic_or_void<typename std::remove_cv< |
| 426 | typename remove_all_pointers<T>::type>::type>::value, |
| 427 | std::true_type, std::false_type>::type; |
| 428 | |
| 429 | template<typename T> |
| 430 | using is_const_lvalue = typename std::conditional< |
| 431 | std::is_lvalue_reference<T>::value && |
| 432 | std::is_const<typename std::remove_reference<T>::type>::value, |
| 433 | std::true_type, std::false_type>::type; |
| 434 | |
| 435 | } // namespace _traits |
| 436 | |
| 437 | // generic fallback case; assume input parameter |
| 438 | // (also covers boxed callerallocates, which pretty much is/becomes input) |
| 439 | template<typename CppArg, typename Enable> |
| 440 | struct cb_arg_handler |
| 441 | { |
| 442 | using c_type = typename traits::ctype<CppArg>::type; |
| 443 | |
| 444 | // use generic CType to handle const differences wrt c_type |
| 445 | template<typename CType, typename Transfer, typename ArgTrait> |
| 446 | typename std::decay<CppArg>::type arg( |
| 447 | CType arg, const Transfer &t, const ArgTrait &) noexcept |
| 448 | { |
| 449 | // wrap to normalized destination target (no const &) |
| 450 | // the destination type here is really only relevant for collection cases |
| 451 | // (since that depends on the contained element) |
| 452 | // otherwise all the info is pretty much in c_type type |
| 453 | return wrap_to<typename std::decay<CppArg>::type>(arg, t); |
| 454 | } |
| 455 | |
| 456 | // minor variation; dynamic sized array input |
| 457 | // also accepts length input |
| 458 | template<typename CType, typename Transfer, typename ArgTrait> |
| 459 | typename std::decay<CppArg>::type arg( |
| 460 | CType arg, int length, const Transfer &t, const ArgTrait &) noexcept |
| 461 | { |
| 462 | // destination also really needed here |
| 463 | return wrap_to<typename std::decay<CppArg>::type>(arg, length, t); |
| 464 | } |
| 465 | |
| 466 | void post(...){}; |
| 467 | }; |
| 468 | |
| 469 | // passthrough on (pointers to) basic values; |
| 470 | // handles input/output of basic values, as well as arrays of such |
| 471 | template<typename CppArg> |
| 472 | struct cb_arg_handler<CppArg, |
| 473 | typename std::enable_if<_traits::is_basic_argument<CppArg>::value>::type> |
| 474 | { |
| 475 | using c_type = CppArg; |
| 476 | |
| 477 | template<typename CType, typename Transfer, typename ArgTrait> |
| 478 | CType arg(CType arg, const Transfer &, const ArgTrait &) noexcept |
| 479 | { |
| 480 | return arg; |
| 481 | } |
| 482 | void post(...){}; |
| 483 | |
| 484 | // array size case; inout C int* to in Cpp int |
| 485 | // (only enable if so tagged by non-default custom trait type) |
| 486 | template<typename CType, typename Transfer, typename ArgTrait> |
| 487 | typename std::enable_if< |
| 488 | !std::is_void<typename arg_traits<ArgTrait>::custom_type>::value, |
| 489 | CType>::type |
| 490 | arg(CType *arg, const Transfer &, const ArgTrait &) noexcept |
| 491 | { |
| 492 | return *arg; |
| 493 | } |
| 494 | }; |
| 495 | |
| 496 | // handle "complex" (in)out argument |
| 497 | // (though also handles e.g. int& case, which might be optimized, but anyways) |
| 498 | // these are recognized/assumed to be a pointer or reference to non-basic type |
| 499 | template<typename CppArg> |
| 500 | struct cb_arg_handler<CppArg, |
| 501 | typename std::enable_if< |
| 502 | !_traits::is_basic_argument<CppArg>::value && |
| 503 | (std::is_pointer<CppArg>::value || |
| 504 | (std::is_lvalue_reference<CppArg>::value && |
| 505 | !_traits::is_const_lvalue<CppArg>::value))>::type> |
| 506 | { |
| 507 | using BaseCppType = |
| 508 | typename std::decay<typename std::remove_pointer<CppArg>::type>::type; |
| 509 | // no more pointer expected here |
| 510 | // (other than for void* cases, which do not introspect well, but anyways) |
| 511 | static_assert(!std::is_pointer<BaseCppType>::value || |
| 512 | std::is_same<BaseCppType, gpointer>::value || |
| 513 | std::is_same<BaseCppType, gconstpointer>::value, |
| 514 | ""); |
| 515 | using c_type = typename traits::ctype<BaseCppType>::type *; |
| 516 | |
| 517 | // intermediate helper storage |
| 518 | BaseCppType var_{}; |
| 519 | |
| 520 | // pointer case |
| 521 | CppArg rv(std::true_type) { return &var_; } |
| 522 | // ref case |
| 523 | CppArg rv(std::false_type) { return var_; } |
| 524 | |
| 525 | // handle const variations (e.g. const char**) |
| 526 | template<typename T, typename X> |
| 527 | static void assign(T *&t, X val) |
| 528 | { |
| 529 | t = const_cast<T *>(val); |
| 530 | } |
| 531 | |
| 532 | template<typename T, typename X> |
| 533 | static void assign(T &t, X val) |
| 534 | { |
| 535 | t = unconst(val); |
| 536 | } |
| 537 | |
| 538 | template<typename Transfer, typename ArgTrait> |
| 539 | CppArg arg(c_type arg, const Transfer &t, const ArgTrait &) |
| 540 | { |
| 541 | bool inout = arg_traits<ArgTrait>::inout; |
| 542 | // a plain type has no special needs |
| 543 | // so we can always take any bogus value as-is |
| 544 | // (which is then less sensitive to incorrect annotation) |
| 545 | if (arg && (inout || traits::is_plain<BaseCppType>::value)) |
| 546 | var_ = wrap_to<BaseCppType>(*arg, t); |
| 547 | return rv(std::is_pointer<CppArg>()); |
| 548 | } |
| 549 | |
| 550 | // overall function call happens following arg call above |
| 551 | // post invoked after function call |
| 552 | template<typename Transfer> |
| 553 | void post(c_type arg, const Transfer &t) |
| 554 | { |
| 555 | if (arg) |
| 556 | assign(*arg, unwrap_maybe_float(std::move(var_), t)); |
| 557 | } |
| 558 | |
| 559 | // sized array variants; arguments (data, size) |
| 560 | // latter could be input (int) or (in)out (int*) |
| 561 | template<typename Transfer, typename Int, typename ArgTrait> |
| 562 | CppArg arg(c_type arg, Int size, const Transfer &t, const ArgTrait &) |
| 563 | { |
| 564 | bool inout = arg_traits<ArgTrait>::inout; |
| 565 | if (arg && inout) |
| 566 | var_ = wrap_to<BaseCppType>(*arg, size, t); |
| 567 | return rv(std::is_pointer<CppArg>()); |
| 568 | } |
| 569 | |
| 570 | template<typename Transfer, typename Int, typename ArgTrait> |
| 571 | CppArg arg(c_type arg, Int *size, const Transfer &t, const ArgTrait &) |
| 572 | { |
| 573 | bool inout = arg_traits<ArgTrait>::inout; |
| 574 | if (arg && size && inout) |
| 575 | var_ = wrap_to<BaseCppType>(*arg, *size, t); |
| 576 | return rv(std::is_pointer<CppArg>()); |
| 577 | } |
| 578 | |
| 579 | template<typename Transfer, typename Int> |
| 580 | void post(c_type arg, Int, const Transfer &t) |
| 581 | { |
| 582 | post(arg, nullptr, t); |
| 583 | } |
| 584 | |
| 585 | template<typename Transfer, typename Int> |
| 586 | void post(c_type arg, Int *size, const Transfer &t) |
| 587 | { |
| 588 | if (arg) |
| 589 | assign(*arg, unwrap_maybe_float(std::move(var_), t)); |
| 590 | if (size) |
| 591 | *size = var_.size(); |
| 592 | } |
| 593 | }; |
| 594 | |
| 595 | // handles callback argument |
| 596 | // (as argument in anther callback, most likely a virtual method) |
| 597 | template<typename CppArg> |
| 598 | struct cb_arg_handler<CppArg, |
| 599 | typename std::enable_if< |
| 600 | std::is_pointer<typename CppArg::cfunction_type>::value>::type> |
| 601 | { |
| 602 | template<typename CType, typename Transfer, typename ArgTrait> |
| 603 | CppArg arg(CType cb, gpointer userdata, ::GDestroyNotify destroy, |
| 604 | const Transfer &, const ArgTrait &) noexcept |
| 605 | { |
| 606 | using ct = typename arg_traits<ArgTrait>::custom_type; |
| 607 | // setup shared pointer to userdata with destroy as Deleter |
| 608 | auto sp = destroy ? std::shared_ptr<void>(userdata, destroy) : nullptr; |
| 609 | // keep userdata/destroy alive as long as lambda handler |
| 610 | auto h = [cb, userdata, sp = std::move(sp)]( |
| 611 | auto &&...args) -> decltype(auto) { |
| 612 | // original callback type CType should match deduced handler_cb_tpe |
| 613 | // but let's make sure as usual |
| 614 | return ct::handler(std::forward<decltype(args)>(args)..., |
| 615 | typename ct::handler_cb_type(cb), userdata); |
| 616 | }; |
| 617 | return h; |
| 618 | } |
| 619 | |
| 620 | template<typename CType, typename Transfer, typename ArgTrait> |
| 621 | CppArg arg(CType cb, gpointer userdata, const Transfer &t, |
| 622 | const ArgTrait &tt) noexcept |
| 623 | { |
| 624 | return arg(cb, userdata, nullptr, t, tt); |
| 625 | } |
| 626 | |
| 627 | void post(...){}; |
| 628 | }; |
| 629 | |
| 630 | class connection_status |
| 631 | { |
| 632 | public: |
| 633 | struct data |
| 634 | { |
| 635 | bool connected{false}; |
| 636 | }; |
| 637 | |
| 638 | bool connected() const |
| 639 | { |
| 640 | auto sp = data_.lock(); |
| 641 | return sp && sp->connected; |
| 642 | } |
| 643 | |
| 644 | protected: |
| 645 | std::weak_ptr<data> data_; |
| 646 | }; |
| 647 | |
| 648 | // callback handling |
| 649 | template<typename G> |
| 650 | class connectable; |
| 651 | |
| 652 | template<typename G, bool AUTODESTROY = false> |
| 653 | class callback_wrapper; |
| 654 | |
| 655 | template<typename R, typename... Args> |
| 656 | class connectable<R(Args...)> |
| 657 | { |
| 658 | friend class callback_wrapper<R(Args...)>; |
| 659 | struct data : public connection_status::data |
| 660 | { |
| 661 | template<typename T, |
| 662 | typename Enable = typename detail::disable_if_same_or_derived<data, T>> |
| 663 | data(T &&t) : callable(std::forward<T>(t)) |
| 664 | {} |
| 665 | |
| 666 | std::function<R(Args... args)> callable; |
| 667 | }; |
| 668 | |
| 669 | struct connection_status_factory : public connection_status |
| 670 | { |
| 671 | connection_status_factory(std::shared_ptr<data> p) |
| 672 | { |
| 673 | data_ = std::weak_ptr<connection_status::data>(p); |
| 674 | } |
| 675 | }; |
| 676 | |
| 677 | public: |
| 678 | // avoid copy constructor mishaps |
| 679 | template<typename T, |
| 680 | typename Enable = |
| 681 | typename detail::disable_if_same_or_derived<connectable, T>> |
| 682 | connectable(T &&t) : data_(std::make_shared<data>(std::forward<T>(t))) |
| 683 | {} |
| 684 | |
| 685 | connection_status connection() const |
| 686 | { |
| 687 | return connection_status_factory(data_); |
| 688 | } |
| 689 | |
| 690 | R operator()(Args... args) const |
| 691 | { |
| 692 | return data_->callable(std::forward<Args>(args)...); |
| 693 | } |
| 694 | |
| 695 | explicit operator bool() const { return data_ && data_->callable; } |
| 696 | |
| 697 | private: |
| 698 | // state management by wrapper |
| 699 | void connected(bool conn) { data_->connected = conn; } |
| 700 | void disconnect() { data_->connected = false; } |
| 701 | |
| 702 | private: |
| 703 | std::shared_ptr<data> data_; |
| 704 | }; |
| 705 | |
| 706 | template<typename R, typename... Args, bool AUTODESTROY> |
| 707 | class callback_wrapper<R(Args...), AUTODESTROY> |
| 708 | { |
| 709 | typedef callback_wrapper self_type; |
| 710 | |
| 711 | public: |
| 712 | template<typename T, |
| 713 | typename Enable = |
| 714 | typename detail::disable_if_same_or_derived<callback_wrapper, T>> |
| 715 | callback_wrapper(T &&t) : _callback(std::forward<T>(t)) |
| 716 | { |
| 717 | // mark connected now that it is wrapped |
| 718 | _callback.connected(true); |
| 719 | } |
| 720 | |
| 721 | // (only) used by manual callback workaround |
| 722 | static R wrapper(Args... args, gpointer user_data) |
| 723 | { |
| 724 | auto t = reinterpret_cast<callback_wrapper *>(user_data); |
| 725 | std::unique_ptr<self_type> wt(AUTODESTROY ? t : nullptr); |
| 726 | return t->_callback(args...); |
| 727 | } |
| 728 | |
| 729 | static void destroy(gpointer user_data) |
| 730 | { |
| 731 | auto t = reinterpret_cast<callback_wrapper *>(user_data); |
| 732 | delete t; |
| 733 | } |
| 734 | |
| 735 | // (async scope) wrapper may have to take ownership of additional data |
| 736 | // (other callback wrapper) |
| 737 | template<typename T> |
| 738 | void take_data(std::shared_ptr<T> d) |
| 739 | { |
| 740 | auto cb = std::move(_callback.data_->callable); |
| 741 | auto newcb = [d, cb](Args &&...args) { |
| 742 | return cb(std::forward<Args>(args)...); |
| 743 | }; |
| 744 | _callback.data_->callable = std::move(newcb); |
| 745 | } |
| 746 | |
| 747 | template<typename T> |
| 748 | void take_data(T *d) |
| 749 | { |
| 750 | take_data(std::shared_ptr<T>(d)); |
| 751 | } |
| 752 | |
| 753 | ~callback_wrapper() |
| 754 | { |
| 755 | // other shared ptr to data might be around (unlikely though) |
| 756 | // but regardless disconnect now as requested (as wrapper is going away) |
| 757 | _callback.disconnect(); |
| 758 | } |
| 759 | |
| 760 | connectable<R(Args... args)> _callback; |
| 761 | }; |
| 762 | |
| 763 | template<typename G, typename CG = typename map_cpp_function<G>::type> |
| 764 | struct transform_callback_wrapper; |
| 765 | |
| 766 | template<typename R, typename... Args, typename CR, typename... CArgs> |
| 767 | struct transform_callback_wrapper<R(Args...), CR(CArgs...)> |
| 768 | { |
| 769 | // transfers of arguments |
| 770 | template<bool AUTODESTROY, typename RetTransfer, typename... Transfers> |
| 771 | class with_transfer : public callback_wrapper<R(Args...)> |
| 772 | { |
| 773 | typedef callback_wrapper<R(Args...)> super_type; |
| 774 | typedef with_transfer self_type; |
| 775 | |
| 776 | public: |
| 777 | template<typename T> |
| 778 | explicit with_transfer(T &&t) : super_type(std::forward<T>(t)) |
| 779 | {} |
| 780 | |
| 781 | private: |
| 782 | static R caller(Args &&...args, void *d) |
| 783 | { |
| 784 | auto this_ = (self_type *)d; |
| 785 | return this_->_callback(std::forward<Args>(args)...); |
| 786 | } |
| 787 | |
| 788 | public: |
| 789 | static CR wrapper(CArgs... args, gpointer user_data) |
| 790 | { |
| 791 | auto t = reinterpret_cast<self_type *>(user_data); |
| 792 | std::unique_ptr<self_type> wt(AUTODESTROY ? t : nullptr); |
| 793 | return transform_caller<R(Args...), RetTransfer, std::tuple<Transfers...>, |
| 794 | CR(CArgs...)>::wrapper(args..., caller, t); |
| 795 | } |
| 796 | }; |
| 797 | }; |
| 798 | |
| 799 | // used early in declarations, so avoid using unknown types in CSigOrVoid |
| 800 | template<typename CppSig, typename RetTransfer, |
| 801 | typename ArgTransfers = std::tuple<>, typename CSigOrVoid = void> |
| 802 | class callback; |
| 803 | |
| 804 | template<typename CppSig, typename RetTransfer, typename... Transfers, |
| 805 | typename CSigOrVoid> |
| 806 | class callback<CppSig, RetTransfer, std::tuple<Transfers...>, CSigOrVoid> |
| 807 | : public connectable<CppSig> |
| 808 | { |
| 809 | typedef connectable<CppSig> super_type; |
| 810 | |
| 811 | public: |
| 812 | typedef CppSig function_type; |
| 813 | typedef typename map_cpp_function<CppSig, CSigOrVoid>::type CSig; |
| 814 | |
| 815 | template<bool ASYNC = false> |
| 816 | using wrapper_type = typename transform_callback_wrapper<function_type, |
| 817 | CSig>::template with_transfer<ASYNC, RetTransfer, Transfers...>; |
| 818 | |
| 819 | typedef typename std::add_pointer<decltype(wrapper_type<>::wrapper)>::type |
| 820 | cfunction_type; |
| 821 | |
| 822 | using super_type::super_type; |
| 823 | }; |
| 824 | |
| 825 | // signal handling; |
| 826 | // transfer none for arguments |
| 827 | // transfer full for return |
| 828 | template<typename G> |
| 829 | struct transform_signal_wrapper; |
| 830 | |
| 831 | template<typename T> |
| 832 | struct signal_arg_transfer |
| 833 | { |
| 834 | typedef transfer_none_t type; |
| 835 | }; |
| 836 | |
| 837 | template<typename R, typename... Args> |
| 838 | struct transform_signal_wrapper<R(Args...)> |
| 839 | : public transform_callback_wrapper<R( |
| 840 | Args...)>::template with_transfer<false, transfer_full_t, |
| 841 | typename detail::signal_arg_transfer<Args>::type...> |
| 842 | { |
| 843 | private: |
| 844 | typedef typename transform_callback_wrapper<R( |
| 845 | Args...)>::template with_transfer<false, transfer_full_t, |
| 846 | typename detail::signal_arg_transfer<Args>::type...> |
| 847 | super_; |
| 848 | |
| 849 | public: |
| 850 | template<typename T> |
| 851 | transform_signal_wrapper(T &&t) : super_(std::forward<T>(t)) |
| 852 | {} |
| 853 | }; |
| 854 | |
| 855 | // connection helpers |
| 856 | class connection_impl |
| 857 | { |
| 858 | public: |
| 859 | connection_impl(gulong id, connection_status s) : id_(id), status_(s) {} |
| 860 | |
| 861 | bool connected() const { return status_.connected(); } |
| 862 | |
| 863 | gulong id() const { return id_; } |
| 864 | |
| 865 | protected: |
| 866 | gulong id_; |
| 867 | connection_status status_; |
| 868 | }; |
| 869 | |
| 870 | template<typename Connection> |
| 871 | class connection |
| 872 | { |
| 873 | typedef Connection impl; |
| 874 | |
| 875 | public: |
| 876 | connection() = default; |
| 877 | |
| 878 | template<typename... Args> |
| 879 | explicit connection(Args... arg) |
| 880 | : conn_(std::make_shared<impl>(std::forward<Args>(arg)...)) |
| 881 | { |
| 882 | // this is to be expected at this time |
| 883 | if (!connected()) { |
| 884 | g_warning("creating non-connected connection"); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | // implicit copy/move |
| 889 | |
| 890 | bool connected() const { return conn_ && conn_->connected(); } |
| 891 | void disconnect() |
| 892 | { |
| 893 | if (connected()) |
| 894 | conn_->disconnect(); |
| 895 | } |
| 896 | |
| 897 | protected: |
| 898 | std::shared_ptr<impl> conn_; |
| 899 | }; |
| 900 | |
| 901 | template<typename ConnectionBase> |
| 902 | class scoped_connection : public ConnectionBase |
| 903 | { |
| 904 | typedef ConnectionBase connection; |
| 905 | |
| 906 | public: |
| 907 | scoped_connection() : connection() {} |
| 908 | ~scoped_connection() { this->disconnect(); } |
| 909 | |
| 910 | void release() { this->conn_.reset(); } |
| 911 | |
| 912 | // ensure default movable |
| 913 | scoped_connection(scoped_connection &&other) = default; |
| 914 | scoped_connection &operator=(scoped_connection &&other) = default; |
| 915 | |
| 916 | // not copyable; to avoid inadvertent disconnect |
| 917 | scoped_connection(const scoped_connection &other) = delete; |
| 918 | scoped_connection &operator=(const scoped_connection &other) = delete; |
| 919 | |
| 920 | // but convert/assign from base class |
| 921 | scoped_connection(const connection &other) : connection(other) {} |
| 922 | scoped_connection &operator=(const connection &other) |
| 923 | { |
| 924 | (*(connection *)(this)) = other; |
| 925 | return *this; |
| 926 | } |
| 927 | scoped_connection &operator=(const connection &&other) |
| 928 | { |
| 929 | (*(connection *)(this)) = std::move(other); |
| 930 | return *this; |
| 931 | } |
| 932 | }; |
| 933 | |
| 934 | } // namespace detail |
| 935 | |
| 936 | // function bind helpers |
| 937 | template<typename R, typename T, typename Tp, typename... Args> |
| 938 | inline std::function<R(Args...)> |
| 939 | mem_fun(R (T::*pm)(Args...), Tp object) |
| 940 | { |
| 941 | return [object, pm](Args... args) { |
| 942 | return (object->*pm)(std::forward<Args>(args)...); |
| 943 | }; |
| 944 | } |
| 945 | |
| 946 | template<typename R, typename T, typename Tp, typename... Args> |
| 947 | inline std::function<R(Args...)> |
| 948 | mem_fun(R (T::*pm)(Args...) const, Tp object) |
| 949 | { |
| 950 | return [object, pm](Args... args) { |
| 951 | return (object->*pm)(std::forward<Args>(args)...); |
| 952 | }; |
| 953 | } |
| 954 | |
| 955 | // expose for use in fallback scenarios |
| 956 | using detail::callback_wrapper; |
| 957 | |
| 958 | } // namespace gi |
| 959 | |
| 960 | #endif // CALLBACK_HPP |