ChatPlexSDK-BS 1.0.1-v6.2.0
C++ ChatPlex BeatSaber SDK
Loading...
Searching...
No Matches
Delegate.hpp
1#pragma once
2
3#include "Internals/DelegateInvokerI.hpp"
4#include "Internals/DelegateInvokerL.hpp"
5#include "Internals/DelegateInvokerS.hpp"
6#include "Internals/LambdaSignatureHelper.hpp"
7
8namespace CP_SDK::Utils {
9
11 template <class t_FnType> class DelegateImpl;
12
15
17 template<class t_Ret, class... t_Args>
18 class DelegateImpl<t_Ret(t_Args...)>
19 {
20 public:
23 {
24 m_Invoker = nullptr;
25 }
28 DelegateImpl(t_Ret(*p_Handler)(t_Args...))
29 {
30 m_Invoker = std::make_shared<Internals::DelegateInvokerS<t_Ret, t_Args...>>(p_Handler);
31 }
35 template<class t_Class> DelegateImpl(t_Class * p_Owner, t_Ret(t_Class::* p_Handler)(t_Args...))
36 {
37 m_Invoker = std::make_shared<Internals::DelegateInvokerI<t_Class, t_Ret, t_Args...>>(p_Owner, p_Handler);
38 }
42 template<class t_Lambda> DelegateImpl(t_Ret(*p_Null)(t_Args...), const t_Lambda & p_Lambda)
43 {
44 m_Invoker = std::make_shared<Internals::DelegateInvokerL<t_Lambda, t_Ret, t_Args...>>(p_Lambda);
45 }
48 DelegateImpl(const DelegateImpl<t_Ret(t_Args...)> & p_Other)
49 {
50 m_Invoker = p_Other.m_Invoker;
51 }
54 DelegateImpl(DelegateImpl<t_Ret(t_Args...)> && p_Other)
55 {
56 m_Invoker = p_Other.m_Invoker;
57 p_Other.m_Invoker = nullptr;
58 }
60 virtual ~DelegateImpl()
61 {
62 m_Invoker = nullptr;
63 }
64
65 public:
69 {
70 m_Invoker = p_Other.m_Invoker;
71 return *this;
72 }
73
74 public:
76 bool IsValid() const
77 {
78 return m_Invoker ? m_Invoker->IsValid() : false;
79 }
80
81 public:
84 bool EqualTo(const DelegateImpl<t_Ret(t_Args...)>* p_Other) const
85 {
86 return m_Invoker && p_Other && m_Invoker->EqualTo(p_Other->m_Invoker.get());
87 }
90 t_Ret operator()(t_Args... p_Args) const
91 {
92 return Invoke(std::forward<t_Args>(p_Args)...);
93 }
96 t_Ret Invoke(t_Args... p_Args) const
97 {
98 if (!IsValid())
99 {
100 if constexpr (std::is_reference<t_Ret>::value)
101 {
102 static typename std::remove_reference<t_Ret>::type s_Defaultt_Ret;
103 return s_Defaultt_Ret;
104 }
105 else
106 return t_Ret();
107 }
108
109 return m_Invoker->Invoke(std::forward<t_Args>(p_Args)...);
110 }
111
112 private:
113 std::shared_ptr<Internals::IDelegateInvoker<t_Ret, t_Args...>> m_Invoker;
114
115 };
116
119
121 template <class t_FnType>
122 class Delegate : public DelegateImpl<t_FnType>
123 {
124 using DelegateImpl<t_FnType>::DelegateImpl;
125
126 public:
129 template<class t_Lambda, class t_Sig = typename Internals::LambdaSignatureHelper<decltype(&t_Lambda::operator())>::Signature>
130 Delegate(const t_Lambda & p_Lambda)
131 : DelegateImpl<t_Sig>((t_Sig*)nullptr, p_Lambda)
132 {
133
134 }
136 virtual ~Delegate()
137 {
138
139 }
140
141 };
142
145
147 template <class... t_Args>
148 using Action = Delegate<void(t_Args...)>;
149
150 template <class... t_Args>
151 using CActionRef = const Delegate<void(t_Args...)>&;
152
155
157 template <class t_Ret, class... t_Args>
158 using Func = Delegate<t_Ret(t_Args...)>;
159
161 template <class t_Ret, class... t_Args>
162 using CFuncRef = const Delegate<t_Ret(t_Args...)>&;
163
164}
Delegate helper class.
Definition Delegate.hpp:123
virtual ~Delegate()
Destructor.
Definition Delegate.hpp:136
Delegate(const t_Lambda &p_Lambda)
Make delegate.
Definition Delegate.hpp:130
t_Ret Invoke(t_Args... p_Args) const
Invoke this delegate.
Definition Delegate.hpp:96
DelegateImpl(const DelegateImpl< t_Ret(t_Args...)> &p_Other)
Copy constructor.
Definition Delegate.hpp:48
bool EqualTo(const DelegateImpl< t_Ret(t_Args...)> *p_Other) const
Is this delegate equal to an other delegate.
Definition Delegate.hpp:84
bool IsValid() const
Is the delegate valid.
Definition Delegate.hpp:76
DelegateImpl(t_Ret(*p_Null)(t_Args...), const t_Lambda &p_Lambda)
Lambda function constructor.
Definition Delegate.hpp:42
DelegateImpl(DelegateImpl< t_Ret(t_Args...)> &&p_Other)
Move constructor.
Definition Delegate.hpp:54
DelegateImpl(t_Class *p_Owner, t_Ret(t_Class::*p_Handler)(t_Args...))
Instance function constructor.
Definition Delegate.hpp:35
t_Ret operator()(t_Args... p_Args) const
Invoke this delegate.
Definition Delegate.hpp:90
DelegateImpl & operator=(const DelegateImpl &p_Other)
Assign operator.
Definition Delegate.hpp:68
DelegateImpl(t_Ret(*p_Handler)(t_Args...))
Static function constructor.
Definition Delegate.hpp:28
Forward declaration.
Definition Delegate.hpp:11
Various platform utils like Delegate/Action/Function/Event system.
Definition Delegate.hpp:8