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

通用回调包装,支持动态参数传递 / Generic callback wrapper supporting dynamic argument passing More...

#include <libxr_cb.hpp>

Collaboration diagram for LibXR::Callback< Args >:
[legend]

Public Member Functions

 Callback ()
 默认构造函数,创建空回调对象 / Default constructor creating an empty callback
 
 Callback (const Callback &)=default
 
Callbackoperator= (const Callback &)=default
 
 Callback (Callback &&other) noexcept
 移动构造函数,转移回调对象的所有权 / Move constructor transferring callback ownership
 
Callbackoperator= (Callback &&other) noexcept
 移动赋值运算符,转移回调对象的所有权 / Move assignment operator transferring callback ownership
 
template<typename... PassArgs>
void Run (bool in_isr, PassArgs &&... args) const
 
bool Empty () const
 检查回调是否为空 / Check whether the callback is empty
 

Static Public Member Functions

template<typename BoundArgType , typename CallableType >
requires CallbackFunctionCompatible<CallableType, BoundArgType, Args...>
static Callback Create (CallableType fun, BoundArgType arg)
 创建回调对象并绑定回调函数与参数 / Create a callback instance with bound function and argument
 
template<typename BoundArgType , typename CallableType >
requires CallbackFunctionCompatible<CallableType, BoundArgType, Args...>
static Callback CreateGuarded (CallableType fun, BoundArgType arg)
 创建带防重入保护的回调 / Create a guarded callback
 

Private Member Functions

 Callback (CallbackBlockHeader< Args... > *cb_block)
 私有构造函数,仅用于内部创建回调实例 / Private constructor used internally to create callback instances
 

Static Private Member Functions

static void FunctionDefault (void *, bool, Args...)
 

Private Attributes

CallbackBlockHeader< Args... > * cb_block_
 回调块指针 / Pointer to the callback block
 

Static Private Attributes

static CallbackBlockHeader< Args... > empty_cb_block_ = {&FunctionDefault}
 

Detailed Description

template<typename... Args>
class LibXR::Callback< Args >

通用回调包装,支持动态参数传递 / Generic callback wrapper supporting dynamic argument passing

Template Parameters
Args额外的参数类型列表 / Additional argument types

Definition at line 143 of file libxr_cb.hpp.

Constructor & Destructor Documentation

◆ Callback() [1/3]

template<typename... Args>
LibXR::Callback< Args >::Callback ( )
inline

默认构造函数,创建空回调对象 / Default constructor creating an empty callback

Definition at line 204 of file libxr_cb.hpp.

204: cb_block_(&empty_cb_block_) {}
CallbackBlockHeader< Args... > * cb_block_
回调块指针 / Pointer to the callback block
Definition libxr_cb.hpp:262

◆ Callback() [2/3]

template<typename... Args>
LibXR::Callback< Args >::Callback ( Callback< Args > && other)
inlinenoexcept

移动构造函数,转移回调对象的所有权 / Move constructor transferring callback ownership

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

Definition at line 215 of file libxr_cb.hpp.

216 : cb_block_(std::exchange(other.cb_block_, &empty_cb_block_))
217 {
218 }

◆ Callback() [3/3]

template<typename... Args>
LibXR::Callback< Args >::Callback ( CallbackBlockHeader< Args... > * cb_block)
inlineexplicitprivate

私有构造函数,仅用于内部创建回调实例 / Private constructor used internally to create callback instances

Parameters
cb_block回调块对象指针 / Pointer to the callback block

Definition at line 257 of file libxr_cb.hpp.

258 : cb_block_((cb_block != nullptr) ? cb_block : &empty_cb_block_)
259 {
260 }

Member Function Documentation

◆ Create()

template<typename... Args>
template<typename BoundArgType , typename CallableType >
requires CallbackFunctionCompatible<CallableType, BoundArgType, Args...>
static Callback LibXR::Callback< Args >::Create ( CallableType fun,
BoundArgType arg )
inlinestaticnodiscard

创建回调对象并绑定回调函数与参数 / Create a callback instance with bound function and argument

Template Parameters
BoundArgType绑定参数类型 / Bound argument type
CallableType回调可调用对象类型 / Callback callable type
Parameters
fun需要绑定的回调函数 / Callback function to bind
arg绑定的参数值 / Bound argument value
Returns
Callback 实例 / Created Callback instance
Note
预期用法是初始化阶段创建并长期持有; 运行时高频创建/销毁不属于设计目标。 Intended usage is initialization-time creation with long-lived retention; runtime churn-style create/destroy patterns are outside the intended model.

Definition at line 167 of file libxr_cb.hpp.

168 {
169 using FunctionType = typename CallbackBlock<BoundArgType, Args...>::FunctionType;
170 auto cb_block =
171 new CallbackBlock<BoundArgType, Args...>(static_cast<FunctionType>(fun),
172 std::move(arg));
173 return Callback(cb_block);
174 }
Callback()
默认构造函数,创建空回调对象 / Default constructor creating an empty callback
Definition libxr_cb.hpp:204

◆ CreateGuarded()

template<typename... Args>
template<typename BoundArgType , typename CallableType >
requires CallbackFunctionCompatible<CallableType, BoundArgType, Args...>
static Callback LibXR::Callback< Args >::CreateGuarded ( CallableType fun,
BoundArgType arg )
inlinestaticnodiscard

创建带防重入保护的回调 / Create a guarded callback

Template Parameters
BoundArgType绑定参数类型 / Bound argument type
CallableType回调可调用对象类型 / Callback callable type
Parameters
fun需要绑定的回调函数 / Callback function to bind
arg绑定的参数值 / Bound argument value
Returns
Callback 实例 / Created Callback instance
Note
该 guarded callback 预期用于单条逻辑回调链上的递归压平, 不承担任意多上下文串行化。 Guarded callbacks are meant for self-recursive flattening on one logical callback chain, not arbitrary multi-context serialization.

Definition at line 192 of file libxr_cb.hpp.

193 {
194 using FunctionType = typename CallbackBlock<BoundArgType, Args...>::FunctionType;
195 auto cb_block =
196 new GuardedCallbackBlock<BoundArgType, Args...>(static_cast<FunctionType>(fun),
197 std::move(arg));
198 return Callback(cb_block);
199 }

◆ Empty()

template<typename... Args>
bool LibXR::Callback< Args >::Empty ( ) const
inline

检查回调是否为空 / Check whether the callback is empty

Returns
true 回调为空 / Callback is empty
false 回调非空 / Callback is not empty

Definition at line 248 of file libxr_cb.hpp.

248{ return cb_block_ == &empty_cb_block_; }

◆ FunctionDefault()

template<typename... Args>
static void LibXR::Callback< Args >::FunctionDefault ( void * ,
bool ,
Args...  )
inlinestaticprivate

Definition at line 145 of file libxr_cb.hpp.

145{}

◆ operator=()

template<typename... Args>
Callback & LibXR::Callback< Args >::operator= ( Callback< Args > && other)
inlinenoexcept

移动赋值运算符,转移回调对象的所有权 / Move assignment operator transferring callback ownership

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

Definition at line 227 of file libxr_cb.hpp.

228 {
229 if (this != &other)
230 {
231 cb_block_ = std::exchange(other.cb_block_, &empty_cb_block_);
232 }
233 return *this;
234 }

◆ Run()

template<typename... Args>
template<typename... PassArgs>
void LibXR::Callback< Args >::Run ( bool in_isr,
PassArgs &&... args ) const
inline

Definition at line 237 of file libxr_cb.hpp.

238 {
239 cb_block_->run_fun_(cb_block_, in_isr, std::forward<PassArgs>(args)...);
240 }

Field Documentation

◆ cb_block_

template<typename... Args>
CallbackBlockHeader<Args...>* LibXR::Callback< Args >::cb_block_
private
Initial value:
=
&empty_cb_block_

回调块指针 / Pointer to the callback block

Definition at line 262 of file libxr_cb.hpp.

◆ empty_cb_block_

template<typename... Args>
CallbackBlockHeader<Args...> LibXR::Callback< Args >::empty_cb_block_ = {&FunctionDefault}
inlinestaticprivate

Definition at line 146 of file libxr_cb.hpp.

146{&FunctionDefault};

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