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:
[legend]

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).
 
void * operator[] (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 () const
 获取队列中最后一个元素的索引 (Get the index of the last element in the queue).
 
int GetFirstElementIndex () const
 获取队列中第一个元素的索引 (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 () const
 获取队列中的元素个数 (Get the number of elements in the queue).
 
size_t EmptySize () const
 获取队列的空闲空间 (Get the available space in the queue).
 
 BaseQueue (const BaseQueue &)=delete
 
BaseQueueoperator= (const BaseQueue &)=delete
 
BaseQueueoperator= (BaseQueue &)=delete
 
BaseQueueoperator= (const BaseQueue &&)=delete
 
BaseQueueoperator= (BaseQueue &&)=delete
 

Data Fields

uint8_t * queue_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).
 
bool own_buffer_ = false
 是否由队列自己管理缓冲区 (Owns buffer memory).
 

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]

BaseQueue::BaseQueue ( uint16_t element_size,
size_t length,
uint8_t * buffer )

构造函数,初始化队列 (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 5 of file queue.cpp.

6 : queue_array_(buffer),
7 ELEMENT_SIZE(element_size),
8 length_(length),
9 own_buffer_(false)
10{
11}
size_t length_
队列最大容量 (Maximum queue capacity).
Definition queue.hpp:154
bool own_buffer_
是否由队列自己管理缓冲区 (Owns buffer memory).
Definition queue.hpp:155
uint8_t * queue_array_
存储队列数据的数组 (Array storing queue data).
Definition queue.hpp:149
const uint16_t ELEMENT_SIZE
每个元素的大小 (Size of each element).
Definition queue.hpp:150

◆ BaseQueue() [2/2]

BaseQueue::BaseQueue ( uint16_t element_size,
size_t length )

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

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

Definition at line 13 of file queue.cpp.

14 : queue_array_(new uint8_t[length * element_size]),
15 ELEMENT_SIZE(element_size),
16 length_(length),
17 own_buffer_(true)
18{
19}

◆ ~BaseQueue()

BaseQueue::~BaseQueue ( )

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

Definition at line 21 of file queue.cpp.

22{
23 if (own_buffer_)
24 {
25 delete[] queue_array_;
26 }
27}

Member Function Documentation

◆ EmptySize()

size_t BaseQueue::EmptySize ( ) const
nodiscard

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

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

Definition at line 226 of file queue.cpp.

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

◆ GetFirstElementIndex()

int BaseQueue::GetFirstElementIndex ( ) const

获取队列中第一个元素的索引 (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 94 of file queue.cpp.

95{
96 if (Size() == 0)
97 {
98 return -1;
99 }
100 return static_cast<int>(head_);
101}
size_t head_
头部索引 (Head index).
Definition queue.hpp:151

◆ GetLastElementIndex()

int BaseQueue::GetLastElementIndex ( ) const

获取队列中最后一个元素的索引 (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 85 of file queue.cpp.

86{
87 if (Size() == 0)
88 {
89 return -1;
90 }
91 return static_cast<int>((tail_ + length_ - 1) % length_);
92}
size_t tail_
尾部索引 (Tail index).
Definition queue.hpp:152

◆ operator[]()

void * BaseQueue::operator[] ( uint32_t index)
nodiscard

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

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

Definition at line 29 of file queue.cpp.

30{
31 return &queue_array_[static_cast<size_t>(index * ELEMENT_SIZE)];
32}

◆ Overwrite()

ErrorCode BaseQueue::Overwrite ( const void * data)

覆盖队列中的数据 (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 186 of file queue.cpp.

187{
188 ASSERT(data != nullptr);
189
190 head_ = tail_ = 0;
191 is_full_ = false;
192
193 memcpy(queue_array_, data, ELEMENT_SIZE * length_);
194
195 tail_ = (tail_ + 1) % length_;
196 if (head_ == tail_)
197 {
198 is_full_ = true;
199 }
200
201 return ErrorCode::OK;
202}
bool is_full_
队列是否已满 (Indicates if the queue is full).
Definition queue.hpp:153

◆ Peek()

ErrorCode BaseQueue::Peek ( void * data)

获取队列头部的元素但不移除 (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 54 of file queue.cpp.

55{
56 ASSERT(data != nullptr);
57
58 if (Size() > 0)
59 {
61 return ErrorCode::OK;
62 }
63 else
64 {
65 return ErrorCode::EMPTY;
66 }
67}

◆ PeekBatch()

ErrorCode BaseQueue::PeekBatch ( void * data,
size_t size )

批量获取多个元素但不移除 (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 161 of file queue.cpp.

162{
163 ASSERT(data != nullptr);
164
165 if (Size() < size)
166 {
167 return ErrorCode::EMPTY;
168 }
169
170 auto index = head_;
171
172 auto tmp = reinterpret_cast<uint8_t *>(data);
173
174 size_t first_part = LibXR::min(size, length_ - index);
175 memcpy(tmp, &queue_array_[index * ELEMENT_SIZE], first_part * ELEMENT_SIZE);
176
177 if (first_part < size)
178 {
179 memcpy(&tmp[first_part * ELEMENT_SIZE], queue_array_,
180 (size - first_part) * ELEMENT_SIZE);
181 }
182
183 return ErrorCode::OK;
184}
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

◆ Pop()

ErrorCode BaseQueue::Pop ( void * data = nullptr)

移除队列头部的元素 (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 69 of file queue.cpp.

70{
71 if (Size() == 0)
72 {
73 return ErrorCode::EMPTY;
74 }
75
76 if (data != nullptr)
77 {
79 }
80 head_ = (head_ + 1) % length_;
81 is_full_ = false;
82 return ErrorCode::OK;
83}

◆ PopBatch()

ErrorCode BaseQueue::PopBatch ( void * data,
size_t size )

批量移除多个元素 (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 132 of file queue.cpp.

133{
134 if (Size() < size)
135 {
136 return ErrorCode::EMPTY;
137 }
138
139 if (size == 0)
140 {
141 return ErrorCode::OK;
142 }
143 is_full_ = false;
144
145 size_t first_part = LibXR::min(size, length_ - head_);
146 if (data != nullptr)
147 {
148 auto tmp = reinterpret_cast<uint8_t *>(data);
149 memcpy(tmp, &queue_array_[head_ * ELEMENT_SIZE], first_part * ELEMENT_SIZE);
150 if (size > first_part)
151 {
152 memcpy(&tmp[first_part * ELEMENT_SIZE], queue_array_,
153 (size - first_part) * ELEMENT_SIZE);
154 }
155 }
156
157 head_ = (head_ + size) % length_;
158 return ErrorCode::OK;
159}

◆ Push()

ErrorCode BaseQueue::Push ( const void * data)

向队列中添加一个元素 (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 34 of file queue.cpp.

35{
36 ASSERT(data != nullptr);
37
38 if (is_full_)
39 {
40 return ErrorCode::FULL;
41 }
42
44
45 tail_ = (tail_ + 1) % length_;
46 if (head_ == tail_)
47 {
48 is_full_ = true;
49 }
50
51 return ErrorCode::OK;
52}

◆ PushBatch()

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

批量推入多个元素 (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 103 of file queue.cpp.

104{
105 ASSERT(data != nullptr);
106
107 auto avail = EmptySize();
108 if (avail < size)
109 {
110 return ErrorCode::FULL;
111 }
112
113 auto tmp = reinterpret_cast<const uint8_t *>(data);
114
115 size_t first_part = LibXR::min(size, length_ - tail_);
116 memcpy(&queue_array_[tail_ * ELEMENT_SIZE], tmp, first_part * ELEMENT_SIZE);
117
118 if (size > first_part)
119 {
120 memcpy(queue_array_, &tmp[first_part * ELEMENT_SIZE],
121 (size - first_part) * ELEMENT_SIZE);
122 }
123
124 tail_ = (tail_ + size) % length_;
125 if (head_ == tail_)
126 {
127 is_full_ = true;
128 }
129 return ErrorCode::OK;
130}
size_t EmptySize() const
获取队列的空闲空间 (Get the available space in the queue).
Definition queue.cpp:226

◆ Reset()

void BaseQueue::Reset ( )

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

Definition at line 204 of file queue.cpp.

205{
206 head_ = tail_ = 0;
207 is_full_ = false;
208}

◆ Size()

size_t BaseQueue::Size ( ) const
nodiscard

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

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

Definition at line 210 of file queue.cpp.

211{
212 if (is_full_)
213 {
214 return length_;
215 }
216 else if (tail_ >= head_)
217 {
218 return tail_ - head_;
219 }
220 else
221 {
222 return length_ + tail_ - head_;
223 }
224}

Field Documentation

◆ ELEMENT_SIZE

const uint16_t LibXR::BaseQueue::ELEMENT_SIZE

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

Definition at line 150 of file queue.hpp.

◆ head_

size_t LibXR::BaseQueue::head_ = 0

头部索引 (Head index).

Definition at line 151 of file queue.hpp.

◆ is_full_

bool LibXR::BaseQueue::is_full_ = false

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

Definition at line 153 of file queue.hpp.

◆ length_

size_t LibXR::BaseQueue::length_

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

Definition at line 154 of file queue.hpp.

◆ own_buffer_

bool LibXR::BaseQueue::own_buffer_ = false

是否由队列自己管理缓冲区 (Owns buffer memory).

Definition at line 155 of file queue.hpp.

◆ queue_array_

uint8_t* LibXR::BaseQueue::queue_array_

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

Definition at line 149 of file queue.hpp.

◆ tail_

size_t LibXR::BaseQueue::tail_ = 0

尾部索引 (Tail index).

Definition at line 152 of file queue.hpp.


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