PageRenderTime 19ms CodeModel.GetById 14ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/core/event.d

http://github.com/wilkie/djehuty
D | 65 lines | 32 code | 12 blank | 21 comment | 2 complexity | ce908b593225a9090d14644ac6c6f668 MD5 | raw file
 1/*
 2 * event.d
 3 *
 4 * This module implements the event dispatching mechanism.
 5 *
 6 */
 7
 8module core.event;
 9
10// Description: This class represents an object that can dispatch signals.
11class Dispatcher {
12
13	void onPush(Responder rsp) {
14	}
15
16	// Description: This function will set the responder that will receive
17	//	signals raised by this class.
18	// rsp: The class that will respond to the signal.
19	void responder(Responder rsp) {
20		_responder = rsp;
21	}
22	
23	Responder responder() {
24		return _responder;
25	}
26
27protected:
28
29	// Description: This function will emit a signal to the Responder that
30	//	is listening to this Dispatcher.
31	// signal: The identifier for the signal.
32	// Returns: Will return true when the signal was handled.
33	bool raiseSignal(uint signal) {
34		if (_responder !is null) {
35			// Raise the event on the Responder, if it does not respond,
36			// tell the Responder to pass the event up the responder
37			// chain.
38			if (!_responder.onSignal(this, signal)) {
39				// recursively raise the event
40				return _responder.raiseSignal(signal);
41			}
42		}
43		return true;
44	}
45	
46private:
47
48	Responder _responder;
49}
50
51// Description: This class represents an object that can respond to signals.
52class Responder : Dispatcher {
53public:
54
55	bool onSignal(Dispatcher dispatcher, uint signal) {
56		return false;
57	}
58	
59	// Description: This function will attach the specified Dispatcher to this
60	//	Responder. It fires an onPush event for the Dispatcher as well.
61	void push(Dispatcher dsp) {
62		dsp.responder = this;
63		dsp.onPush(this);
64	}
65}