事件管理系统,允许基于事件 ID 注册和触发回调函数。 Event management system that allows registration and triggering of callbacks based on event IDs.
More...
#include <event.hpp>
|
struct | Block |
| 用于存储事件回调的数据结构。 Data structure for storing event callbacks. More...
|
|
|
| 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.
|
|
事件管理系统,允许基于事件 ID 注册和触发回调函数。 Event management system that allows registration and triggering of callbacks based on event IDs.
Definition at line 17 of file event.hpp.
◆ Callback
◆ 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.
◆ Event()
构造函数,初始化用于存储事件的红黑树。 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 }
◆ 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 {
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
84 }
Node< Data > * Search(const Key &key)
搜索红黑树中的节点 (Search for a node in the Red-Black Tree).
用于存储事件回调的数据结构。 Data structure for storing event callbacks.
◆ 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
158
159 sources.Register(source_event, cb);
160 }
static Callback Create(FunType fun, ArgType arg)
◆ GetList()
获取指定事件的回调链表指针(必须在非中断上下文中调用)。 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 {
122 if (!node)
123 {
124 auto list = new RBTree<uint32_t>::Node<LockFreeList>;
126 node = list;
127 }
128 return &node->data_;
129 }
void Insert(BaseNode &node, KeyType &&key)
在树中插入新节点 (Insert a new node into the tree).
◆ 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 {
49
50 if (!list)
51 {
52 list = new RBTree<uint32_t>::Node<LockFreeList>;
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 }
◆ 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: