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#include <tuple>
6
7#include "libxr_def.hpp"
8#include "libxr_type.hpp"
9#include "list.hpp"
10
11namespace LibXR
12{
17template <typename T>
18struct Entry
19{
20 T& object;
21 std::initializer_list<const char*> aliases;
22};
23
28{
29 public:
34 template <typename... Entries>
36 {
37 (RegisterAliases(entries), ...);
38 }
39
43 template <typename T>
44 T* Find(const char* alias) const
45 {
46 T* result = nullptr;
47 alias_list_.Foreach<AliasEntry>(
48 [&](AliasEntry& entry)
49 {
50 if (std::strcmp(entry.name, alias) == 0)
51 {
52 result = static_cast<T*>(entry.object);
53 return ErrorCode::FAILED;
54 }
55 return ErrorCode::OK;
56 });
57 return result;
58 }
59
63 template <typename T>
64 T* Find(std::initializer_list<const char*> aliases) const
65 {
66 for (const auto& alias : aliases)
67 {
68 if (T* obj = Find<T>(alias))
69 {
70 return obj;
71 }
72 }
73 return nullptr;
74 }
75
79 template <typename T>
80 T* FindOrExit(std::initializer_list<const char*> aliases) const
81 {
82 T* result = Find<T>(aliases);
83 ASSERT(result != nullptr);
84 return result;
85 }
86
94 template <typename T>
95 void Register(const Entry<T>& entry)
96 {
97 RegisterAliases(entry);
98 }
99
100 private:
102 {
103 const char* name;
104 void* object;
105 TypeID::ID id;
106 };
107
108 mutable LibXR::List alias_list_;
109
110 template <typename T>
111 void RegisterAliases(const Entry<T>& entry)
112 {
113 for (const auto& alias : entry.aliases)
114 {
116 alias, static_cast<void*>(&entry.object), TypeID::GetID<T>()};
117 alias_list_.Add(*node);
118 }
119 }
120};
121
127{
128 public:
129 virtual void OnMonitor() = 0;
130 virtual ~Application() = default;
131};
132
138{
139 public:
141
149 {
152 }
153
159 {
161 [](Application* app)
162 {
163 app->OnMonitor();
164 return ErrorCode::OK;
165 });
166 }
167
174 size_t Size() { return app_list_.Size(); }
175};
176
177} // namespace LibXR
应用模块抽象类,需实现 OnMonitor 方法
virtual void OnMonitor()=0
周期性任务 / Periodic update
应用模块管理器
void MonitorAll()
调用所有模块的 OnMonitor
void Register(Application &app)
注册一个应用模块
size_t Size()
获取模块数量
LibXR::List app_list_
模块链表 / Module list
T * FindOrExit(std::initializer_list< const char * > aliases) const
T * Find(std::initializer_list< const char * > aliases) const
void Register(const Entry< T > &entry)
手动注册一个设备条目(及其别名)
constexpr HardwareContainer(Entries &&... entries)
T * Find(const char *alias) const
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
Definition list.hpp:60
链表实现,用于存储和管理数据节点。 A linked list implementation for storing and managing data nodes.
Definition list.hpp:23
uint32_t Size() noexcept
获取链表中的节点数量。 Gets the number of nodes in the linked list.
Definition list.hpp:158
void Add(BaseNode &data)
向链表添加一个节点。 Adds a node to the linked list.
Definition list.hpp:143
ErrorCode Foreach(Func func)
遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
Definition list.hpp:214
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值