libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::Event Class Reference

事件管理系统,允许基于事件 ID 注册和触发回调函数。 Event management system that allows registration and triggering of callbacks based on event IDs. More...

#include <event.hpp>

Collaboration diagram for LibXR::Event:

Data Structures

struct  Block
 用于存储事件回调的数据结构。 Data structure for storing event callbacks. More...
 

Public Member Functions

 Event ()
 构造函数,初始化用于存储事件的红黑树。 Constructs an Event object with an empty red-black tree for event storage.
 
void Register (uint32_t event, const Callback< uint32_t > &cb)
 为特定事件注册回调函数。 Registers a callback function for a specific event.
 
void Active (uint32_t event)
 触发与特定事件关联的所有回调函数。 Triggers all callbacks associated with a specific event.
 
void ActiveFromCallback (uint32_t event, bool in_isr)
 在中断服务程序(ISR)上下文中触发事件回调。 Triggers event callbacks from an interrupt service routine (ISR) context.
 
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 Event instance.
 

Private Attributes

RBTree< uint32_trbt_
 

Detailed Description

事件管理系统,允许基于事件 ID 注册和触发回调函数。 Event management system that allows registration and triggering of callbacks based on event IDs.

Definition at line 17 of file event.hpp.

Constructor & Destructor Documentation

◆ Event()

LibXR::Event::Event ( )
inline

构造函数,初始化用于存储事件的红黑树。 Constructs an Event object with an empty red-black tree for event storage.

Definition at line 24 of file event.hpp.

25 : rbt_([](const uint32_t &a, const uint32_t &b)
26 { return static_cast<int>(a) - static_cast<int>(b); })
27 {
28 }
RBTree< uint32_t > rbt_
Definition event.hpp:142
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

Member Function Documentation

◆ Active()

void LibXR::Event::Active ( uint32_t  event)
inline

触发与特定事件关联的所有回调函数。 Triggers all callbacks associated with a specific event.

Parameters
event要激活的事件 ID。 The event ID to activate.

Definition at line 58 of file event.hpp.

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
72 list->data_.Foreach<LibXR::Event::Block>(foreach_fun);
73 }
Node< Data > * Search(const Key &key)
搜索红黑树中的节点 (Search for a node in the Red-Black Tree).
Definition rbt.hpp:122
用于存储事件回调的数据结构。 Data structure for storing event callbacks.
Definition event.hpp:136

◆ ActiveFromCallback()

void LibXR::Event::ActiveFromCallback ( uint32_t  event,
bool  in_isr 
)
inline

在中断服务程序(ISR)上下文中触发事件回调。 Triggers event callbacks from an interrupt service routine (ISR) context.

Parameters
event要激活的事件 ID。 The event ID to activate.
in_isr是否从 ISR 调用该函数。 Whether the function is being called from an ISR.

Definition at line 82 of file event.hpp.

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
96 list->data_.Foreach<Block>(foreach_fun);
97 }

◆ 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 Event instance.

Parameters
sources包含原始事件的源 Event 实例。 The source Event instance containing the original event.
source_event源事件实例中的事件 ID。 The event ID in the source Event instance.
target_event当前事件实例中的目标事件 ID。 The corresponding event ID in the current Event instance.

Definition at line 109 of file event.hpp.

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 }
static Callback Create(FunType fun, ArgType arg)
创建一个新的回调对象,并绑定回调函数和参数。 Creates a new callback instance, binding a function and an argument.
Definition libxr_cb.hpp:142

◆ Register()

void LibXR::Event::Register ( uint32_t  event,
const Callback< uint32_t > &  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 36 of file event.hpp.

37 {
38 auto list = rbt_.Search<List>(event);
39
40 if (!list)
41 {
42 list = new RBTree<uint32_t>::Node<List>;
43 rbt_.Insert(*list, event);
44 }
45
46 List::Node<Block> *node = new List::Node<Block>;
47
48 node->data_.event = event;
49 node->data_.cb = cb;
50 list->data_.Add(*node);
51 }
void Insert(BaseNode &node, KeyType &&key)
在树中插入新节点 (Insert a new node into the tree).
Definition rbt.hpp:237

Field Documentation

◆ rbt_

RBTree<uint32_t> LibXR::Event::rbt_
private

用于管理已注册事件的红黑树。 Red-black tree for managing registered events.

Definition at line 142 of file event.hpp.


The documentation for this class was generated from the following file: