ChatPlexSDK-BS 1.0.1-v6.2.0
C++ ChatPlex BeatSaber SDK
Loading...
Searching...
No Matches
Event.hpp
1#pragma once
2
3#include "Delegate.hpp"
4
5#include <mutex>
6#include <vector>
7
8namespace CP_SDK::Utils {
9
11 template <class t_FnType> class EventImpl;
12
15
17 template <class t_Ret, class... t_Args> class EventImpl<t_Ret(t_Args...)>
18 {
20 EventImpl(const EventImpl&) = delete;
22 EventImpl(EventImpl&&) = delete;
23
24 public:
27 {
28
29 }
30
33 EventImpl& operator+=(const Delegate<void(t_Args...)> & p_Delegate)
34 {
35 std::lock_guard l_Guard(m_Mutex);
36
37 auto l_It = std::find_if(m_Delegates.begin(), m_Delegates.end(), [&p_Delegate](const Delegate<void(t_Args...)> & p_Item) -> bool {
38 return p_Item.EqualTo(&p_Delegate);
39 });
40
41 if (l_It == m_Delegates.end())
42 m_Delegates.push_back(p_Delegate);
43
44 return *this;
45 }
48 EventImpl& operator-=(const Delegate<void(t_Args...)> & p_Delegate)
49 {
50 std::lock_guard l_Guard(m_Mutex);
51
52 auto l_It = std::find_if(m_Delegates.begin(), m_Delegates.end(), [&p_Delegate](const Delegate<void(t_Args...)> & p_Item) -> bool {
53 return p_Item.EqualTo(&p_Delegate);
54 });
55
56 if (l_It != m_Delegates.end())
57 m_Delegates.erase(l_It);
58
59 return *this;
60 }
61
63 void Clear()
64 {
65 m_Mutex.lock();
66 m_Delegates.clear();
67 m_Mutex.unlock();
68 }
69
72 void operator()(t_Args... p_Args)
73 {
74 Invoke(std::forward<t_Args>(p_Args)...);
75 }
78 void Invoke(t_Args... p_Args)
79 {
80 m_Mutex.lock();
81 std::vector<Delegate<void(t_Args...)>> l_Copy = m_Delegates;
82 m_Mutex.unlock();
83
84 for (const auto & l_Delegate : l_Copy)
85 l_Delegate.Invoke(std::forward<t_Args>(p_Args)...);
86 }
87
88 private:
89 std::mutex m_Mutex;
90 std::vector<Delegate<void(t_Args...)>> m_Delegates;
91
92 };
93
96
98 template <class... t_Args>
99 class Event : public EventImpl<void(t_Args...)>
100 {
101 using EventImpl<void(t_Args...)>::EventImpl;
102 };
103
104}
Delegate helper class.
Definition Delegate.hpp:123
Event class helper.
Definition Event.hpp:100
EventImpl & operator+=(const Delegate< void(t_Args...)> &p_Delegate)
Definition Event.hpp:33
EventImpl & operator-=(const Delegate< void(t_Args...)> &p_Delegate)
Definition Event.hpp:48
Forward declaration.
Definition Event.hpp:11
Various platform utils like Delegate/Action/Function/Event system.
Definition Delegate.hpp:8