Uniot Core
0.8.1
Loading...
Searching...
No Matches
LimitedQueue.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2023 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include "ClearQueue.h"
22
35template <typename T>
36class LimitedQueue : public ClearQueue<T> {
37 public:
44 : ClearQueue<T>(), mLimit(0), mSize(0) {}
45
51 inline size_t limit() const {
52 return mLimit;
53 }
54
60 inline size_t size() const {
61 return mSize;
62 }
63
72 void limit(size_t limit) {
73 limit = limit < 0 ? 0 : limit;
74 mLimit = limit;
75 applyLimit();
76 }
77
84 inline bool isFull() const {
85 return mSize >= mLimit;
86 }
87
93 void applyLimit() {
94 for (; mSize > mLimit; --mSize) {
96 }
97 }
98
106 void pushLimited(const T &value) {
107 ClearQueue<T>::push(value);
108 mSize++;
109 applyLimit();
110 }
111
119 T popLimited(const T &errorCode) {
120 if (mSize) {
121 mSize--;
122 }
123 return ClearQueue<T>::pop(errorCode);
124 }
125
133 size_t calcSize() const {
134 mSize = ClearQueue<T>::calcSize();
135 return mSize;
136 }
137
143 void clean() {
145 mSize = 0;
146 }
147
148 private:
149 size_t mLimit;
150 size_t mSize;
151};
152
ClearQueue(ClearQueue const &)=delete
Deleted copy constructor to prevent copying.
size_t calcSize() const
Calculate the number of elements in the queue by traversal.
Definition ClearQueue.h:305
void push(const T &value)
Adds an element to the end of the queue.
Definition ClearQueue.h:207
T pop(const T &errorCode)
Safely removes and returns the element at the front of the queue.
Definition ClearQueue.h:240
T hardPop()
Removes and returns the element at the front of the queue.
Definition ClearQueue.h:221
void clean()
Removes all elements from the queue.
Definition ClearQueue.h:314
bool isFull() const
Check if the queue has reached its size limit.
Definition LimitedQueue.h:84
void applyLimit()
Enforce the size limit by removing oldest elements if necessary.
Definition LimitedQueue.h:93
size_t calcSize() const
Recalculate the size by traversing the queue.
Definition LimitedQueue.h:133
T popLimited(const T &errorCode)
Remove and return the oldest element from the queue.
Definition LimitedQueue.h:119
void pushLimited(const T &value)
Add an element to the queue while respecting the size limit.
Definition LimitedQueue.h:106
size_t limit() const
Get the current size limit of the queue.
Definition LimitedQueue.h:51
LimitedQueue()
Construct a new LimitedQueue with default settings.
Definition LimitedQueue.h:43
void clean()
Remove all elements from the queue.
Definition LimitedQueue.h:143
size_t size() const
Get the current number of elements in the queue.
Definition LimitedQueue.h:60
void limit(size_t limit)
Set the maximum size limit for the queue.
Definition LimitedQueue.h:72