libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::Topic::Callback Class Reference

每次发布时直接执行函数的订阅句柄 / Subscription handle that runs a function on each publish More...

#include <callback.hpp>

Collaboration diagram for LibXR::Topic::Callback:
[legend]

Data Structures

struct  BlockHeader
 回调句柄真正指向的那块数据的公共头 / Common header of the data block actually pointed to by one callback handle More...
 
struct  Factory
 看回调有几个参数,决定分配哪种回调块 / Choose which callback block to allocate from the callback parameter count More...
 
struct  Factory< Function, BoundArg, 3 >
 三参数回调块工厂 / Factory for three-argument callback blocks More...
 
struct  Factory< Function, BoundArg, 4 >
 四参数回调块工厂 / Factory for four-argument callback blocks More...
 
struct  FunctionTraits
 把一个无捕获回调函数拆成返回类型和参数类型 / Split one callback function without captures into its return type and parameter types More...
 
struct  FunctionTraits< Return(*)(Args...)>
 
struct  MessageViewTraits
 判断某个参数是否为 MessageView<T> / Tell whether one argument is MessageView<T> More...
 
struct  MessageViewTraits< MessageView< Data > >
 
struct  PayloadOnlyBlock
 只转发负载的回调块 / Callback block that forwards only the payload More...
 
struct  TimestampPayloadBlock
 同时转发时间戳和负载的回调块 / Callback block that forwards both timestamp and payload More...
 

Public Member Functions

 Callback ()=default
 构造一个空回调句柄 / Construct one empty callback handle
 
 Callback (const Callback &other)=default
 拷贝构造回调句柄 / Copy-construct one callback handle
 
Callbackoperator= (const Callback &other)=default
 拷贝赋值回调句柄 / Copy-assign one callback handle
 
void Run (bool in_isr, MicrosecondTimestamp timestamp, void *payload_addr, size_t payload_size) const
 执行回调 / Run the callback
 
TypeID::ID PayloadTypeID () const
 读取这个回调句柄期望的 payload 类型标识 / Read the payload type ID expected by this callback handle
 
bool IsRawPayloadView () const
 判断该回调是否按 raw payload 视图接收消息 / Tell whether this callback receives messages as a raw payload view
 

Static Public Member Functions

template<typename BoundArg , typename Callable >
static Callback Create (Callable fun, BoundArg arg)
 创建一个回调订阅句柄 / Create a callback subscription handle
 

Private Types

template<typename T >
using RemoveCVRef = std::remove_cv_t<std::remove_reference_t<T>>
 去掉引用和 cv 限定后的规范类型 / Canonical type after removing reference and cv qualifiers
 

Private Member Functions

 Callback (BlockHeader *block)
 用指定执行块构造回调句柄 / Construct one callback handle from the given execution block
 

Static Private Member Functions

template<typename PayloadArg >
static TypeID::ID PayloadTypeID ()
 取回调 payload 参数对应的精确类型标识 / Get the exact type ID expected by one callback payload argument
 
template<typename PayloadArg >
static constexpr bool AcceptsRawPayload ()
 回调 payload 参数是否是 raw 字节视图 / Whether the callback payload argument is a raw byte view
 
template<typename Function , typename BoundArg , typename PayloadArg >
static void InvokePayload (Function fun, BoundArg &arg, bool in_isr, MicrosecondTimestamp timestamp, void *payload_addr, size_t payload_size)
 按回调声明的参数形式,把当前消息拼成实参再调函数 / Build the actual call arguments from the current message in the form declared by the callback, then invoke it
 
static void EmptyRun (const BlockHeader *header, bool in_isr, MicrosecondTimestamp timestamp, void *payload_addr, size_t payload_size)
 空回调块的执行函数 / Execution function of the empty callback block
 

Private Attributes

BlockHeaderblock_ = &empty_block_
 

Static Private Attributes

template<typename T >
static constexpr bool IS_MESSAGE_VIEW
 是否为带时间戳的类型化消息视图参数 / Whether one callback payload argument is a timestamped typed message view
 
template<typename T >
static constexpr bool IS_RAW_DATA_VIEW
 是否为只读 raw payload 视图参数 / Whether one callback payload argument is a read-only raw payload view
 
template<typename T >
static constexpr bool IS_TYPED_DATA
 是否把 payload 直接当成一个对象接收 / Whether one callback payload argument receives the payload directly as one object
 
template<typename T >
static constexpr bool IS_CALLBACK_PAYLOAD
 是否是回调接口允许的 payload 参数写法 / Whether one payload argument form is accepted by this callback interface
 
template<typename T >
static constexpr bool IS_MUTABLE_MESSAGE_VIEW_REF
 是否错误地使用了可变 MessageView<T>& / Whether one payload argument is an invalid mutable MessageView<T>&
 
static BlockHeader empty_block_
 

Detailed Description

每次发布时直接执行函数的订阅句柄 / Subscription handle that runs a function on each publish

每次发布时直接执行函数的回调订阅句柄 / Callback subscription handle that runs a function on each publish

Definition at line 15 of file callback.hpp.

Member Typedef Documentation

◆ RemoveCVRef

template<typename T >
using LibXR::Topic::Callback::RemoveCVRef = std::remove_cv_t<std::remove_reference_t<T>>
private

去掉引用和 cv 限定后的规范类型 / Canonical type after removing reference and cv qualifiers

Template Parameters
T待规范化的类型 / Type to normalize

Definition at line 82 of file callback.hpp.

Constructor & Destructor Documentation

◆ Callback() [1/2]

LibXR::Topic::Callback::Callback ( const Callback & other)
default

拷贝构造回调句柄 / Copy-construct one callback handle

Parameters
other被拷贝的回调句柄 / Callback handle to copy from
Note
这里只复制句柄本身,不复制底层回调块;两个句柄会指向同一块回调数据 / This copies only the handle itself, not the underlying callback block; both handles point to the same callback data block

◆ Callback() [2/2]

LibXR::Topic::Callback::Callback ( BlockHeader * block)
inlineexplicitprivate

用指定执行块构造回调句柄 / Construct one callback handle from the given execution block

Parameters
block回调块公共头;为空时回退到静态空块 / Callback block header; falls back to the shared empty block when null

Definition at line 554 of file callback.hpp.

554: block_(block ? block : &empty_block_) {}
static BlockHeader empty_block_
Definition callback.hpp:467
BlockHeader * block_
Definition callback.hpp:556

Member Function Documentation

◆ AcceptsRawPayload()

template<typename PayloadArg >
static constexpr bool LibXR::Topic::Callback::AcceptsRawPayload ( )
inlinestaticconstexprprivate

回调 payload 参数是否是 raw 字节视图 / Whether the callback payload argument is a raw byte view

Template Parameters
PayloadArg回调 payload 参数类型 / Callback payload argument type
Returns
是 raw 字节视图则返回 true / Returns true for raw byte view payloads

Definition at line 201 of file callback.hpp.

202 {
204 }
static constexpr bool IS_RAW_DATA_VIEW
是否为只读 raw payload 视图参数 / Whether one callback payload argument is a read-only raw payload view
Definition callback.hpp:101

◆ Create()

template<typename BoundArg , typename Callable >
static Callback LibXR::Topic::Callback::Create ( Callable fun,
BoundArg arg )
inlinestaticnodiscard

创建一个回调订阅句柄 / Create a callback subscription handle

Template Parameters
BoundArg绑定参数类型 / Bound argument type
Callable回调可调用对象类型 / Callback callable type
Parameters
fun捕获为空的回调函数 / Capture-free callback function
arg绑定到回调的参数 / Argument bound into the callback
Returns
创建后的回调订阅句柄 / Returns the created callback subscription handle
Note
这里会把 arg 拷进新分配的回调块里;之后回调执行读写的就是这份块内副本 / This copies arg into the newly allocated callback block; later callback execution reads or writes that stored copy

Definition at line 508 of file callback.hpp.

509 {
510 using Function = decltype(+std::declval<Callable>());
511 using Traits = FunctionTraits<Function>;
512 static_assert(Traits::ARITY == 3 || Traits::ARITY == 4,
513 "LibXR::Topic::Callback::Create expects a capture-free callable "
514 "with bool and bound-argument parameters.");
515 return Callback(
516 Factory<Function, BoundArg, Traits::ARITY>::Create(+fun, std::move(arg)));
517 }
Callback()=default
构造一个空回调句柄 / Construct one empty callback handle

◆ EmptyRun()

static void LibXR::Topic::Callback::EmptyRun ( const BlockHeader * header,
bool in_isr,
MicrosecondTimestamp timestamp,
void * payload_addr,
size_t payload_size )
inlinestaticprivate

空回调块的执行函数 / Execution function of the empty callback block

Parameters
header空块头指针 / Empty block header pointer
in_isr当前是否处于中断上下文 / Whether the current path runs in ISR context
timestamp当前消息时间戳 / Current message timestamp
payload_addr当前消息 payload 对象地址 / Address of the current payload object

Definition at line 456 of file callback.hpp.

459 {
460 UNUSED(header);
461 UNUSED(in_isr);
462 UNUSED(timestamp);
463 UNUSED(payload_addr);
464 UNUSED(payload_size);
465 }

◆ InvokePayload()

template<typename Function , typename BoundArg , typename PayloadArg >
static void LibXR::Topic::Callback::InvokePayload ( Function fun,
BoundArg & arg,
bool in_isr,
MicrosecondTimestamp timestamp,
void * payload_addr,
size_t payload_size )
inlinestaticprivate

按回调声明的参数形式,把当前消息拼成实参再调函数 / Build the actual call arguments from the current message in the form declared by the callback, then invoke it

Template Parameters
Function回调函数类型 / Callback function type
BoundArg绑定参数类型 / Bound argument type
PayloadArg负载参数类型 / Payload argument type
Parameters
fun回调函数 / Callback function
arg绑定参数 / Bound argument
in_isr当前是否处于中断上下文 / Whether the current path runs in ISR context
timestamp当前消息时间戳 / Current message timestamp
payload_addr当前消息 payload 对象地址 / Address of the current payload object

Definition at line 221 of file callback.hpp.

224 {
225 if constexpr (std::same_as<RemoveCVRef<PayloadArg>, ConstRawData>)
226 {
227 ConstRawData raw_payload{payload_addr, payload_size};
228 fun(in_isr, arg, raw_payload);
229 }
230 else if constexpr (std::same_as<RemoveCVRef<PayloadArg>, RawMessageView>)
231 {
232 RawMessageView raw_message{timestamp, ConstRawData{payload_addr, payload_size}};
233 fun(in_isr, arg, raw_message);
234 }
235 else if constexpr (IS_MESSAGE_VIEW<PayloadArg>)
236 {
237 using View = MessageViewTraits<RemoveCVRef<PayloadArg>>;
238 using Data = typename View::DataType;
239 fun(in_isr, arg,
240 MessageView<Data>{timestamp, reinterpret_cast<Data*>(payload_addr)});
241 }
242 else
243 {
244 using Data = RemoveCVRef<PayloadArg>;
245 fun(in_isr, arg, *reinterpret_cast<Data*>(payload_addr));
246 }
247 }
static constexpr bool IS_MESSAGE_VIEW
是否为带时间戳的类型化消息视图参数 / Whether one callback payload argument is a timestamped typed message view
Definition callback.hpp:90
std::remove_cv_t< std::remove_reference_t< T > > RemoveCVRef
去掉引用和 cv 限定后的规范类型 / Canonical type after removing reference and cv qualifiers
Definition callback.hpp:82

◆ IsRawPayloadView()

bool LibXR::Topic::Callback::IsRawPayloadView ( ) const
inline

判断该回调是否按 raw payload 视图接收消息 / Tell whether this callback receives messages as a raw payload view

Returns
是 raw payload 视图回调则返回 true / Returns true for raw payload view callbacks

Definition at line 545 of file callback.hpp.

◆ operator=()

Callback & LibXR::Topic::Callback::operator= ( const Callback & other)
default

拷贝赋值回调句柄 / Copy-assign one callback handle

Parameters
other被拷贝的回调句柄 / Callback handle to copy from
Returns
当前回调句柄 / Returns the current callback handle
Note
这里只复制句柄本身,不复制底层回调块;赋值后两个句柄会指向同一块回调数据 / This copies only the handle itself, not the underlying callback block; after assignment both handles point to the same callback data block

◆ PayloadTypeID() [1/2]

template<typename PayloadArg >
static TypeID::ID LibXR::Topic::Callback::PayloadTypeID ( )
inlinestaticprivate

取回调 payload 参数对应的精确类型标识 / Get the exact type ID expected by one callback payload argument

Template Parameters
PayloadArg回调 payload 参数类型 / Callback payload argument type
Returns
对应的精确类型标识 / Exact type ID expected by this callback payload argument

Definition at line 170 of file callback.hpp.

171 {
173 "LibXR::Topic::Callback does not accept MessageView<T>&; "
174 "use MessageView<T> or const MessageView<T>& instead.");
175
176 if constexpr (IS_RAW_DATA_VIEW<PayloadArg>)
177 {
178 return nullptr;
179 }
180 else if constexpr (IS_MESSAGE_VIEW<PayloadArg>)
181 {
182 using View = MessageViewTraits<RemoveCVRef<PayloadArg>>;
184 }
185 else
186 {
187 static_assert(IS_TYPED_DATA<PayloadArg>,
188 "LibXR::Topic::Callback payload must be Topic::MessageView<T>, "
189 "Topic::RawMessageView, ConstRawData, T, T&, or const T&.");
191 }
192 }
static constexpr bool IS_MUTABLE_MESSAGE_VIEW_REF
是否错误地使用了可变 MessageView<T>& / Whether one payload argument is an invalid mutable MessageView<T>&
Definition callback.hpp:133
static constexpr bool IS_TYPED_DATA
是否把 payload 直接当成一个对象接收 / Whether one callback payload argument receives the payload directly as one o...
Definition callback.hpp:110
static ID GetID()
获取类型的唯一标识符 / Get a unique identifier for type T

◆ PayloadTypeID() [2/2]

TypeID::ID LibXR::Topic::Callback::PayloadTypeID ( ) const
inline

读取这个回调句柄期望的 payload 类型标识 / Read the payload type ID expected by this callback handle

Returns
payload 类型标识 / Payload type ID

Definition at line 537 of file callback.hpp.

◆ Run()

void LibXR::Topic::Callback::Run ( bool in_isr,
MicrosecondTimestamp timestamp,
void * payload_addr,
size_t payload_size ) const
inline

执行回调 / Run the callback

Parameters
in_isr当前是否处于中断上下文 / Whether the current path runs in ISR context
timestamp当前消息时间戳 / Current message timestamp
payload_addr当前消息 payload 对象地址 / Address of the current payload object

Definition at line 526 of file callback.hpp.

528 {
529 block_->run(block_, in_isr, timestamp, payload_addr, payload_size);
530 }
RunFun run
当前块的执行函数。Execution function of the current block.
Definition callback.hpp:156

Field Documentation

◆ block_

BlockHeader* LibXR::Topic::Callback::block_ = &empty_block_
private

当前回调句柄绑定的执行块。Execution block bound to the current callback handle.

Definition at line 556 of file callback.hpp.

◆ empty_block_

BlockHeader LibXR::Topic::Callback::empty_block_
inlinestaticprivate
Initial value:
{
&EmptyRun, nullptr, false}
static void EmptyRun(const BlockHeader *header, bool in_isr, MicrosecondTimestamp timestamp, void *payload_addr, size_t payload_size)
空回调块的执行函数 / Execution function of the empty callback block
Definition callback.hpp:456

空回调句柄共用的静态空执行块。Shared static empty execution block used by empty callback handles.

Definition at line 467 of file callback.hpp.

467 {
468 &EmptyRun, nullptr, false};

◆ IS_CALLBACK_PAYLOAD

template<typename T >
bool LibXR::Topic::Callback::IS_CALLBACK_PAYLOAD
staticconstexprprivate
Initial value:

是否是回调接口允许的 payload 参数写法 / Whether one payload argument form is accepted by this callback interface

Template Parameters
T待判断的参数类型 / Argument type to inspect 是否是这个回调接口允许的负载参数写法。Whether this callback interface accepts this payload argument form.

Definition at line 122 of file callback.hpp.

◆ IS_MESSAGE_VIEW

template<typename T >
bool LibXR::Topic::Callback::IS_MESSAGE_VIEW
staticconstexprprivate
Initial value:
= MessageViewTraits<

是否为带时间戳的类型化消息视图参数 / Whether one callback payload argument is a timestamped typed message view

Template Parameters
T待判断的参数类型 / Argument type to inspect 是否为带时间戳的类型化消息视图参数。Whether the argument is a timestamped typed message view.

Definition at line 90 of file callback.hpp.

◆ IS_MUTABLE_MESSAGE_VIEW_REF

template<typename T >
bool LibXR::Topic::Callback::IS_MUTABLE_MESSAGE_VIEW_REF
staticconstexprprivate
Initial value:
=
IS_MESSAGE_VIEW<T> && std::is_lvalue_reference_v<T> &&
!std::is_const_v<std::remove_reference_t<T>>

是否错误地使用了可变 MessageView<T>& / Whether one payload argument is an invalid mutable MessageView<T>&

Template Parameters
T待判断的参数类型 / Argument type to inspect

Definition at line 133 of file callback.hpp.

◆ IS_RAW_DATA_VIEW

template<typename T >
bool LibXR::Topic::Callback::IS_RAW_DATA_VIEW
staticconstexprprivate
Initial value:
= std::same_as<RemoveCVRef<T>, ConstRawData> ||
std::same_as<RemoveCVRef<T>, RawMessageView>

是否为只读 raw payload 视图参数 / Whether one callback payload argument is a read-only raw payload view

Template Parameters
T待判断的参数类型 / Argument type to inspect

Definition at line 101 of file callback.hpp.

◆ IS_TYPED_DATA

template<typename T >
bool LibXR::Topic::Callback::IS_TYPED_DATA
staticconstexprprivate
Initial value:
=
topic 可承载 payload 的类型约束 / Type constraint for payloads carried by one topic
Definition topic.hpp:27

是否把 payload 直接当成一个对象接收 / Whether one callback payload argument receives the payload directly as one object

Template Parameters
T待判断的参数类型 / Argument type to inspect 是否把负载直接当成一个对象来接收。Whether the payload is received directly as one typed object.

Definition at line 110 of file callback.hpp.


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