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

Added multiple pop_front as an atomic operation #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
24 changes: 19 additions & 5 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_front(size_t count);
reference front();
reference back();
const_reference front() const;
Expand Down Expand Up @@ -119,7 +120,7 @@ class CircularBuffer {

private:
void _increment_bufferstate();
void _decrement_bufferstate();
void _decrement_bufferstate(size_t count);
mutable std::mutex _mtx;
std::unique_ptr<value_type[]> _buff;
size_type _head = 0;
Expand Down Expand Up @@ -366,14 +367,27 @@ void CircularBuffer<T>::pop_front(){
std::lock_guard<std::mutex> _lck(_mtx);
if(empty())
throw std::length_error("pop_front called on empty buffer");
_decrement_bufferstate();
_decrement_bufferstate(1);
}

template<typename T>
inline
void CircularBuffer<T>::pop_front(size_t count){
if(count > 0) {
if(empty())
throw std::length_error("pop_front called on empty buffer");
if (count > _size) {
count = _size;
}
_decrement_bufferstate(count);
}
}

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

template<typename T>
Expand Down
28 changes: 28 additions & 0 deletions tests/member_func_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,34 @@ TEST_F(CircularBufferTest, PopFrontTest){
}


TEST_F(CircularBufferTest, MultiPopFrontTest){
// push back & pop individual elements
test_buff.push_back("string1");
test_buff.push_back("string2");
test_buff.push_back("string3");
EXPECT_EQ(test_buff.size(), 3);
test_buff.pop_front(2);
EXPECT_EQ(test_buff.size(), 1);
EXPECT_EQ(test_buff.front(), "string3");

//create full buffer
for(int i=0; i<TEST_BUFFER_SIZE; i++)
test_buff.push_back("string" + std::to_string(i));
//pop all elements
test_buff.pop_front(TEST_BUFFER_SIZE);
EXPECT_TRUE(test_buff.empty());
EXPECT_EQ(test_buff.size(), 0);
//try pop on empty buffer
try {
test_buff.pop_front(10);
FAIL() << "Expected std::length_error";
}
catch(const std::length_error& err){
EXPECT_EQ(err.what(), std::string("pop_front called on empty buffer"));
}
}


TEST_F(CircularBufferTest, FrontTest){
// push back & front for individual elements
test_buff.push_back("string1");
Expand Down