💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
2.8 KiB

  1. /*
  2. * Copyright (C) 2023 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "event-dispatcher.hpp"
  20. event_dispatcher::event_dispatcher():
  21. updating(false)
  22. {}
  23. event_dispatcher::~event_dispatcher()
  24. {
  25. clear();
  26. }
  27. void event_dispatcher::update(double time)
  28. {
  29. updating = true;
  30. // Process pending subscriptions
  31. for (auto it = to_subscribe.begin(); it != to_subscribe.end(); ++it)
  32. {
  33. handler_map[std::get<0>(*it)].push_back(std::get<1>(*it));
  34. }
  35. to_subscribe.clear();
  36. // Process pending unsubscriptions
  37. for (auto it = to_unsubscribe.begin(); it != to_unsubscribe.end(); ++it)
  38. {
  39. handler_map[std::get<0>(*it)].remove(std::get<1>(*it));
  40. }
  41. to_unsubscribe.clear();
  42. // Dispatch queued events
  43. flush();
  44. // For each scheduled event
  45. for (auto event = scheduled_events.begin(); event != scheduled_events.end();)
  46. {
  47. // If the event is due
  48. if (time >= event->first)
  49. {
  50. // Dispatch event
  51. dispatch(*(event->second));
  52. // Delete event
  53. delete event->second;
  54. event = scheduled_events.erase(event);
  55. }
  56. else
  57. {
  58. break;
  59. }
  60. }
  61. updating = false;
  62. }
  63. void event_dispatcher::dispatch(const event_base& event)
  64. {
  65. // Get list of handlers for this type of event
  66. const std::list<event_handler_base*>& handlers = handler_map[event.get_event_type_id()];
  67. // For each handler
  68. for (auto handler = handlers.begin(); handler != handlers.end(); ++handler)
  69. {
  70. // Pass event to the handler
  71. (*handler)->route_event(event);
  72. }
  73. }
  74. void event_dispatcher::flush()
  75. {
  76. // For each event in the queue
  77. for (auto event = queued_events.begin(); event != queued_events.end(); ++event)
  78. {
  79. // Dispatch event
  80. dispatch(**event);
  81. // Delete event
  82. delete (*event);
  83. }
  84. // Clear event queue
  85. queued_events.clear();
  86. }
  87. void event_dispatcher::clear()
  88. {
  89. // For each event in the queue
  90. for (auto event = queued_events.begin(); event != queued_events.end(); ++event)
  91. {
  92. // Delete event
  93. delete (*event);
  94. }
  95. // Clear event queue
  96. queued_events.clear();
  97. // For each scheduled event
  98. for (auto event = scheduled_events.begin(); event != scheduled_events.end(); ++event)
  99. {
  100. // Delete event
  101. delete event->second;
  102. }
  103. // Clear scheduled events
  104. scheduled_events.clear();
  105. }