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:
[legend]

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.
 

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 119 of file list.hpp.

119: 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:232

◆ ~List()

LibXR::List::~List ( )
inline

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

Definition at line 125 of file list.hpp.

126 {
127 for (auto pos = head_.next_; pos != &head_;)
128 {
129 auto tmp = pos->next_;
130 pos->next_ = nullptr;
131 pos = tmp;
132 }
133
134 head_.next_ = nullptr;
135 }

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 144 of file list.hpp.

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

◆ 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 182 of file list.hpp.

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

◆ 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 215 of file list.hpp.

216 {
217 mutex_.Lock();
218 for (auto pos = head_.next_; pos != &head_; pos = pos->next_)
219 {
220 Assert::SizeLimitCheck<LimitMode>(sizeof(Data), pos->size_);
221 if (auto res = func(static_cast<Node<Data>*>(pos)->data_); res != ErrorCode::OK)
222 {
223 mutex_.Unlock();
224 return res;
225 }
226 }
227 mutex_.Unlock();
228 return ErrorCode::OK;
229 }
static void SizeLimitCheck(size_t limit, size_t size)
在非调试模式下的占位大小检查函数(无实际作用)。 Dummy size limit check for non-debug builds.

◆ 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 159 of file list.hpp.

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

Field Documentation

◆ head_

BaseNode LibXR::List::head_
private

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

Definition at line 232 of file list.hpp.

◆ mutex_

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

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

Definition at line 233 of file list.hpp.


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