libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::List Class Reference

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

#include <list.hpp>

Classes

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.
 

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

Constructor & Destructor Documentation

◆ List()

List::List ( )
noexcept

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

Definition at line 9 of file list.cpp.

9: head_(0) { head_.next_ = &head_; }
BaseNode * next_
指向下一个节点的指针 / Pointer to the next node.
Definition list.hpp:46

◆ ~List()

List::~List ( )

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

Definition at line 11 of file list.cpp.

12{
13 for (auto pos = head_.next_; pos != &head_;)
14 {
15 auto tmp = pos->next_;
16 pos->next_ = nullptr;
17 pos = tmp;
18 }
19
20 head_.next_ = nullptr;
21}

Member Function Documentation

◆ Add()

void List::Add ( BaseNode & data)

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

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

Definition at line 23 of file list.cpp.

24{
25 mutex_.Lock();
26 data.next_ = head_.next_;
27 head_.next_ = &data;
28 mutex_.Unlock();
29}
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 Lock the mutex, blocking if it is already locked.
Definition mutex.cpp:16
void Unlock()
解锁互斥锁 Unlock the mutex.
Definition mutex.cpp:40

◆ Delete()

ErrorCode List::Delete ( BaseNode & data)
noexcept

从链表中删除指定的节点。 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 45 of file list.cpp.

46{
47 mutex_.Lock();
48 for (auto pos = &head_; pos->next_ != &head_; pos = pos->next_)
49 {
50 if (pos->next_ == &data)
51 {
52 pos->next_ = data.next_;
53 data.next_ = nullptr;
54 mutex_.Unlock();
55 return ErrorCode::OK;
56 }
57 }
58 mutex_.Unlock();
60}
@ NOT_FOUND
未找到 / Not found
@ OK
操作成功 / Operation successful

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

172 {
173 mutex_.Lock();
174 for (auto pos = head_.next_; pos != &head_; pos = pos->next_)
175 {
176 ASSERT(LibXR::SizeLimitCheck(LimitMode, sizeof(Data), pos->size_));
177 if (auto res = func(static_cast<Node<Data>*>(pos)->data_); res != ErrorCode::OK)
178 {
179 mutex_.Unlock();
180 return res;
181 }
182 }
183 mutex_.Unlock();
184 return ErrorCode::OK;
185 }
constexpr bool SizeLimitCheck(SizeLimitMode mode, size_t limit, size_t size) noexcept
尺寸约束的纯判断函数 / Pure predicate for size-limit comparisons

◆ Size()

uint32_t List::Size ( )
noexcept

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

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

Definition at line 31 of file list.cpp.

32{
33 uint32_t size = 0;
34 mutex_.Lock();
35
36 for (auto pos = head_.next_; pos != &head_; pos = pos->next_)
37 {
38 ++size;
39 }
40
41 mutex_.Unlock();
42 return size;
43}

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