Simple C++ Game
Dispatcher.hpp
1 #pragma once
2 
3 #include <Event.hpp>
4 
5 #include <functional>
6 #include <map>
7 #include <string>
8 #include <vector>
9 
18 {
19 public:
24  Dispatcher() = default;
25 
34  void subscribe(const std::string type, const std::function<void(const Event &)> observer);
35 
42  void subscribeGlobal(const std::function<void(const Event &)> observer);
43 
55  void dispatch(const Event &event);
56 
68  void queue(const Event &event);
69 
75  void flush();
76 
77 private:
78  std::map<std::string, std::vector<std::function<void(const Event &)>>> _observers;
79  std::vector<std::function<void(const Event &)>> _globalObservers;
80  std::vector<Event> _queue;
81 };
A class for managing events.
Definition: Dispatcher.hpp:18
void subscribe(const std::string type, const std::function< void(const Event &)> observer)
Subscribe a specific listener to a specific event type.
void dispatch(const Event &event)
Dispatches an event.
Dispatcher()=default
Construct a new Dispatcher object.
void subscribeGlobal(const std::function< void(const Event &)> observer)
Subscribe a specific listener to all event types.
void queue(const Event &event)
Queues an event to be dispatched later.
void flush()
Dispatches all queued events.
A class representing an event that has occurred.
Definition: Event.hpp:10