/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. module core.event;
  8. // Description: This class represents an object that can dispatch signals.
  9. class Dispatcher {
  10. void onPush(Responder rsp) {
  11. }
  12. // Description: This function will set the responder that will receive
  13. // signals raised by this class.
  14. // rsp: The class that will respond to the signal.
  15. void responder(Responder rsp) {
  16. _responder = rsp;
  17. }
  18. Responder responder() {
  19. return _responder;
  20. }
  21. protected:
  22. // Description: This function will emit a signal to the Responder that
  23. // is listening to this Dispatcher.
  24. // signal: The identifier for the signal.
  25. // Returns: Will return true when the signal was handled.
  26. bool raiseSignal(uint signal) {
  27. if (_responder !is null) {
  28. // Raise the event on the Responder, if it does not respond,
  29. // tell the Responder to pass the event up the responder
  30. // chain.
  31. if (!_responder.onSignal(this, signal)) {
  32. // recursively raise the event
  33. return _responder.raiseSignal(signal);
  34. }
  35. }
  36. return true;
  37. }
  38. private:
  39. Responder _responder;
  40. }
  41. // Description: This class represents an object that can respond to signals.
  42. class Responder : Dispatcher {
  43. public:
  44. bool onSignal(Dispatcher dispatcher, uint signal) {
  45. return false;
  46. }
  47. // Description: This function will attach the specified Dispatcher to this
  48. // Responder. It fires an onPush event for the Dispatcher as well.
  49. void push(Dispatcher dsp) {
  50. dsp.responder = this;
  51. dsp.onPush(this);
  52. }
  53. }