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>

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
 执行回调函数并传递参数 / Execute the callback with arguments
 
bool Empty () const
 检查回调是否为空 / Check whether the callback is empty
 

Static Public Member Functions

template<typename FunType , typename ArgType >
static Callback Create (FunType fun, ArgType arg)
 创建回调对象并绑定回调函数与参数 / Create a callback instance with bound function and argument
 

Private Member Functions

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

Static Private Member Functions

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

Private Attributes

void * cb_block_ = nullptr
 回调块指针 / Pointer to the callback block
 
void(* cb_fun_ )(bool, void *, Args...)
 回调执行函数指针 / Callback invocation function pointer
 

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 149 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 184 of file libxr_cb.hpp.

184{}

◆ 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 195 of file libxr_cb.hpp.

196 : cb_block_(std::exchange(other.cb_block_, nullptr)),
197 cb_fun_(std::exchange(other.cb_fun_, nullptr))
198 {
199 }
void * cb_block_
回调块指针 / Pointer to the callback block
Definition libxr_cb.hpp:251
void(* cb_fun_)(bool, void *, Args...)
回调执行函数指针 / Callback invocation function pointer
Definition libxr_cb.hpp:252

◆ Callback() [3/3]

template<typename... Args>
LibXR::Callback< Args >::Callback ( void * cb_block,
void(* cb_fun )(bool, void *, Args...) )
inlineprivate

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

Parameters
cb_block回调块对象指针 / Pointer to the callback block
cb_fun回调执行函数指针 / Callback invocation function pointer

Definition at line 246 of file libxr_cb.hpp.

247 : cb_block_(cb_block), cb_fun_(cb_fun)
248 {
249 }

Member Function Documentation

◆ Create()

template<typename... Args>
template<typename FunType , typename ArgType >
static Callback LibXR::Callback< Args >::Create ( FunType fun,
ArgType arg )
inlinestaticnodiscard

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

Template Parameters
FunType回调函数类型 / Callback function type
ArgType绑定参数类型 / Bound argument type
Parameters
fun需要绑定的回调函数 / Callback function to bind
arg绑定的参数值 / Bound argument value
Returns
Callback 实例 / Created Callback instance
Note
包含动态内存分配 / Contains dynamic memory allocation

Definition at line 167 of file libxr_cb.hpp.

168 {
169 void (*fun_ptr)(bool, ArgType, Args...) = fun;
170 auto cb_block = new CallbackBlock<ArgType, Args...>(fun_ptr, arg);
171
172 auto cb_fun = [](bool in_isr, void* cb_block, Args... args)
173 {
174 auto* cb = static_cast<CallbackBlock<ArgType, Args...>*>(cb_block);
175 cb->Call(in_isr, std::forward<Args>(args)...);
176 };
177
178 return Callback(cb_block, cb_fun);
179 }
Callback()
默认构造函数,创建空回调对象 / Default constructor creating an empty callback
Definition libxr_cb.hpp:184

◆ 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 236 of file libxr_cb.hpp.

236{ return cb_block_ == nullptr; }

◆ FunctionDefault()

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

Definition at line 151 of file libxr_cb.hpp.

151{}

◆ 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 208 of file libxr_cb.hpp.

209 {
210 if (this != &other)
211 {
212 cb_block_ = std::exchange(other.cb_block_, nullptr);
213 cb_fun_ = std::exchange(other.cb_fun_, nullptr);
214 }
215 return *this;
216 }

◆ Run()

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

执行回调函数并传递参数 / Execute the callback with arguments

Parameters
in_isr是否在中断上下文中执行 / Whether executed in ISR context
args额外传递的参数 / Additional arguments to pass

Definition at line 225 of file libxr_cb.hpp.

226 {
227 cb_fun_(in_isr, cb_block_, std::forward<PassArgs>(args)...);
228 }

Field Documentation

◆ cb_block_

template<typename... Args>
void* LibXR::Callback< Args >::cb_block_ = nullptr
private

回调块指针 / Pointer to the callback block

Definition at line 251 of file libxr_cb.hpp.

◆ cb_fun_

template<typename... Args>
void(* LibXR::Callback< Args >::cb_fun_) (bool, void *, Args...)
private
Initial value:
=
FunctionDefault

回调执行函数指针 / Callback invocation function pointer

Definition at line 252 of file libxr_cb.hpp.


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