libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::CallbackBlock< ArgType, Args > Class Template Reference

回调函数封装块,提供重入保护与参数绑定 / Callback block with argument binding and reentrancy guard More...

#include <libxr_cb.hpp>

Public Types

using FunctionType = void (*)(bool, ArgType, Args...)
 回调函数类型定义 / Callback function type definition
 

Public Member Functions

template<typename FunType , typename ArgT >
 CallbackBlock (FunType &&fun, ArgT &&arg)
 构造回调块,绑定回调函数与参数 / Construct a callback block with bound function and argument
 
void Call (bool in_isr, Args... args)
 触发回调执行(带重入保护) / Trigger callback execution with reentrancy guard
 
 CallbackBlock (const CallbackBlock &other)=delete
 禁用拷贝构造与拷贝赋值 / Copy construction and copy assignment are disabled
 
CallbackBlockoperator= (const CallbackBlock &other)=delete
 
 CallbackBlock (CallbackBlock &&other) noexcept
 移动构造函数,转移回调函数与参数 / Move constructor transferring function and argument
 
CallbackBlockoperator= (CallbackBlock &&other) noexcept
 移动赋值运算符,转移回调函数与参数 / Move assignment operator transferring function and argument
 

Private Attributes

bool running_ = false
 
bool pending_ = false
 
std::tuple< std::decay_t< Args >... > pending_args_ {}
 
void(* fun_ )(bool, ArgType, Args...)
 绑定的回调函数 / Bound callback function
 
ArgType arg_
 绑定的参数 / Bound argument
 
bool in_isr_ = false
 是否在中断上下文中执行 / Whether executed in ISR context
 

Detailed Description

template<typename ArgType, typename... Args>
class LibXR::CallbackBlock< ArgType, Args >

回调函数封装块,提供重入保护与参数绑定 / Callback block with argument binding and reentrancy guard

当回调正在执行时再次触发(重入),不会递归调用回调函数,而是缓存一次“待执行请求”; 待当前执行结束后在同一调用点以循环方式补跑,从而避免无限嵌套(trampoline 扁平化)。 When reentered while running, the callback is not invoked recursively. Instead, one pending request is cached and replayed in a loop after the current invocation completes, flattening recursion via a trampoline-style execution.

Template Parameters
ArgType绑定的第一个参数类型 / Type of the first bound argument
Args额外的参数类型列表 / Additional argument types

Definition at line 27 of file libxr_cb.hpp.

Member Typedef Documentation

◆ FunctionType

template<typename ArgType , typename... Args>
using LibXR::CallbackBlock< ArgType, Args >::FunctionType = void (*)(bool, ArgType, Args...)

回调函数类型定义 / Callback function type definition

Definition at line 37 of file libxr_cb.hpp.

Constructor & Destructor Documentation

◆ CallbackBlock() [1/2]

template<typename ArgType , typename... Args>
template<typename FunType , typename ArgT >
LibXR::CallbackBlock< ArgType, Args >::CallbackBlock ( FunType && fun,
ArgT && arg )
inline

构造回调块,绑定回调函数与参数 / Construct a callback block with bound function and argument

Template Parameters
FunType函数类型 / Function type
ArgT绑定参数类型 / Bound argument type
Parameters
fun需要调用的回调函数 / Callback function to be invoked
arg绑定的参数值 / Bound argument value

Definition at line 49 of file libxr_cb.hpp.

50 : fun_(std::forward<FunType>(fun)), arg_(std::forward<ArgT>(arg))
51 {
52 }
ArgType arg_
绑定的参数 / Bound argument
Definition libxr_cb.hpp:138
void(* fun_)(bool, ArgType, Args...)
绑定的回调函数 / Bound callback function
Definition libxr_cb.hpp:137

◆ CallbackBlock() [2/2]

template<typename ArgType , typename... Args>
LibXR::CallbackBlock< ArgType, Args >::CallbackBlock ( CallbackBlock< ArgType, Args > && other)
inlinenoexcept

移动构造函数,转移回调函数与参数 / Move constructor transferring function and argument

Parameters
other另一个 CallbackBlock 实例 / Another CallbackBlock instance

Definition at line 111 of file libxr_cb.hpp.

112 : fun_(std::exchange(other.fun_, nullptr)),
113 arg_(std::move(other.arg_)),
114 in_isr_(other.in_isr_)
115 {
116 }
bool in_isr_
是否在中断上下文中执行 / Whether executed in ISR context
Definition libxr_cb.hpp:139

Member Function Documentation

◆ Call()

template<typename ArgType , typename... Args>
void LibXR::CallbackBlock< ArgType, Args >::Call ( bool in_isr,
Args... args )
inline

触发回调执行(带重入保护) / Trigger callback execution with reentrancy guard

Parameters
in_isr是否在中断上下文中执行 / Whether executed in ISR context
args额外参数 / Additional arguments
Note
若在执行过程中发生重入,本次重入不会递归执行回调,而是写入待执行参数并置 pending 标志;当前执行结束后将补跑一次。 On reentry, the callback is not invoked recursively. A pending flag is set and arguments are stored for a single replay after the current run.

Definition at line 66 of file libxr_cb.hpp.

67 {
68 if (!fun_)
69 {
70 return;
71 }
72
73 if (!running_)
74 {
75 running_ = true;
76
77 auto cur_args = std::tuple<std::decay_t<Args>...>{args...};
78
79 do
80 {
81 pending_ = false;
82 std::apply([&](auto&... a) { fun_(in_isr, arg_, a...); }, cur_args);
83
84 if (pending_)
85 {
86 cur_args = pending_args_; // overwrite pending args on reentry
87 }
88 } while (pending_);
89
90 running_ = false;
91 return;
92 }
93
94 // reentrant: cache one pending request (overwrite)
95 pending_args_ = std::tuple<std::decay_t<Args>...>{args...};
96 pending_ = true;
97 }

◆ operator=()

template<typename ArgType , typename... Args>
CallbackBlock & LibXR::CallbackBlock< ArgType, Args >::operator= ( CallbackBlock< ArgType, Args > && other)
inlinenoexcept

移动赋值运算符,转移回调函数与参数 / Move assignment operator transferring function and argument

Parameters
other另一个 CallbackBlock 实例 / Another CallbackBlock instance
Returns
当前对象引用 / Reference to the current object

Definition at line 125 of file libxr_cb.hpp.

126 {
127 if (this != &other)
128 {
129 fun_ = std::exchange(other.fun_, nullptr);
130 arg_ = std::move(other.arg_);
131 in_isr_ = other.in_isr_;
132 }
133 return *this;
134 }

Field Documentation

◆ arg_

template<typename ArgType , typename... Args>
ArgType LibXR::CallbackBlock< ArgType, Args >::arg_
private

绑定的参数 / Bound argument

Definition at line 138 of file libxr_cb.hpp.

◆ fun_

template<typename ArgType , typename... Args>
void(* LibXR::CallbackBlock< ArgType, Args >::fun_) (bool, ArgType, Args...)
private

绑定的回调函数 / Bound callback function

Definition at line 137 of file libxr_cb.hpp.

◆ in_isr_

template<typename ArgType , typename... Args>
bool LibXR::CallbackBlock< ArgType, Args >::in_isr_ = false
private

是否在中断上下文中执行 / Whether executed in ISR context

Definition at line 139 of file libxr_cb.hpp.

◆ pending_

template<typename ArgType , typename... Args>
bool LibXR::CallbackBlock< ArgType, Args >::pending_ = false
private

Definition at line 30 of file libxr_cb.hpp.

◆ pending_args_

template<typename ArgType , typename... Args>
std::tuple<std::decay_t<Args>...> LibXR::CallbackBlock< ArgType, Args >::pending_args_ {}
private

Definition at line 31 of file libxr_cb.hpp.

31{};

◆ running_

template<typename ArgType , typename... Args>
bool LibXR::CallbackBlock< ArgType, Args >::running_ = false
private

Definition at line 29 of file libxr_cb.hpp.


The documentation for this class was generated from the following file: