Commit 012554de authored by Colomban Wendling's avatar Colomban Wendling
Browse files

Add OVCC.SigQueue object for easy signal connections management

This makes easy to disconnect signals form one or more objects, by
keeping a list of signals and their object an allowing for example
to disconnect all signals from the list.
parent b71513d7
......@@ -12,6 +12,7 @@ libovcc_la_SOURCES = board.vala \
game.vala \
pawn.vala \
player.vala \
sigqueue.vala \
stack.vala \
tile.vala \
tileobject.vala \
......
/*
*
* Copyright (C) 2011 Colomban Wendling <ban@herbesfolles.org>
* Jonathan Michalon <studios.chalmion@no-log.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
[compact]
public class OVCC.SigQueue
{
private struct Entry {
Object obj;
ulong id;
}
private SList<Entry?> list = null;
/**
* Add a signal to the list
*
* @param obj The object on which the signal is registered
* @param id The signal ID
*/
public void add (Object obj,
ulong id)
{
Entry e = {obj, id};
list.prepend (e);
}
/* disconnects an entry */
private bool disconnect (Entry e)
{
if (SignalHandler.is_connected (e.obj, e.id)) {
e.obj.disconnect (e.id);
return true;
} else {
warning ("trying to disconnect a non-existent signal");
return false;
}
}
/* removes an entry */
private bool remove_entry (Entry e)
{
bool removed = disconnect (e);
list.remove (e);
return removed;
}
/**
* Removes all signals in the list that are owned by @obj
*
* @param obj The object to remove from the list
* @return The count of removed signals
*/
public uint remove_object (Object obj)
{
var n = 0;
foreach (var e in list) {
if (e.obj == obj) {
remove_entry (e);
n ++;
}
}
return n;
}
/**
* Removes a signal from the list, and disconnect it.
*
* @param obj The object owning the signal
* @param id The signal ID
* @return whether the signal was disconnected
*/
public bool remove (Object obj,
ulong id)
{
foreach (var e in list) {
if (e.obj == obj && e.id == id) {
return remove_entry (e);
}
}
return false;
}
/**
* Removes all signals in the list.
*/
public void remove_all ()
{
foreach (var e in list) {
disconnect (e);
}
list = null;
}
}
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