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
93
template
<
typename
T>
94
void
Register
(
const
Entry<T>
& entry)
95
{
96
for
(
const
auto
& alias : entry.
aliases
)
97
{
98
auto
node =
new
(std::align_val_t(LIBXR_CACHE_LINE_SIZE))
99
LibXR::LockFreeList::Node<AliasEntry>
{alias,
static_cast<
void
*
>
(&entry.
object
),
100
TypeID::GetID<T>
()};
101
alias_list_.
Add
(*node);
102
}
103
}
104
105
protected
:
106
struct
AliasEntry
107
{
108
const
char
* name;
109
void
* object;
110
TypeID::ID id;
111
};
112
113
mutable
LibXR::LockFreeList
alias_list_;
114
};
115
120
class
Application
121
{
122
public
:
123
virtual
void
OnMonitor
() = 0;
124
virtual
~Application
() =
default
;
125
};
126
131
class
ApplicationManager
132
{
133
public
:
134
LibXR::LockFreeList
app_list_
;
135
145
void
Register
(
Application
& app)
146
{
147
auto
node =
new
(std::align_val_t(LIBXR_CACHE_LINE_SIZE))
148
LibXR::LockFreeList::Node<Application*>
(&app);
149
app_list_
.
Add
(*node);
150
}
151
156
void
MonitorAll
()
157
{
158
app_list_
.
Foreach
<
Application
*>(
159
[](
Application
* app)
160
{
161
app->OnMonitor();
162
return
ErrorCode::OK;
163
});
164
}
165
172
size_t
Size
() {
return
app_list_
.
Size
(); }
173
};
174
175
}
// namespace LibXR
LibXR::Application
应用模块抽象类,需实现 OnMonitor 方法
Definition
app_framework.hpp:121
LibXR::Application::OnMonitor
virtual void OnMonitor()=0
周期性任务 / Periodic update
LibXR::ApplicationManager
应用模块管理器
Definition
app_framework.hpp:132
LibXR::ApplicationManager::app_list_
LibXR::LockFreeList app_list_
模块链表 / Module list
Definition
app_framework.hpp:134
LibXR::ApplicationManager::MonitorAll
void MonitorAll()
调用所有模块的 OnMonitor
Definition
app_framework.hpp:156
LibXR::ApplicationManager::Register
void Register(Application &app)
注册一个应用模块
Definition
app_framework.hpp:145
LibXR::ApplicationManager::Size
size_t Size()
获取模块数量
Definition
app_framework.hpp:172
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:94
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_flash.hpp:12
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:107
src
middleware
app_framework.hpp
Generated by
1.12.0