libxr
1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
app_framework.hpp
1
#pragma once
2
3
#include <cstring>
4
#include <initializer_list>
5
6
#include "libxr_def.hpp"
7
#include "libxr_type.hpp"
8
#include "lockfree_list.hpp"
9
10
namespace
LibXR
11
{
16
template
<
typename
T>
17
struct
Entry
18
{
19
T&
object
;
20
std::initializer_list<const char*>
aliases
;
21
};
22
26
class
HardwareContainer
27
{
28
public
:
33
template
<
typename
... Entries>
34
constexpr
HardwareContainer
(Entries&&... entries)
35
{
36
(
Register
(std::forward<Entries>(entries)), ...);
37
}
38
42
template
<
typename
T>
43
T*
Find
(
const
char
* alias)
const
44
{
45
T* result =
nullptr
;
46
alias_list_.
Foreach
<
AliasEntry
>(
47
[&](
AliasEntry
& entry)
48
{
49
if
(std::strcmp(entry.name, alias) == 0)
50
{
51
result =
static_cast<
T*
>
(entry.object);
52
return
ErrorCode::FAILED;
53
}
54
return
ErrorCode::OK;
55
});
56
return
result;
57
}
58
62
template
<
typename
T>
63
T*
Find
(
const
std::initializer_list<const char*> ALIASES)
const
64
{
65
for
(
const
auto
& alias : ALIASES)
66
{
67
if
(T* obj =
Find<T>
(alias))
68
{
69
return
obj;
70
}
71
}
72
return
nullptr
;
73
}
74
78
template
<
typename
T>
79
T*
FindOrExit
(std::initializer_list<const char*> aliases)
const
80
{
81
T* result =
Find<T>
(aliases);
82
ASSERT(result !=
nullptr
);
83
return
result;
84
}
85
91
template
<
typename
T>
92
void
Register
(
const
Entry<T>
& entry)
93
{
94
for
(
const
auto
& alias : entry.
aliases
)
95
{
96
auto
node =
new
(std::align_val_t(LIBXR_CACHE_LINE_SIZE))
97
LibXR::LockFreeList::Node<AliasEntry>
{alias,
static_cast<
void
*
>
(&entry.
object
),
98
TypeID::GetID<T>
()};
99
alias_list_.
Add
(*node);
100
}
101
}
102
103
protected
:
104
struct
AliasEntry
105
{
106
const
char
* name;
107
void
* object;
108
TypeID::ID id;
109
};
110
111
mutable
LibXR::LockFreeList
alias_list_;
112
};
113
118
class
Application
119
{
120
public
:
121
virtual
void
OnMonitor
() = 0;
122
virtual
~Application
() =
default
;
123
};
124
129
class
ApplicationManager
130
{
131
public
:
132
LibXR::LockFreeList
app_list_
;
133
140
void
Register
(
Application
& app)
141
{
142
auto
node =
new
(std::align_val_t(LIBXR_CACHE_LINE_SIZE))
143
LibXR::LockFreeList::Node<Application*>
(&app);
144
app_list_
.
Add
(*node);
145
}
146
151
void
MonitorAll
()
152
{
153
app_list_
.
Foreach
<
Application
*>(
154
[](
Application
* app)
155
{
156
app->OnMonitor();
157
return
ErrorCode::OK;
158
});
159
}
160
167
size_t
Size
() {
return
app_list_
.
Size
(); }
168
};
169
170
}
// namespace LibXR
LibXR::Application
应用模块抽象类,需实现 OnMonitor 方法
Definition
app_framework.hpp:119
LibXR::Application::OnMonitor
virtual void OnMonitor()=0
周期性任务 / Periodic update
LibXR::ApplicationManager
应用模块管理器
Definition
app_framework.hpp:130
LibXR::ApplicationManager::app_list_
LibXR::LockFreeList app_list_
模块链表 / Module list
Definition
app_framework.hpp:132
LibXR::ApplicationManager::MonitorAll
void MonitorAll()
调用所有模块的 OnMonitor
Definition
app_framework.hpp:151
LibXR::ApplicationManager::Register
void Register(Application &app)
注册一个应用模块
Definition
app_framework.hpp:140
LibXR::ApplicationManager::Size
size_t Size()
获取模块数量
Definition
app_framework.hpp:167
LibXR::HardwareContainer
Definition
app_framework.hpp:27
LibXR::HardwareContainer::FindOrExit
T * FindOrExit(std::initializer_list< const char * > aliases) const
Definition
app_framework.hpp:79
LibXR::HardwareContainer::Find
T * Find(const std::initializer_list< const char * > ALIASES) const
Definition
app_framework.hpp:63
LibXR::HardwareContainer::Register
void Register(const Entry< T > &entry)
注册一个硬件条目
Definition
app_framework.hpp:92
LibXR::HardwareContainer::HardwareContainer
constexpr HardwareContainer(Entries &&... entries)
Definition
app_framework.hpp:34
LibXR::HardwareContainer::Find
T * Find(const char *alias) const
Definition
app_framework.hpp:43
LibXR::LockFreeList::Node
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
Definition
lockfree_list.hpp:61
LibXR::LockFreeList
链表实现,用于存储和管理数据节点。 A linked list implementation for storing and managing data nodes.
Definition
lockfree_list.hpp:23
LibXR::LockFreeList::Foreach
ErrorCode Foreach(Func func)
遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
Definition
lockfree_list.hpp:161
LibXR::LockFreeList::Add
void Add(BaseNode &data)
向链表添加一个节点。 Adds a node to the linked list.
Definition
lockfree_list.cpp:26
LibXR::LockFreeList::Size
uint32_t Size() noexcept
获取链表中的节点数量。 Gets the number of nodes in the linked list.
Definition
lockfree_list.cpp:37
LibXR::TypeID::GetID
static ID GetID()
获取类型的唯一标识符
Definition
libxr_type.hpp:246
LibXR
LibXR 命名空间
Definition
ch32_gpio.hpp:9
LibXR::Entry
Definition
app_framework.hpp:18
LibXR::Entry::object
T & object
设备对象 / Device object
Definition
app_framework.hpp:19
LibXR::Entry::aliases
std::initializer_list< const char * > aliases
别名列表 / Alias list
Definition
app_framework.hpp:20
LibXR::HardwareContainer::AliasEntry
Definition
app_framework.hpp:105
src
middleware
app_framework.hpp
Generated by
1.12.0