Skip to content

Commit 67a6a80

Browse files
committed
added copy constructor
1 parent a4aeb5b commit 67a6a80

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

circular_buffer.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <memory>
77
#include <iterator>
88
#include <type_traits>
9+
#include <algorithm>
910

1011
template<typename T>
1112
class CircularBuffer {
@@ -28,6 +29,20 @@ class CircularBuffer {
2829
explicit CircularBuffer(size_t size)
2930
:_buff{std::unique_ptr<T[]>(new T[size])}, _max_size{size}{}
3031

32+
CircularBuffer(const CircularBuffer& other)
33+
:_buff{std::unique_ptr<T[]>(new T[other.capacity()])},
34+
_max_size{other.capacity()}{
35+
36+
std::copy(other.data(), other.data() + _max_size, _buff.get());
37+
}
38+
39+
CircularBuffer& operator=(const CircularBuffer& other){
40+
_buff = std::unique_ptr<T[]>(new T[other.capacity()]);
41+
_max_size = other.capacity();
42+
std::copy(other.data(), other.data() + _max_size, _buff.get());
43+
return *this;
44+
}
45+
3146
void push_back(const value_type& data);
3247
void pop_front();
3348
reference front();
@@ -40,6 +55,7 @@ class CircularBuffer {
4055
size_type capacity() const ;
4156
size_type size() const;
4257
size_type buffer_size() const {return sizeof(T)*_max_size;};
58+
const_pointer data() const { return _buff.get(); }
4359

4460
const_reference operator[](size_type index) const;
4561
reference operator[](size_type index);
@@ -56,6 +72,7 @@ class CircularBuffer {
5672
void _decrement_bufferstate();
5773
mutable std::mutex _mtx;
5874
std::unique_ptr<T[]> _buff;
75+
5976
size_type _head = 0;
6077
size_type _tail = 0;
6178
size_type _size = 0;

samples/sample_circularbuffer.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ struct test_struct{
1313
test_struct(){
1414
bytes = (char*)malloc(100);
1515
std::cout<<"constructing test_struct: "<<count++<<"\n";
16-
}
16+
}
17+
1718
test_struct(const test_struct& other){
1819
bytes = (char*)malloc(100);
1920
memcpy(bytes, other.bytes,100);
@@ -28,12 +29,11 @@ struct test_struct{
2829
}
2930

3031
~test_struct(){
31-
std::cout<<"destructing test_struct"<<--count<<"\n";
3232
free(bytes);
3333
}
3434

3535
friend std::ostream& operator<<(std::ostream& os, const test_struct& ts){
36-
return os<<"test\n";
36+
return os<<"\ntest";
3737
}
3838

3939

@@ -121,6 +121,12 @@ int main(int argc, char *argv[])
121121

122122
test_structbuf.pop_front();
123123

124+
CircularBuffer<std::string> test_stringbufcopy{test_stringbuf};
125+
std::cout<<"Checking maxsize of copy buffer"<<test_stringbufcopy.capacity()<<"\n";
126+
std::cout<<"Checking size of copy buffer"<<test_stringbufcopy.size()<<"\n";
127+
std::cout<<"Checking [] in copybuffer"<<test_stringbufcopy[1]<<"\n";
128+
std::cout<<"Checking maxsize buffer"<<test_structbuf.capacity()<<"\n";
129+
std::cout<<"Checking size buffer"<<test_structbuf.size()<<"\n";
124130

125131
return 0;
126132
}

0 commit comments

Comments
 (0)