-
Notifications
You must be signed in to change notification settings - Fork 61
/
nano_function.hpp
86 lines (72 loc) · 1.98 KB
/
nano_function.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#pragma once
#include <array>
#include <cstdint>
namespace Nano
{
using Delegate_Key = std::array<std::uintptr_t, 2>;
template <typename RT> class Function;
template <typename RT, typename... Args>
class Function<RT(Args...)> final
{
// Only Nano::Observer is allowed private access
template <typename> friend class Observer;
using Thunk = RT(*)(void*, Args&&...);
static inline Function bind(Delegate_Key const& delegate_key)
{
return
{
reinterpret_cast<void*>(delegate_key[0]),
reinterpret_cast<Thunk>(delegate_key[1])
};
}
public:
void* const instance_pointer;
const Thunk function_pointer;
template <auto fun_ptr>
static inline Function bind()
{
return
{
nullptr, [](void* /*NULL*/, Args&&... args)
{
return (*fun_ptr)(std::forward<Args>(args)...);
}
};
}
template <auto mem_ptr, typename T>
static inline Function bind(T* pointer)
{
return
{
pointer, [](void* this_ptr, Args&&... args)
{
return (static_cast<T*>(this_ptr)->*mem_ptr)(std::forward<Args>(args)...);
}
};
}
template <typename L>
static inline Function bind(L* pointer)
{
return
{
pointer, [](void* this_ptr, Args&&... args)
{
return static_cast<L*>(this_ptr)->operator()(std::forward<Args>(args)...);
}
};
}
template <typename... Uref>
inline RT operator() (Uref&&... args) const
{
return (*function_pointer)(instance_pointer, static_cast<Args&&>(args)...);
}
inline operator Delegate_Key() const
{
return
{
reinterpret_cast<std::uintptr_t>(instance_pointer),
reinterpret_cast<std::uintptr_t>(function_pointer)
};
}
};
} // namespace Nano ------------------------------------------------------------