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:
32
44
48 ~BaseQueue() { delete[] queue_array_; }
49
56 {
57 return &queue_array_[static_cast<size_t>(index * ELEMENT_SIZE)];
58 }
59
66 ErrorCode Push(const void *data)
67 {
68 ASSERT(data != nullptr);
69
70 if (is_full_)
71 {
72 return ErrorCode::FULL;
73 }
74
76
77 tail_ = (tail_ + 1) % length_;
78 if (head_ == tail_)
79 {
80 is_full_ = true;
81 }
82
83 return ErrorCode::OK;
84 }
85
92 ErrorCode Peek(void *data)
93 {
94 ASSERT(data != nullptr);
95
96 if (Size() > 0)
97 {
99 return ErrorCode::OK;
100 }
101 else
102 {
103 return ErrorCode::EMPTY;
104 }
105 }
106
114 ErrorCode Pop(void *data = nullptr)
115 {
116 if (Size() > 0)
117 {
118 if (data != nullptr)
119 {
121 }
122 head_ = (head_ + 1) % length_;
123 is_full_ = false;
124 return ErrorCode::OK;
125 }
126 else
127 {
128 return ErrorCode::EMPTY;
129 }
130 }
131
139 {
140 if (Size() > 0)
141 {
142 return static_cast<int>((tail_ + length_ - 1) % length_);
143 }
144 else
145 {
146 return -1;
147 }
148 }
149
157 {
158 if (Size() > 0)
159 {
160 return static_cast<int>(head_);
161 }
162 else
163 {
164 return -1;
165 }
166 }
167
175 ErrorCode PushBatch(const void *data, size_t size)
176 {
177 ASSERT(data != nullptr);
178
179 auto avail = EmptySize();
180 if (avail < size)
181 {
182 return ErrorCode::FULL;
183 }
184
185 auto tmp = reinterpret_cast<const uint8_t *>(data);
186
187 for (size_t i = 0; i < size; i++)
188 {
190 tail_ = (tail_ + 1) % length_;
191 }
192
193 if (head_ == tail_)
194 {
195 is_full_ = true;
196 }
197 return ErrorCode::OK;
198 }
199
208 ErrorCode PopBatch(void *data, size_t size)
209 {
210 if (Size() < size)
211 {
212 return ErrorCode::EMPTY;
213 }
214
215 if (size > 0)
216 {
217 is_full_ = false;
218 }
219 else
220 {
221 return ErrorCode::OK;
222 }
223
224 auto tmp = reinterpret_cast<uint8_t *>(data);
225
226 for (size_t i = 0; i < size; i++)
227 {
228 if (data != nullptr)
229 {
231 }
232 head_ = (head_ + 1) % length_;
233 }
234
235 return ErrorCode::OK;
236 }
237
245 ErrorCode PeekBatch(void *data, size_t size)
246 {
247 ASSERT(data != nullptr);
248
249 if (Size() < size)
250 {
251 return ErrorCode::EMPTY;
252 }
253
254 auto index = head_;
255
256 auto tmp = reinterpret_cast<uint8_t *>(data);
257
258 for (size_t i = 0; i < size; i++)
259 {
262 index = (index + 1) % length_;
263 }
264
265 return ErrorCode::OK;
266 }
267
274 ErrorCode Overwrite(const void *data)
275 {
276 ASSERT(data != nullptr);
277
278 head_ = tail_ = 0;
279 is_full_ = false;
280
282
283 tail_ = (tail_ + 1) % length_;
284 if (head_ == tail_)
285 {
286 is_full_ = true;
287 }
288
289 return ErrorCode::OK;
290 }
291
295 void Reset()
296 {
297 head_ = tail_ = 0;
298 is_full_ = false;
299 }
300
305 size_t Size()
306 {
307 if (is_full_)
308 {
309 return length_;
310 }
311 else if (tail_ >= head_)
312 {
313 return tail_ - head_;
314 }
315 else
316 {
317 return length_ + tail_ - head_;
318 }
319 }
320
325 size_t EmptySize() { return length_ - Size(); }
326
327 BaseQueue(const BaseQueue &) = delete;
328 BaseQueue operator=(const BaseQueue &) = delete;
329 BaseQueue operator=(BaseQueue &) = delete;
330 BaseQueue operator=(const BaseQueue &&) = delete;
331 BaseQueue operator=(BaseQueue &&) = delete;
332
335 size_t head_ = 0;
336 size_t tail_ = 0;
337 bool is_full_ = false;
338 size_t length_;
339};
340
352template <typename Data>
353class Queue : public BaseQueue
354{
355 public:
362
370
386 {
387 if (index >= 0)
388 {
389 index = (head_ + index) % length_;
390 }
391 else
392 {
393 index = (tail_ + index + length_) % length_;
394 }
395
396 return *reinterpret_cast<Data *>(&queue_array_[index * ELEMENT_SIZE]);
397 }
398
407 ErrorCode Push(const Data &data) { return BaseQueue::Push(&data); }
408
417 ErrorCode Pop(Data &data) { return BaseQueue::Pop(&data); }
418
426 ErrorCode Pop() { return BaseQueue::Pop(); }
427
436 ErrorCode Peek(Data &data) { return BaseQueue::Peek(&data); }
437
447 ErrorCode PushBatch(const Data *data, size_t size)
448 {
449 return BaseQueue::PushBatch(data, size);
450 }
451
462 ErrorCode PopBatch(Data *data, size_t size) { return BaseQueue::PopBatch(data, size); }
463
474 ErrorCode PeekBatch(Data *data, size_t size)
475 {
476 return BaseQueue::PeekBatch(data, size);
477 }
478
486 ErrorCode Overwrite(const Data &data) { return BaseQueue::Overwrite(&data); }
487};
488
489} // namespace LibXR
基础队列类,提供固定大小的循环缓冲区 (Base queue class providing a fixed-size circular buffer).
Definition queue.hpp:20
ErrorCode Pop(void *data=nullptr)
移除队列头部的元素 (Pop the front element from the queue).
Definition queue.hpp:114
bool is_full_
队列是否已满 (Indicates if the queue is full).
Definition queue.hpp:337
size_t tail_
尾部索引 (Tail index).
Definition queue.hpp:336
ErrorCode Overwrite(const void *data)
覆盖队列中的数据 (Overwrite the queue with new data).
Definition queue.hpp:274
ErrorCode Push(const void *data)
向队列中添加一个元素 (Push an element into the queue).
Definition queue.hpp:66
size_t head_
头部索引 (Head index).
Definition queue.hpp:335
size_t length_
队列最大容量 (Maximum queue capacity).
Definition queue.hpp:338
size_t Size()
获取队列中的元素个数 (Get the number of elements in the queue).
Definition queue.hpp:305
ErrorCode PopBatch(void *data, size_t size)
批量移除多个元素 (Pop multiple elements from the queue).
Definition queue.hpp:208
BaseQueue(uint16_t element_size, size_t length, uint8_t *buffer)
构造函数,初始化队列 (Constructor to initialize the queue).
Definition queue.hpp:28
void Reset()
重置队列,清空所有数据 (Reset the queue and clear all data).
Definition queue.hpp:295
ErrorCode PeekBatch(void *data, size_t size)
批量获取多个元素但不移除 (Peek at multiple elements without removing them).
Definition queue.hpp:245
int GetFirstElementIndex()
获取队列中第一个元素的索引 (Get the index of the first element in the queue).
Definition queue.hpp:156
void * operator[](uint32_t index)
访问指定索引的元素 (Access an element at a specified index).
Definition queue.hpp:55
ErrorCode Peek(void *data)
获取队列头部的元素但不移除 (Peek at the front element without removing it).
Definition queue.hpp:92
BaseQueue(uint16_t element_size, size_t length)
构造函数,初始化队列 (Constructor to initialize the queue).
Definition queue.hpp:38
~BaseQueue()
析构函数,释放队列内存 (Destructor to free queue memory).
Definition queue.hpp:48
size_t EmptySize()
获取队列的空闲空间 (Get the available space in the queue).
Definition queue.hpp:325
uint8_t * queue_array_
存储队列数据的数组 (Array storing queue data).
Definition queue.hpp:333
ErrorCode PushBatch(const void *data, size_t size)
批量推入多个元素 (Push multiple elements into the queue).
Definition queue.hpp:175
const uint16_t ELEMENT_SIZE
每个元素的大小 (Size of each element).
Definition queue.hpp:334
int GetLastElementIndex()
获取队列中最后一个元素的索引 (Get the index of the last element in the queue).
Definition queue.hpp:138
基于 BaseQueue 的泛型队列模板类 (Generic queue template class based on BaseQueue).
Definition queue.hpp:354
ErrorCode PopBatch(Data *data, size_t size)
批量移除多个元素,并获取数据 (Pop multiple elements from the queue and retrieve data).
Definition queue.hpp:462
Queue(size_t length, uint8_t *buffer)
构造函数,初始化队列 (Constructor to initialize the queue).
Definition queue.hpp:369
ErrorCode PeekBatch(Data *data, size_t size)
批量查看多个元素但不移除 (Peek at multiple elements without removing them).
Definition queue.hpp:474
ErrorCode PushBatch(const Data *data, size_t size)
批量推入多个元素 (Push multiple elements into the queue).
Definition queue.hpp:447
ErrorCode Peek(Data &data)
查看队列头部的元素但不移除 (Peek at the front element without removing it).
Definition queue.hpp:436
ErrorCode Overwrite(const Data &data)
覆盖队列中的数据 (Overwrite the queue with new data).
Definition queue.hpp:486
ErrorCode Pop()
仅从队列中移除头部元素,不获取数据 (Remove the front element from the queue without retrieving data).
Definition queue.hpp:426
Queue(size_t length)
构造函数,初始化队列 (Constructor to initialize the queue).
Definition queue.hpp:361
ErrorCode Pop(Data &data)
从队列中移除头部元素,并获取该元素的数据 (Remove the front element from the queue and retrieve its data).
Definition queue.hpp:417
Data & operator[](int32_t index)
访问队列中的元素 (Access an element in the queue).
Definition queue.hpp:385
ErrorCode Push(const Data &data)
向队列中添加一个元素 (Push an element into the queue).
Definition queue.hpp:407
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值