libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
hardware.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
10namespace LibXR
11{
28template <typename T>
29struct Entry
30{
31 T& object;
32 std::initializer_list<const char*>
34};
35
45{
46 public:
51 template <typename... Entries>
52 constexpr HardwareContainer(Entries&&... entries)
53 {
54 (Register(std::forward<Entries>(entries)), ...);
55 }
56
62 template <typename T>
63 T* Find(const char* alias) const
64 {
65 ASSERT(alias != nullptr);
66 T* result = nullptr;
67 const auto wanted_id = TypeID::GetID<T>();
69 [&](AliasEntry& entry)
70 {
71 if (std::strcmp(entry.name, alias) == 0 && entry.id == wanted_id)
72 {
73 result = static_cast<T*>(entry.object);
74 return ErrorCode::FAILED;
75 }
76 return ErrorCode::OK;
77 });
78 return result;
79 }
80
86 template <typename T>
87 T* Find(const std::initializer_list<const char*> ALIASES) const
88 {
89 for (const auto& alias : ALIASES)
90 {
91 ASSERT(alias != nullptr);
92 if (T* obj = Find<T>(alias))
93 {
94 return obj;
95 }
96 }
97 return nullptr;
98 }
99
105 template <typename T>
106 T* FindOrExit(std::initializer_list<const char*> aliases) const
107 {
108 T* result = Find<T>(aliases);
109 REQUIRE(result != nullptr);
110 return result;
111 }
112
122 template <typename T>
123 void Register(const Entry<T>& entry)
124 {
125 for (const auto& alias : entry.aliases)
126 {
127 ASSERT(alias != nullptr);
128 auto node = new (std::align_val_t(LibXR::CACHE_LINE_SIZE))
129 LibXR::LockFreeList::Node<AliasEntry>{alias, static_cast<void*>(&entry.object),
131 alias_list_.Add(*node);
132 }
133 }
134
135 protected:
141 {
142 const char* name;
143 void* object;
144 TypeID::ID id;
145 };
146
148};
149
150} // namespace LibXR
T * FindOrExit(std::initializer_list< const char * > aliases) const
Definition hardware.hpp:106
T * Find(const std::initializer_list< const char * > ALIASES) const
Definition hardware.hpp:87
void Register(const Entry< T > &entry)
注册一个硬件条目
Definition hardware.hpp:123
LibXR::LockFreeList alias_list_
当前已注册的全部别名链表 / List of all currently registered aliases.
Definition hardware.hpp:147
constexpr HardwareContainer(Entries &&... entries)
Definition hardware.hpp:52
T * Find(const char *alias) const
Definition hardware.hpp:63
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
链表实现,用于存储和管理数据节点。 A linked list implementation for storing and managing data nodes.
ErrorCode Foreach(Func func)
遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
void Add(BaseNode &data)
向链表添加一个节点。 Adds a node to the linked list.
static ID GetID()
获取类型的唯一标识符 / Get a unique identifier for type T
LibXR 命名空间
Definition ch32_can.hpp:14
@ FAILED
操作失败 | Operation failed
@ OK
操作成功 | Operation successful
constexpr size_t CACHE_LINE_SIZE
缓存行大小 / Cache line size
Definition libxr_def.hpp:56
app_framework 的硬件注册片段 Hardware-registration fragment of app_framework
Definition hardware.hpp:30
T & object
被注册的设备对象引用 / Reference to the device object being registered.
Definition hardware.hpp:31
std::initializer_list< const char * > aliases
指向同一设备的别名集合 / Alias set pointing to the same device.
Definition hardware.hpp:33
一条硬件别名记录 One registered hardware-alias record
Definition hardware.hpp:141
void * object
擦除类型后的设备对象指针 / Type-erased device object pointer.
Definition hardware.hpp:143
const char * name
硬件别名 / Hardware alias string.
Definition hardware.hpp:142
TypeID::ID id
设备对象类型 ID / Device object type ID.
Definition hardware.hpp:144