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 "libxr_def.hpp"
7#include "mutex.hpp"
8
9namespace LibXR
10{
11
22class List
23{
24 public:
30 {
31 public:
39 BaseNode(size_t size) : size_(size) {}
40
45 ~BaseNode() { ASSERT(next_ == nullptr); }
46
47 BaseNode* next_ = nullptr;
48 size_t size_;
49 };
50
58 template <typename Data>
59 class Node : public BaseNode
60 {
61 public:
66 Node() : BaseNode(sizeof(Data)) {}
67
75 explicit Node(const Data& data) : BaseNode(sizeof(Data)), data_(data) {}
76
82 template <typename... Args>
83 explicit Node(Args&&... args)
84 : BaseNode(sizeof(Data)), data_{std::forward<Args>(args)...}
85 {
86 }
87
97 Node& operator=(const Data& data)
98 {
99 data_ = data;
100 return *this;
101 }
102
107 Data* operator->() noexcept { return &data_; }
108 const Data* operator->() const noexcept { return &data_; }
109 Data& operator*() noexcept { return data_; }
110 operator Data&() noexcept { return data_; }
111
112 Data data_;
113 };
114
119 List() noexcept : head_(0) { head_.next_ = &head_; }
120
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 }
136
144 void Add(BaseNode& data)
145 {
146 mutex_.Lock();
147 data.next_ = head_.next_;
148 head_.next_ = &data;
149 mutex_.Unlock();
150 }
151
159 uint32_t Size() noexcept
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 }
172
182 ErrorCode Delete(BaseNode& data) noexcept
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 }
198
214 template <typename Data, typename Func, SizeLimitMode LimitMode = SizeLimitMode::MORE>
215 ErrorCode Foreach(Func func)
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 }
230
231 private:
234};
235
236} // namespace LibXR
static void SizeLimitCheck(size_t limit, size_t size)
在非调试模式下的占位大小检查函数(无实际作用)。 Dummy size limit check for non-debug builds.
链表基础节点,所有节点都继承自该类。 Base node for the linked list, serving as a parent for all nodes.
Definition list.hpp:30
BaseNode * next_
指向下一个节点的指针。 Pointer to the next node.
Definition list.hpp:47
~BaseNode()
析构函数,确保节点不会在列表中残留。 Destructor ensuring the node does not remain in the list.
Definition list.hpp:45
size_t size_
当前节点的数据大小(字节)。 Size of the current node (in bytes).
Definition list.hpp:48
BaseNode(size_t size)
构造 BaseNode 并设置节点大小。 Constructs a BaseNode and sets its size.
Definition list.hpp:39
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
Definition list.hpp:60
Data * operator->() noexcept
操作符重载,提供数据访问接口。 Operator overloads providing access to the data.
Definition list.hpp:107
Node & operator=(const Data &data)
赋值运算符重载,允许直接对节点赋值。 Overloaded assignment operator for assigning values to the node.
Definition list.hpp:97
Node(Args &&... args)
通过参数列表构造节点 (Constructor initializing a node using arguments list).
Definition list.hpp:83
Data data_
存储的数据。 The stored data.
Definition list.hpp:112
Node()
默认构造函数,初始化节点大小。 Default constructor initializing the node size.
Definition list.hpp:66
Node(const Data &data)
使用数据值构造 Node 节点。 Constructs a Node with the given data value.
Definition list.hpp:75
链表实现,用于存储和管理数据节点。 A linked list implementation for storing and managing data nodes.
Definition list.hpp:23
uint32_t Size() noexcept
获取链表中的节点数量。 Gets the number of nodes in the linked list.
Definition list.hpp:159
~List()
析构函数,释放所有节点。 Destructor releasing all nodes.
Definition list.hpp:125
LibXR::Mutex mutex_
线程安全的互斥锁。 Thread-safe mutex.
Definition list.hpp:233
void Add(BaseNode &data)
向链表添加一个节点。 Adds a node to the linked list.
Definition list.hpp:144
List() noexcept
默认构造函数,初始化链表头节点。 Default constructor initializing the linked list head node.
Definition list.hpp:119
ErrorCode Delete(BaseNode &data) noexcept
从链表中删除指定的节点。 Deletes a specified node from the linked list.
Definition list.hpp:182
ErrorCode Foreach(Func func)
遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
Definition list.hpp:215
BaseNode head_
链表头节点。 The head node of the list.
Definition list.hpp:232
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
Definition mutex.hpp:18
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
Definition mutex.cpp:14
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:28
LibXR 命名空间
Definition ch32_gpio.hpp:9