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:
38 Stack(uint32_t depth) : stack_(new Data[depth]), depth_(depth) {}
39
46 Data& operator[](int32_t index)
47 {
48 if (index >= 0)
49 {
50 ASSERT(static_cast<uint32_t>(index) < depth_);
51 return stack_[index];
52 }
53 else
54 {
55 ASSERT(static_cast<int32_t>(depth_) + index >= 0);
56 return stack_[top_ + index];
57 }
58 }
59
63 [[nodiscard]] uint32_t Size() const { return top_; }
64
68 [[nodiscard]] uint32_t EmptySize() const { return depth_ - top_; }
69
77 ErrorCode Push(const Data& data)
78 {
79 mutex_.Lock();
80
81 if (top_ >= depth_)
82 {
83 mutex_.Unlock();
84 return ErrorCode::FULL;
85 }
86 stack_[top_++] = data;
87 mutex_.Unlock();
88 return ErrorCode::OK;
89 }
90
98 ErrorCode Pop(Data& data)
99 {
100 mutex_.Lock();
101
102 if (top_ == 0)
103 {
104 mutex_.Unlock();
105 return ErrorCode::EMPTY;
106 }
107 data = stack_[--top_];
108 mutex_.Unlock();
109 return ErrorCode::OK;
110 }
111
119 ErrorCode Pop()
120 {
121 mutex_.Lock();
122
123 if (top_ == 0)
124 {
125 mutex_.Unlock();
126 return ErrorCode::EMPTY;
127 }
128 --top_;
129 mutex_.Unlock();
130 return ErrorCode::OK;
131 }
132
140 ErrorCode Peek(Data& data)
141 {
142 mutex_.Lock();
143
144 if (top_ == 0)
145 {
146 mutex_.Unlock();
147
148 return ErrorCode::EMPTY;
149 }
150 data = stack_[top_ - 1];
151 mutex_.Unlock();
152
153 return ErrorCode::OK;
154 }
155
165 ErrorCode Insert(const Data& data, uint32_t index)
166 {
167 mutex_.Lock();
168 if (top_ >= depth_)
169 {
170 mutex_.Unlock();
171 return ErrorCode::FULL;
172 }
173
174 if (index > top_)
175 {
176 mutex_.Unlock();
177 return ErrorCode::OUT_OF_RANGE;
178 }
179
180 memmove(&stack_[index + 1], &stack_[index], (top_ - index) * sizeof(Data));
181
182 stack_[index] = data;
183 top_++;
184
185 mutex_.Unlock();
186
187 return ErrorCode::OK;
188 }
189
197 ErrorCode Delete(uint32_t index)
198 {
199 mutex_.Lock();
200 if (index >= top_)
201 {
202 mutex_.Unlock();
203 return ErrorCode::OUT_OF_RANGE;
204 }
205
206 for (uint32_t i = index; i < top_ - 1; i++)
207 {
208 stack_[i] = stack_[i + 1];
209 }
210 top_--;
211 mutex_.Unlock();
212 return ErrorCode::OK;
213 }
214
221 void Reset()
222 {
223 mutex_.Lock();
224 top_ = 0;
225 mutex_.Unlock();
226 }
227};
228} // 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:32
线程安全的栈数据结构 / Thread-safe stack data structure
Definition stack.hpp:23
ErrorCode Delete(uint32_t index)
删除指定位置的数据 / Deletes data at a specified position
Definition stack.hpp:197
uint32_t EmptySize() const
获取栈的剩余可用空间 / Returns the remaining available space in the stack
Definition stack.hpp:68
ErrorCode Peek(Data &data)
获取栈顶数据但不弹出 / Retrieves the top data of the stack without popping
Definition stack.hpp:140
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:46
ErrorCode Insert(const Data &data, uint32_t index)
在指定位置插入数据 / Inserts data at a specified position
Definition stack.hpp:165
void Reset()
重置栈 / Resets the stack
Definition stack.hpp:221
Data * stack_
栈存储数组 / Stack storage array
Definition stack.hpp:25
ErrorCode Push(const Data &data)
向栈中推入数据 / Pushes data onto the stack
Definition stack.hpp:77
ErrorCode Pop(Data &data)
从栈中弹出数据 / Pops data from the stack
Definition stack.hpp:98
ErrorCode Pop()
从栈中弹出数据(不返回数据) / Pops data from the stack (without returning data)
Definition stack.hpp:119
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:38
uint32_t Size() const
获取栈中当前元素数量 / Returns the number of elements currently in the stack
Definition stack.hpp:63
LibXR 命名空间
Definition ch32_can.hpp:14