libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
stack.hpp
1#pragma once
2
3#include <cstring>
4#include <type_traits>
5
6#include "libxr_def.hpp"
7#include "mutex.hpp"
8
9namespace LibXR
10{
23template <typename Data>
24class Stack
25{
26 private:
27 Data* stack_;
28 uint32_t top_ = 0;
29 uint32_t depth_;
30 LibXR::Mutex mutex_;
31
32 public:
40 Stack(uint32_t depth) : stack_(new Data[depth]), depth_(depth) {}
41
45 ~Stack() { delete[] stack_; }
46
48 Stack(const Stack&) = delete;
50 Stack& operator=(const Stack&) = delete;
51
58 Data& operator[](int32_t index)
59 {
60 if (index >= 0)
61 {
62 ASSERT(static_cast<uint32_t>(index) < depth_);
63 return stack_[index];
64 }
65 else
66 {
67 ASSERT(static_cast<int32_t>(top_) + index >= 0);
68 return stack_[top_ + index];
69 }
70 }
71
75 [[nodiscard]] uint32_t Size() const { return top_; }
76
80 [[nodiscard]] uint32_t EmptySize() const { return depth_ - top_; }
81
89 ErrorCode Push(const Data& data)
90 {
91 mutex_.Lock();
92
93 if (top_ >= depth_)
94 {
95 mutex_.Unlock();
96 return ErrorCode::FULL;
97 }
98 stack_[top_++] = data;
99 mutex_.Unlock();
100 return ErrorCode::OK;
101 }
102
110 ErrorCode Pop(Data& data)
111 {
112 mutex_.Lock();
113
114 if (top_ == 0)
115 {
116 mutex_.Unlock();
117 return ErrorCode::EMPTY;
118 }
119 data = stack_[--top_];
120 mutex_.Unlock();
121 return ErrorCode::OK;
122 }
123
132 {
133 mutex_.Lock();
134
135 if (top_ == 0)
136 {
137 mutex_.Unlock();
138 return ErrorCode::EMPTY;
139 }
140 --top_;
141 mutex_.Unlock();
142 return ErrorCode::OK;
143 }
144
152 ErrorCode Peek(Data& data)
153 {
154 mutex_.Lock();
155
156 if (top_ == 0)
157 {
158 mutex_.Unlock();
159
160 return ErrorCode::EMPTY;
161 }
162 data = stack_[top_ - 1];
163 mutex_.Unlock();
164
165 return ErrorCode::OK;
166 }
167
181 ErrorCode Insert(const Data& data, uint32_t index)
182 {
183 static_assert(std::is_trivially_copyable_v<Data>,
184 "Stack::Insert moves elements with memmove and requires a trivially "
185 "copyable element type");
186
187 mutex_.Lock();
188 if (top_ >= depth_)
189 {
190 mutex_.Unlock();
191 return ErrorCode::FULL;
192 }
193
194 if (index > top_)
195 {
196 mutex_.Unlock();
198 }
199
200 memmove(&stack_[index + 1], &stack_[index], (top_ - index) * sizeof(Data));
201
202 stack_[index] = data;
203 top_++;
204
205 mutex_.Unlock();
206
207 return ErrorCode::OK;
208 }
209
217 ErrorCode Delete(uint32_t index)
218 {
219 mutex_.Lock();
220 if (index >= top_)
221 {
222 mutex_.Unlock();
224 }
225
226 for (uint32_t i = index; i < top_ - 1; i++)
227 {
228 stack_[i] = stack_[i + 1];
229 }
230 top_--;
231 mutex_.Unlock();
232 return ErrorCode::OK;
233 }
234
241 void Reset()
242 {
243 mutex_.Lock();
244 top_ = 0;
245 mutex_.Unlock();
246 }
247};
248} // 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:16
void Unlock()
解锁互斥锁 Unlock the mutex.
Definition mutex.cpp:40
线程安全的栈数据结构 / Thread-safe stack data structure
Definition stack.hpp:25
ErrorCode Delete(uint32_t index)
删除指定位置的数据 / Deletes data at a specified position
Definition stack.hpp:217
uint32_t EmptySize() const
获取栈的剩余可用空间 / Returns the remaining available space in the stack
Definition stack.hpp:80
~Stack()
析构栈并释放存储数组 / Destroys the stack and frees the storage array
Definition stack.hpp:45
Stack(const Stack &)=delete
禁止复制构造 / Not copy-constructible
ErrorCode Peek(Data &data)
获取栈顶数据但不弹出 / Retrieves the top data of the stack without popping
Definition stack.hpp:152
Data & operator[](int32_t index)
获取指定索引的元素 / Retrieves the element at a specified index
Definition stack.hpp:58
Stack & operator=(const Stack &)=delete
禁止复制赋值 / Not copy-assignable
ErrorCode Insert(const Data &data, uint32_t index)
在指定位置插入数据 / Inserts data at a specified position
Definition stack.hpp:181
void Reset()
重置栈 / Resets the stack
Definition stack.hpp:241
ErrorCode Push(const Data &data)
向栈中推入数据 / Pushes data onto the stack
Definition stack.hpp:89
ErrorCode Pop(Data &data)
从栈中弹出数据 / Pops data from the stack
Definition stack.hpp:110
ErrorCode Pop()
从栈中弹出数据(不返回数据) / Pops data from the stack (without returning data)
Definition stack.hpp:131
Stack(uint32_t depth)
栈的构造函数 / Stack constructor
Definition stack.hpp:40
uint32_t Size() const
获取栈中当前元素数量 / Returns the number of elements currently in the stack
Definition stack.hpp:75
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ OUT_OF_RANGE
超出范围 / Out of range
@ EMPTY
为空 / Empty
@ FULL
已满 / Full
@ OK
操作成功 / Operation successful