| 1 | #ifndef _GI_GOBJECT_CLOSURE_EXTRA_DEF_HPP_ |
| 2 | #define _GI_GOBJECT_CLOSURE_EXTRA_DEF_HPP_ |
| 3 | |
| 4 | namespace gi |
| 5 | { |
| 6 | namespace repository |
| 7 | { |
| 8 | namespace GObject |
| 9 | { |
| 10 | class Closure; |
| 11 | |
| 12 | namespace base |
| 13 | { |
| 14 | class ClosureExtra : public GI_GOBJECT_CLOSURE_BASE |
| 15 | { |
| 16 | typedef GI_GOBJECT_CLOSURE_BASE super; |
| 17 | |
| 18 | public: |
| 19 | template<typename Wrapper> |
| 20 | static GClosure *from_wrapper(Wrapper *w) |
| 21 | { |
| 22 | auto closure = g_cclosure_new( |
| 23 | (GCallback)&w->wrapper, w, (GClosureNotify)(GCallback)&w->destroy); |
| 24 | g_closure_sink(g_closure_ref(closure)); |
| 25 | g_closure_set_marshal(closure, g_cclosure_marshal_generic); |
| 26 | return closure; |
| 27 | } |
| 28 | |
| 29 | // add some convenience helper to create a Closure |
| 30 | // Closure is incomplete type at this stage, so use Ret to avoid complaints |
| 31 | |
| 32 | // Functor is a callable F = C++ signature |
| 33 | // where all arguments are implicitly assumed transfer_none |
| 34 | template<typename F, typename Functor, typename Ret = Closure> |
| 35 | static Ret from_functor(Functor &&f) |
| 36 | { |
| 37 | auto w = |
| 38 | new gi::detail::transform_signal_wrapper<F>(std::forward<Functor>(f)); |
| 39 | return gi::wrap(from_wrapper(w), gi::transfer_full); |
| 40 | } |
| 41 | |
| 42 | // Callback is a generated callback type |
| 43 | template<typename Callback, typename Ret = Closure> |
| 44 | static Ret from_callback(Callback &&cb) |
| 45 | { |
| 46 | auto w = gi::unwrap(cb, gi::scope_notified); |
| 47 | return gi::wrap(from_wrapper(w), gi::transfer_full); |
| 48 | } |
| 49 | }; // class |
| 50 | |
| 51 | #undef GI_GOBJECT_CLOSURE_BASE |
| 52 | #define GI_GOBJECT_CLOSURE_BASE base::ClosureExtra |
| 53 | |
| 54 | } // namespace base |
| 55 | |
| 56 | } // namespace GObject |
| 57 | |
| 58 | } // namespace repository |
| 59 | |
| 60 | } // namespace gi |
| 61 | |
| 62 | #endif |