💿🐜 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.

107 lines
2.4 KiB

  1. /*
  2. * Copyright (C) 2021 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. {}
  22. event_dispatcher::~event_dispatcher()
  23. {
  24. clear();
  25. }
  26. void event_dispatcher::update(double time)
  27. {
  28. // Process pending subscriptions
  29. for (auto it = to_subscribe.begin(); it != to_subscribe.end(); ++it)
  30. {
  31. handler_map[std::get<0>(*it)].push_back(std::get<1>(*it));
  32. }
  33. to_subscribe.clear();
  34. // Process pending unsubscriptions
  35. for (auto it = to_unsubscribe.begin(); it != to_unsubscribe.end(); ++it)
  36. {
  37. handler_map[std::get<0>(*it)].remove(std::get<1>(*it));
  38. }
  39. to_unsubscribe.clear();
  40. // Dispatch queued events
  41. flush();
  42. // For each scheduled event
  43. for (auto event = scheduled_events.begin(); event != scheduled_events.end();)
  44. {
  45. // If the event is due
  46. if (time >= event->first)
  47. {
  48. // Dispatch event
  49. dispatch(*(event->second));
  50. // Delete event
  51. delete event->second;
  52. event = scheduled_events.erase(event);
  53. }
  54. else
  55. {
  56. break;
  57. }
  58. }
  59. }
  60. void event_dispatcher::flush()
  61. {
  62. // For each event in the queue
  63. for (auto event = queued_events.begin(); event != queued_events.end(); ++event)
  64. {
  65. // Dispatch event
  66. dispatch(**event);
  67. // Delete event
  68. delete (*event);
  69. }
  70. // Clear event queue
  71. queued_events.clear();
  72. }
  73. void event_dispatcher::clear()
  74. {
  75. // For each event in the queue
  76. for (auto event = queued_events.begin(); event != queued_events.end(); ++event)
  77. {
  78. // Delete event
  79. delete (*event);
  80. }
  81. // Clear event queue
  82. queued_events.clear();
  83. // For each scheduled event
  84. for (auto event = scheduled_events.begin(); event != scheduled_events.end(); ++event)
  85. {
  86. // Delete event
  87. delete event->second;
  88. }
  89. // Clear scheduled events
  90. scheduled_events.clear();
  91. }