libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
queue.hpp
1#pragma once
2
3#include <cstddef>
4#include <cstdio>
5#include <cstring>
6
7#include "libxr_def.hpp"
8
9namespace LibXR
10{
20{
21 public:
28 BaseQueue(uint16_t element_size, size_t length, uint8_t* buffer);
29
38 BaseQueue(uint16_t element_size, size_t length);
39
43 ~BaseQueue();
44
50 [[nodiscard]] void* operator[](uint32_t index);
51
58 ErrorCode Push(const void* data);
59
66 ErrorCode Peek(void* data);
67
75 ErrorCode Pop(void* data = nullptr);
76
83 int GetLastElementIndex() const;
84
91 int GetFirstElementIndex() const;
92
100 ErrorCode PushBatch(const void* data, size_t size);
101
110 ErrorCode PopBatch(void* data, size_t size);
111
119 ErrorCode PeekBatch(void* data, size_t size);
120
127 ErrorCode Overwrite(const void* data);
128
132 void Reset();
133
138 [[nodiscard]] size_t Size() const;
139
144 [[nodiscard]] size_t EmptySize() const;
145
150 [[nodiscard]] size_t MaxSize() const { return length_; }
151
152 BaseQueue(const BaseQueue&) = delete;
153 BaseQueue& operator=(const BaseQueue&) = delete;
154 BaseQueue& operator=(BaseQueue&) = delete;
155 BaseQueue& operator=(const BaseQueue&&) = delete;
156 BaseQueue& operator=(BaseQueue&&) = delete;
157
158 uint8_t* queue_array_;
159 const uint16_t ELEMENT_SIZE;
160 size_t head_ = 0;
161 size_t tail_ = 0;
162 bool is_full_ = false;
163 size_t length_;
164 bool own_buffer_ = false;
165};
166
178template <typename Data>
179class Queue : public BaseQueue
180{
181 public:
187 Queue(size_t length) : BaseQueue(sizeof(Data), length) {}
188
195 Queue(size_t length, uint8_t* buffer) : BaseQueue(sizeof(Data), length, buffer) {}
196
211 Data& operator[](int32_t index)
212 {
213 if (index >= 0)
214 {
215 index = (head_ + index) % length_;
216 }
217 else
218 {
219 index = (tail_ + index + length_) % length_;
220 }
221
222 return *reinterpret_cast<Data*>(&queue_array_[index * ELEMENT_SIZE]);
223 }
224
233 ErrorCode Push(const Data& data) { return BaseQueue::Push(&data); }
234
243 ErrorCode Pop(Data& data) { return BaseQueue::Pop(&data); }
244
252 ErrorCode Pop() { return BaseQueue::Pop(); }
253
262 ErrorCode Peek(Data& data) { return BaseQueue::Peek(&data); }
263
273 ErrorCode PushBatch(const Data* data, size_t size)
274 {
275 return BaseQueue::PushBatch(data, size);
276 }
277
288 ErrorCode PopBatch(Data* data, size_t size) { return BaseQueue::PopBatch(data, size); }
289
300 ErrorCode PeekBatch(Data* data, size_t size)
301 {
302 return BaseQueue::PeekBatch(data, size);
303 }
304
312 ErrorCode Overwrite(const Data& data) { return BaseQueue::Overwrite(&data); }
313
319 [[nodiscard]] size_t MaxSize() const { return length_; }
320};
321
322} // namespace LibXR
基础队列类,提供固定大小的循环缓冲区 (Base queue class providing a fixed-size circular buffer).
Definition queue.hpp:20
bool is_full_
队列是否已满 (Indicates if the queue is full).
Definition queue.hpp:162
size_t tail_
尾部索引 (Tail index).
Definition queue.hpp:161
ErrorCode Peek(void *data)
获取队列头部的元素但不移除 (Peek at the front element without removing it).
Definition queue.cpp:54
size_t head_
头部索引 (Head index).
Definition queue.hpp:160
size_t length_
队列最大容量 (Maximum queue capacity).
Definition queue.hpp:163
ErrorCode Overwrite(const void *data)
覆盖队列中的数据 (Overwrite the queue with new data).
Definition queue.cpp:189
ErrorCode PeekBatch(void *data, size_t size)
批量获取多个元素但不移除 (Peek at multiple elements without removing them).
Definition queue.cpp:163
void * operator[](uint32_t index)
访问指定索引的元素 (Access an element at a specified index).
Definition queue.cpp:29
size_t MaxSize() const
获取队列的最大容量 (Get the maximum capacity of the queue).
Definition queue.hpp:150
ErrorCode PushBatch(const void *data, size_t size)
批量推入多个元素 (Push multiple elements into the queue).
Definition queue.cpp:103
size_t Size() const
获取队列中的元素个数 (Get the number of elements in the queue).
Definition queue.cpp:213
BaseQueue(uint16_t element_size, size_t length, uint8_t *buffer)
构造函数,初始化队列 (Constructor to initialize the queue).
Definition queue.cpp:5
bool own_buffer_
是否由队列自己管理缓冲区 (Owns buffer memory).
Definition queue.hpp:164
~BaseQueue()
析构函数,释放队列内存 (Destructor to free queue memory).
Definition queue.cpp:21
int GetFirstElementIndex() const
获取队列中第一个元素的索引 (Get the index of the first element in the queue).
Definition queue.cpp:94
ErrorCode Push(const void *data)
向队列中添加一个元素 (Push an element into the queue).
Definition queue.cpp:34
ErrorCode Pop(void *data=nullptr)
移除队列头部的元素 (Pop the front element from the queue).
Definition queue.cpp:69
void Reset()
重置队列,清空所有数据 (Reset the queue and clear all data).
Definition queue.cpp:207
size_t EmptySize() const
获取队列的空闲空间 (Get the available space in the queue).
Definition queue.cpp:229
ErrorCode PopBatch(void *data, size_t size)
批量移除多个元素 (Pop multiple elements from the queue).
Definition queue.cpp:133
int GetLastElementIndex() const
获取队列中最后一个元素的索引 (Get the index of the last element in the queue).
Definition queue.cpp:85
uint8_t * queue_array_
存储队列数据的数组 (Array storing queue data).
Definition queue.hpp:158
const uint16_t ELEMENT_SIZE
每个元素的大小 (Size of each element).
Definition queue.hpp:159
基于 BaseQueue 的泛型队列模板类 (Generic queue template class based on BaseQueue).
Definition queue.hpp:180
ErrorCode PopBatch(Data *data, size_t size)
批量移除多个元素,并获取数据 (Pop multiple elements from the queue and retrieve data).
Definition queue.hpp:288
Queue(size_t length, uint8_t *buffer)
构造函数,初始化队列 (Constructor to initialize the queue).
Definition queue.hpp:195
ErrorCode PeekBatch(Data *data, size_t size)
批量查看多个元素但不移除 (Peek at multiple elements without removing them).
Definition queue.hpp:300
ErrorCode PushBatch(const Data *data, size_t size)
批量推入多个元素 (Push multiple elements into the queue).
Definition queue.hpp:273
ErrorCode Peek(Data &data)
查看队列头部的元素但不移除 (Peek at the front element without removing it).
Definition queue.hpp:262
ErrorCode Overwrite(const Data &data)
覆盖队列中的数据 (Overwrite the queue with new data).
Definition queue.hpp:312
size_t MaxSize() const
获取队列的最大容量 (Get the maximum capacity of the queue).
Definition queue.hpp:319
ErrorCode Pop()
仅从队列中移除头部元素,不获取数据 (Remove the front element from the queue without retrieving data).
Definition queue.hpp:252
Queue(size_t length)
构造函数,初始化队列 (Constructor to initialize the queue).
Definition queue.hpp:187
ErrorCode Pop(Data &data)
从队列中移除头部元素,并获取该元素的数据 (Remove the front element from the queue and retrieve its data).
Definition queue.hpp:243
Data & operator[](int32_t index)
访问队列中的元素 (Access an element in the queue).
Definition queue.hpp:211
ErrorCode Push(const Data &data)
向队列中添加一个元素 (Push an element into the queue).
Definition queue.hpp:233
LibXR 命名空间
Definition ch32_can.hpp:14