Skip to content

Commit ae0e3f2

Browse files
committed
added at operator for circular with size checking
1 parent db27aaf commit ae0e3f2

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

circular_buffer.h

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ class CircularBuffer {
4040
size_type capacity() const ;
4141
size_type size() const;
4242
size_type buffer_size() const {return sizeof(T)*_max_size;};
43+
4344
const_reference operator[](size_type index) const;
4445
reference operator[](size_type index);
46+
const_reference at(size_type index) const;
47+
reference at(size_type index);
4548

4649
iterator begin();
4750
const_iterator begin() const;
@@ -287,16 +290,16 @@ void CircularBuffer<T>::pop_front(){
287290
template<typename T>
288291
inline
289292
void CircularBuffer<T>::_decrement_bufferstate(){
290-
//_full = false;
291293
--_size;
292294
_tail = (_tail + 1)%_max_size;
293295
}
294296

295297
template<typename T>
296298
inline
297299
typename CircularBuffer<T>::reference CircularBuffer<T>::operator[](size_t index) {
298-
if((index<0)||(index>_max_size))
299-
throw std::out_of_range("Index is out of Range of buffer size");
300+
std::lock_guard<std::mutex> _lck(_mtx);
301+
if((index<0)||(index>=_max_size))
302+
throw std::out_of_range("Index is out of Range of buffer capacity");
300303
index += _tail;
301304
index %= _max_size;
302305
return _buff[index];
@@ -305,7 +308,29 @@ typename CircularBuffer<T>::reference CircularBuffer<T>::operator[](size_t index
305308
template<typename T>
306309
inline
307310
typename CircularBuffer<T>::const_reference CircularBuffer<T>::operator[](size_t index) const {
308-
if((index<0)||(index>_max_size))
311+
std::lock_guard<std::mutex> _lck(_mtx);
312+
if((index<0)||(index>=_max_size))
313+
throw std::out_of_range("Index is out of Range of buffer capacity");
314+
index += _tail;
315+
index %= _max_size;
316+
return _buff[index];
317+
}
318+
319+
template<typename T>
320+
inline
321+
typename CircularBuffer<T>::reference CircularBuffer<T>::at(size_t index) {
322+
std::lock_guard<std::mutex> _lck(_mtx);
323+
if((index<0)||(index>=_size))
324+
throw std::out_of_range("Index is out of Range of buffer size");
325+
index += _tail;
326+
index %= _max_size;
327+
return _buff[index];
328+
}
329+
330+
template<typename T>
331+
inline
332+
typename CircularBuffer<T>::const_reference CircularBuffer<T>::at(size_t index) const {
333+
if((index<0)||(index>=_size))
309334
throw std::out_of_range("Index is out of Range of buffer size");
310335
index += _tail;
311336
index %= _max_size;

samples/sample_circularbuffer.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ struct test_struct{
3131
std::cout<<"destructing test_struct"<<--count<<"\n";
3232
free(bytes);
3333
}
34+
35+
friend std::ostream& operator<<(std::ostream& os, const test_struct& ts){
36+
return os<<"test\n";
37+
}
3438

3539

3640
};
@@ -91,13 +95,24 @@ int main(int argc, char *argv[])
9195
std::cout<<"Checking deference -- operator "<<*(--it)<<"\n";
9296

9397
CircularBuffer<test_struct> test_structbuf{5};
98+
std::cout<<"Checking [] operator"<<test_structbuf[0]<<"\n";
99+
94100
auto temp_struct = test_struct();
101+
95102
test_structbuf.push_back(temp_struct);
103+
std::cout<<"Checking [] operator"<<test_structbuf[1]<<"\n";
104+
std::cout<<"Checking at operator"<<test_structbuf.at(0)<<"\n";
105+
96106
test_structbuf.push_back(temp_struct);
107+
std::cout<<"Checking at operator"<<test_structbuf.at(1)<<"\n";
97108
test_structbuf.push_back(temp_struct);
109+
std::cout<<"Checking at operator"<<test_structbuf.at(2)<<"\n";
98110
test_structbuf.push_back(temp_struct);
111+
std::cout<<"Checking at operator"<<test_structbuf.at(3)<<"\n";
99112
test_structbuf.push_back(temp_struct);
113+
std::cout<<"Checking at operator"<<test_structbuf.at(4)<<"\n";
100114
test_structbuf.push_back(temp_struct);
115+
std::cout<<"Checking at operator"<<test_structbuf.at(0)<<"\n";
101116
test_structbuf.push_back(temp_struct);
102117
test_structbuf.push_back(temp_struct);
103118
test_structbuf.push_back(temp_struct);

0 commit comments

Comments
 (0)