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:
67
75 explicit Node(const Data& data) : BaseNode(sizeof(Data)), data_(data) {}
76
82 template <typename... Args>
83 explicit Node(Args... args) : BaseNode(sizeof(Data)), data_{args...}
84 {
85 }
86
96 Node& operator=(const Data& data)
97 {
98 data_ = data;
99 return *this;
100 }
101
107 const Data* operator->() const noexcept { return &data_; }
108 Data& operator*() noexcept { return data_; }
109 operator Data&() noexcept { return data_; }
110
112 };
113
119
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 }
135
143 void Add(BaseNode& data)
144 {
145 mutex_.Lock();
146 data.next_ = head_.next_;
147 head_.next_ = &data;
148 mutex_.Unlock();
149 }
150
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 }
171
181 ErrorCode Delete(BaseNode& data) noexcept
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 }
197
213 template <typename Data, typename Func, SizeLimitMode LimitMode = SizeLimitMode::MORE>
214 ErrorCode Foreach(Func func)
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 }
229
247 template <typename Data, typename Func, SizeLimitMode LimitMode = SizeLimitMode::MORE>
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 }
267
268 private:
271};
272
273} // namespace LibXR
链表基础节点,所有节点都继承自该类。 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:106
Node & operator=(const Data &data)
赋值运算符重载,允许直接对节点赋值。 Overloaded assignment operator for assigning values to the node.
Definition list.hpp:96
Data data_
存储的数据。 The stored data.
Definition list.hpp:111
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
Node(Args... args)
通过参数列表构造节点 (Constructor initializing a node using arguments list).
Definition list.hpp:83
链表实现,用于存储和管理数据节点。 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:158
~List()
析构函数,释放所有节点。 Destructor releasing all nodes.
Definition list.hpp:124
LibXR::Mutex mutex_
线程安全的互斥锁。 Thread-safe mutex.
Definition list.hpp:270
void Add(BaseNode &data)
向链表添加一个节点。 Adds a node to the linked list.
Definition list.hpp:143
ErrorCode ForeachFromCallback(Func func, bool in_isr)
在回调环境中遍历链表节点,并应用回调函数。 Iterates over each node in a callback environment and applies a function.
Definition list.hpp:248
List() noexcept
默认构造函数,初始化链表头节点。 Default constructor initializing the linked list head node.
Definition list.hpp:118
ErrorCode Delete(BaseNode &data) noexcept
从链表中删除指定的节点。 Deletes a specified node from the linked list.
Definition list.hpp:181
ErrorCode Foreach(Func func)
遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
Definition list.hpp:214
BaseNode head_
链表头节点。 The head node of the list.
Definition list.hpp:269
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
Definition mutex.hpp:18
ErrorCode TryLockInCallback(bool in_isr)
在回调(ISR)中尝试加锁 (Attempt to lock the mutex inside an interrupt service routine (ISR)).
Definition mutex.cpp:29
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
Definition mutex.cpp:13
void UnlockFromCallback(bool in_isr)
在回调(ISR)中解锁 (Unlock the mutex inside an interrupt service routine (ISR)).
Definition mutex.cpp:49
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:27
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值