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 "list.hpp"
6#include "mutex.hpp"
7#include "rbt.hpp"
8
9namespace LibXR
10{
11
17class Event
18{
19 public:
26 { return static_cast<int>(a) - static_cast<int>(b); })
27 {
28 }
29
36 void Register(uint32_t event, const Callback<uint32_t> &cb)
37 {
38 auto list = rbt_.Search<List>(event);
39
40 if (!list)
41 {
43 rbt_.Insert(*list, event);
44 }
45
47
48 node->data_.event = event;
49 node->data_.cb = cb;
50 list->data_.Add(*node);
51 }
52
58 void Active(uint32_t event)
59 {
60 auto list = rbt_.Search<List>(event);
61 if (!list)
62 {
63 return;
64 }
65
66 auto foreach_fun = [=](Block &block)
67 {
68 block.cb.Run(false, event);
69 return ErrorCode::OK;
70 };
71
73 }
74
83 {
84 auto list = rbt_.Search<List>(event);
85 if (!list)
86 {
87 return;
88 }
89
90 auto foreach_fun = [=](Block &block)
91 {
92 block.cb.Run(in_isr, event);
93 return ErrorCode::OK;
94 };
95
97 }
98
110 {
111 struct BindBlock
112 {
113 Event *target;
114 uint32_t event;
115 };
116
117 auto block = new BindBlock{this, target_event};
118
119 auto bind_fun = [](bool in_isr, BindBlock *block, uint32_t event)
120 {
121 UNUSED(event);
122 block->target->ActiveFromCallback(block->event, in_isr);
123 };
124
126
127 sources.Register(source_event, cb);
128 }
129
130 private:
141
144};
145
146} // namespace LibXR
提供一个通用的回调包装,支持动态参数传递。 Provides a generic callback wrapper, supporting dynamic argument passing.
Definition libxr_cb.hpp:124
static Callback Create(FunType fun, ArgType arg)
创建一个新的回调对象,并绑定回调函数和参数。 Creates a new callback instance, binding a function and an argument.
Definition libxr_cb.hpp:142
事件管理系统,允许基于事件 ID 注册和触发回调函数。 Event management system that allows registration and triggering of callba...
Definition event.hpp:18
void Register(uint32_t event, const Callback< uint32_t > &cb)
为特定事件注册回调函数。 Registers a callback function for a specific event.
Definition event.hpp:36
void ActiveFromCallback(uint32_t event, bool in_isr)
在中断服务程序(ISR)上下文中触发事件回调。 Triggers event callbacks from an interrupt service routine (ISR) context.
Definition event.hpp:82
RBTree< uint32_t > rbt_
Definition event.hpp:142
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 Eve...
Definition event.hpp:109
void Active(uint32_t event)
触发与特定事件关联的所有回调函数。 Triggers all callbacks associated with a specific event.
Definition event.hpp:58
Event()
构造函数,初始化用于存储事件的红黑树。 Constructs an Event object with an empty red-black tree for event storage.
Definition event.hpp:24
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
Definition list.hpp:60
Data data_
存储的数据。 The stored data.
Definition list.hpp:111
链表实现,用于存储和管理数据节点。 A linked list implementation for storing and managing data nodes.
Definition list.hpp:23
ErrorCode Foreach(Func func)
遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
Definition list.hpp:214
红黑树的泛型数据节点,继承自 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 Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值
用于存储事件回调的数据结构。 Data structure for storing event callbacks.
Definition event.hpp:136
uint32_t event
与该回调关联的事件 ID。 Event ID associated with this callback.
Definition event.hpp:137
Callback< uint32_t > cb
关联该事件的回调函数。 Callback function associated with this event.
Definition event.hpp:139