Skip to content

Commit 522d443

Browse files
committed
Make code signal safe, fix some minor issues
Make line number incompatible with custom labels
1 parent 85de533 commit 522d443

File tree

7 files changed

+658
-406
lines changed

7 files changed

+658
-406
lines changed

bindings/buffer.hh

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,20 @@ class RingBuffer {
1111
back_index_(0),
1212
front_index_(0) {}
1313

14+
size_t capacity() const { return capacity_; }
1415
bool full() const { return size_ == capacity_; }
1516
bool empty() const { return size_ == 0; }
1617
size_t size() const { return size_; }
1718

1819
T& front() { return buffer[front_index_]; }
19-
2020
const T& front() const { return buffer[front_index_]; }
2121

22-
template <typename T2>
23-
void push_back(T2&& t) {
24-
if (full()) {
25-
if (empty()) {
26-
return;
27-
}
28-
// overwrite buffer head
29-
buffer[back_index_] = std::forward<T2>(t);
30-
increment(back_index_);
31-
// move buffer head
32-
front_index_ = back_index_;
33-
} else {
34-
buffer[back_index_] = std::forward<T2>(t);
35-
increment(back_index_);
36-
++size_;
22+
void push_back(const T& t) { push_back_(t); }
23+
void push_back(T&& t) { push_back_(std::move(t)); }
24+
25+
void clear() {
26+
while (!empty()) {
27+
pop_front();
3728
}
3829
}
3930

@@ -45,6 +36,24 @@ class RingBuffer {
4536
}
4637

4738
private:
39+
template <typename U>
40+
void push_back_(U&& t) {
41+
const bool is_full = full();
42+
43+
if (is_full && empty()) {
44+
return;
45+
}
46+
buffer[back_index_] = std::forward<U>(t);
47+
increment(back_index_);
48+
49+
if (is_full) {
50+
// move buffer head
51+
front_index_ = back_index_;
52+
} else {
53+
++size_;
54+
}
55+
}
56+
4857
void increment(size_t& idx) const {
4958
idx = idx + 1 == capacity_ ? 0 : idx + 1;
5059
}

0 commit comments

Comments
 (0)