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

提供固定大小循环缓冲区的字节 FIFO 队列。 More...

#include <queue_base.hpp>

Inheritance diagram for LibXR::QueueBase:
[legend]

Public Member Functions

 QueueBase (uint16_t element_size, size_t length, uint8_t *buffer)
 使用外部缓冲区构造队列。
 
 QueueBase (uint16_t element_size, size_t length)
 由队列内部申请缓冲区并构造队列。
 
 ~QueueBase ()
 析构队列。
 
void * operator[] (uint32_t index)
 访问指定物理槽位的原始元素地址。
 
ErrorCode PushBytes (const void *data)
 按字节入队一个元素。
 
ErrorCode PeekBytes (void *data)
 按字节查看队头元素但不出队。
 
ErrorCode PopBytes (void *data=nullptr)
 按字节出队一个元素;传空指针时仅丢弃队头。
 
int GetLastElementIndex () const
 获取当前最后一个已入队元素的物理槽位下标。
 
int GetFirstElementIndex () const
 获取当前第一个已入队元素的物理槽位下标。
 
ErrorCode PushBatchBytes (const void *data, size_t size)
 按字节批量入队多个元素。
 
ErrorCode PopBatchBytes (void *data, size_t size)
 按字节批量出队多个元素。
 
ErrorCode PeekBatchBytes (void *data, size_t size)
 按字节批量查看多个元素但不出队。
 
ErrorCode OverwriteBytes (const void *data)
 清空当前状态后,用一个新元素覆盖队列内容。
 
void Reset ()
 重置队列状态。
 
size_t Size () const
 获取当前已存储元素个数。
 
size_t EmptySize () const
 获取当前剩余空槽数。
 
size_t MaxSize () const
 获取队列最大容量。
 

Data Fields

uint8_t * queue_array_
 队列数据缓冲区。 Queue data buffer.
 
const uint16_t ELEMENT_SIZE
 单个元素的字节数。 Byte size of one element.
 
size_t head_ = 0
 当前队头物理槽位下标。 Physical slot index of the current head.
 
size_t tail_ = 0
 下一个待写入物理槽位下标。 Physical slot index of the next enqueue position.
 
bool is_full_ = false
 当前队列是否已满。 Whether the queue is currently full.
 
size_t length_
 队列最大容量。 Maximum queue capacity.
 
bool own_buffer_ = false
 是否由当前队列拥有缓冲区。 Whether this queue owns the buffer.
 

Private Member Functions

 QueueBase (const QueueBase &)
 禁止拷贝构造。 Non-copyable.
 
QueueBaseoperator= (const QueueBase &)
 禁止拷贝赋值。 Non-copy-assignable.
 
QueueBaseoperator= (QueueBase &)
 禁止同类型左值赋值重载。 Non-copy-assignable overload for non-const lvalues.
 
QueueBaseoperator= (const QueueBase &&)
 禁止移动赋值(const rvalue 形式)。 Non-move-assignable (const rvalue form).
 
QueueBaseoperator= (QueueBase &&)
 禁止移动赋值。 Non-move-assignable.
 

Detailed Description

提供固定大小循环缓冲区的字节 FIFO 队列。

Byte FIFO queue providing a fixed-size circular buffer.

该类只按固定元素大小搬运字节,不管理强类型对象的构造、析构或所有权。 This class moves fixed-size byte elements only and does not manage typed object construction, destruction, or ownership.

Definition at line 19 of file queue_base.hpp.

Constructor & Destructor Documentation

◆ QueueBase() [1/2]

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

使用外部缓冲区构造队列。

Construct the queue with an external buffer.

Parameters
element_size队列中每个元素的字节数。 Byte size of each queue element.
length队列最大容量。 Maximum queue capacity.
buffer外部缓冲区指针。 Pointer to the external buffer.

Definition at line 7 of file queue_base.cpp.

8 : queue_array_(buffer),
9 ELEMENT_SIZE(element_size),
10 length_(length),
11 own_buffer_(false)
12{
13}
const uint16_t ELEMENT_SIZE
单个元素的字节数。 Byte size of one element.
uint8_t * queue_array_
队列数据缓冲区。 Queue data buffer.
size_t length_
队列最大容量。 Maximum queue capacity.
bool own_buffer_
是否由当前队列拥有缓冲区。 Whether this queue owns the buffer.

◆ QueueBase() [2/2]

QueueBase::QueueBase ( uint16_t element_size,
size_t length )

由队列内部申请缓冲区并构造队列。

Construct the queue with an internally allocated buffer.

Parameters
element_size队列中每个元素的字节数。 Byte size of each queue element.
length队列最大容量。 Maximum queue capacity.
Note
包含动态内存分配。 Contains dynamic memory allocation.

Definition at line 15 of file queue_base.cpp.

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

◆ ~QueueBase()

QueueBase::~QueueBase ( )

析构队列。

Destroy the queue.

Definition at line 23 of file queue_base.cpp.

24{
25 if (own_buffer_)
26 {
27 delete[] queue_array_;
28 }
29}

Member Function Documentation

◆ EmptySize()

size_t QueueBase::EmptySize ( ) const
nodiscard

获取当前剩余空槽数。

Get the current free-slot count.

Returns
当前剩余空槽数。 Current number of free slots.

Definition at line 231 of file queue_base.cpp.

231{ return length_ - Size(); }
size_t Size() const
获取当前已存储元素个数。

◆ GetFirstElementIndex()

int QueueBase::GetFirstElementIndex ( ) const

获取当前第一个已入队元素的物理槽位下标。

Get the physical slot index of the current first queued element.

Returns
队列非空时返回第一个元素的物理槽位下标,否则返回 -1。 Returns the physical slot index of the first element, or -1 when the queue is empty.

Definition at line 96 of file queue_base.cpp.

97{
98 if (Size() == 0)
99 {
100 return -1;
101 }
102 return static_cast<int>(head_);
103}
size_t head_
当前队头物理槽位下标。 Physical slot index of the current head.

◆ GetLastElementIndex()

int QueueBase::GetLastElementIndex ( ) const

获取当前最后一个已入队元素的物理槽位下标。

Get the physical slot index of the current last queued element.

Returns
队列非空时返回最后一个元素的物理槽位下标,否则返回 -1。 Returns the physical slot index of the last element, or -1 when the queue is empty.

Definition at line 87 of file queue_base.cpp.

88{
89 if (Size() == 0)
90 {
91 return -1;
92 }
93 return static_cast<int>((tail_ + length_ - 1) % length_);
94}
size_t tail_
下一个待写入物理槽位下标。 Physical slot index of the next enqueue position.

◆ MaxSize()

size_t LibXR::QueueBase::MaxSize ( ) const
inlinenodiscard

获取队列最大容量。

Get the maximum queue capacity.

Returns
队列最大容量。 Maximum queue capacity.

Definition at line 163 of file queue_base.hpp.

163{ return length_; }

◆ operator[]()

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

访问指定物理槽位的原始元素地址。

Access the raw element address of one physical slot.

Parameters
index目标物理槽位下标。 Target physical slot index.
Returns
指向该槽位元素起始地址的指针。 Pointer to the element base address of the slot.

Definition at line 31 of file queue_base.cpp.

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

◆ OverwriteBytes()

ErrorCode QueueBase::OverwriteBytes ( const void * data)

清空当前状态后,用一个新元素覆盖队列内容。

Reset the queue state and overwrite it with one new element.

Parameters
data指向新元素的指针。 Pointer to the new element.
Returns
成功返回 ErrorCode::OK。 Returns ErrorCode::OK on success.

Definition at line 191 of file queue_base.cpp.

192{
193 ASSERT(data != nullptr);
194
195 head_ = tail_ = 0;
196 is_full_ = false;
197
199
200 tail_ = (tail_ + 1) % length_;
201 if (head_ == tail_)
202 {
203 is_full_ = true;
204 }
205
206 return ErrorCode::OK;
207}
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
bool is_full_
当前队列是否已满。 Whether the queue is currently full.
@ OK
操作成功 | Operation successful

◆ PeekBatchBytes()

ErrorCode QueueBase::PeekBatchBytes ( void * data,
size_t size )

按字节批量查看多个元素但不出队。

Peek multiple elements by bytes without dequeuing them.

Parameters
data接收查看结果的缓冲区。 Buffer receiving the peeked elements.
size要查看的元素个数。 Number of elements to peek.
Returns
成功返回 ErrorCode::OK,元素不足返回 ErrorCode::EMPTY。 Returns ErrorCode::OK on success and ErrorCode::EMPTY when stored elements are insufficient.

Definition at line 165 of file queue_base.cpp.

166{
167 ASSERT(data != nullptr);
168
169 if (Size() < size)
170 {
171 return ErrorCode::EMPTY;
172 }
173
174 auto index = head_;
175
176 auto tmp = reinterpret_cast<uint8_t*>(data);
177
178 size_t first_part = LibXR::min(size, length_ - index);
180 first_part * ELEMENT_SIZE);
181
182 if (first_part < size)
183 {
185 (size - first_part) * ELEMENT_SIZE);
186 }
187
188 return ErrorCode::OK;
189}
@ EMPTY
为空 | Empty
constexpr auto min(LeftType a, RightType b) -> std::common_type_t< LeftType, RightType >
计算两个数的最小值

◆ PeekBytes()

ErrorCode QueueBase::PeekBytes ( void * data)

按字节查看队头元素但不出队。

Peek the front element by bytes without dequeuing it.

Parameters
data接收队头元素的缓冲区。 Buffer receiving the front element.
Returns
成功返回 ErrorCode::OK,队列空返回 ErrorCode::EMPTY。 Returns ErrorCode::OK on success and ErrorCode::EMPTY when the queue is empty.

Definition at line 56 of file queue_base.cpp.

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

◆ PopBatchBytes()

ErrorCode QueueBase::PopBatchBytes ( void * data,
size_t size )

按字节批量出队多个元素。

Dequeue multiple elements by bytes.

Parameters
data接收出队元素的缓冲区;传 nullptr 时仅丢弃。 Buffer receiving dequeued elements; pass nullptr to discard only.
size要出队的元素个数。 Number of elements to dequeue.
Returns
成功返回 ErrorCode::OK,元素不足返回 ErrorCode::EMPTY。 Returns ErrorCode::OK on success and ErrorCode::EMPTY when stored elements are insufficient.

Definition at line 135 of file queue_base.cpp.

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

◆ PopBytes()

ErrorCode QueueBase::PopBytes ( void * data = nullptr)

按字节出队一个元素;传空指针时仅丢弃队头。

Dequeue one element by bytes; pass null to discard the front item only.

Parameters
data接收出队元素的缓冲区;传 nullptr 时仅丢弃。 Buffer receiving the dequeued element; pass nullptr to discard only.
Returns
成功返回 ErrorCode::OK,队列空返回 ErrorCode::EMPTY。 Returns ErrorCode::OK on success and ErrorCode::EMPTY when the queue is empty.

Definition at line 71 of file queue_base.cpp.

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

◆ PushBatchBytes()

ErrorCode QueueBase::PushBatchBytes ( const void * data,
size_t size )

按字节批量入队多个元素。

Enqueue multiple elements by bytes.

Parameters
data指向元素数组的缓冲区。 Buffer pointing to the element array.
size要入队的元素个数。 Number of elements to enqueue.
Returns
成功返回 ErrorCode::OK,空间不足返回 ErrorCode::FULL。 Returns ErrorCode::OK on success and ErrorCode::FULL when free space is insufficient.

Definition at line 105 of file queue_base.cpp.

106{
107 ASSERT(data != nullptr);
108
109 auto avail = EmptySize();
110 if (avail < size)
111 {
112 return ErrorCode::FULL;
113 }
114
115 auto tmp = reinterpret_cast<const uint8_t*>(data);
116
117 size_t first_part = LibXR::min(size, length_ - tail_);
119 first_part * ELEMENT_SIZE);
120
121 if (size > first_part)
122 {
124 (size - first_part) * ELEMENT_SIZE);
125 }
126
127 tail_ = (tail_ + size) % length_;
128 if (head_ == tail_)
129 {
130 is_full_ = true;
131 }
132 return ErrorCode::OK;
133}
size_t EmptySize() const
获取当前剩余空槽数。
@ FULL
已满 | Full

◆ PushBytes()

ErrorCode QueueBase::PushBytes ( const void * data)

按字节入队一个元素。

Enqueue one element by bytes.

Parameters
data指向待入队元素的指针。 Pointer to the element to enqueue.
Returns
成功返回 ErrorCode::OK,队列满返回 ErrorCode::FULL。 Returns ErrorCode::OK on success and ErrorCode::FULL when the queue is full.

Definition at line 36 of file queue_base.cpp.

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

◆ Reset()

void QueueBase::Reset ( )

重置队列状态。

Reset the queue state.

Definition at line 209 of file queue_base.cpp.

210{
211 head_ = tail_ = 0;
212 is_full_ = false;
213}

◆ Size()

size_t QueueBase::Size ( ) const
nodiscard

获取当前已存储元素个数。

Get the current stored element count.

Returns
当前已存储元素个数。 Current number of stored elements.

Definition at line 215 of file queue_base.cpp.

216{
217 if (is_full_)
218 {
219 return length_;
220 }
221 else if (tail_ >= head_)
222 {
223 return tail_ - head_;
224 }
225 else
226 {
227 return length_ + tail_ - head_;
228 }
229}

Field Documentation

◆ ELEMENT_SIZE

const uint16_t LibXR::QueueBase::ELEMENT_SIZE

单个元素的字节数。 Byte size of one element.

Definition at line 179 of file queue_base.hpp.

◆ head_

size_t LibXR::QueueBase::head_ = 0

当前队头物理槽位下标。 Physical slot index of the current head.

Definition at line 180 of file queue_base.hpp.

◆ is_full_

bool LibXR::QueueBase::is_full_ = false

当前队列是否已满。 Whether the queue is currently full.

Definition at line 182 of file queue_base.hpp.

◆ length_

size_t LibXR::QueueBase::length_

队列最大容量。 Maximum queue capacity.

Definition at line 183 of file queue_base.hpp.

◆ own_buffer_

bool LibXR::QueueBase::own_buffer_ = false

是否由当前队列拥有缓冲区。 Whether this queue owns the buffer.

Definition at line 184 of file queue_base.hpp.

◆ queue_array_

uint8_t* LibXR::QueueBase::queue_array_

队列数据缓冲区。 Queue data buffer.

Definition at line 178 of file queue_base.hpp.

◆ tail_

size_t LibXR::QueueBase::tail_ = 0

下一个待写入物理槽位下标。 Physical slot index of the next enqueue position.

Definition at line 181 of file queue_base.hpp.


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