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
8 : rbt_([](const uint32_t &a, const uint32_t &b)
9 { return static_cast<int>(a) - static_cast<int>(b); })
10{
11}
12
13void Event::Register(uint32_t event, const Callback &cb)
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}
29
30void Event::Active(uint32_t event)
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}
46
47void Event::ActiveFromCallback(CallbackList list, uint32_t event)
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}
62
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}
74
75void Event::Bind(Event &sources, uint32_t source_event, uint32_t target_event)
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}
提供一个通用的回调包装,支持动态参数传递。 Provides a generic callback wrapper, supporting dynamic argument passing.
Definition libxr_cb.hpp:124
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
RBTree< uint32_t > rbt_
Definition event.hpp:96
Event()
构造函数,初始化用于存储事件的红黑树。 Constructs an Event object with an empty red-black tree for event storage.
Definition event.cpp:7
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:75
void Active(uint32_t event)
触发与特定事件关联的所有回调函数(非中断上下文)。 Triggers all callbacks associated with a specific event (non-interrupt cont...
Definition event.cpp:30
void Register(uint32_t event, const Callback &cb)
为特定事件注册回调函数。 Registers a callback function for a specific event.
Definition event.cpp:13
void ActiveFromCallback(CallbackList list, uint32_t event)
从回调函数中触发与特定事件关联的所有回调函数。 Triggers all callbacks associated with a specific event (interrupt context).
Definition event.cpp:47
CallbackList GetList(uint32_t event)
获取指定事件的回调链表指针(必须在非中断上下文中调用)。 Returns the callback list pointer for the given event (must be called ou...
Definition event.cpp:63
数据节点模板,继承自 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
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:90