-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathringbuffer.h
More file actions
167 lines (127 loc) · 3.74 KB
/
Copy pathringbuffer.h
File metadata and controls
167 lines (127 loc) · 3.74 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#ifndef RINGBUFFER_H
#define RINGBUFFER_H
#include <array>
#include <stdexcept>
#ifdef OPTIONAL_INCLUDED
#include <optional>
#endif
#ifdef QT_CORE_LIB
#include <QByteArray>
#endif
namespace {
/* S must be one of the number series of powers of 2 */
template<size_t S>
struct ring_buffer_size_validator__ {
constexpr static bool value = S > 0 && (S & (S - 1)) == 0;
};
}
template<typename T, size_t S>
class ring_buffer
{
static_assert(ring_buffer_size_validator__<S>::value, "S must be one of the number series of powers of 2");
using index_type = std::size_t;
using value_type = T;
std::array<T, S> m_storage;
index_type m_cwrite {0};
index_type m_cread {0};
const index_type m_mask = S - 1;
public:
ring_buffer()
: m_cwrite(0), m_cread(0)
{ }
const value_type* raw() const & {
return &m_storage[0];
}
/*! \brief Read element to\e value from the end of buffer and drop element, returns true if operation was successed. */
bool read(value_type & value) {
if(is_empty()) return false;
value = m_storage[m_cread++ & m_mask];
return true;
}
#ifdef OPTIONAL_INCLUDED
optional<value_type> read() {
if(!is_empty())
return m_storage[m_cread++ & m_mask];
return nullopt;
}
#endif
/*! \brief Append \e value to the end of buffer, returns true if operation was successed. */
bool write(const value_type & value) {
if(is_full()) return false;
m_storage[m_cwrite++ & m_mask] = value;
return true;
}
bool write(value_type && value) {
if(is_full()) return false;
m_storage[m_cwrite++ & m_mask] = std::move(value);
return true;
}
#ifdef QT_CORE_LIB
bool write(QByteArray && a) {
if(size() - count() < a.size())
return false;
for(auto && ch : a) {
write(ch);
}
return true;
}
#endif
bool write(const void * const p, ssize_t len) {
if(!len || (size() - count()) < len)
return false;
const value_type * ptr = (const value_type*)p;
int i = 0;
while(i < len) write(ptr[i++]);
return true;
}
/*! \brief Returns true if buffer is fulled. */
bool is_full() const noexcept
{ return ((index_type)(m_cwrite - m_cread) & (index_type)~(m_mask)) != 0; }
/*! \brief Returns true if buffer is empty. */
bool is_empty() const noexcept
{ return m_cwrite == m_cread; }
/*! \brief Returns count of elements into buffer. */
index_type count() const noexcept
{ return (m_cwrite - m_cread) & m_mask; }
/*! \brief Returns size capacity of buffer. */
constexpr size_t size() const noexcept
{ return S; }
/*! \brief Returns first element from buffer. Don't drop element. May throw OOE exception. */
value_type& first() &
{ return operator[](0); }
/*! \brief Returns last element from buffer. May throw OOE exception. */
value_type& last() &
{ return operator[](count() - 1); }
/*! \brief Returns first element from buffer. Don't drop element. May throw OOE exception. */
const value_type& first() const &
{ return operator[](0); }
/*! \brief Returns last element from buffer. May throw OOE exception. */
const value_type& last() const &
{ return operator[](count() - 1); }
/*! \brief Reset count elements. */
void clear() noexcept
{ m_cread = 0; m_cwrite = 0; }
ring_buffer& operator<<(const value_type & v)
{
write(v);
return *this;
}
ring_buffer& operator<<(value_type && v)
{
write(std::forward<value_type>(v));
return *this;
}
value_type& operator[](index_type i) &
{
if (is_empty() || i > count())
throw std::out_of_range("index located in invalid range for access ring buffer");
return m_storage[(m_cread + i) & m_mask];
}
const value_type operator[] (index_type i) const &
{
if (is_empty() || i > count())
throw std::out_of_range("index located in invalid range for access ring buffer");
return m_storage[(m_cread + i) & m_mask];
}
};
#endif // RINGBUFFER_H