/// Allows to bind Qt signals with arguments `({cpp_args})` to a Rust closure. /// /// Create an object using `new()` and bind your closure using `set()`. /// The closure will be called with the signal's arguments when the slot is invoked. /// Use `connect()` method of a `qt_core::connection::Signal` object to connect the signal /// to this slot. The closure will be executed each time the slot is invoked /// until source signals are disconnected or the slot object is destroyed. /// /// The slot object takes ownership of the passed closure. If `set()` is called again, /// previously set closure is dropped. Make sure that the slot object does not outlive /// objects referenced by the closure. /// /// If `set()` was not called, slot invokation has no effect. pub struct {pub_type_name}<'a> {{ wrapper: ::cpp_utils::CppBox<{type_name}>, func: ::std::option::Option>>, }} impl<'a> {pub_type_name}<'a> {{ /// Constructs a new object. pub fn new(f: F) -> {pub_type_name}<'a> {{ let mut obj = {pub_type_name}::default(); obj.set(f); obj }} /// Sets `f` as the callback closure. If `set()` is called again, previous closure is dropped. pub fn set(&mut self, f: F) {{ self.clear(); let mut func_box: Box> = Box::new(Box::new(f)); unsafe {{ self.wrapper.set({callback_name}, ::std::mem::transmute(func_box.as_mut())); }} self.func = Some(func_box); }} /// Drops the previously set closure, if any. After this, slot invokation will have no effect /// until a new closure is set. pub fn clear(&mut self) {{ if self.func.is_some() {{ unsafe {{ self.wrapper.set( ::std::mem::transmute(0usize), ::std::ptr::null_mut()); }} self.func = None; }} }} }} impl<'a> Default for {pub_type_name}<'a> {{ fn default() -> Self {{ {pub_type_name} {{ wrapper: {type_name}::new(), func: None, }} }} }} impl<'a> {connections_mod}::Receiver for {pub_type_name}<'a> {{ type Arguments = ({args_tuple}); fn object(&self) -> &{object_type_name} {{ {connections_mod}::Receiver::object(self.wrapper.as_ref()) }} fn receiver_id() -> &'static [u8] {{ <{type_name} as {connections_mod}::Receiver>::receiver_id() }} }} extern "C" fn {callback_name}(data: *mut ::libc::c_void, {callback_args}) {{ let func: &mut Box = unsafe {{ ::std::mem::transmute(data) }}; func({func_args}); }}