7#include "libxr_def.hpp"
26template <
typename ArgType,
typename... Args>
29 bool running_ =
false;
30 bool pending_ =
false;
31 std::tuple<std::decay_t<Args>...> pending_args_{};
48 template <
typename FunType,
typename ArgT>
50 :
fun_(std::forward<FunType>(fun)),
arg_(std::forward<ArgT>(arg))
66 void Call(
bool in_isr, Args... args)
77 auto cur_args = std::tuple<std::decay_t<Args>...>{args...};
82 std::apply([&](
auto&... a) {
fun_(in_isr,
arg_, a...); }, cur_args);
86 cur_args = pending_args_;
95 pending_args_ = std::tuple<std::decay_t<Args>...>{args...};
112 :
fun_(std::exchange(other.fun_,
nullptr)),
113 arg_(std::move(other.arg_)),
129 fun_ = std::exchange(other.fun_,
nullptr);
130 arg_ = std::move(other.arg_);
137 void (*
fun_)(bool, ArgType, Args...);
148template <
typename... Args>
151 static void FunctionDefault(
bool,
void*, Args...) {}
166 template <
typename FunType,
typename ArgType>
169 void (*fun_ptr)(bool, ArgType, Args...) = fun;
170 auto cb_block =
new CallbackBlock<ArgType, Args...>(fun_ptr, arg);
172 auto cb_fun = [](
bool in_isr,
void* cb_block, Args... args)
174 auto* cb =
static_cast<CallbackBlock<ArgType, Args...
>*>(cb_block);
175 cb->
Call(in_isr, std::forward<Args>(args)...);
196 :
cb_block_(std::exchange(other.cb_block_,
nullptr)),
197 cb_fun_(std::exchange(other.cb_fun_,
nullptr))
212 cb_block_ = std::exchange(other.cb_block_,
nullptr);
213 cb_fun_ = std::exchange(other.cb_fun_,
nullptr);
224 template <
typename... PassArgs>
225 void Run(
bool in_isr, PassArgs&&... args)
const
246 Callback(
void* cb_block,
void (*cb_fun)(
bool,
void*, Args...))
回调函数封装块,提供重入保护与参数绑定 / Callback block with argument binding and reentrancy guard
void(*)(bool, ArgType, Args...) FunctionType
回调函数类型定义 / Callback function type definition
CallbackBlock(const CallbackBlock &other)=delete
禁用拷贝构造与拷贝赋值 / Copy construction and copy assignment are disabled
CallbackBlock(FunType &&fun, ArgT &&arg)
构造回调块,绑定回调函数与参数 / Construct a callback block with bound function and argument
CallbackBlock & operator=(CallbackBlock &&other) noexcept
移动赋值运算符,转移回调函数与参数 / Move assignment operator transferring function and argument
void Call(bool in_isr, Args... args)
触发回调执行(带重入保护) / Trigger callback execution with reentrancy guard
bool in_isr_
是否在中断上下文中执行 / Whether executed in ISR context
CallbackBlock(CallbackBlock &&other) noexcept
移动构造函数,转移回调函数与参数 / Move constructor transferring function and argument
ArgType arg_
绑定的参数 / Bound argument
void(* fun_)(bool, ArgType, Args...)
绑定的回调函数 / Bound callback function
通用回调包装,支持动态参数传递 / Generic callback wrapper supporting dynamic argument passing
void * cb_block_
回调块指针 / Pointer to the callback block
Callback(void *cb_block, void(*cb_fun)(bool, void *, Args...))
私有构造函数,仅用于内部创建回调实例 / Private constructor used internally to create callback instances
Callback & operator=(Callback &&other) noexcept
移动赋值运算符,转移回调对象的所有权 / Move assignment operator transferring callback ownership
static Callback Create(FunType fun, ArgType arg)
创建回调对象并绑定回调函数与参数 / Create a callback instance with bound function and argument
void Run(bool in_isr, PassArgs &&... args) const
执行回调函数并传递参数 / Execute the callback with arguments
Callback(Callback &&other) noexcept
移动构造函数,转移回调对象的所有权 / Move constructor transferring callback ownership
bool Empty() const
检查回调是否为空 / Check whether the callback is empty
void(* cb_fun_)(bool, void *, Args...)
回调执行函数指针 / Callback invocation function pointer
Callback()
默认构造函数,创建空回调对象 / Default constructor creating an empty callback