7#ifndef _NIM_CPP_PROXY_H_
8#define _NIM_CPP_PROXY_H_
15 template <
typename TR =
void>
16 using CallbackProxyClosure = std::function<TR(
void)>;
19 template <
typename TCallback,
typename TDoCall>
20 static auto DoSafeCallback(
const void* callback,
const TDoCall& closure,
bool delete_callback =
false)
21 ->
decltype(closure((*(TCallback*)(callback)))) {
22 using TCallbackPtr = TCallback*;
23 using TReturn =
decltype(closure((*(TCallbackPtr)(callback))));
25 Deleter(TCallbackPtr cb_ptr)
28 if (cb_ptr_ !=
nullptr) {
37 if (callback !=
nullptr) {
38 auto&& real_type_cb_ptr = (TCallbackPtr)(callback);
39 Deleter deleter(delete_callback ? real_type_cb_ptr :
nullptr);
40 return (*real_type_cb_ptr ==
nullptr) ? (TReturn()) : (closure(std::forward<TCallback>(*real_type_cb_ptr)));
44 template <
class F,
class... Args,
class =
typename std::enable_if<!std::is_member_function_pointer<F>::value>::type>
45 static auto Invoke(F&& f, Args&&... args) ->
decltype(f(std::forward<Args>(args)...)) {
46 using TReturn =
decltype(f(std::forward<Args>(args)...));
47 return Run<TReturn>(std::bind(f, std::forward<Args>(args)...));
49 template <
class R,
class C,
class... DArgs,
class P,
class... Args>
50 static R Invoke(R (C::*f)(DArgs...)
const, P&& p, Args&&... args) {
52 return Run<TReturn>(std::bind(f, p, std::forward<Args>(args)...));
54 template <
class R,
class C,
class... DArgs,
class P,
class... Args>
55 static R Invoke(R (C::*f)(DArgs...), P&& p, Args&&... args) {
57 return Run<TReturn>(std::bind(f, p, std::forward<Args>(args)...));
61 template <
typename TR>
62 static TR Run(
const CallbackProxyClosure<TR>& task) {
67 static std::function<void(
const std::function<
void()>&)> docallback_async_;
70void CallbackProxy::Run<void>(
const CallbackProxyClosure<void>& task);
Definition: callback_proxy.h:14