libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
queue_base.cpp
1#include "queue_base.hpp"
2
3#include "libxr_mem.hpp"
4
5using namespace LibXR;
6
7QueueBase::QueueBase(uint16_t element_size, size_t length, uint8_t* buffer)
8 : queue_array_(buffer),
9 ELEMENT_SIZE(element_size),
10 length_(length),
11 own_buffer_(false)
12{
13}
14
15QueueBase::QueueBase(uint16_t element_size, size_t length)
16 : queue_array_(new uint8_t[length * element_size]),
17 ELEMENT_SIZE(element_size),
18 length_(length),
19 own_buffer_(true)
20{
21}
22
24{
25 if (own_buffer_)
26 {
27 delete[] queue_array_;
28 }
29}
30
31[[nodiscard]] void* QueueBase::operator[](uint32_t index)
32{
33 return &queue_array_[static_cast<size_t>(index * ELEMENT_SIZE)];
34}
35
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}
55
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}
70
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}
86
88{
89 if (Size() == 0)
90 {
91 return -1;
92 }
93 return static_cast<int>((tail_ + length_ - 1) % length_);
94}
95
97{
98 if (Size() == 0)
99 {
100 return -1;
101 }
102 return static_cast<int>(head_);
103}
104
105ErrorCode QueueBase::PushBatchBytes(const void* data, size_t size)
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}
134
135ErrorCode QueueBase::PopBatchBytes(void* data, size_t size)
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}
164
165ErrorCode QueueBase::PeekBatchBytes(void* data, size_t size)
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}
190
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}
208
210{
211 head_ = tail_ = 0;
212 is_full_ = false;
213}
214
215[[nodiscard]] size_t QueueBase::Size() const
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}
230
231[[nodiscard]] size_t QueueBase::EmptySize() const { return length_ - Size(); }
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
~QueueBase()
析构队列。
size_t EmptySize() const
获取当前剩余空槽数。
bool is_full_
当前队列是否已满。 Whether the queue is currently full.
ErrorCode PushBytes(const void *data)
按字节入队一个元素。
const uint16_t ELEMENT_SIZE
单个元素的字节数。 Byte size of one element.
uint8_t * queue_array_
队列数据缓冲区。 Queue data buffer.
ErrorCode PopBatchBytes(void *data, size_t size)
按字节批量出队多个元素。
int GetLastElementIndex() const
获取当前最后一个已入队元素的物理槽位下标。
void Reset()
重置队列状态。
QueueBase(uint16_t element_size, size_t length, uint8_t *buffer)
使用外部缓冲区构造队列。
Definition queue_base.cpp:7
ErrorCode OverwriteBytes(const void *data)
清空当前状态后,用一个新元素覆盖队列内容。
ErrorCode PeekBatchBytes(void *data, size_t size)
按字节批量查看多个元素但不出队。
void * operator[](uint32_t index)
访问指定物理槽位的原始元素地址。
size_t Size() const
获取当前已存储元素个数。
ErrorCode PushBatchBytes(const void *data, size_t size)
按字节批量入队多个元素。
int GetFirstElementIndex() const
获取当前第一个已入队元素的物理槽位下标。
size_t length_
队列最大容量。 Maximum queue capacity.
size_t head_
当前队头物理槽位下标。 Physical slot index of the current head.
bool own_buffer_
是否由当前队列拥有缓冲区。 Whether this queue owns the buffer.
size_t tail_
下一个待写入物理槽位下标。 Physical slot index of the next enqueue position.
ErrorCode PopBytes(void *data=nullptr)
按字节出队一个元素;传空指针时仅丢弃队头。
ErrorCode PeekBytes(void *data)
按字节查看队头元素但不出队。
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
@ EMPTY
为空 | Empty
@ FULL
已满 | Full
@ OK
操作成功 | Operation successful
constexpr auto min(LeftType a, RightType b) -> std::common_type_t< LeftType, RightType >
计算两个数的最小值