NIM 跨平台 C++ SDK
载入中...
搜索中...
未找到
callback_proxy.h
浏览该文件的文档.
1
7#ifndef _NIM_CPP_PROXY_H_
8#define _NIM_CPP_PROXY_H_
9
10#include <functional>
11#include <memory>
12
13namespace nim {
15 template <typename TR = void>
16 using CallbackProxyClosure = std::function<TR(void)>;
17
18public:
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))));
24 struct Deleter {
25 Deleter(TCallbackPtr cb_ptr)
26 : cb_ptr_(cb_ptr) {}
27 ~Deleter() {
28 if (cb_ptr_ != nullptr) {
29 delete cb_ptr_;
30 cb_ptr_ = nullptr;
31 }
32 }
33
34 private:
35 TCallbackPtr cb_ptr_;
36 };
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)));
41 }
42 return TReturn();
43 }
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)...));
48 }
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) {
51 using TReturn = R;
52 return Run<TReturn>(std::bind(f, p, std::forward<Args>(args)...));
53 }
54 template <class R, class C, class... DArgs, class P, class... Args>
55 static R Invoke(R (C::*f)(DArgs...), P&& p, Args&&... args) {
56 using TReturn = R;
57 return Run<TReturn>(std::bind(f, p, std::forward<Args>(args)...));
58 }
59
60protected:
61 template <typename TR>
62 static TR Run(const CallbackProxyClosure<TR>& task) {
63 return task();
64 }
65
66public:
67 static std::function<void(const std::function<void()>&)> docallback_async_;
68};
69template <>
70void CallbackProxy::Run<void>(const CallbackProxyClosure<void>& task);
71} // namespace nim
72
73#endif
Definition: callback_proxy.h:14
std::function< TR(void)> CallbackProxyClosure
Definition: callback_proxy.h:16
static TR Run(const CallbackProxyClosure< TR > &task)
Definition: callback_proxy.h:62
static auto DoSafeCallback(const void *callback, const TDoCall &closure, bool delete_callback=false) -> decltype(closure((*(TCallback *)(callback))))
Definition: callback_proxy.h:20
static R Invoke(R(C::*f)(DArgs...) const, P &&p, Args &&... args)
Definition: callback_proxy.h:50
static R Invoke(R(C::*f)(DArgs...), P &&p, Args &&... args)
Definition: callback_proxy.h:55
static auto Invoke(F &&f, Args &&... args) -> decltype(f(std::forward< Args >(args)...))
Definition: callback_proxy.h:45
static std::function< void(const std::function< void()> &)> docallback_async_
Definition: callback_proxy.h:67
namespace nim