-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixed_elem_size_queue.h
87 lines (76 loc) · 1.81 KB
/
fixed_elem_size_queue.h
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* @file fixed_elem_size_queue.h
* A std:queue with fixed size that you insiantiate like:
* @code
* auto q = fssb::FixedElemSizeQueue<std::string>(size);
* @endcode
*
* @author Sandeep <[email protected]>
* @version 1.0
*
* @section LICENSE
*
* MIT License <http://opensource.org/licenses/MIT>
*
* @section DESCRIPTION
*
* See discussion at:
*
* from https://stackoverflow.com/questions/56334492/c-create-fixed-size-queue
*
* https://github.com/d-e-e-p/fixed-size-string-buffer
* Copyright (c) 2023 Sandeep <[email protected]>
*
*/
#pragma once
#include <cstddef> // for size_t
#include <queue>
/// @namespace fssb
/// @brief fssb
namespace fssb {
/// @class FixedElemSizeQueue
/// @brief A templated queue with upper limit on number of elements
/// @tparam T type of element
///
/// Example usage:
/// @code{.cpp}
/// #include "fssb/fixed_elem_size_queue.h"
/// int main() {
/// auto rb = FixedElemSizeQueue<std::string>(10);
/// rb.push("123");
/// rb.push("456");
/// rb.pop();
/// return 0;
/// }
/// @endcode
///
///
template <typename T>
class FixedElemSizeQueue : public std::queue<T> {
private:
size_t free_space_ = 0;
public:
/// @brief Constructor that creates a buffer of fixed number of elements
/// @param[in] max_elements max num of elements in queue
explicit FixedElemSizeQueue(size_t max_elements)
: free_space_(max_elements)
{
}
/// @brief add elements to back of queue,
/// silently pop() if queue has reached max_elements capacity
/// @param[in] value element to add to queue
void push(const T& value) {
if (free_space_ == 0) {
pop();
}
std::queue<T>::push(value);
free_space_ --;
}
T pop() {
auto elem = this->front();
std::queue<T>::pop();
free_space_ ++;
return elem;
}
};
} // namespace fssb