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

#include <hardware.hpp>

Collaboration diagram for LibXR::HardwareContainer:
[legend]

Data Structures

struct  AliasEntry
 一条硬件别名记录 One registered hardware-alias record More...
 

Public Member Functions

template<typename... Entries>
constexpr HardwareContainer (Entries &&... entries)
 
template<typename T >
T * Find (const char *alias) const
 
template<typename T >
T * Find (const std::initializer_list< const char * > ALIASES) const
 
template<typename T >
T * FindOrExit (std::initializer_list< const char * > aliases) const
 
template<typename T >
void Register (const Entry< T > &entry)
 注册一个硬件条目
 

Protected Attributes

LibXR::LockFreeList alias_list_
 当前已注册的全部别名链表 / List of all currently registered aliases.
 

Detailed Description

硬件容器类 / Hardware container

Each registered alias is stored together with the object's erased pointer and its TypeID, so name lookup and type filtering happen in one pass. 每个注册别名都会连同对象的擦除指针和对应 TypeID 一起存储,因此名称匹配和类型过滤 会在一次遍历里同时完成。

Definition at line 44 of file hardware.hpp.

Constructor & Destructor Documentation

◆ HardwareContainer()

template<typename... Entries>
LibXR::HardwareContainer::HardwareContainer ( Entries &&... entries)
inlineconstexpr

构造并注册所有别名 / Construct and register aliases

Parameters
entries硬件条目列表 / Hardware entry list

Definition at line 52 of file hardware.hpp.

53 {
54 (Register(std::forward<Entries>(entries)), ...);
55 }
void Register(const Entry< T > &entry)
注册一个硬件条目
Definition hardware.hpp:123

Member Function Documentation

◆ Find() [1/2]

template<typename T >
T * LibXR::HardwareContainer::Find ( const char * alias) const
inline

查找设备(单个别名) / Find device by alias

Note
alias 必须非空。 alias must be non-null.

Definition at line 63 of file hardware.hpp.

64 {
65 ASSERT(alias != nullptr);
66 T* result = nullptr;
67 const auto wanted_id = TypeID::GetID<T>();
68 alias_list_.Foreach<AliasEntry>(
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 }
LibXR::LockFreeList alias_list_
当前已注册的全部别名链表 / List of all currently registered aliases.
Definition hardware.hpp:147
ErrorCode Foreach(Func func)
遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
static ID GetID()
获取类型的唯一标识符 / Get a unique identifier for type T
@ FAILED
操作失败 | Operation failed
@ OK
操作成功 | Operation successful

◆ Find() [2/2]

template<typename T >
T * LibXR::HardwareContainer::Find ( const std::initializer_list< const char * > ALIASES) const
inline

查找设备(多个别名) / Find device with fallback aliases

Note
ALIASES 中的每个别名都必须非空。 Every alias in ALIASES must be non-null.

Definition at line 87 of file hardware.hpp.

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 }
T * Find(const char *alias) const
Definition hardware.hpp:63

◆ FindOrExit()

template<typename T >
T * LibXR::HardwareContainer::FindOrExit ( std::initializer_list< const char * > aliases) const
inline

查找或强约束失败 / Find or REQUIRE if not found

Note
aliases 中的每个别名都必须非空。 Every alias in aliases must be non-null.

Definition at line 106 of file hardware.hpp.

107 {
108 T* result = Find<T>(aliases);
109 REQUIRE(result != nullptr);
110 return result;
111 }

◆ Register()

template<typename T >
void LibXR::HardwareContainer::Register ( const Entry< T > & entry)
inline

注册一个硬件条目

Register a hardware entry

Note
包含动态内存分配。 Contains dynamic memory allocation.
entry.aliases 中的每个别名都必须非空。 Every alias in entry.aliases must be non-null.

Definition at line 123 of file hardware.hpp.

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 }
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
void Add(BaseNode &data)
向链表添加一个节点。 Adds a node to the linked list.
constexpr size_t CACHE_LINE_SIZE
缓存行大小 / Cache line size
Definition libxr_def.hpp:56

Field Documentation

◆ alias_list_

LibXR::LockFreeList LibXR::HardwareContainer::alias_list_
mutableprotected

当前已注册的全部别名链表 / List of all currently registered aliases.

Definition at line 147 of file hardware.hpp.


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