//
// Created by LCBHSStudent on 2020/2/25.
//
#ifndef SLOT_SIGNAL_BASE_FUNCTION_H
#define SLOT_SIGNAL_BASE_FUNCTION_H
#include <cstdint>
#include <array>
//namespace van {
constexpr int FUNCID = 0;
constexpr int FUNCADDR = 1;
using Delegate_Key = std::array<uintptr_t, 2>;
// 前向声明
template <typename RT> class Function;
// 为了实现Function< returnType (paraType1, paraType2...)> 类的声明
template <typename RT, typename... Args>
class Function<RT(Args...)> {
// 定义函数指针类型
using funcP = RT(*)(void*, Args&&...);
static inline Function
bind(Delegate_Key const& delegate_key) {
return {
reinterpret_cast<void*>(delegate_key[FUNCID]),
reinterpret_cast<funcP>(delegate_key[FUNCADDR])
};
}
public:
Function() = default;
void* const instance_pointer = nullptr;
funcP const function_pointer = nullptr;
template<auto fun_ptr>
static inline Function bind() {
}
// 调用函数
inline RT operator() (Args... args) {
return
(*function_pointer) // 保留参数的左右值属性
(instance_pointer, std::forward<Args>(args)...);
}
inline explicit operator Delegate_Key() const {
return {
reinterpret_cast<std::uintptr_t>(instance_pointer),
reinterpret_cast<std::uintptr_t>(function_pointer)
};
}
};
//}
#endif //SLOT_SIGNAL_BASE_FUNCTION_H
闂茬潃鏃犺亰鍐欑偣浠�涔�
最新推荐文章于 2021-08-10 10:28:12 发布