Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update circular_buffer.h #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions circular_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class CircularBuffer {
void push_back(const value_type& data);
void push_back(value_type&& data) noexcept;
void pop_front();
void pop_back();
reference front();
reference back();
const_reference front() const;
Expand Down Expand Up @@ -120,6 +121,7 @@ class CircularBuffer {
private:
void _increment_bufferstate();
void _decrement_bufferstate();
void _decrement_bufferstate_r();
mutable std::mutex _mtx;
std::unique_ptr<value_type[]> _buff;
size_type _head = 0;
Expand Down Expand Up @@ -369,13 +371,31 @@ void CircularBuffer<T>::pop_front(){
_decrement_bufferstate();
}

template<typename T>
inline
void CircularBuffer<T>::pop_back() {
std::lock_guard<std::mutex> _lck(_mtx);
if (empty())
throw std::length_error("pop_back called on empty buffer");
_decrement_bufferstate_r();

}

template<typename T>
inline
void CircularBuffer<T>::_decrement_bufferstate(){
--_size;
_tail = (_tail + 1)%_max_size;
}

template<typename T>
inline
void CircularBuffer<T>::_decrement_bufferstate_r() {
--_size;
if (!_head) _head = _max_size;
_head--;
}

template<typename T>
inline
typename CircularBuffer<T>::reference CircularBuffer<T>::operator[](size_t index) {
Expand Down