gdk-graphics 0b051eb9b5c1eaa0658babaa4463dd7a80aa9d2c
Loading...
Searching...
No Matches
event.h
1// © Joseph Cameron - All Rights Reserved
2
3#ifndef JFC_EVENT_H
4#define JFC_EVENT_H
5
6#include <algorithm>
7#include <mutex>
8#include <vector>
9
10namespace jfc {
13 template<class... event_parameter_types_param>
14 class event final {
15 public:
16 using observer_type = std::function<void(event_parameter_types_param...)>;
17
19 void subscribe(observer_type observer) {
20 m_Observers.push_back(std::move(observer));
21 }
22
24 void unsubscribe(const observer_type &observer) {
25 auto it = std::find_if(m_Observers.begin(), m_Observers.end(),
26 [&](const observer_type& registeredObserver) {
27 return registeredObserver.target_type() == observer.target_type();
28 });
29
30 if (it != m_Observers.end()) m_Observers.erase(it);
31 }
32
34 void notify(event_parameter_types_param... params) const {
35 std::lock_guard<std::mutex> lock(m_ObserversMutex);
36 for (const auto &observer : m_Observers) {
37 observer(params...);
38 }
39 }
40
41 private:
42 std::vector<observer_type> m_Observers;
43 mutable std::mutex m_ObserversMutex;
44 };
45}
46
47#endif
48
event class that requires the user to clean up after themselves
Definition event.h:14
void subscribe(observer_type observer)
adds an observer
Definition event.h:19
void unsubscribe(const observer_type &observer)
removes an observer
Definition event.h:24
void notify(event_parameter_types_param... params) const
notify observers
Definition event.h:34