-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixed_char_size_queue.h
131 lines (116 loc) · 3.29 KB
/
fixed_char_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* @file fixed_char_size_queue.h
* A string queue with upper limit on number of characters:
* @code
* auto buf = fssb::FixedCharSizeQueue(max_size);
* @endcode
*
* @author Sandeep <[email protected]>
* @version 1.0
*
* @section LICENSE
*
* MIT License <http://opensource.org/licenses/MIT>
*
* @section DESCRIPTION
*
* 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 <iostream> // for <<
#include <queue> // for queue
#include <string> // for string
#include <string_view> // for string_view
/// @namespace fssb
/// @brief fssb
namespace fssb {
// the string and a cache of it's length
/// @struct Element
/// @brief string and cache strlen
struct Element {
std::string str;
size_t len;
Element(std::string_view e_str, size_t e_len)
: str(e_str), len(e_len)
{ }
};
/// @class FixedCharSizeQueue
/// @brief A string queue with upper limit on number of characters:
///
/// Example usage:
/// @code{.cpp}
/// #include "fssb/fixed_char_size_queue.h"
/// int main() {
/// auto rb = FixedCharSizeQueue(10);
/// rb.push("123");
/// rb.push("456");
/// rb.pop();
/// return 0;
/// }
/// @endcode
///
///
class FixedCharSizeQueue : public std::queue<Element> {
private:
size_t free_space_ = 0;
size_t max_chars_ = 0;
//using std::queue<Element>::push_range; // disallow push_range
public:
/// @brief Constructor that creates a string buffer of fixed character size
/// @param[in] SIZE max char size of queue
explicit FixedCharSizeQueue(size_t SIZE)
: free_space_(SIZE), max_chars_(SIZE)
{
}
void push(const std::string& str); ///< @brief add string to back of queue
std::string pop(); ///< @brief remove string from front of queue
/// @brief push new entry into queue using any std::string constructor arguments
template <typename... Args>
void emplace(Args&&... args) {
push(std::string(std::forward<Args>(args)...));
}
/// @brief oldest element still in buffer
std::string front() {
return std::queue<Element>::front().str;
}
/// @brief newest element pushed into buffer
std::string back() {
return std::queue<Element>::back().str;
}
};
///
/// push first creates space in buffer by silently removing oldest
/// string in queue until there is enough space.
///
/// @param[in] str string to push into queue
/// @warning attemping to push a string larger than capacity fails
/// just a with warning message, and queue is unchanged
///
inline void FixedCharSizeQueue::push(const std::string& str)
{
// clear space for str
size_t strlen = str.length();
if (strlen > max_chars_) {
std::string msg = "string length : " + std::to_string(strlen) +
" > max size " + std::to_string(max_chars_);
std::cerr << msg << "\n";
return;
}
while (free_space_ < strlen) {
free_space_ += std::queue<Element>::front().len;
std::queue<Element>::pop();
}
std::queue<Element>::emplace(str, strlen);
free_space_ -= strlen;
}
inline std::string FixedCharSizeQueue::pop()
{
auto elem = std::queue<Element>::front();
std::queue<Element>::pop();
free_space_ += elem.len;
return elem.str;
}
} // namespace fssb