libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
basic_queue.hpp
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
6#include "queue_base.hpp"
7#include "queue_typed_base.hpp"
8
9namespace LibXR
10{
23template <typename Data>
24class Queue final : public QueueTypedBase<Queue<Data>, Data>, public QueueBase
25{
26 public:
27 using ValueType = Data;
32
40 explicit Queue(size_t length) : QueueBase(sizeof(Data), length) {}
41
52 Queue(size_t length, uint8_t* buffer) : QueueBase(sizeof(Data), length, buffer) {}
53
67 Data& operator[](int32_t index)
68 {
69 if (index >= 0)
70 {
71 index = (head_ + index) % length_;
72 }
73 else
74 {
75 index = (tail_ + index + length_) % length_;
76 }
77
78 return *reinterpret_cast<Data*>(&queue_array_[index * ELEMENT_SIZE]);
79 }
80
88 ErrorCode Peek(Data& data) { return QueueBase::PeekBytes(&data); }
89
98 ErrorCode PushBatch(const Data* data, size_t size)
99 {
100 return QueueBase::PushBatchBytes(data, size);
101 }
102
112 ErrorCode PopBatch(Data* data, size_t size)
113 {
114 return QueueBase::PopBatchBytes(data, size);
115 }
116
125 ErrorCode PeekBatch(Data* data, size_t size)
126 {
127 return QueueBase::PeekBatchBytes(data, size);
128 }
129
136 ErrorCode Overwrite(const Data& data) { return QueueBase::OverwriteBytes(&data); }
137
142 [[nodiscard]] size_t MaxSize() const { return QueueBase::MaxSize(); }
143};
144} // namespace LibXR
提供固定大小循环缓冲区的字节 FIFO 队列。 Byte FIFO queue providing a fixed-size circular buffer.
const uint16_t ELEMENT_SIZE
单个元素的字节数 / Byte size of one element.
uint8_t * queue_array_
队列数据缓冲区 / Queue data buffer.
ErrorCode PopBatchBytes(void *data, size_t size)
按字节批量出队多个元素 / Dequeue multiple elements by bytes.
ErrorCode OverwriteBytes(const void *data)
清空当前状态后,用一个新元素覆盖队列内容。 Reset the queue state and overwrite it with one new element.
ErrorCode PeekBatchBytes(void *data, size_t size)
按字节批量查看多个元素但不出队。 Peek multiple elements by bytes without dequeuing them.
size_t MaxSize() const
获取队列最大容量 / Get the maximum queue capacity.
ErrorCode PushBatchBytes(const void *data, size_t size)
按字节批量入队多个元素 / Enqueue multiple elements by bytes.
size_t length_
队列最大容量 / Maximum queue capacity.
size_t head_
当前队头物理槽位下标 / Physical slot index of the current head.
ErrorCode PeekBytes(void *data)
按字节查看队头元素但不出队。 Peek the front element by bytes without dequeuing it.
基于 QueueBase 的泛型队列模板类。 Generic queue template class based on QueueBase.
ErrorCode PopBatch(Data *data, size_t size)
批量移除多个元素并复制到输出数组。 Pop multiple elements and copy them into the output array.
Queue(size_t length, uint8_t *buffer)
使用外部缓冲区构造强类型队列。 Construct one typed queue with an external buffer.
ErrorCode PeekBatch(Data *data, size_t size)
批量查看多个元素但不移除 / Peek multiple elements without removing them.
ErrorCode PushBatch(const Data *data, size_t size)
批量推入多个元素 / Push multiple elements into the queue.
ErrorCode Peek(Data &data)
查看队头元素但不出队 / Peek the front element without removing it.
ErrorCode Overwrite(const Data &data)
清空当前状态后,用一个新元素覆盖队列内容。 Reset the queue state and overwrite it with one new element.
size_t MaxSize() const
获取队列最大容量 / Get the maximum queue capacity.
Queue(size_t length)
构造一个由内部缓冲区支撑的强类型队列。 Construct one typed queue backed by an internal buffer.
Data & operator[](int32_t index)
按逻辑索引访问队列中的元素 / Access an element in the queue by logical index.
强类型队列的公共薄包装 / Common thin wrapper for typed queues.
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes