chatGPT-Event Dispatcher.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // chatGPT-Event Dispatcher.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3. #include <iostream>
  4. #include <iostream>
  5. #include <functional>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <map>
  9. #include <memory>
  10. #include <string>
  11. // EventType: unique identifier for event types
  12. using EventType = int;
  13. // Event base class
  14. class Event {
  15. public:
  16. explicit Event(EventType type) : type_(type) {}
  17. virtual ~Event() {}
  18. EventType GetType() const { return type_; }
  19. private:
  20. EventType type_;
  21. };
  22. // Example derived event with data
  23. class ButtonClickEvent : public Event {
  24. public:
  25. ButtonClickEvent(int buttonId, const std::string& label)
  26. : Event(1), buttonId_(buttonId), label_(label) {
  27. }
  28. int GetButtonId() const { return buttonId_; }
  29. const std::string& GetLabel() const { return label_; }
  30. private:
  31. int buttonId_;
  32. std::string label_;
  33. };
  34. // Event handler function: receives an Event pointer
  35. using EventHandler = std::function<void(const Event&)>;
  36. class EventDispatcher {
  37. public:
  38. // Connect a handler to an event type, returns a connection id
  39. size_t Connect(EventType type, EventHandler handler) {
  40. size_t id = ++last_id_;
  41. handlers_[type].push_back(std::make_pair(id, handler));
  42. return id;
  43. }
  44. // Disconnect a handler by id (for a specific event type)
  45. void Disconnect(EventType type, size_t id) {
  46. auto& vec = handlers_[type];
  47. vec.erase(std::remove_if(vec.begin(), vec.end(),
  48. [id](const std::pair<size_t, EventHandler>& pair) {
  49. return pair.first == id;
  50. }), vec.end());
  51. }
  52. // Emit (post) an event
  53. void Emit(const Event& evt) {
  54. auto it = handlers_.find(evt.GetType());
  55. if (it != handlers_.end()) {
  56. for (auto& pair : it->second) {
  57. pair.second(evt);
  58. }
  59. }
  60. }
  61. private:
  62. std::map<EventType, std::vector<std::pair<size_t, EventHandler>>> handlers_;
  63. size_t last_id_ = 0;
  64. };
  65. int main() {
  66. EventDispatcher dispatcher;
  67. // Connect a handler for ButtonClickEvent (event type 1)
  68. size_t conn = dispatcher.Connect(1, [](const Event& evt) {
  69. // Downcast to correct event type
  70. auto& clickEvt = static_cast<const ButtonClickEvent&>(evt);
  71. std::cout << "Button clicked! id=" << clickEvt.GetButtonId()
  72. << ", label=" << clickEvt.GetLabel() << std::endl;
  73. });
  74. // Emit a button click event
  75. ButtonClickEvent evt(42, "OK");
  76. dispatcher.Emit(evt);
  77. // Disconnect the handler
  78. dispatcher.Disconnect(1, conn);
  79. // Emit again (no output expected)
  80. dispatcher.Emit(evt);
  81. return 0;
  82. }