libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
stack.hpp
1#pragma once
2
3#include <cstring>
4
5#include "libxr_def.hpp"
6#include "mutex.hpp"
7
8namespace LibXR
9{
21template <typename Data>
22class Stack
23{
24 private:
25 Data *stack_;
26 uint32_t top_ = 0;
27 uint32_t depth_;
29
30 public:
35 Stack(uint32_t depth) : stack_(new Data[depth]), depth_(depth) {}
36
43 Data &operator[](int32_t index)
44 {
45 if (index >= 0)
46 {
47 ASSERT(static_cast<uint32_t>(index) < depth_);
48 return stack_[index];
49 }
50 else
51 {
52 ASSERT(static_cast<int32_t>(depth_) + index >= 0);
53 return stack_[top_ + index];
54 }
55 }
56
60 [[nodiscard]] uint32_t Size() const { return top_; }
61
65 [[nodiscard]] uint32_t EmptySize() const { return depth_ - top_; }
66
74 ErrorCode Push(const Data &data)
75 {
76 mutex_.Lock();
77
78 if (top_ >= depth_)
79 {
80 mutex_.Unlock();
81 return ErrorCode::FULL;
82 }
83 stack_[top_++] = data;
84 mutex_.Unlock();
85 return ErrorCode::OK;
86 }
87
95 ErrorCode Pop(Data &data)
96 {
97 mutex_.Lock();
98
99 if (top_ == 0)
100 {
101 mutex_.Unlock();
102 return ErrorCode::EMPTY;
103 }
104 data = stack_[--top_];
105 mutex_.Unlock();
106 return ErrorCode::OK;
107 }
108
116 ErrorCode Pop()
117 {
118 mutex_.Lock();
119
120 if (top_ == 0)
121 {
122 mutex_.Unlock();
123 return ErrorCode::EMPTY;
124 }
125 --top_;
126 mutex_.Unlock();
127 return ErrorCode::OK;
128 }
129
137 ErrorCode Peek(Data &data)
138 {
139 mutex_.Lock();
140
141 if (top_ == 0)
142 {
143 mutex_.Unlock();
144
145 return ErrorCode::EMPTY;
146 }
147 data = stack_[top_ - 1];
148 mutex_.Unlock();
149
150 return ErrorCode::OK;
151 }
152
162 ErrorCode Insert(const Data &data, uint32_t index)
163 {
164 mutex_.Lock();
165 if (top_ >= depth_)
166 {
167 mutex_.Unlock();
168 return ErrorCode::FULL;
169 }
170
171 if (index > top_)
172 {
173 mutex_.Unlock();
174 return ErrorCode::OUT_OF_RANGE;
175 }
176
177 memmove(&stack_[index + 1], &stack_[index], (top_ - index) * sizeof(Data));
178
179 stack_[index] = data;
180 top_++;
181
182 mutex_.Unlock();
183
184 return ErrorCode::OK;
185 }
186
194 ErrorCode Delete(uint32_t index)
195 {
196 mutex_.Lock();
197 if (index >= top_)
198 {
199 mutex_.Unlock();
200 return ErrorCode::OUT_OF_RANGE;
201 }
202
203 for (uint32_t i = index; i < top_ - 1; i++)
204 {
205 stack_[i] = stack_[i + 1];
206 }
207 top_--;
208 mutex_.Unlock();
209 return ErrorCode::OK;
210 }
211
218 void Reset()
219 {
220 mutex_.Lock();
221 top_ = 0;
222 mutex_.Unlock();
223 }
224};
225} // namespace LibXR
互斥锁类,提供线程同步机制 (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
线程安全的栈数据结构 / Thread-safe stack data structure
Definition stack.hpp:23
ErrorCode Delete(uint32_t index)
删除指定位置的数据 / Deletes data at a specified position
Definition stack.hpp:194
uint32_t EmptySize() const
获取栈的剩余可用空间 / Returns the remaining available space in the stack
Definition stack.hpp:65
ErrorCode Peek(Data &data)
获取栈顶数据但不弹出 / Retrieves the top data of the stack without popping
Definition stack.hpp:137
uint32_t depth_
栈的最大容量 / Maximum capacity of the stack
Definition stack.hpp:27
Data & operator[](int32_t index)
获取指定索引的元素 / Retrieves the element at a specified index
Definition stack.hpp:43
ErrorCode Insert(const Data &data, uint32_t index)
在指定位置插入数据 / Inserts data at a specified position
Definition stack.hpp:162
void Reset()
重置栈 / Resets the stack
Definition stack.hpp:218
Data * stack_
栈存储数组 / Stack storage array
Definition stack.hpp:25
ErrorCode Push(const Data &data)
向栈中推入数据 / Pushes data onto the stack
Definition stack.hpp:74
ErrorCode Pop(Data &data)
从栈中弹出数据 / Pops data from the stack
Definition stack.hpp:95
ErrorCode Pop()
从栈中弹出数据(不返回数据) / Pops data from the stack (without returning data)
Definition stack.hpp:116
uint32_t top_
当前栈顶索引 / Current top index of the stack
Definition stack.hpp:26
LibXR::Mutex mutex_
互斥锁,确保线程安全 / Mutex to ensure thread safety
Definition stack.hpp:28
Stack(uint32_t depth)
栈的构造函数 / Stack constructor
Definition stack.hpp:35
uint32_t Size() const
获取栈中当前元素数量 / Returns the number of elements currently in the stack
Definition stack.hpp:60
LibXR 命名空间
Definition ch32_gpio.hpp:9