www.digitalmars.com         C & C++   DMDScript  

D - signal slot mechanism

reply pedro_alves <pedro_alves_member pathlink.com> writes:
Hi guys, I just started to look at the D language, and I can't seem to find an
answer to a simple question.

What about a signal/slot mechanism?
Is there any mechanism that imprements a callback system in the D language that
is similar the the signal/slot mechanism found in libsig++ or QT from trolltech?


Compliments,

Pedro Alves
Portugal
Jan 15 2003
parent reply Burton Radons <loth users.sourceforge.net> writes:
pedro_alves wrote:
 Hi guys, I just started to look at the D language, and I can't seem to find an
 answer to a simple question.
 
 What about a signal/slot mechanism?
 Is there any mechanism that imprements a callback system in the D language that
 is similar the the signal/slot mechanism found in libsig++ or QT from
trolltech?
Yeah, delegates, under the Types section of the spec.
Jan 15 2003
parent diablito bk.ru writes:
Like this

Module ss.d

typedef void delegate(...)  SIGNAL;
typedef void delegate(...)  SLOT;

struct SIGNAL_SLOT
{
SIGNAL _in;
SLOT _out;
}

struct SIGNAL_SIGNAL
{
SIGNAL _in;
SIGNAL _out;
}

extern (D) SIGNAL_SLOT[int] signal_slot;
extern (D) SIGNAL_SIGNAL[int] signal_signal;


static class SS
{
//**************************************************************************
static void connect(SIGNAL _in, SLOT _out)
{
int index = -1;
int keys[] = signal_slot.keys;

foreach (int key; keys)
{
if (signal_slot[key]._in==_in && signal_slot[key]._out==_out )
{
index = key;
break;
}
}

if (index==-1)
{
SIGNAL_SLOT tmp;
tmp._in = _in;
tmp._out = _out;
signal_slot[signal_slot.length+1] = tmp;
}

}


//**************************************************************************
static void connect2(SIGNAL _in, SIGNAL _out)
{
if (_in==_out){return;}

int index = -1;
int keys[] = signal_signal.keys;

foreach (int key; keys)
{
if (signal_signal[key]._in==_in && signal_signal[key]._out==_out )
{
index = key;
break;
}
}

if (index==-1)
{
SIGNAL_SIGNAL tmp;
tmp._in = _in;
tmp._out = _out;
signal_signal[signal_signal.length+1] = tmp;
}
}


//**************************************************************************
static void disconnect(SIGNAL _in, SLOT _out)
{
int keys[] = signal_slot.keys;

foreach (int key; keys)
{
if (signal_slot[key]._in==_in && signal_slot[key]._out==_out )
{
signal_slot.remove(key);
break;
}
}
}

//**************************************************************************
static void disconnect2(SIGNAL _in, SIGNAL _out)
{
int keys[] = signal_signal.keys;

foreach (int key; keys)
{
if (signal_signal[key]._in==_in && signal_signal[key]._out==_out )
{
signal_signal.remove(key);
break;
}
}
}


//**************************************************************************
static void signal(SIGNAL _in, void *_argptr, TypeInfo[] _arguments)
{
int keys[] = signal_slot.keys;
foreach (int key; keys)
{
if (signal_slot[key]._in==_in )
{
signal_slot[key]._out(_argptr,_arguments);
}
}

int keys2[] = signal_signal.keys;
foreach (int key; keys2)
{
if (signal_signal[key]._in==_in )
{
//signal_signal[key]._out(_argptr,_arguments);
}
}
}


}



Example how use

import ss;


class test
{
this(){}
~this(){}


void signal(...)
{
printf("signal\n");
SS.signal(&this.signal,_argptr,_arguments);

}

void signal2(...)
{
printf("signal2\n");
SS.signal(&this.signal2,_argptr,_arguments);
}



void slot(...)
{
printf("slot\n");
}

void slot2(...)
{
printf("slot2\n");
}
}


void f(int f)
{
test t = new test();

SS.connect(&t.signal, &t.slot);
SS.connect(&t.signal2, &t.slot2);
SS.connect(&t.signal, &t.signal2);
t.signal();

delete t;
}
Jul 16 2006