libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::Stack< Data > Class Template Reference

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

#include <stack.hpp>

Collaboration diagram for LibXR::Stack< Data >:
[legend]

Public Member Functions

 Stack (uint32_t depth)
 栈的构造函数 / Stack constructor
 
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
 

Private Attributes

Data * stack_
 栈存储数组 / Stack storage array
 
uint32_t top_ = 0
 当前栈顶索引 / Current top index of the stack
 
uint32_t depth_
 栈的最大容量 / Maximum capacity of the stack
 
LibXR::Mutex mutex_
 互斥锁,确保线程安全 / Mutex to ensure thread safety
 

Detailed Description

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

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

该类实现了一个基于数组的线程安全栈,支持基本的 PushPopPeek 等操作,并使用互斥锁 (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 22 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 38 of file stack.hpp.

38: stack_(new Data[depth]), depth_(depth) {}
uint32_t depth_
栈的最大容量 / Maximum capacity of the stack
Definition stack.hpp:27
Data * stack_
栈存储数组 / Stack storage array
Definition stack.hpp:25

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 197 of file stack.hpp.

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 }
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
Definition mutex.cpp:14
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:28
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

◆ EmptySize()

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

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

Definition at line 68 of file stack.hpp.

68{ 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

Definition at line 165 of file stack.hpp.

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 }

◆ 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 46 of file stack.hpp.

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 }

◆ 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 140 of file stack.hpp.

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 }

◆ 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 119 of file stack.hpp.

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 }

◆ 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 98 of file stack.hpp.

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 }

◆ 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 77 of file stack.hpp.

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 }

◆ 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 221 of file stack.hpp.

222 {
223 mutex_.Lock();
224 top_ = 0;
225 mutex_.Unlock();
226 }

◆ Size()

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

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

Definition at line 63 of file stack.hpp.

63{ return top_; }

Field Documentation

◆ depth_

template<typename Data >
uint32_t LibXR::Stack< Data >::depth_
private

栈的最大容量 / Maximum capacity of the stack

Definition at line 27 of file stack.hpp.

◆ mutex_

template<typename Data >
LibXR::Mutex LibXR::Stack< Data >::mutex_
private

互斥锁,确保线程安全 / Mutex to ensure thread safety

Definition at line 28 of file stack.hpp.

◆ stack_

template<typename Data >
Data* LibXR::Stack< Data >::stack_
private

栈存储数组 / Stack storage array

Definition at line 25 of file stack.hpp.

◆ top_

template<typename Data >
uint32_t LibXR::Stack< Data >::top_ = 0
private

当前栈顶索引 / Current top index of the stack

Definition at line 26 of file stack.hpp.


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