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()

LibXR::Event::Event ( )
inline

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

Definition at line 33 of file event.hpp.

34 : rbt_([](const uint32_t &a, const uint32_t &b)
35 { return static_cast<int>(a) - static_cast<int>(b); })
36 {
37 }
RBTree< uint32_t > rbt_
Definition event.hpp:175

Member Function Documentation

◆ Active()

void LibXR::Event::Active ( uint32_t event)
inline

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

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

Definition at line 69 of file event.hpp.

70 {
71 auto list = rbt_.Search<LockFreeList>(event);
72 if (!list)
73 {
74 return;
75 }
76
77 auto foreach_fun = [=](Block &block)
78 {
79 block.cb.Run(false, event);
80 return ErrorCode::OK;
81 };
82
83 list->data_.Foreach<LibXR::Event::Block>(foreach_fun);
84 }
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:169

◆ ActiveFromCallback()

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

从回调函数中触发与特定事件关联的所有回调函数。 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 95 of file event.hpp.

96 {
97 if (!list)
98 {
99 return;
100 }
101
102 auto foreach_fun = [=](Block &block)
103 {
104 block.cb.Run(true, event);
105 return ErrorCode::OK;
106 };
107
108 list->Foreach<Block>(foreach_fun);
109 }

◆ Bind()

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

将源事件绑定到当前事件实例中的目标事件。 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 139 of file event.hpp.

140 {
141 struct BindBlock
142 {
143 Event *target;
144 uint32_t event;
145 };
146
147 auto block = new BindBlock{this, target_event};
148
149 auto bind_fun = [](bool in_isr, BindBlock *block, uint32_t event)
150 {
151 UNUSED(event);
152 UNUSED(in_isr);
153 block->target->ActiveFromCallback(block->target->GetList(block->event),
154 block->event);
155 };
156
157 auto cb = Callback::Create(bind_fun, block);
158
159 sources.Register(source_event, cb);
160 }
static Callback Create(FunType fun, ArgType arg)
Definition libxr_cb.hpp:143

◆ GetList()

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

获取指定事件的回调链表指针(必须在非中断上下文中调用)。 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 119 of file event.hpp.

120 {
121 auto node = rbt_.Search<LockFreeList>(event);
122 if (!node)
123 {
124 auto list = new RBTree<uint32_t>::Node<LockFreeList>;
125 rbt_.Insert(*list, event);
126 node = list;
127 }
128 return &node->data_;
129 }
void Insert(BaseNode &node, KeyType &&key)
在树中插入新节点 (Insert a new node into the tree).
Definition rbt.hpp:237

◆ Register()

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

为特定事件注册回调函数。 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 46 of file event.hpp.

47 {
48 auto list = rbt_.Search<LockFreeList>(event);
49
50 if (!list)
51 {
52 list = new RBTree<uint32_t>::Node<LockFreeList>;
53 rbt_.Insert(*list, event);
54 }
55
56 LockFreeList::Node<Block> *node = new LockFreeList::Node<Block>;
57
58 node->data_.event = event;
59 node->data_.cb = cb;
60 list->data_.Add(*node);
61 }

Field Documentation

◆ rbt_

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

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

Definition at line 175 of file event.hpp.


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