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

链表实现,用于存储和管理数据节点。 A linked list implementation for storing and managing data nodes. More...

#include <list.hpp>

Collaboration diagram for LibXR::List:

Data Structures

class  BaseNode
 链表基础节点,所有节点都继承自该类。 Base node for the linked list, serving as a parent for all nodes. More...
 
class  Node
 数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data types. More...
 

Public Member Functions

 List () noexcept
 默认构造函数,初始化链表头节点。 Default constructor initializing the linked list head node.
 
 ~List ()
 析构函数,释放所有节点。 Destructor releasing all nodes.
 
void Add (BaseNode &data)
 向链表添加一个节点。 Adds a node to the linked list.
 
uint32_t Size () noexcept
 获取链表中的节点数量。 Gets the number of nodes in the linked list.
 
ErrorCode Delete (BaseNode &data) noexcept
 从链表中删除指定的节点。 Deletes a specified node from the linked list.
 
template<typename Data , typename Func , SizeLimitMode LimitMode = SizeLimitMode::MORE>
ErrorCode Foreach (Func func)
 遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
 
template<typename Data , typename Func , SizeLimitMode LimitMode = SizeLimitMode::MORE>
ErrorCode ForeachFromCallback (Func func, bool in_isr)
 在回调环境中遍历链表节点,并应用回调函数。 Iterates over each node in a callback environment and applies a function.
 

Private Attributes

BaseNode head_
 链表头节点。 The head node of the list.
 
LibXR::Mutex mutex_
 线程安全的互斥锁。 Thread-safe mutex.
 

Detailed Description

链表实现,用于存储和管理数据节点。 A linked list implementation for storing and managing data nodes.

该类提供了基本的链表操作,包括添加、删除节点,以及遍历链表的功能, 具有线程安全的特性。 This class provides fundamental linked list operations, including adding, deleting nodes, and traversing the list, with thread-safety features.

Definition at line 22 of file list.hpp.

Constructor & Destructor Documentation

◆ List()

LibXR::List::List ( )
inlinenoexcept

默认构造函数,初始化链表头节点。 Default constructor initializing the linked list head node.

Definition at line 118 of file list.hpp.

118: head_(0) { head_.next_ = &head_; }
BaseNode * next_
指向下一个节点的指针。 Pointer to the next node.
Definition list.hpp:47
BaseNode head_
链表头节点。 The head node of the list.
Definition list.hpp:269

◆ ~List()

LibXR::List::~List ( )
inline

析构函数,释放所有节点。 Destructor releasing all nodes.

Definition at line 124 of file list.hpp.

125 {
126 for (auto pos = head_.next_; pos != &head_;)
127 {
128 auto tmp = pos->next_;
129 pos->next_ = nullptr;
130 pos = tmp;
131 }
132
133 head_.next_ = nullptr;
134 }
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

Member Function Documentation

◆ Add()

void LibXR::List::Add ( BaseNode data)
inline

向链表添加一个节点。 Adds a node to the linked list.

Parameters
data要添加的 BaseNode 节点。 The BaseNode node to be added.

Definition at line 143 of file list.hpp.

144 {
145 mutex_.Lock();
146 data.next_ = head_.next_;
147 head_.next_ = &data;
148 mutex_.Unlock();
149 }
LibXR::Mutex mutex_
线程安全的互斥锁。 Thread-safe mutex.
Definition list.hpp:270
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
Definition mutex.cpp:13
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:27

◆ Delete()

ErrorCode LibXR::List::Delete ( BaseNode data)
inlinenoexcept

从链表中删除指定的节点。 Deletes a specified node from the linked list.

Parameters
data要删除的 BaseNode 节点。 The BaseNode node to be deleted.
Returns
返回 ErrorCode,指示操作是否成功。 Returns ErrorCode, indicating whether the operation was successful.

Definition at line 181 of file list.hpp.

182 {
183 mutex_.Lock();
184 for (auto pos = &head_; pos->next_ != &head_; pos = pos->next_)
185 {
186 if (pos->next_ == &data)
187 {
188 pos->next_ = data.next_;
189 data.next_ = nullptr;
190 mutex_.Unlock();
191 return ErrorCode::OK;
192 }
193 }
194 mutex_.Unlock();
195 return ErrorCode::NOT_FOUND;
196 }

◆ Foreach()

template<typename Data , typename Func , SizeLimitMode LimitMode = SizeLimitMode::MORE>
ErrorCode LibXR::List::Foreach ( Func  func)
inline

遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.

Template Parameters
Data存储的数据类型。 The type of stored data.
Func回调函数类型。 The callback function type.
LimitMode大小限制模式,默认为 MORE。 Size limit mode, default is MORE.
Parameters
func需要应用于每个节点数据的回调函数。 The callback function to be applied to each node's data.
Returns
返回 ErrorCode,指示操作是否成功。 Returns ErrorCode, indicating whether the operation was successful.

Definition at line 214 of file list.hpp.

215 {
216 mutex_.Lock();
217 for (auto pos = head_.next_; pos != &head_; pos = pos->next_)
218 {
219 Assert::SizeLimitCheck<LimitMode>(sizeof(Data), pos->size_);
220 if (auto res = func(static_cast<Node<Data>*>(pos)->data_); res != ErrorCode::OK)
221 {
222 mutex_.Unlock();
223 return res;
224 }
225 }
226 mutex_.Unlock();
227 return ErrorCode::OK;
228 }

◆ ForeachFromCallback()

template<typename Data , typename Func , SizeLimitMode LimitMode = SizeLimitMode::MORE>
ErrorCode LibXR::List::ForeachFromCallback ( Func  func,
bool  in_isr 
)
inline

在回调环境中遍历链表节点,并应用回调函数。 Iterates over each node in a callback environment and applies a function.

Template Parameters
Data存储的数据类型。 The type of stored data.
Func回调函数类型。 The callback function type.
LimitMode大小限制模式,默认为 MORE。 Size limit mode, default is MORE.
Parameters
func需要应用于每个节点数据的回调函数。 The callback function to be applied to each node's data.
in_isr指示当前是否在中断服务例程(ISR)中。 Indicates whether the function is called in an ISR.
Returns
返回 ErrorCode,指示操作是否成功。 Returns ErrorCode, indicating whether the operation was successful.

Definition at line 248 of file list.hpp.

249 {
250 if (mutex_.TryLockInCallback(in_isr) != ErrorCode::OK)
251 {
252 return ErrorCode::BUSY;
253 }
254
255 for (auto pos = head_.next_; pos != &head_; pos = pos->next_)
256 {
257 Assert::SizeLimitCheck<LimitMode>(sizeof(Data), pos->size_);
258 if (auto res = func(static_cast<Node<Data>*>(pos)->data_); res != ErrorCode::OK)
259 {
261 return res;
262 }
263 }
265 return ErrorCode::OK;
266 }
ErrorCode TryLockInCallback(bool in_isr)
在回调(ISR)中尝试加锁 (Attempt to lock the mutex inside an interrupt service routine (ISR)).
Definition mutex.cpp:29
void UnlockFromCallback(bool in_isr)
在回调(ISR)中解锁 (Unlock the mutex inside an interrupt service routine (ISR)).
Definition mutex.cpp:49

◆ Size()

uint32_t LibXR::List::Size ( )
inlinenoexcept

获取链表中的节点数量。 Gets the number of nodes in the linked list.

Returns
返回链表中节点的数量。 Returns the number of nodes in the list.

Definition at line 158 of file list.hpp.

159 {
160 uint32_t size = 0;
161 mutex_.Lock();
162
163 for (auto pos = head_.next_; pos != &head_; pos = pos->next_)
164 {
165 ++size;
166 }
167
168 mutex_.Unlock();
169 return size;
170 }

Field Documentation

◆ head_

BaseNode LibXR::List::head_
private

链表头节点。 The head node of the list.

Definition at line 269 of file list.hpp.

◆ mutex_

LibXR::Mutex LibXR::List::mutex_
private

线程安全的互斥锁。 Thread-safe mutex.

Definition at line 270 of file list.hpp.


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