2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit 2d4761ba authored by Artur Grunau's avatar Artur Grunau
Browse files

Add support for disconnecting slots defined in Lua

Up until now it wasn't possible to manually disconnect slots defined in
Lua: they could only be disconnected automatically when their
corresponding signals were destroyed.

Add a special `disconnect` method to sigslot's Lua wrapper that makes it
possible to manually disconnect slots defined in Lua.

References #1
parent a337b188
......@@ -130,6 +130,25 @@ namespace sigslot {
return _dummy_dest;
}
/**
* Check if this connection's slot function is the same as the given Lua function.
*
* @param slot_fn reference to a Lua function acting as a slot
* @return true if this connection wraps the given Lua function, false otherwise
*/
bool wrapsSlotFunction(SWIGLUA_REF slot_fn) {
if (slot_fn.L != _slot_fn.L)
return false;
swiglua_ref_get(&_slot_fn);
swiglua_ref_get(&slot_fn);
bool result = lua_rawequal(slot_fn.L, -1, -2) == 1;
lua_pop(_slot_fn.L, 2);
return result;
}
private:
SWIGLUA_REF _slot_fn; ///< Reference to a Lua function acting as a slot
mutable has_slots<mt_policy>* _dummy_dest; ///< Dummy destination object needed to support getdest()
......@@ -158,6 +177,32 @@ namespace sigslot {
new sigslot::_lua_connection1<arg1_type, mt_policy>(slot_fn);
$self->m_connected_slots.push_back(conn);
}
/**
* Disconnect a Lua function from this signal.
*
* @param slot_fn reference to a Lua function acting as a slot
*/
void disconnect(SWIGLUA_REF slot_fn) {
typedef sigslot::_signal_base1<arg1_type, mt_policy>::connections_list connections_list;
sigslot::lock_block_write<mt_policy> lock($self);
connections_list::iterator it = $self->m_connected_slots.begin();
connections_list::iterator itEnd = $self->m_connected_slots.end();
while (it != itEnd) {
sigslot::_lua_connection1<arg1_type, mt_policy>* lua_connection =
dynamic_cast<sigslot::_lua_connection1<arg1_type, mt_policy>*>(*it);
if (lua_connection != nullptr && lua_connection->wrapsSlotFunction(slot_fn)) {
delete lua_connection;
$self->m_connected_slots.erase(it);
return;
}
++it;
}
}
}
};
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment