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

基于 QueueBase 的泛型队列模板类。 More...

#include <basic_queue.hpp>

Inheritance diagram for LibXR::Queue< Data >:
[legend]
Collaboration diagram for LibXR::Queue< Data >:
[legend]

Public Types

using ValueType = Data
 
- Public Types inherited from LibXR::QueueTypedBase< Queue< Data >, Data >
using ValueType
 队列元素类型。 Queue element type.
 

Public Member Functions

 Queue (size_t length)
 构造一个由内部缓冲区支撑的强类型队列。
 
 Queue (size_t length, uint8_t *buffer)
 使用外部缓冲区构造强类型队列。
 
Data & operator[] (int32_t index)
 按逻辑索引访问队列中的元素。
 
ErrorCode Peek (Data &data)
 查看队头元素但不出队。
 
ErrorCode PushBatch (const Data *data, size_t size)
 批量推入多个元素。
 
ErrorCode PopBatch (Data *data, size_t size)
 批量移除多个元素并复制到输出数组。
 
ErrorCode PeekBatch (Data *data, size_t size)
 批量查看多个元素但不移除。
 
ErrorCode Overwrite (const Data &data)
 清空当前状态后,用一个新元素覆盖队列内容。
 
size_t MaxSize () const
 获取队列最大容量。
 
- Public Member Functions inherited from LibXR::QueueTypedBase< Queue< Data >, Data >
ErrorCode Push (const Data &item)
 推入一个强类型元素。
 
ErrorCode Pop (Data &item)
 弹出一个强类型元素。
 
ErrorCode Pop ()
 丢弃一个队头元素。
 
- Public Member Functions inherited from LibXR::QueueBase
 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
 获取队列最大容量。
 

Additional Inherited Members

- Data Fields inherited from LibXR::QueueBase
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
 
bool is_full_ = false
 当前队列是否已满。 Whether the queue is currently full.
 
size_t length_
 队列最大容量。 Maximum queue capacity.
 
bool own_buffer_
 是否由当前队列拥有缓冲区。 Whether this queue owns the buffer.
 

Detailed Description

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

基于 QueueBase 的泛型队列模板类。

Generic queue template class based on QueueBase.

该类提供一个类型安全的队列,用于存储 Data 类型的元素。 它支持标准的队列操作,如推入(Push)、弹出(Pop)、查看(Peek)以及批量操作。 This class provides a type-safe queue for storing elements of type Data. It supports standard queue operations such as push, pop, peek, and batch operations.

Template Parameters
Data队列存储的数据类型。 Type of data stored in the queue.

Definition at line 24 of file basic_queue.hpp.

Member Typedef Documentation

◆ ValueType

template<typename Data >
using LibXR::Queue< Data >::ValueType = Data

队列元素类型。 Queue element type.

Definition at line 27 of file basic_queue.hpp.

Constructor & Destructor Documentation

◆ Queue() [1/2]

template<typename Data >
LibXR::Queue< Data >::Queue ( size_t length)
inlineexplicit

构造一个由内部缓冲区支撑的强类型队列。

Construct one typed queue backed by an internal buffer.

Parameters
length队列最大容量。 Maximum queue capacity.
Note
包含动态内存分配。 Contains dynamic memory allocation.

Definition at line 40 of file basic_queue.hpp.

40: QueueBase(sizeof(Data), length) {}
QueueBase(uint16_t element_size, size_t length, uint8_t *buffer)
使用外部缓冲区构造队列。
Definition queue_base.cpp:7

◆ Queue() [2/2]

template<typename Data >
LibXR::Queue< Data >::Queue ( size_t length,
uint8_t * buffer )
inline

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

Construct one typed queue with an external buffer.

Parameters
length队列最大容量。 Maximum queue capacity.
buffer外部缓冲区指针。 Pointer to the external buffer.
Note
调用方负责保证外部缓冲区至少能容纳 length * sizeof(Data) 字节。 The caller must ensure that the external buffer can hold at least length * sizeof(Data) bytes.

Definition at line 52 of file basic_queue.hpp.

52: QueueBase(sizeof(Data), length, buffer) {}

Member Function Documentation

◆ MaxSize()

template<typename Data >
size_t LibXR::Queue< Data >::MaxSize ( ) const
inlinenodiscard

获取队列最大容量。

Get the maximum queue capacity.

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

Definition at line 147 of file basic_queue.hpp.

147{ return QueueBase::MaxSize(); }
size_t MaxSize() const
获取队列最大容量。

◆ operator[]()

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

按逻辑索引访问队列中的元素。

Access an element in the queue by logical index.

该函数允许使用索引访问队列中的元素。 如果索引为正,则表示从 head_(队列头部)开始的偏移量。 如果索引为负,则表示从 tail_(队列尾部)开始的偏移量。 This function provides indexed access to elements in the queue. If the index is positive, it is relative to head_ (front of the queue). If negative, it is relative to tail_ (end of the queue).

Parameters
index访问的逻辑索引值。 Logical index to access.
Returns
索引对应的元素引用。 Reference to the element at the specified index.

Definition at line 68 of file basic_queue.hpp.

69 {
70 if (index >= 0)
71 {
72 index = (head_ + index) % length_;
73 }
74 else
75 {
76 index = (tail_ + index + length_) % length_;
77 }
78
79 return *reinterpret_cast<Data*>(&queue_array_[index * ELEMENT_SIZE]);
80 }
const uint16_t ELEMENT_SIZE
单个元素的字节数。 Byte size of one element.
uint8_t * queue_array_
队列数据缓冲区。 Queue data buffer.
size_t length_
队列最大容量。 Maximum queue capacity.
size_t head_
当前队头物理槽位下标。 Physical slot index of the current head.

◆ Overwrite()

template<typename Data >
ErrorCode LibXR::Queue< Data >::Overwrite ( const Data & data)
inline

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

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

Parameters
data用于覆盖队列的新元素。 New element used to overwrite the queue.
Returns
成功返回 ErrorCode::OK。 Returns ErrorCode::OK on success.

Definition at line 140 of file basic_queue.hpp.

140{ return QueueBase::OverwriteBytes(&data); }
ErrorCode OverwriteBytes(const void *data)
清空当前状态后,用一个新元素覆盖队列内容。

◆ Peek()

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

查看队头元素但不出队。

Peek the front element without removing it.

Parameters
data用于接收查看结果的引用。 Reference receiving the peeked element.
Returns
成功返回 ErrorCode::OK,队列空返回 ErrorCode::EMPTY。 Returns ErrorCode::OK on success and ErrorCode::EMPTY when the queue is empty.

Definition at line 90 of file basic_queue.hpp.

90{ return QueueBase::PeekBytes(&data); }
ErrorCode PeekBytes(void *data)
按字节查看队头元素但不出队。

◆ PeekBatch()

template<typename Data >
ErrorCode LibXR::Queue< Data >::PeekBatch ( Data * data,
size_t size )
inline

批量查看多个元素但不移除。

Peek multiple elements without removing them.

Parameters
data指向输出数组的指针。 Pointer to the array receiving peeked elements.
size要查看的元素个数。 Number of elements to retrieve.
Returns
成功返回 ErrorCode::OK,元素不足返回 ErrorCode::EMPTY。 Returns ErrorCode::OK on success and ErrorCode::EMPTY when stored elements are insufficient.

Definition at line 129 of file basic_queue.hpp.

130 {
131 return QueueBase::PeekBatchBytes(data, size);
132 }
ErrorCode PeekBatchBytes(void *data, size_t size)
按字节批量查看多个元素但不出队。

◆ PopBatch()

template<typename Data >
ErrorCode LibXR::Queue< Data >::PopBatch ( Data * data,
size_t size )
inline

批量移除多个元素并复制到输出数组。

Pop multiple elements and copy them into the output array.

Parameters
data指向输出数组的指针。 Pointer to the array receiving popped elements.
size要移除的元素个数。 Number of elements to remove.
Returns
成功返回 ErrorCode::OK,元素不足返回 ErrorCode::EMPTY。 Returns ErrorCode::OK on success and ErrorCode::EMPTY when stored elements are insufficient.

Definition at line 115 of file basic_queue.hpp.

116 {
117 return QueueBase::PopBatchBytes(data, size);
118 }
ErrorCode PopBatchBytes(void *data, size_t size)
按字节批量出队多个元素。

◆ PushBatch()

template<typename Data >
ErrorCode LibXR::Queue< Data >::PushBatch ( const Data * data,
size_t size )
inline

批量推入多个元素。

Push multiple elements into the queue.

Parameters
data指向元素数组的指针。 Pointer to the array of elements to push.
size要推入的元素个数。 Number of elements to push.
Returns
成功返回 ErrorCode::OK,空间不足返回 ErrorCode::FULL。 Returns ErrorCode::OK on success and ErrorCode::FULL when free space is insufficient.

Definition at line 101 of file basic_queue.hpp.

102 {
103 return QueueBase::PushBatchBytes(data, size);
104 }
ErrorCode PushBatchBytes(const void *data, size_t size)
按字节批量入队多个元素。

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