Potato Engine
Toggle main menu visibility
Loading...
Searching...
No Matches
EventDelegate.hpp
Go to the documentation of this file.
1
2
#pragma once
3
4
#include <functional>
5
11
template
<
typename
... CallbackArgs>
12
struct
EventDelegate
13
{
14
private
:
15
std::function<void(CallbackArgs...)> callback;
16
void
* instance =
nullptr
;
17
18
public
:
19
20
EventDelegate() =
default
;
21
~EventDelegate() =
default
;
22
30
template
<
typename
BindingClass>
31
EventDelegate
(BindingClass* obj,
void
(BindingClass::*method)(CallbackArgs...)) {
32
instance = obj;
33
callback = [obj, method](CallbackArgs... args) {
34
(obj->*method)(std::forward<CallbackArgs>(args)...);
// forward ensures value category preserved
35
};
36
}
37
43
EventDelegate
(
void
(*func)(CallbackArgs...)) {
44
callback = func;
45
}
46
51
bool
Fire
(CallbackArgs... args)
const
{
52
if
(callback ==
nullptr
) {
return
false
; }
53
callback( std::forward<CallbackArgs>(args)... );
54
return
true
;
55
}
56
58
inline
void
*
GetInstance
()
const
{
return
instance; }
59
60
};
EventDelegate::EventDelegate
EventDelegate(void(*func)(CallbackArgs...))
Constructs bare delegate.
Definition
EventDelegate.hpp:43
EventDelegate::GetInstance
void * GetInstance() const
Gets binded object.
Definition
EventDelegate.hpp:58
EventDelegate::EventDelegate
EventDelegate(BindingClass *obj, void(BindingClass::*method)(CallbackArgs...))
Constructs delegate with class method binding.
Definition
EventDelegate.hpp:31
EventDelegate::Fire
bool Fire(CallbackArgs... args) const
Fires event and forwards arguments to callback.
Definition
EventDelegate.hpp:51
Core
Event
EventDelegate.hpp
Generated by
1.17.0