libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
event.cpp
1#include "event.hpp"
2
3using namespace LibXR;
4
5template class LibXR::Callback<uint32_t>;
6
7namespace
8{
9
10int CompareEventId(uint32_t a, uint32_t b)
11{
12 if (a < b)
13 {
14 return -1;
15 }
16 if (a > b)
17 {
18 return 1;
19 }
20 return 0;
21}
22
23} // namespace
24
26 : rbt_([](const uint32_t& a, const uint32_t& b) { return CompareEventId(a, b); })
27{
28}
29
30void Event::Register(uint32_t event, const Callback& cb)
31{
32 auto list = rbt_.Search<LockFreeList>(event);
33
34 if (!list)
35 {
37 rbt_.Insert(*list, event);
38 }
39
41
42 node->data_.event = event;
43 node->data_.cb = cb;
44 list->data_.Add(*node);
45}
46
47void Event::Active(uint32_t event)
48{
49 auto list = rbt_.Search<LockFreeList>(event);
50 if (!list)
51 {
52 return;
53 }
54
55 auto foreach_fun = [=](Block& block)
56 {
57 block.cb.Run(false, event);
58 return ErrorCode::OK;
59 };
60
61 list->data_.Foreach<LibXR::Event::Block>(foreach_fun);
62}
63
64void Event::ActiveFromCallback(CallbackList list, uint32_t event, bool in_isr)
65{
66 if (!list)
67 {
68 return;
69 }
70
71 auto foreach_fun = [=](Block& block)
72 {
73 block.cb.Run(in_isr, event);
74 return ErrorCode::OK;
75 };
76
77 list->Foreach<Block>(foreach_fun);
78}
79
81{
82 auto node = rbt_.Search<LockFreeList>(event);
83 if (!node)
84 {
85 auto list = new RBTree<uint32_t>::Node<LockFreeList>;
86 rbt_.Insert(*list, event);
87 node = list;
88 }
89 return &node->data_;
90}
91
92void Event::Bind(Event& sources, uint32_t source_event, uint32_t target_event)
93{
94 struct BindBlock
95 {
96 Event* target;
98 uint32_t event;
99 };
100
101 auto block = new BindBlock{this, GetList(target_event), target_event};
102
103 auto bind_fun = [](bool in_isr, BindBlock* block, uint32_t event)
104 {
105 UNUSED(event);
106 block->target->ActiveFromCallback(block->list, block->event, in_isr);
107 };
108
109 auto cb = Callback::Create(bind_fun, block);
110
111 sources.Register(source_event, cb);
112}
通用回调包装,支持动态参数传递 / Generic callback wrapper supporting dynamic argument passing
Definition libxr_cb.hpp:144
static Callback Create(CallableType fun, BoundArgType arg)
Definition libxr_cb.hpp:167
事件管理系统,允许基于事件 ID 注册和触发回调函数。 Event management system that allows registration and triggering of callba...
Definition event.hpp:18
void ActiveFromCallback(CallbackList list, uint32_t event, bool in_isr=true)
从 callback-safe 路径触发与特定事件关联的所有回调函数。 Triggers all callbacks associated with a specific event from a ca...
Definition event.cpp:64
RBTree< uint32_t > rbt_
Definition event.hpp:126
Event()
构造函数,初始化用于存储事件的红黑树。 Constructs an Event object with an empty red-black tree for event storage.
Definition event.cpp:25
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.cpp:92
void Active(uint32_t event)
触发与特定事件关联的所有回调函数(非中断上下文)。 Triggers all callbacks associated with a specific event (non-interrupt cont...
Definition event.cpp:47
void Register(uint32_t event, const Callback &cb)
为特定事件注册回调函数。 Registers a callback function for a specific event.
Definition event.cpp:30
CallbackList GetList(uint32_t event)
获取指定事件的回调链表指针(必须在非中断上下文中调用)。 Returns the callback list pointer for the given event (must be called ou...
Definition event.cpp:80
数据节点模板,继承自 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:63
Node< Data > * Search(const Key &key)
搜索红黑树中的节点 (Search for a node in the Red-Black Tree).
Definition rbt.hpp:120
void Insert(BaseNode &node, KeyType &&key)
在树中插入新节点 (Insert a new node into the tree).
Definition rbt.hpp:235
LibXR 命名空间
Definition ch32_can.hpp:14
@ OK
操作成功 | Operation successful
用于存储事件回调的数据结构。 Data structure for storing event callbacks.
Definition event.hpp:120