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

事件管理系统,允许基于事件 ID 注册和触发回调函数。 Event management system that allows registration and triggering of callbacks based on event IDs. More...

#include <event.hpp>

Collaboration diagram for LibXR::Event:
[legend]

Data Structures

struct  Block
 用于存储事件回调的数据结构。 Data structure for storing event callbacks. More...
 

Public Types

using Callback = LibXR::Callback<uint32_t>
 
using CallbackList = LockFreeList *
 回调链表指针类型,用于事件触发时从 ISR 中安全调用。 Pointer to the callback list, safe to use in ISR after acquired in non-interrupt context.
 

Public Member Functions

 Event ()
 构造函数,初始化用于存储事件的红黑树。 Constructs an Event object with an empty red-black tree for event storage.
 
void Register (uint32_t event, const Callback &cb)
 为特定事件注册回调函数。 Registers a callback function for a specific event.
 
void Active (uint32_t event)
 触发与特定事件关联的所有回调函数(非中断上下文)。 Triggers all callbacks associated with a specific event (non-interrupt context).
 
void ActiveFromCallback (CallbackList list, uint32_t event)
 从回调函数中触发与特定事件关联的所有回调函数。 Triggers all callbacks associated with a specific event (interrupt context).
 
CallbackList GetList (uint32_t event)
 获取指定事件的回调链表指针(必须在非中断上下文中调用)。 Returns the callback list pointer for the given event (must be called outside ISR).
 
void Bind (Event &sources, uint32_t source_event, uint32_t target_event)
 将源事件绑定到当前事件实例中的目标事件。 Binds an event from a source Event instance to a target event in the current instance.
 

Private Attributes

RBTree< uint32_t > rbt_
 

Detailed Description

事件管理系统,允许基于事件 ID 注册和触发回调函数。 Event management system that allows registration and triggering of callbacks based on event IDs.

Definition at line 17 of file event.hpp.

Member Typedef Documentation

◆ Callback

Definition at line 20 of file event.hpp.

◆ CallbackList

回调链表指针类型,用于事件触发时从 ISR 中安全调用。 Pointer to the callback list, safe to use in ISR after acquired in non-interrupt context.

Definition at line 27 of file event.hpp.

Constructor & Destructor Documentation

◆ Event()

Event::Event ( )

构造函数,初始化用于存储事件的红黑树。 Constructs an Event object with an empty red-black tree for event storage.

Definition at line 7 of file event.cpp.

8 : rbt_([](const uint32_t &a, const uint32_t &b)
9 { return static_cast<int>(a) - static_cast<int>(b); })
10{
11}
RBTree< uint32_t > rbt_
Definition event.hpp:96

Member Function Documentation

◆ Active()

void Event::Active ( uint32_t event)

触发与特定事件关联的所有回调函数(非中断上下文)。 Triggers all callbacks associated with a specific event (non-interrupt context).

Parameters
event要激活的事件 ID。 The event ID to activate.

Definition at line 30 of file event.cpp.

31{
32 auto list = rbt_.Search<LockFreeList>(event);
33 if (!list)
34 {
35 return;
36 }
37
38 auto foreach_fun = [=](Block &block)
39 {
40 block.cb.Run(false, event);
41 return ErrorCode::OK;
42 };
43
44 list->data_.Foreach<LibXR::Event::Block>(foreach_fun);
45}
链表实现,用于存储和管理数据节点。 A linked list implementation for storing and managing data nodes.
ErrorCode Foreach(Func func)
遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
Node< Data > * Search(const Key &key)
搜索红黑树中的节点 (Search for a node in the Red-Black Tree).
Definition rbt.hpp:122
用于存储事件回调的数据结构。 Data structure for storing event callbacks.
Definition event.hpp:90

◆ ActiveFromCallback()

void Event::ActiveFromCallback ( CallbackList list,
uint32_t event )

从回调函数中触发与特定事件关联的所有回调函数。 Triggers all callbacks associated with a specific event (interrupt context).

Parameters
list在非回调函数中获取的事件回调链表指针。 The event callback list pointer obtained from the non-callback function.
event要激活的事件 ID。 The event ID to activate.

Definition at line 47 of file event.cpp.

48{
49 if (!list)
50 {
51 return;
52 }
53
54 auto foreach_fun = [=](Block &block)
55 {
56 block.cb.Run(true, event);
57 return ErrorCode::OK;
58 };
59
60 list->Foreach<Block>(foreach_fun);
61}

◆ Bind()

void Event::Bind ( Event & sources,
uint32_t source_event,
uint32_t target_event )

将源事件绑定到当前事件实例中的目标事件。 Binds an event from a source Event instance to a target event in the current instance.

Parameters
sources包含原始事件的源 Event 实例。 The source Event instance.
source_event源事件实例中的事件 ID。 The source event ID.
target_event当前实例中的目标事件 ID。 The target event ID in this instance.

Definition at line 75 of file event.cpp.

76{
77 struct BindBlock
78 {
79 Event *target;
80 uint32_t event;
81 };
82
83 auto block = new BindBlock{this, target_event};
84
85 auto bind_fun = [](bool in_isr, BindBlock *block, uint32_t event)
86 {
87 UNUSED(event);
88 UNUSED(in_isr);
89 block->target->ActiveFromCallback(block->target->GetList(block->event), block->event);
90 };
91
92 auto cb = Callback::Create(bind_fun, block);
93
94 sources.Register(source_event, cb);
95}
static Callback Create(FunType fun, ArgType arg)
Definition libxr_cb.hpp:142
事件管理系统,允许基于事件 ID 注册和触发回调函数。 Event management system that allows registration and triggering of callba...
Definition event.hpp:18
void Register(uint32_t event, const Callback &cb)
为特定事件注册回调函数。 Registers a callback function for a specific event.
Definition event.cpp:13

◆ GetList()

Event::CallbackList Event::GetList ( uint32_t event)

获取指定事件的回调链表指针(必须在非中断上下文中调用)。 Returns the callback list pointer for the given event (must be called outside ISR).

Parameters
event要查询的事件 ID。 The event ID to search.
Returns
回调链表指针,如果未注册则主动创建。 The callback list pointer, if not registered, it is actively created.

Definition at line 63 of file event.cpp.

64{
65 auto node = rbt_.Search<LockFreeList>(event);
66 if (!node)
67 {
68 auto list = new RBTree<uint32_t>::Node<LockFreeList>;
69 rbt_.Insert(*list, event);
70 node = list;
71 }
72 return &node->data_;
73}
红黑树的泛型数据节点,继承自 BaseNode (Generic data node for Red-Black Tree, inheriting from BaseNode).
Definition rbt.hpp:64
void Insert(BaseNode &node, KeyType &&key)
在树中插入新节点 (Insert a new node into the tree).
Definition rbt.hpp:237

◆ Register()

void Event::Register ( uint32_t event,
const Callback & cb )

为特定事件注册回调函数。 Registers a callback function for a specific event.

Parameters
event要注册回调的事件 ID。 The event ID to register the callback for.
cb事件触发时执行的回调函数。 The callback function to be executed when the event occurs.

Definition at line 13 of file event.cpp.

14{
15 auto list = rbt_.Search<LockFreeList>(event);
16
17 if (!list)
18 {
20 rbt_.Insert(*list, event);
21 }
22
24
25 node->data_.event = event;
26 node->data_.cb = cb;
27 list->data_.Add(*node);
28}
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
Data data_
存储的数据。 The stored data.

Field Documentation

◆ rbt_

RBTree<uint32_t> LibXR::Event::rbt_
private

用于管理已注册事件的红黑树。 Red-black tree for managing registered events.

Definition at line 96 of file event.hpp.


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