From 0d61761de3b02e8008f8a7cae4e07346d7d12404 Mon Sep 17 00:00:00 2001 From: vinit Date: Wed, 28 Apr 2021 20:12:47 +0200 Subject: [PATCH 1/3] added move constructor and move assignment operator --- circular_buffer.h | 60 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/circular_buffer.h b/circular_buffer.h index 0ce1fcd..538ae84 100644 --- a/circular_buffer.h +++ b/circular_buffer.h @@ -7,6 +7,7 @@ #include #include #include +#include template class CircularBuffer { @@ -30,20 +31,56 @@ class CircularBuffer { :_buff{std::unique_ptr(new T[size])}, _max_size{size}{} CircularBuffer(const CircularBuffer& other) - :_buff{std::unique_ptr(new T[other.capacity()])}, - _max_size{other.capacity()}, + :_buff{std::unique_ptr(new T[other._max_size])}, + _max_size{other._max_size}, _size{other._size}, _head{other._head}, - _tail{other._tail}, - _full{other._full}{ - + _tail{other._tail}{ std::copy(other.data(), other.data() + _max_size, _buff.get()); } + CircularBuffer& operator=(const CircularBuffer& other){ - _buff = std::unique_ptr(new T[other.capacity()]); - _max_size = other.capacity(); - std::copy(other.data(), other.data() + _max_size, _buff.get()); + if ( this != &other){ + _buff.reset(new T[other._max_size]); + _max_size = other._max_size; + _size = other._size; + _head = other._head; + _tail = other._tail; + std::copy(other.data(), other.data() + _max_size, _buff.get()); + } + return *this; + } + + CircularBuffer(CircularBuffer&& other) noexcept + :_buff{std::move(other._buff)}, + _max_size{other._max_size}, + _size{other._size}, + _head{other._head}, + _tail{other._tail}{ + + other._buff = nullptr; + other._max_size = 0; + other._size = 0; + other._head = 0; + other._tail = 0; + } + + + CircularBuffer& operator=(CircularBuffer&& other) noexcept{ + if ( this != &other){ + _buff = std::move(other._buff); + _max_size = other._max_size; + _size = other._size; + _head = other._head; + _tail = other._tail; + + other._buff = nullptr; + other._max_size = 0; + other._size = 0; + other._head = 0; + other._tail = 0; + } return *this; } @@ -81,10 +118,9 @@ class CircularBuffer { size_type _tail = 0; size_type _size = 0; size_type _max_size = 0; - bool _full = false; template - class BufferIterator{ + struct BufferIterator{ public: typedef std::random_access_iterator_tag iterator_type; @@ -212,14 +248,12 @@ class CircularBuffer { template inline bool CircularBuffer::full() const{ - //return _full; return _size == _max_size; } template inline bool CircularBuffer::empty() const{ - //return ((!full()) &&(_head == _tail)); return _size == 0; } @@ -258,7 +292,6 @@ typename CircularBuffer::reference CircularBuffer::back() { std::lock_guard _lck(_mtx); if(empty()) throw std::length_error("back function called on empty buffer"); - //return _buff[_head - 1]; return _head == 0 ? _buff[_max_size - 1] : _buff[_head - 1]; } @@ -277,7 +310,6 @@ typename CircularBuffer::const_reference CircularBuffer::back() const{ std::lock_guard _lck(_mtx); if(empty()) throw std::length_error("back function called on empty buffer"); - //return _buff[_head - 1]; return _head == 0 ? _buff[_max_size - 1] : _buff[_head - 1]; } From 32686964ea376ee941e0e9441fe218f0c801150b Mon Sep 17 00:00:00 2001 From: vinit Date: Thu, 29 Apr 2021 09:28:50 +0200 Subject: [PATCH 2/3] added push back with move semantics --- circular_buffer.h | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/circular_buffer.h b/circular_buffer.h index 538ae84..03fb98d 100644 --- a/circular_buffer.h +++ b/circular_buffer.h @@ -28,10 +28,10 @@ class CircularBuffer { public: explicit CircularBuffer(size_t size) - :_buff{std::unique_ptr(new T[size])}, _max_size{size}{} + :_buff{std::unique_ptr(new value_type[size])}, _max_size{size}{} CircularBuffer(const CircularBuffer& other) - :_buff{std::unique_ptr(new T[other._max_size])}, + :_buff{std::unique_ptr(new value_type[other._max_size])}, _max_size{other._max_size}, _size{other._size}, _head{other._head}, @@ -42,7 +42,7 @@ class CircularBuffer { CircularBuffer& operator=(const CircularBuffer& other){ if ( this != &other){ - _buff.reset(new T[other._max_size]); + _buff.reset(new value_type[other._max_size]); _max_size = other._max_size; _size = other._size; _head = other._head; @@ -85,6 +85,7 @@ class CircularBuffer { } void push_back(const value_type& data); + void push_back(value_type&& data) noexcept; void pop_front(); reference front(); reference back(); @@ -95,7 +96,7 @@ class CircularBuffer { bool full() const ; size_type capacity() const ; size_type size() const; - size_type buffer_size() const {return sizeof(T)*_max_size;}; + size_type buffer_size() const {return sizeof(value_type)*_max_size;}; const_pointer data() const { return _buff.get(); } const_reference operator[](size_type index) const; @@ -112,7 +113,7 @@ class CircularBuffer { void _increment_bufferstate(); void _decrement_bufferstate(); mutable std::mutex _mtx; - std::unique_ptr _buff; + std::unique_ptr _buff; size_type _head = 0; size_type _tail = 0; @@ -124,8 +125,8 @@ class CircularBuffer { public: typedef std::random_access_iterator_tag iterator_type; - typedef typename std::conditional::type reference; - typedef typename std::conditional::type pointer; + typedef typename std::conditional::type reference; + typedef typename std::conditional::type pointer; typedef CircularBuffer* cbuf_pointer; cbuf_pointer _ptrToBuffer; @@ -313,7 +314,8 @@ typename CircularBuffer::const_reference CircularBuffer::back() const{ return _head == 0 ? _buff[_max_size - 1] : _buff[_head - 1]; } -template +template +inline void CircularBuffer::push_back(const T& data){ std::lock_guard _lck(_mtx); //if(full()) @@ -322,6 +324,15 @@ void CircularBuffer::push_back(const T& data){ _increment_bufferstate(); } +template +inline +void CircularBuffer::push_back(T&& data) noexcept{ + std::lock_guard _lck(_mtx); + _buff[_head] = std::move(data); + _increment_bufferstate(); +} + + template inline void CircularBuffer::_increment_bufferstate(){ From 5b7394b1a3b00bdf4149b5d01e558f4e14f4d990 Mon Sep 17 00:00:00 2001 From: vinit Date: Fri, 30 Apr 2021 14:53:13 +0200 Subject: [PATCH 3/3] modified samples --- samples/CMakeLists.txt | 14 ++- samples/sample_circularbuffer.cpp | 141 ------------------------------ samples/sample_custom_buffer.cpp | 75 ++++++++++++++++ samples/sample_int_buffer.cpp | 58 ++++++++++++ samples/sample_string_buffer.cpp | 81 +++++++++++++++++ samples/sample_stringbuffer.cpp | 107 ----------------------- 6 files changed, 224 insertions(+), 252 deletions(-) delete mode 100644 samples/sample_circularbuffer.cpp create mode 100644 samples/sample_custom_buffer.cpp create mode 100644 samples/sample_int_buffer.cpp create mode 100644 samples/sample_string_buffer.cpp delete mode 100644 samples/sample_stringbuffer.cpp diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 34ae026..14508c1 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -1,14 +1,20 @@ set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) -add_executable(sample_circularbuffer sample_circularbuffer.cpp) -target_link_libraries(sample_circularbuffer +add_executable(sample_int_buffer sample_int_buffer.cpp) +target_link_libraries(sample_int_buffer PRIVATE circularbuffer ) -add_executable(sample_stringbuffer sample_stringbuffer.cpp) -target_link_libraries(sample_stringbuffer +add_executable(sample_string_buffer sample_string_buffer.cpp) +target_link_libraries(sample_string_buffer + PRIVATE + circularbuffer + ) + +add_executable(sample_custom_buffer sample_custom_buffer.cpp) +target_link_libraries(sample_custom_buffer PRIVATE circularbuffer ) diff --git a/samples/sample_circularbuffer.cpp b/samples/sample_circularbuffer.cpp deleted file mode 100644 index 8244114..0000000 --- a/samples/sample_circularbuffer.cpp +++ /dev/null @@ -1,141 +0,0 @@ -#include "circular_buffer.h" -#include -#include -#include "circular_buffer.h" -#include -#include - -struct test_struct{ - static int count ; - char* bytes =nullptr; - test_struct(){ - bytes = (char*)malloc(100); - std::cout<<"constructing test_struct: "< test{5}; - //std::cout<<"size of data "< test_stringbuf{10}; - std::cout<<"Checking is buffer empty function "< test_structbuf{5}; - std::cout<<"Checking [] operator"< test_stringbufcopy{test_stringbuf}; - std::cout<<"Checking maxsize of copy buffer"< +#include +#include +#include +#include + +struct custom_struct{ + static int count ; + char* bytes =nullptr; + int id = 0; + custom_struct(){ + bytes = (char*)malloc(100); + id = count; + std::cout<<"constructing custom_struct: "< custom_buffer(5); + custom_struct element; + for (int i = 0; i < 10; ++i) { + custom_buffer.push_back(element); + } + + CircularBuffer buffermoved = std::move(custom_buffer); + CircularBuffer buffermoveassigned{10}; + buffermoveassigned = std::move(buffermoved); + buffermoveassigned.push_back(std::move(element)); + return 0; +} + diff --git a/samples/sample_int_buffer.cpp b/samples/sample_int_buffer.cpp new file mode 100644 index 0000000..3481ece --- /dev/null +++ b/samples/sample_int_buffer.cpp @@ -0,0 +1,58 @@ +#include "circular_buffer.h" +#include +#include + +int main(int argc, char *argv[]) +{ + //Initializing a buffer + CircularBuffer int_buff{5}; + + //checking buffer empty function + std::cout<<"Calling buffer empty function "< +#include +#include "circular_buffer.h" +#include +#include +#include + + +int main(int argc, char *argv[]) +{ + + CircularBuffer string_buff{10}; + //checking buffer empty function + std::cout<<"Calling buffer empty function "< string_buff_copy1{string_buff}; + std::cout<<"Buffer Copy Created, copied buffer size "< string_buff_copy2{10}; + string_buff_copy2 = string_buff; + std::cout<<"Buffer Copy assigned , copied buffer size "< string_buff_move1{std::move(string_buff)}; + std::cout<<"Buffer created with move constructor, moved buffer size "< string_buff_move2{10}; + string_buff_move2 = std::move(string_buff_move1); + std::cout<<"Buffer moved with assignment operator, moved buffer size "< -#include -#include "circular_buffer.h" -#include -#include - -int main(int argc, char *argv[]) -{ - - CircularBuffer test{5}; - //std::cout<<"size of data "< test_stringbuf{10}; - std::cout<<"Checking is buffer empty function "< test_structbuf{5}; - std::cout<<"Checking [] operator"< test_stringbufcopy{test_stringbuf}; - std::cout<<"Checking maxsize of copy buffer"<