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

提供一个通用的回调包装,支持动态参数传递。 Provides a generic callback wrapper, supporting dynamic argument passing. More...

#include <libxr_cb.hpp>

Public Member Functions

 Callback ()
 默认构造函数,创建空回调对象。 Default constructor, creating an empty callback instance.
 
 Callback (const Callback &)=default
 
Callbackoperator= (const Callback &)=default
 
 Callback (Callback &&other) noexcept
 移动构造函数,转移回调对象的所有权。 Move constructor, transferring ownership of the callback object.
 
Callbackoperator= (Callback &&other) noexcept
 移动赋值运算符,转移回调对象的所有权。 Move assignment operator, transferring ownership of the callback object.
 
template<typename... PassArgs>
void Run (bool in_isr, PassArgs &&...args) const
 执行回调函数,并传递参数。 Executes the callback function, passing the arguments.
 
bool Empty () const
 检查回调是否为空。 Checks if the callback is empty.
 

Static Public Member Functions

template<typename FunType , typename ArgType >
static Callback Create (FunType fun, ArgType arg)
 创建一个新的回调对象,并绑定回调函数和参数。 Creates a new callback instance, binding a function and an argument.
 

Private Member Functions

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

Private Attributes

void * cb_block_
 存储回调块的指针。 Pointer to the callback block.
 
void(* cb_fun_ )(bool, void *, Args...)
 存储回调执行的函数指针。 Pointer to the callback execution function.
 

Detailed Description

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

提供一个通用的回调包装,支持动态参数传递。 Provides a generic callback wrapper, supporting dynamic argument passing.

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

Definition at line 124 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 instance.

Definition at line 161 of file libxr_cb.hpp.

161: cb_block_(nullptr), cb_fun_(nullptr) {}
void * cb_block_
存储回调块的指针。 Pointer to the callback block.
Definition libxr_cb.hpp:240
void(* cb_fun_)(bool, void *, Args...)
存储回调执行的函数指针。 Pointer to the callback execution function.
Definition libxr_cb.hpp:241

◆ Callback() [2/3]

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

移动构造函数,转移回调对象的所有权。 Move constructor, transferring ownership of the callback object.

Parameters
other另一个 Callback 实例。 Another instance of Callback.

Definition at line 173 of file libxr_cb.hpp.

174 : cb_block_(std::exchange(other.cb_block_, nullptr)),
175 cb_fun_(std::exchange(other.cb_fun_, nullptr))
176 {
177 }

◆ Callback() [3/3]

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

私有构造函数,仅用于内部创建回调实例。 Private constructor, used internally for creating callback instances.

Parameters
cb_block绑定的回调块对象。 The bound callback block object.
cb_fun处理回调调用的函数指针。 Function pointer for handling callback invocation.

Definition at line 235 of file libxr_cb.hpp.

236 : cb_block_(cb_block), cb_fun_(cb_fun)
237 {
238 }

Member Function Documentation

◆ Create()

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

创建一个新的回调对象,并绑定回调函数和参数。 Creates a new callback instance, binding a function and an argument.

Template Parameters
FunType回调函数类型。 Type of the callback function.
ArgType绑定的参数类型。 Type of the bound argument.
Parameters
fun需要绑定的回调函数。 The callback function to bind.
arg绑定的参数值。 The bound argument value.
Returns
生成的 Callback 实例。 The created Callback instance.

Definition at line 143 of file libxr_cb.hpp.

144 {
145 void (*fun_ptr)(bool, ArgType, Args...) = fun;
146 auto cb_block = new CallbackBlock<ArgType, Args...>(fun_ptr, arg);
147
148 auto cb_fun = [](bool in_isr, void *cb_block, Args... args)
149 {
150 auto *cb = static_cast<CallbackBlock<ArgType, Args...> *>(cb_block);
151 cb->Call(in_isr, std::forward<Args>(args)...);
152 };
153
154 return Callback(cb_block, cb_fun);
155 }
Callback()
默认构造函数,创建空回调对象。 Default constructor, creating an empty callback instance.
Definition libxr_cb.hpp:161

◆ Empty()

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

检查回调是否为空。 Checks if the callback is empty.

Returns
true
false

Definition at line 223 of file libxr_cb.hpp.

223{ return cb_block_ == nullptr || cb_fun_ == nullptr; }

◆ operator=()

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

移动赋值运算符,转移回调对象的所有权。 Move assignment operator, transferring ownership of the callback object.

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

Definition at line 188 of file libxr_cb.hpp.

189 {
190 if (this != &other)
191 {
192 cb_block_ = std::exchange(other.cb_block_, nullptr);
193 cb_fun_ = std::exchange(other.cb_fun_, nullptr);
194 }
195 return *this;
196 }

◆ Run()

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

执行回调函数,并传递参数。 Executes the callback function, passing the arguments.

Parameters
in_isr指示是否在中断上下文中执行。 Indicates whether the call is executed within an ISR context.
args额外传递的参数。 Additional arguments to pass.

Definition at line 208 of file libxr_cb.hpp.

209 {
210 if (cb_fun_)
211 {
212 cb_fun_(in_isr, cb_block_, std::forward<PassArgs>(args)...);
213 }
214 }

Field Documentation

◆ cb_block_

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

存储回调块的指针。 Pointer to the callback block.

Definition at line 240 of file libxr_cb.hpp.

◆ cb_fun_

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

存储回调执行的函数指针。 Pointer to the callback execution function.

Definition at line 241 of file libxr_cb.hpp.


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