32template <
typename _Ty,
typename _Ret,
typename... _Args>
35 template <
typename _Uty, _Ret (_Uty::*)(_Args...)>
38 template <
typename _Uty, _Ret (_Uty::*)(_Args...)
const>
41 template <
typename _Uty>
44 template <
typename _Uty>
45 static char Test(
ClassMember<_Uty, &_Uty::operator()>*);
47 template <
typename _Uty>
52 typename _Uret =
typename std::decay<decltype(std::declval<_Uty>().operator()(std::declval<_Args>()...))>::type,
53 typename =
typename std::enable_if<std::is_convertible<_Ret, _Uret>::value>::type>
54 static char Test(
int);
56 static constexpr bool value =
sizeof(Test<_Ty>(0)) ==
sizeof(
char);
59template <
typename _Ty,
typename _Ret,
typename... _Args>
60struct IsCallable :
public std::bool_constant<IsCallableHelper<_Ty, _Ret, _Args...>::value>
68template <
typename _Ret,
typename... _Args>
74 virtual void Retain() = 0;
75 virtual void Release() = 0;
76 virtual _Ret Invoke(_Args&&... args)
const = 0;
78 virtual const std::type_info& TargetType()
const noexcept = 0;
80 virtual const void* Target(
const std::type_info& type)
const noexcept = 0;
83template <
typename _Ret,
typename... _Args>
92 virtual void Retain()
override
97 virtual void Release()
override
110template <
typename _Ty,
typename _Ret,
typename... _Args>
115 : callee_(std::move(val))
119 virtual _Ret Invoke(_Args&&... args)
const override
121 return std::invoke(callee_, std::forward<_Args>(args)...);
124 virtual const std::type_info& TargetType()
const noexcept
129 virtual const void* Target(
const std::type_info& type)
const noexcept
131 if (type == this->TargetType())
136 static inline Callable<_Ret, _Args...>* Make(_Ty&& val)
145template <
typename _Ty,
typename _Ret,
typename... _Args>
149 typedef _Ret (_Ty::*_FuncType)(_Args...);
151 virtual _Ret Invoke(_Args&&... args)
const override
153 return std::invoke(func_, ptr_, std::forward<_Args>(args)...);
156 virtual const std::type_info& TargetType()
const noexcept
161 virtual const void* Target(
const std::type_info& type)
const noexcept
163 if (type == this->TargetType())
168 static inline Callable<_Ret, _Args...>* Make(_Ty* ptr, _FuncType func)
185template <
typename _Ty,
typename _Ret,
typename... _Args>
189 typedef _Ret (_Ty::*_FuncType)(_Args...)
const;
191 virtual _Ret Invoke(_Args&&... args)
const override
193 return std::invoke(func_, ptr_, std::forward<_Args>(args)...);
196 virtual const std::type_info& TargetType()
const noexcept
201 virtual const void* Target(
const std::type_info& type)
const noexcept
203 if (type == this->TargetType())
208 static inline Callable<_Ret, _Args...>* Make(_Ty* ptr, _FuncType func)
227template <
typename _Ty>
230template <
typename _Ret,
typename... _Args>
247 SetCallable(rhs.callable_);
251 : callable_(rhs.callable_)
253 rhs.callable_ =
nullptr;
262 template <
typename _Ty,
263 typename =
typename std::enable_if<
details::IsCallable<_Ty, _Ret, _Args...>::value,
int>::type>
270 template <
typename _Ty,
typename _Uty,
271 typename =
typename std::enable_if<std::is_same<_Ty, _Uty>::value || std::is_base_of<_Ty, _Uty>::value,
273 Function(_Uty* ptr, _Ret (_Ty::*func)(_Args...))
279 template <
typename _Ty,
typename _Uty,
280 typename =
typename std::enable_if<std::is_same<_Ty, _Uty>::value || std::is_base_of<_Ty, _Uty>::value,
282 Function(_Uty* ptr, _Ret (_Ty::*func)(_Args...)
const)
290 SetCallable(
nullptr);
293 inline _Ret operator()(_Args... args)
const
296 throw std::bad_function_call();
297 return callable_->Invoke(std::forward<_Args>(args)...);
300 inline operator bool()
const
307 SetCallable(rhs.callable_);
313 SetCallable(
nullptr);
314 callable_ = rhs.callable_;
315 rhs.callable_ =
nullptr;
319 inline void swap(
const Function& rhs)
321 std::swap(callable_, rhs.callable_);
324 const std::type_info& target_type()
const noexcept
326 return callable_->TargetType();
330 _Fx* target()
noexcept
332 return reinterpret_cast<_Fx*
>(
const_cast<void*
>(callable_->Target(
typeid(_Fx))));
336 const _Fx* target()
const noexcept
338 return reinterpret_cast<const _Fx*
>(callable_->Target(
typeid(_Fx)));
344 if (callable_ != callable)
348 callable_->Release();
352 callable_ = callable;
365 typename _Ty,
typename _Uty,
366 typename =
typename std::enable_if<std::is_same<_Ty, _Uty>::value || std::is_base_of<_Ty, _Uty>::value,
int>::type,
367 typename _Ret,
typename... _Args>
368inline Function<_Ret(_Args...)> Closure(_Uty* ptr, _Ret (_Ty::*func)(_Args...))
370 return Function<_Ret(_Args...)>(ptr, func);
374 typename _Ty,
typename _Uty,
375 typename =
typename std::enable_if<std::is_same<_Ty, _Uty>::value || std::is_base_of<_Ty, _Uty>::value,
int>::type,
376 typename _Ret,
typename... _Args>
377inline Function<_Ret(_Args...)> Closure(_Uty* ptr, _Ret (_Ty::*func)(_Args...)
const)
379 return Function<_Ret(_Args...)>(ptr, func);
382template <
typename _Ret,
typename... _Args>
Definition: Function.h:228
Definition: Function.h:70
Definition: Function.h:112
Definition: Function.h:187
Definition: Function.h:147
Definition: Function.h:85
Definition: Function.h:39
Definition: Function.h:36
Definition: Function.h:34
Definition: Function.h:61