-
Notifications
You must be signed in to change notification settings - Fork 1
/
CappedSizeQueue.hpp
57 lines (44 loc) · 1.08 KB
/
CappedSizeQueue.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) 2022 Nicholas Corgan
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <ThreadSafeQueue.h>
#include <atomic>
#include <cassert>
template <class T>
class CappedSizeQueue: public codepi::ThreadSafeQueue<T>
{
public:
using Base = codepi::ThreadSafeQueue<T>;
CappedSizeQueue(const size_t maxSize):
_maxSize(maxSize)
{
assert(_maxSize > 0);
}
virtual ~CappedSizeQueue(void) = default;
void enqueue(T t) override
{
assert(Base::size() <= _maxSize);
if(Base::size() == _maxSize)
{
(void)Base::dequeue();
_overflow = true;
}
Base::enqueue(std::forward<T>(t));
}
inline bool dequeue(double timeout_sec, T &rVal) override
{
_overflow = false;
return Base::dequeue(timeout_sec, rVal);
}
inline bool overflow(void) const noexcept
{
return _overflow;
}
inline void resetOverflow(void) noexcept
{
_overflow = false;
}
private:
size_t _maxSize{0};
std::atomic_bool _overflow{false};
};