libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
event.hpp
1#pragma once
2
3#include "libxr_cb.hpp"
4#include "libxr_def.hpp"
5#include "lockfree_list.hpp"
6#include "rbt.hpp"
7
8namespace LibXR
9{
10
17class Event
18{
19 public:
21
28
34 : rbt_([](const uint32_t &a, const uint32_t &b)
35 { return static_cast<int>(a) - static_cast<int>(b); })
36 {
37 }
38
46 void Register(uint32_t event, const Callback &cb)
47 {
48 auto list = rbt_.Search<LockFreeList>(event);
49
50 if (!list)
51 {
53 rbt_.Insert(*list, event);
54 }
55
57
58 node->data_.event = event;
59 node->data_.cb = cb;
60 list->data_.Add(*node);
61 }
62
69 void Active(uint32_t event)
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 }
85
95 void ActiveFromCallback(CallbackList list, uint32_t event)
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 }
110
119 CallbackList GetList(uint32_t event)
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 }
130
139 void Bind(Event &sources, uint32_t source_event, uint32_t target_event)
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 }
161
162 private:
168 struct Block
169 {
170 uint32_t event;
173 };
174
177};
178
179} // namespace LibXR
static Callback Create(FunType fun, ArgType arg)
Definition libxr_cb.hpp:143
事件管理系统,允许基于事件 ID 注册和触发回调函数。 Event management system that allows registration and triggering of callba...
Definition event.hpp:18
RBTree< uint32_t > rbt_
Definition event.hpp:175
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 ins...
Definition event.hpp:139
void Active(uint32_t event)
触发与特定事件关联的所有回调函数(非中断上下文)。 Triggers all callbacks associated with a specific event (non-interrupt cont...
Definition event.hpp:69
void ActiveFromCallback(CallbackList list, uint32_t event)
从回调函数中触发与特定事件关联的所有回调函数。 Triggers all callbacks associated with a specific event (interrupt context).
Definition event.hpp:95
void Register(uint32_t event, const Callback &cb)
为特定事件注册回调函数。 Registers a callback function for a specific event.
Definition event.hpp:46
Event()
构造函数,初始化用于存储事件的红黑树。 Constructs an Event object with an empty red-black tree for event storage.
Definition event.hpp:33
CallbackList GetList(uint32_t event)
获取指定事件的回调链表指针(必须在非中断上下文中调用)。 Returns the callback list pointer for the given event (must be called ou...
Definition event.hpp:119
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
Data data_
存储的数据。 The stored data.
链表实现,用于存储和管理数据节点。 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.
红黑树的泛型数据节点,继承自 BaseNode (Generic data node for Red-Black Tree, inheriting from BaseNode).
Definition rbt.hpp:64
红黑树实现,支持泛型键和值,并提供线程安全操作 (Red-Black Tree implementation supporting generic keys and values with thread...
Definition rbt.hpp:24
Node< Data > * Search(const Key &key)
搜索红黑树中的节点 (Search for a node in the Red-Black Tree).
Definition rbt.hpp:122
void Insert(BaseNode &node, KeyType &&key)
在树中插入新节点 (Insert a new node into the tree).
Definition rbt.hpp:237
LibXR 命名空间
Definition ch32_gpio.hpp:9
用于存储事件回调的数据结构。 Data structure for storing event callbacks.
Definition event.hpp:169
Callback cb
关联该事件的回调函数。 Callback function associated with this event.
Definition event.hpp:172
uint32_t event
与该回调关联的事件 ID。 Event ID associated with this callback.
Definition event.hpp:170