#ifndef _SIGSLOT_h_
#define _SIGSLOT_h_
// sigslot.h – autor Kluev Alexander [email protected]
template lt;class Arggt;
class signal;
class slot {
friend class signal_base;
slot *_prev;
slot *_next;
struct Thunk {};
typedef void (Thunk::*Func)();
Thunk *_trg;
Func _mfn;
public:
slot(): _trg(0), _mfn(0), _prev(0), _next(0) {}
~slot() {clear();}
public:
void clear() {
if (_next) _next-gt;_prev = _prev;
if (_prev) _prev-gt;_next = _next;
_prev = _next = 0;
}
template lt;class Owner, class Arggt;
void init(signallt;Arggt;amp;sig, void (Owner::*mpfn)(Arg), Owner *This) {
clear();
_trg = (Thunk*)This;
_mfn = (Func)mpfn;
sig._add(*this);
}
template lt;class Ownergt;
void init(signallt;voidgt;amp;sig, void (Owner::*mpfn)(), Owner *This) {
clear();
_trg = (Thunk*)This;
_mfn = (Func)mpfn; sig._add(*this);
}
private:
template lt;class Arggt;
void _call(Arg a) {
typedef void (Thunk::*XFunc)(Arg);
XFunc f = (XFunc)_mfn;
(_trg-gt;*f)(a);
}
void _call() {
(_trg-gt;*_mfn)();
}
};
class signal_base {
protected:
friend class slot;
slot _head;
void _add(slotamp;s) {
s._prev =amp;_head;
s._next = _head._next;
if (_head._next) _head._next-gt;_prev =amp;s;
_head._next =amp;s;
}
template lt;class Arggt;
void _raise(Arg a) {
slot *p = _head._next;
while (p) {
p-gt;_call(a);
p = p-gt;_next;
}
}
void _raise() {
slot *p = _head._next;
while (p) {
p-gt;_call();
p = p-gt;_next;
}
}
public:
~signal_base() {
clear();
}
public:
void clear() {
while (_head._next) _head._next-gt;clear();
}
};
template lt;class Arggt;
class signal: public signal_base {
public:
void raise(Arg);
};
typedef void VOID;
template lt;gt;
void signallt;VOIDgt;::raise() {
signal_base::_raise();
}
template lt;class Arggt;
void signallt;Arggt;::raise(Arg a) {
signal_base::_raise(a);
}
#endif // _SIGSLOT_h_