libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
list.hpp
1#pragma once
2
3#include <utility>
4
5#include "libxr_assert.hpp"
6#include "mutex.hpp"
7
8namespace LibXR
9{
10
21class List
22{
23 public:
29 {
30 public:
38 BaseNode(size_t size);
39
44 ~BaseNode();
45
46 BaseNode* next_ = nullptr;
47 size_t size_;
48 };
49
57 template <typename Data>
58 class Node : public BaseNode
59 {
60 public:
65 Node() : BaseNode(sizeof(Data)) {}
66
74 explicit Node(const Data& data) : BaseNode(sizeof(Data)), data_(data) {}
75
81 template <typename... Args>
82 explicit Node(Args&&... args)
83 : BaseNode(sizeof(Data)), data_{std::forward<Args>(args)...}
84 {
85 }
86
96 Node& operator=(const Data& data)
97 {
98 data_ = data;
99 return *this;
100 }
101
106 Data* operator->() noexcept { return &data_; }
107 const Data* operator->() const noexcept { return &data_; }
108 Data& operator*() noexcept { return data_; }
109 operator Data&() noexcept { return data_; }
110
111 Data data_;
112 };
113
118 List() noexcept;
119
124 ~List();
125
133 void Add(BaseNode& data);
134
142 uint32_t Size() noexcept;
143
153 ErrorCode Delete(BaseNode& data) noexcept;
154
170 template <typename Data, typename Func, SizeLimitMode LimitMode = SizeLimitMode::MORE>
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 }
186
187 private:
190};
191
192} // namespace LibXR
链表基础节点,所有节点都继承自该类。 Base node for the linked list, serving as a parent for all nodes.
Definition list.hpp:29
BaseNode * next_
指向下一个节点的指针。 Pointer to the next node.
Definition list.hpp:46
~BaseNode()
析构函数,确保节点不会在列表中残留。 Destructor ensuring the node does not remain in the list.
Definition list.cpp:7
size_t size_
当前节点的数据大小(字节)。 Size of the current node (in bytes).
Definition list.hpp:47
BaseNode(size_t size)
构造 BaseNode 并设置节点大小。 Constructs a BaseNode and sets its size.
Definition list.cpp:5
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
Definition list.hpp:59
Data * operator->() noexcept
操作符重载,提供数据访问接口。 Operator overloads providing access to the data.
Definition list.hpp:106
Node & operator=(const Data &data)
赋值运算符重载,允许直接对节点赋值。 Overloaded assignment operator for assigning values to the node.
Definition list.hpp:96
Node(Args &&... args)
通过参数列表构造节点 (Constructor initializing a node using arguments list).
Definition list.hpp:82
Data data_
存储的数据。 The stored data.
Definition list.hpp:111
Node()
默认构造函数,初始化节点大小。 Default constructor initializing the node size.
Definition list.hpp:65
Node(const Data &data)
使用数据值构造 Node 节点。 Constructs a Node with the given data value.
Definition list.hpp:74
链表实现,用于存储和管理数据节点。 A linked list implementation for storing and managing data nodes.
Definition list.hpp:22
LibXR::Mutex mutex_
线程安全的互斥锁。 Thread-safe mutex.
Definition list.hpp:189
ErrorCode Delete(BaseNode &data) noexcept
从链表中删除指定的节点。 Deletes a specified node from the linked list.
Definition list.cpp:45
uint32_t Size() noexcept
获取链表中的节点数量。 Gets the number of nodes in the linked list.
Definition list.cpp:31
void Add(BaseNode &data)
向链表添加一个节点。 Adds a node to the linked list.
Definition list.cpp:23
List() noexcept
默认构造函数,初始化链表头节点。 Default constructor initializing the linked list head node.
Definition list.cpp:9
ErrorCode Foreach(Func func)
遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
Definition list.hpp:171
BaseNode head_
链表头节点。 The head node of the list.
Definition list.hpp:188
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
Definition mutex.hpp:18
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
Definition mutex.cpp:16
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:40
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
@ OK
操作成功 | Operation successful
SizeLimitMode
定义尺寸限制模式
@ MORE
尺寸必须大于等于 | Size must be greater than or equal