1 #if !defined(_BTKSIGNAL_FUNCTION_HPP_) 2 #define _BTKSIGNAL_FUNCTION_HPP_ 13 template<
class T,
class RetT,
class ...Args>
18 fn(std::forward<T>(f)){}
21 static RetT Invoke(
void *__self,Args ...args){
23 return self->fn(std::forward<Args>(args)...);
25 static void Delete(
void *__self){
26 delete static_cast<Manager*
>(__self);
31 [[noreturn]]
void throwBadFunctionCall();
36 delete_ptr(
nullptr){};
39 manager_ptr = fn.manager_ptr;
40 delete_ptr = fn.delete_ptr;
41 if(delete_ptr !=
nullptr){
48 manager_ptr = fn.manager_ptr;
49 delete_ptr = fn.delete_ptr;
51 fn.manager_ptr =
nullptr;
52 fn.delete_ptr =
nullptr;
60 if(delete_ptr !=
nullptr){
64 if(base->refcount == 0){
65 delete_ptr(manager_ptr);
68 manager_ptr =
nullptr;
73 auto m = fn.manager_ptr;
74 auto dptr = fn.delete_ptr;
76 fn.manager_ptr = manager_ptr;
77 fn.delete_ptr = delete_ptr;
84 void (*delete_ptr)(
void *manager);
91 template<
class RetT,
class ...Args>
102 assign_impl(std::forward<Fn>(fn));
106 invoke_ptr = fn.invoke_ptr;
110 invoke_ptr = fn.invoke_ptr;
111 fn.invoke_ptr =
nullptr;
115 void assign_impl(Fn &&fn){
117 if constexpr(std::is_pointer<Fn>::value){
119 delete_ptr =
nullptr;
120 manager_ptr =
reinterpret_cast<void*
>(fn);
121 invoke_ptr = InvokeCFunction<Fn>;
126 manager_ptr =
new Manager(std::forward<Fn>(fn));
127 delete_ptr = Manager::Delete;
128 invoke_ptr = Manager::Invoke;
133 void assign(Fn &&fn){
137 assign_impl(std::forward<Fn>(fn));
148 manager_ptr = fn.manager_ptr;
149 delete_ptr = fn.delete_ptr;
150 if(delete_ptr !=
nullptr){
159 FunctionBase::reset();
160 invoke_ptr =
nullptr;
164 FunctionBase::swap(fn);
165 auto invp = fn.invoke_ptr;
166 fn.invoke_ptr = invoke_ptr;
172 assign<T>(std::forward<T>(dat));
176 typedef RetT result_type;
178 typedef RetT (*InvokeFn)(
void*,Args...);
179 result_type call(Args ...args)
const{
180 if(invoke_ptr ==
nullptr){
182 throwBadFunctionCall();
185 return invoke_ptr(manager_ptr,std::forward<Args>(args)...);
189 bool empty()
const noexcept{
190 return invoke_ptr ==
nullptr;
192 result_type operator()(Args ...args)
const{
193 return call(std::forward<Args>(args)...);
195 bool operator ==(std::nullptr_t)
const noexcept{
196 return invoke_ptr ==
nullptr;
198 operator bool()
const noexcept{
202 template<
class Fn = RetT(*)(Args...)>
203 static RetT InvokeCFunction(
void *fn,Args ...args){
204 return reinterpret_cast<Fn
>(fn)(
205 std::forward<Args>(args)...
212 #endif // _BTKSIGNAL_FUNCTION_HPP_ Definition: function.hpp:10
This header include many useful containers.
Definition: async.hpp:7
Definition: function.hpp:14
Definition: function.hpp:89
Definition: function.hpp:33