libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::Stack< Data > Class Template Reference

线程安全的栈数据结构 / Thread-safe stack data structure More...

#include <stack.hpp>

Public Member Functions

 Stack (uint32_t depth)
 栈的构造函数 / Stack constructor
 
 ~Stack ()
 析构栈并释放存储数组 / Destroys the stack and frees the storage array
 
 Stack (const Stack &)=delete
 禁止复制构造 / Not copy-constructible
 
Stack & operator= (const Stack &)=delete
 禁止复制赋值 / Not copy-assignable
 
Data & operator[] (int32_t index)
 获取指定索引的元素 / Retrieves the element at a specified index
 
uint32_t Size () const
 获取栈中当前元素数量 / Returns the number of elements currently in the stack
 
uint32_t EmptySize () const
 获取栈的剩余可用空间 / Returns the remaining available space in the stack
 
ErrorCode Push (const Data &data)
 向栈中推入数据 / Pushes data onto the stack
 
ErrorCode Pop (Data &data)
 从栈中弹出数据 / Pops data from the stack
 
ErrorCode Pop ()
 从栈中弹出数据(不返回数据) / Pops data from the stack (without returning data)
 
ErrorCode Peek (Data &data)
 获取栈顶数据但不弹出 / Retrieves the top data of the stack without popping
 
ErrorCode Insert (const Data &data, uint32_t index)
 在指定位置插入数据 / Inserts data at a specified position
 
ErrorCode Delete (uint32_t index)
 删除指定位置的数据 / Deletes data at a specified position
 
void Reset ()
 重置栈 / Resets the stack
 

Detailed Description

template<typename Data>
class LibXR::Stack< Data >

线程安全的栈数据结构 / Thread-safe stack data structure

该类实现了一个基于数组的线程安全栈,支持基本的 Push、Pop、Peek 等操作,并使用互斥锁 (Mutex) 保护数据安全。 This class implements a thread-safe stack based on an array, supporting basic operations such as Push, Pop, and Peek, with mutex (Mutex) protection to ensure data safety.

Template Parameters
Data栈中存储的数据类型 / The type of data stored in the stack

Definition at line 24 of file stack.hpp.

Constructor & Destructor Documentation

◆ Stack()

template<typename Data >
LibXR::Stack< Data >::Stack ( uint32_t depth)
inline

栈的构造函数 / Stack constructor

Parameters
depth栈的最大容量 / Maximum capacity of the stack
Note
包含动态内存分配。 Contains dynamic memory allocation.

Definition at line 40 of file stack.hpp.

40: stack_(new Data[depth]), depth_(depth) {}

◆ ~Stack()

template<typename Data >
LibXR::Stack< Data >::~Stack ( )
inline

析构栈并释放存储数组 / Destroys the stack and frees the storage array

Definition at line 45 of file stack.hpp.

45{ delete[] stack_; }

Member Function Documentation

◆ Delete()

template<typename Data >
ErrorCode LibXR::Stack< Data >::Delete ( uint32_t index)
inline

删除指定位置的数据 / Deletes data at a specified position

Parameters
index要删除的索引位置 / Index of the data to be deleted
Returns
操作结果,成功返回 ErrorCode::OK,索引超出范围返回 ErrorCode::OUT_OF_RANGE / Operation result: returns ErrorCode::OK on success, ErrorCode::OUT_OF_RANGE if the index is out of range

Definition at line 217 of file stack.hpp.

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 }
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 Lock the mutex, blocking if it is already locked.
Definition mutex.cpp:16
void Unlock()
解锁互斥锁 Unlock the mutex.
Definition mutex.cpp:40
@ OUT_OF_RANGE
超出范围 / Out of range
@ OK
操作成功 / Operation successful

◆ EmptySize()

template<typename Data >
uint32_t LibXR::Stack< Data >::EmptySize ( ) const
inlinenodiscard

获取栈的剩余可用空间 / Returns the remaining available space in the stack

Definition at line 80 of file stack.hpp.

80{ return depth_ - top_; }

◆ Insert()

template<typename Data >
ErrorCode LibXR::Stack< Data >::Insert ( const Data & data,
uint32_t index )
inline

在指定位置插入数据 / Inserts data at a specified position

Parameters
data要插入的数据 / The data to be inserted
index插入位置索引 / Index at which the data is inserted
Returns
操作结果,成功返回 ErrorCode::OK,栈满返回 ErrorCode::FULL,索引超出范围返回 ErrorCode::OUT_OF_RANGE / Operation result: returns ErrorCode::OK on success, ErrorCode::FULL if the stack is full, ErrorCode::OUT_OF_RANGE if the index is out of range
Note
插入位置之后的元素按字节搬移,因此 Data 必须可平凡复制,否则编译失败。 The elements after the position are moved byte by byte, so Data must be trivially copyable; otherwise the call does not compile.

Definition at line 181 of file stack.hpp.

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 }
@ FULL
已满 / Full

◆ operator[]()

template<typename Data >
Data & LibXR::Stack< Data >::operator[] ( int32_t index)
inline

获取指定索引的元素 / Retrieves the element at a specified index

Parameters
index元素索引,支持负索引(从栈顶向下索引) / Element index, supports negative indexing (relative to the top)
Returns
该索引位置的元素 / The element at the given index

Definition at line 58 of file stack.hpp.

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 }

◆ Peek()

template<typename Data >
ErrorCode LibXR::Stack< Data >::Peek ( Data & data)
inline

获取栈顶数据但不弹出 / Retrieves the top data of the stack without popping

Parameters
data用于存储栈顶数据 / Variable to store the retrieved data
Returns
操作结果,成功返回 ErrorCode::OK,栈为空返回 ErrorCode::EMPTY / Operation result: returns ErrorCode::OK on success, ErrorCode::EMPTY if the stack is empty

Definition at line 152 of file stack.hpp.

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 }
@ EMPTY
为空 / Empty

◆ Pop() [1/2]

template<typename Data >
ErrorCode LibXR::Stack< Data >::Pop ( )
inline

从栈中弹出数据(不返回数据) / Pops data from the stack (without returning data)

Returns
操作结果,成功返回 ErrorCode::OK,栈为空返回 ErrorCode::EMPTY / Operation result: returns ErrorCode::OK on success, ErrorCode::EMPTY if the stack is empty

Definition at line 131 of file stack.hpp.

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 }

◆ Pop() [2/2]

template<typename Data >
ErrorCode LibXR::Stack< Data >::Pop ( Data & data)
inline

从栈中弹出数据 / Pops data from the stack

Parameters
data用于存储弹出的数据 / Variable to store the popped data
Returns
操作结果,成功返回 ErrorCode::OK,栈为空返回 ErrorCode::EMPTY / Operation result: returns ErrorCode::OK on success, ErrorCode::EMPTY if the stack is empty

Definition at line 110 of file stack.hpp.

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 }

◆ Push()

template<typename Data >
ErrorCode LibXR::Stack< Data >::Push ( const Data & data)
inline

向栈中推入数据 / Pushes data onto the stack

Parameters
data要推入的元素 / The element to be pushed
Returns
操作结果,成功返回 ErrorCode::OK,栈满返回 ErrorCode::FULL / Operation result: returns ErrorCode::OK on success, ErrorCode::FULL if the stack is full

Definition at line 89 of file stack.hpp.

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 }

◆ Reset()

template<typename Data >
void LibXR::Stack< Data >::Reset ( )
inline

重置栈 / Resets the stack

该方法清空栈,并将 top_ 归零。 This method clears the stack and resets top_ to zero.

Definition at line 241 of file stack.hpp.

242 {
243 mutex_.Lock();
244 top_ = 0;
245 mutex_.Unlock();
246 }

◆ Size()

template<typename Data >
uint32_t LibXR::Stack< Data >::Size ( ) const
inlinenodiscard

获取栈中当前元素数量 / Returns the number of elements currently in the stack

Definition at line 75 of file stack.hpp.

75{ return top_; }

The documentation for this class was generated from the following file: