9#include "libxr_def.hpp"
18template <
typename CallableType,
typename BoundArgType,
typename... CallbackArgs>
21 static_cast<void (*)(
bool, BoundArgType, CallbackArgs...)
>(callable);
24template <
typename... Args>
27 using InvokeFunType = void (*)(
void*, bool, Args...);
29 InvokeFunType run_fun_ =
nullptr;
39template <
typename ArgType,
typename... Args>
66 static void InvokeThunk(
void* cb_block,
bool in_isr, Args... args)
68 auto* cb =
static_cast<CallbackBlock<ArgType, Args...
>*>(cb_block);
69 cb->Invoke(in_isr, std::forward<Args>(args)...);
73 void Invoke(
bool in_isr, Args... args)
79 fun_(in_isr,
arg_, std::forward<Args>(args)...);
86template <
typename ArgType,
typename... Args>
97 this->run_fun_ = &InvokeThunk;
100 static void InvokeThunk(
void* cb_block,
bool in_isr, Args... args)
107 auto cur_args = std::tuple<std::decay_t<Args>...>{std::forward<Args>(args)...};
110 cb->pending_ =
false;
111 std::apply([&](
auto&... a) { cb->Invoke(in_isr, a...); }, cur_args);
114 cur_args = cb->pending_args_;
116 }
while (cb->pending_);
117 cb->running_ =
false;
124 cb->pending_args_ = std::tuple<std::decay_t<Args>...>{std::forward<Args>(args)...};
129 bool running_ =
false;
130 bool pending_ =
false;
131 std::tuple<std::decay_t<Args>...> pending_args_{};
140template <
typename... Args>
143 static void FunctionDefault(
void*,
bool, Args...) {}
157 template <
typename BoundArgType,
typename CallableType>
161 using FunctionType =
typename CallbackBlock<BoundArgType, Args...>::FunctionType;
163 new CallbackBlock<BoundArgType, Args...>(
static_cast<FunctionType
>(fun),
177 template <
typename BoundArgType,
typename CallableType>
181 using FunctionType =
typename CallbackBlock<BoundArgType, Args...>::FunctionType;
203 :
cb_block_(std::exchange(other.cb_block_, &empty_cb_block_))
218 cb_block_ = std::exchange(other.cb_block_, &empty_cb_block_);
223 template <
typename... PassArgs>
224 void Run(
bool in_isr, PassArgs&&... args)
const
245 :
cb_block_((cb_block != nullptr) ? cb_block : &empty_cb_block_)
回调函数封装块,提供参数绑定与擦除调用入口 / Callback block with bound argument and erased invoke entry
void(*)(bool, ArgType, Args...) FunctionType
回调函数类型定义 / Callback function type definition
CallbackBlock(const CallbackBlock &other)=delete
禁用拷贝构造与拷贝赋值 / Copy construction and copy assignment are disabled
FunctionType fun_
绑定的回调函数 / Bound callback function
ArgType arg_
绑定的参数 / Bound argument
CallbackBlock(FunctionType fun, ArgType &&arg)
构造回调块,绑定回调函数与参数 / Construct a callback block with bound function and argument
通用回调包装,支持动态参数传递 / Generic callback wrapper supporting dynamic argument passing
Callback & operator=(Callback &&other) noexcept
移动赋值运算符,转移回调对象的所有权 / Move assignment operator transferring callback ownership
static Callback Create(CallableType fun, BoundArgType arg)
创建回调对象并绑定回调函数与参数 / Create a callback instance with bound function and argument
Callback(CallbackBlockHeader< Args... > *cb_block)
私有构造函数,仅用于内部创建回调实例 / Private constructor used internally to create callback instances
static Callback CreateGuarded(CallableType fun, BoundArgType arg)
创建带防重入保护的回调 / Create a guarded callback
Callback(Callback &&other) noexcept
移动构造函数,转移回调对象的所有权 / Move constructor transferring callback ownership
bool Empty() const
检查回调是否为空 / Check whether the callback is empty
CallbackBlockHeader< Args... > * cb_block_
回调块指针 / Pointer to the callback block
Callback()
默认构造函数,创建空回调对象 / Default constructor creating an empty callback
GuardedCallbackBlock(typename CallbackBlock< ArgType, Args... >::FunctionType fun, ArgType &&arg)
带防重入保护的回调块 / Callback block with reentry guard