libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::BaseQueue Class Reference

基础队列类,提供固定大小的循环缓冲区 (Base queue class providing a fixed-size circular buffer). More...

#include <queue.hpp>

Inheritance diagram for LibXR::BaseQueue:

Public Member Functions

 BaseQueue (uint16_t element_size, size_t length, uint8_t *buffer)
 构造函数,初始化队列 (Constructor to initialize the queue).
 
 BaseQueue (uint16_t element_size, size_t length)
 构造函数,初始化队列 (Constructor to initialize the queue).
 
 ~BaseQueue ()
 析构函数,释放队列内存 (Destructor to free queue memory).
 
voidoperator[] (uint32_t index)
 访问指定索引的元素 (Access an element at a specified index).
 
ErrorCode Push (const void *data)
 向队列中添加一个元素 (Push an element into the queue).
 
ErrorCode Peek (void *data)
 获取队列头部的元素但不移除 (Peek at the front element without removing it).
 
ErrorCode Pop (void *data=nullptr)
 移除队列头部的元素 (Pop the front element from the queue).
 
int GetLastElementIndex ()
 获取队列中最后一个元素的索引 (Get the index of the last element in the queue).
 
int GetFirstElementIndex ()
 获取队列中第一个元素的索引 (Get the index of the first element in the queue).
 
ErrorCode PushBatch (const void *data, size_t size)
 批量推入多个元素 (Push multiple elements into the queue).
 
ErrorCode PopBatch (void *data, size_t size)
 批量移除多个元素 (Pop multiple elements from the queue).
 
ErrorCode PeekBatch (void *data, size_t size)
 批量获取多个元素但不移除 (Peek at multiple elements without removing them).
 
ErrorCode Overwrite (const void *data)
 覆盖队列中的数据 (Overwrite the queue with new data).
 
void Reset ()
 重置队列,清空所有数据 (Reset the queue and clear all data).
 
size_t Size ()
 获取队列中的元素个数 (Get the number of elements in the queue).
 
size_t EmptySize ()
 获取队列的空闲空间 (Get the available space in the queue).
 
 BaseQueue (const BaseQueue &)=delete
 
BaseQueue operator= (const BaseQueue &)=delete
 
BaseQueue operator= (BaseQueue &)=delete
 
BaseQueue operator= (const BaseQueue &&)=delete
 
BaseQueue operator= (BaseQueue &&)=delete
 

Data Fields

uint8_tqueue_array_
 存储队列数据的数组 (Array storing queue data).
 
const uint16_t ELEMENT_SIZE
 每个元素的大小 (Size of each element).
 
size_t head_ = 0
 头部索引 (Head index).
 
size_t tail_ = 0
 尾部索引 (Tail index).
 
bool is_full_ = false
 队列是否已满 (Indicates if the queue is full).
 
size_t length_
 队列最大容量 (Maximum queue capacity).
 

Detailed Description

基础队列类,提供固定大小的循环缓冲区 (Base queue class providing a fixed-size circular buffer).

This class implements a circular queue that supports element insertion, retrieval, and batch operations. 该类实现了一个循环队列,支持元素插入、读取和批量操作。

Definition at line 19 of file queue.hpp.

Constructor & Destructor Documentation

◆ BaseQueue() [1/2]

LibXR::BaseQueue::BaseQueue ( uint16_t  element_size,
size_t  length,
uint8_t buffer 
)
inline

构造函数,初始化队列 (Constructor to initialize the queue).

Parameters
element_size队列中每个元素的大小 (Size of each element in the queue).
length队列的最大容量 (Maximum capacity of the queue).
buffer指向缓冲区的指针 (Pointer to the buffer).

Definition at line 28 of file queue.hpp.

30 {
31 }
size_t length_
队列最大容量 (Maximum queue capacity).
Definition queue.hpp:338
uint8_t * queue_array_
存储队列数据的数组 (Array storing queue data).
Definition queue.hpp:333
const uint16_t ELEMENT_SIZE
每个元素的大小 (Size of each element).
Definition queue.hpp:334
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

◆ BaseQueue() [2/2]

LibXR::BaseQueue::BaseQueue ( uint16_t  element_size,
size_t  length 
)
inline

构造函数,初始化队列 (Constructor to initialize the queue).

Parameters
element_size队列中每个元素的大小 (Size of each element in the queue).
length队列的最大容量 (Maximum capacity of the queue).

Definition at line 38 of file queue.hpp.

◆ ~BaseQueue()

LibXR::BaseQueue::~BaseQueue ( )
inline

析构函数,释放队列内存 (Destructor to free queue memory).

Definition at line 48 of file queue.hpp.

48{ delete[] queue_array_; }

Member Function Documentation

◆ EmptySize()

size_t LibXR::BaseQueue::EmptySize ( )
inline

获取队列的空闲空间 (Get the available space in the queue).

Returns
队列中可存储的元素个数 (Number of available slots in the queue).

Definition at line 325 of file queue.hpp.

325{ return length_ - Size(); }
size_t Size()
获取队列中的元素个数 (Get the number of elements in the queue).
Definition queue.hpp:305

◆ GetFirstElementIndex()

int LibXR::BaseQueue::GetFirstElementIndex ( )
inline

获取队列中第一个元素的索引 (Get the index of the first element in the queue).

Returns
如果队列非空,返回第一个元素的索引,否则返回 -1 (Returns the index of the first element if the queue is not empty, otherwise -1).

Definition at line 156 of file queue.hpp.

157 {
158 if (Size() > 0)
159 {
160 return static_cast<int>(head_);
161 }
162 else
163 {
164 return -1;
165 }
166 }
size_t head_
头部索引 (Head index).
Definition queue.hpp:335

◆ GetLastElementIndex()

int LibXR::BaseQueue::GetLastElementIndex ( )
inline

获取队列中最后一个元素的索引 (Get the index of the last element in the queue).

Returns
如果队列非空,返回最后一个元素的索引,否则返回 -1 (Returns the index of the last element if the queue is not empty, otherwise -1).

Definition at line 138 of file queue.hpp.

139 {
140 if (Size() > 0)
141 {
142 return static_cast<int>((tail_ + length_ - 1) % length_);
143 }
144 else
145 {
146 return -1;
147 }
148 }
size_t tail_
尾部索引 (Tail index).
Definition queue.hpp:336

◆ operator[]()

void * LibXR::BaseQueue::operator[] ( uint32_t  index)
inline

访问指定索引的元素 (Access an element at a specified index).

Parameters
index目标索引 (Target index).
Returns
指向该索引处元素的指针 (Pointer to the element at the specified index).

Definition at line 55 of file queue.hpp.

56 {
57 return &queue_array_[static_cast<size_t>(index * ELEMENT_SIZE)];
58 }

◆ Overwrite()

ErrorCode LibXR::BaseQueue::Overwrite ( const void data)
inline

覆盖队列中的数据 (Overwrite the queue with new data).

Parameters
data指向新数据的缓冲区 (Pointer to the new data buffer).
Returns
操作结果,成功返回 ErrorCode::OK (Operation result: ErrorCode::OK on success).

Definition at line 274 of file queue.hpp.

275 {
276 ASSERT(data != nullptr);
277
278 head_ = tail_ = 0;
279 is_full_ = false;
280
282
283 tail_ = (tail_ + 1) % length_;
284 if (head_ == tail_)
285 {
286 is_full_ = true;
287 }
288
289 return ErrorCode::OK;
290 }
bool is_full_
队列是否已满 (Indicates if the queue is full).
Definition queue.hpp:337

◆ Peek()

ErrorCode LibXR::BaseQueue::Peek ( void data)
inline

获取队列头部的元素但不移除 (Peek at the front element without removing it).

Parameters
data指向存储数据的缓冲区 (Pointer to buffer where the data is stored).
Returns
操作结果,成功返回 ErrorCode::OK,队列为空时返回 ErrorCode::EMPTY (Operation result: ErrorCode::OK on success, ErrorCode::EMPTY if empty).

Definition at line 92 of file queue.hpp.

93 {
94 ASSERT(data != nullptr);
95
96 if (Size() > 0)
97 {
99 return ErrorCode::OK;
100 }
101 else
102 {
103 return ErrorCode::EMPTY;
104 }
105 }

◆ PeekBatch()

ErrorCode LibXR::BaseQueue::PeekBatch ( void data,
size_t  size 
)
inline

批量获取多个元素但不移除 (Peek at multiple elements without removing them).

Parameters
data指向存储数据的缓冲区 (Pointer to buffer where the data is stored).
size要获取的元素个数 (Number of elements to retrieve).
Returns
操作结果,成功返回 ErrorCode::OK,队列为空时返回 ErrorCode::EMPTY (Operation result: ErrorCode::OK on success, ErrorCode::EMPTY if empty).

Definition at line 245 of file queue.hpp.

246 {
247 ASSERT(data != nullptr);
248
249 if (Size() < size)
250 {
251 return ErrorCode::EMPTY;
252 }
253
254 auto index = head_;
255
256 auto tmp = reinterpret_cast<uint8_t *>(data);
257
258 for (size_t i = 0; i < size; i++)
259 {
262 index = (index + 1) % length_;
263 }
264
265 return ErrorCode::OK;
266 }

◆ Pop()

ErrorCode LibXR::BaseQueue::Pop ( void data = nullptr)
inline

移除队列头部的元素 (Pop the front element from the queue).

Parameters
data指向存储数据的缓冲区 (Pointer to buffer where the removed data is stored).
Returns
操作结果,成功返回 ErrorCode::OK,队列为空时返回 ErrorCode::EMPTY (Operation result: ErrorCode::OK on success, ErrorCode::EMPTY if empty).

Definition at line 114 of file queue.hpp.

115 {
116 if (Size() > 0)
117 {
118 if (data != nullptr)
119 {
121 }
122 head_ = (head_ + 1) % length_;
123 is_full_ = false;
124 return ErrorCode::OK;
125 }
126 else
127 {
128 return ErrorCode::EMPTY;
129 }
130 }

◆ PopBatch()

ErrorCode LibXR::BaseQueue::PopBatch ( void data,
size_t  size 
)
inline

批量移除多个元素 (Pop multiple elements from the queue).

Parameters
data指向存储数据的缓冲区 (Pointer to buffer where the removed data is stored).
size要移除的元素个数 (Number of elements to remove).
Returns
操作结果,成功返回 ErrorCode::OK,队列为空时返回 ErrorCode::EMPTY (Operation result: ErrorCode::OK on success, ErrorCode::EMPTY if empty).

Definition at line 208 of file queue.hpp.

209 {
210 if (Size() < size)
211 {
212 return ErrorCode::EMPTY;
213 }
214
215 if (size > 0)
216 {
217 is_full_ = false;
218 }
219 else
220 {
221 return ErrorCode::OK;
222 }
223
224 auto tmp = reinterpret_cast<uint8_t *>(data);
225
226 for (size_t i = 0; i < size; i++)
227 {
228 if (data != nullptr)
229 {
231 }
232 head_ = (head_ + 1) % length_;
233 }
234
235 return ErrorCode::OK;
236 }

◆ Push()

ErrorCode LibXR::BaseQueue::Push ( const void data)
inline

向队列中添加一个元素 (Push an element into the queue).

Parameters
data指向要添加的数据 (Pointer to the data to be added).
Returns
操作结果,成功返回 ErrorCode::OK,队列满时返回 ErrorCode::FULL (Operation result: ErrorCode::OK on success, ErrorCode::FULL if full).

Definition at line 66 of file queue.hpp.

67 {
68 ASSERT(data != nullptr);
69
70 if (is_full_)
71 {
72 return ErrorCode::FULL;
73 }
74
76
77 tail_ = (tail_ + 1) % length_;
78 if (head_ == tail_)
79 {
80 is_full_ = true;
81 }
82
83 return ErrorCode::OK;
84 }

◆ PushBatch()

ErrorCode LibXR::BaseQueue::PushBatch ( const void data,
size_t  size 
)
inline

批量推入多个元素 (Push multiple elements into the queue).

Parameters
data指向数据缓冲区 (Pointer to the data buffer).
size要推入的元素个数 (Number of elements to push).
Returns
操作结果,成功返回 ErrorCode::OK,队列满时返回 ErrorCode::FULL (Operation result: ErrorCode::OK on success, ErrorCode::FULL if full).

Definition at line 175 of file queue.hpp.

176 {
177 ASSERT(data != nullptr);
178
179 auto avail = EmptySize();
180 if (avail < size)
181 {
182 return ErrorCode::FULL;
183 }
184
185 auto tmp = reinterpret_cast<const uint8_t *>(data);
186
187 for (size_t i = 0; i < size; i++)
188 {
190 tail_ = (tail_ + 1) % length_;
191 }
192
193 if (head_ == tail_)
194 {
195 is_full_ = true;
196 }
197 return ErrorCode::OK;
198 }
size_t EmptySize()
获取队列的空闲空间 (Get the available space in the queue).
Definition queue.hpp:325

◆ Reset()

void LibXR::BaseQueue::Reset ( )
inline

重置队列,清空所有数据 (Reset the queue and clear all data).

Definition at line 295 of file queue.hpp.

296 {
297 head_ = tail_ = 0;
298 is_full_ = false;
299 }

◆ Size()

size_t LibXR::BaseQueue::Size ( )
inline

获取队列中的元素个数 (Get the number of elements in the queue).

Returns
当前队列中的元素个数 (Current number of elements in the queue).

Definition at line 305 of file queue.hpp.

306 {
307 if (is_full_)
308 {
309 return length_;
310 }
311 else if (tail_ >= head_)
312 {
313 return tail_ - head_;
314 }
315 else
316 {
317 return length_ + tail_ - head_;
318 }
319 }

Field Documentation

◆ ELEMENT_SIZE

const uint16_t LibXR::BaseQueue::ELEMENT_SIZE

每个元素的大小 (Size of each element).

Definition at line 334 of file queue.hpp.

◆ head_

size_t LibXR::BaseQueue::head_ = 0

头部索引 (Head index).

Definition at line 335 of file queue.hpp.

◆ is_full_

bool LibXR::BaseQueue::is_full_ = false

队列是否已满 (Indicates if the queue is full).

Definition at line 337 of file queue.hpp.

◆ length_

size_t LibXR::BaseQueue::length_

队列最大容量 (Maximum queue capacity).

Definition at line 338 of file queue.hpp.

◆ queue_array_

uint8_t* LibXR::BaseQueue::queue_array_

存储队列数据的数组 (Array storing queue data).

Definition at line 333 of file queue.hpp.

◆ tail_

size_t LibXR::BaseQueue::tail_ = 0

尾部索引 (Tail index).

Definition at line 336 of file queue.hpp.


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