libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
list.cpp
1#include "list.hpp"
2
3using namespace LibXR;
4
5List::BaseNode::BaseNode(size_t size) : size_(size) {}
6
7List::BaseNode::~BaseNode() { ASSERT(next_ == nullptr); }
8
9List::List() noexcept : head_(0) { head_.next_ = &head_; }
10
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}
22
24{
25 mutex_.Lock();
26 data.next_ = head_.next_;
27 head_.next_ = &data;
28 mutex_.Unlock();
29}
30
31uint32_t List::Size() noexcept
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}
44
45ErrorCode List::Delete(BaseNode& data) noexcept
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();
59 return ErrorCode::NOT_FOUND;
60}
链表基础节点,所有节点都继承自该类。 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.cpp:7
BaseNode(size_t size)
构造 BaseNode 并设置节点大小。 Constructs a BaseNode and sets its size.
Definition list.cpp:5
LibXR::Mutex mutex_
线程安全的互斥锁。 Thread-safe mutex.
Definition list.hpp:190
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
~List()
析构函数,释放所有节点。 Destructor releasing all nodes.
Definition list.cpp:11
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
BaseNode head_
链表头节点。 The head node of the list.
Definition list.hpp:189
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