Skip to content

Commit 96a59b8

Browse files
committed
fixed bug with constant iterator
1 parent 78b49d8 commit 96a59b8

File tree

2 files changed

+75
-9
lines changed

2 files changed

+75
-9
lines changed

circular_buffer.h

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ class CircularBuffer {
108108
const_iterator begin() const;
109109
iterator end();
110110
const_iterator end() const;
111+
const_iterator cbegin() const noexcept;
112+
const_iterator cend() const noexcept;
111113

112114
private:
113115
void _increment_bufferstate();
@@ -127,14 +129,15 @@ class CircularBuffer {
127129
typedef std::random_access_iterator_tag iterator_type;
128130
typedef typename std::conditional<isConst, const value_type&, value_type&>::type reference;
129131
typedef typename std::conditional<isConst, const value_type*, value_type*>::type pointer;
130-
typedef CircularBuffer* cbuf_pointer;
132+
typedef typename std::conditional<isConst, const CircularBuffer<value_type>*,
133+
CircularBuffer<value_type>*>::type cbuf_pointer;
131134

132135
cbuf_pointer _ptrToBuffer;
133136
size_type _offset;
134137
size_type _index;
135138
bool _reverse;
136139

137-
bool _comparable(const BufferIterator& other){
140+
bool _comparable(const BufferIterator<isConst>& other) const{
138141
return (_ptrToBuffer == other._ptrToBuffer)&&(_reverse == other._reverse);
139142
}
140143

@@ -208,37 +211,37 @@ class CircularBuffer {
208211
return *this;
209212
}
210213

211-
bool operator==(const BufferIterator& other){
214+
bool operator==(const BufferIterator& other) const{
212215
if (!_comparable(other))
213216
return false;
214217
return ((_index == other._index)&&(_offset == other._offset));
215218
}
216219

217-
bool operator!=(const BufferIterator& other){
220+
bool operator!=(const BufferIterator& other) const{
218221
if (!_comparable(other))
219222
return true;
220223
return ((_index != other._index)||(_offset != other._offset));
221224
}
222225

223-
bool operator<(const BufferIterator& other){
226+
bool operator<(const BufferIterator& other) const {
224227
if (!_comparable(other))
225228
return false;
226229
return ((_index + _offset)<(other._index+other._offset));
227230
}
228231

229-
bool operator>(const BufferIterator& other){
232+
bool operator>(const BufferIterator& other) const{
230233
if (!_comparable(other))
231234
return false;
232235
return ((_index + _offset)>(other._index+other._offset));
233236
}
234237

235-
bool operator<=(const BufferIterator& other){
238+
bool operator<=(const BufferIterator& other) const {
236239
if (!_comparable(other))
237240
return false;
238241
return ((_index + _offset)<=(other._index+other._offset));
239242
}
240243

241-
bool operator>=(const BufferIterator& other){
244+
bool operator>=(const BufferIterator& other) const {
242245
if (!_comparable(other))
243246
return false;
244247
return ((_index + _offset)>=(other._index+other._offset));
@@ -452,4 +455,27 @@ typename CircularBuffer<T>::const_iterator CircularBuffer<T>::end() const{
452455
return iter;
453456
}
454457

458+
template<typename T>
459+
inline
460+
typename CircularBuffer<T>::const_iterator CircularBuffer<T>::cbegin() const noexcept{
461+
std::lock_guard<std::mutex> _lck(_mtx);
462+
const_iterator iter;
463+
iter._ptrToBuffer = this;
464+
iter._offset = _tail;
465+
iter._index = 0;
466+
iter._reverse = false;
467+
return iter;
468+
}
469+
470+
template<typename T>
471+
inline
472+
typename CircularBuffer<T>::const_iterator CircularBuffer<T>::cend() const noexcept{
473+
std::lock_guard<std::mutex> _lck(_mtx);
474+
const_iterator iter;
475+
iter._ptrToBuffer = this;
476+
iter._offset = _tail;
477+
iter._index = _size;
478+
iter._reverse = false;
479+
return iter;
480+
}
455481
#endif /* CIRCULAR_BUFFER_H */

tests/string_buffer.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,25 @@ TEST_F(CircularBufferTest, BeginIteratorTest){
316316
}
317317
}
318318

319+
TEST_F(CircularBufferTest, CbeginIteratorTest){
320+
//create full buffer
321+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
322+
test_buff.push_back("string" + std::to_string(i));
323+
//test first element with iterator
324+
CircularBuffer<std::string>::const_iterator const_it = test_buff.cbegin();
325+
EXPECT_EQ(*const_it, "string0" );
326+
//access with begin iterator
327+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
328+
EXPECT_EQ(*(const_it++), "string" + std::to_string(i));
329+
330+
try {
331+
std::string out_of_bound = *(const_it);
332+
FAIL() << "Expected std::out_of_range error";
333+
}
334+
catch(const std::out_of_range& err){
335+
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
336+
}
337+
}
319338

320339
TEST_F(CircularBufferTest, EndIteratorTest){
321340
//create full buffer
@@ -348,9 +367,28 @@ TEST_F(CircularBufferTest, EndIteratorTest){
348367
catch(const std::out_of_range& err){
349368
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
350369
}
351-
352370
}
353371

372+
TEST_F(CircularBufferTest, CendIteratorTest){
373+
//create full buffer
374+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
375+
test_buff.push_back("string" + std::to_string(i));
376+
377+
CircularBuffer<std::string>::const_iterator const_it = test_buff.cend();
378+
//access with end iterator
379+
for(int i = TEST_BUFFER_SIZE-1; i>=0; i--)
380+
EXPECT_EQ(*(--const_it), "string" + std::to_string(i));
381+
382+
try {
383+
std::string out_of_bound = *(--const_it);
384+
FAIL() << "Expected std::out_of_range error";
385+
}
386+
catch(const std::out_of_range& err){
387+
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
388+
}
389+
}
390+
391+
354392
TEST_F(CircularBufferTest, IteratorBasedLoopTest){
355393
//create full buffer
356394
for(int i=0; i<TEST_BUFFER_SIZE; i++)
@@ -368,6 +406,7 @@ TEST_F(CircularBufferTest, IteratorBasedLoopTest){
368406
EXPECT_EQ(*it, "string" + std::to_string(i++));
369407
//test size with increment variable
370408
EXPECT_EQ(i, TEST_BUFFER_SIZE/2);
409+
371410

372411
}
373412

@@ -412,3 +451,4 @@ TEST_F(CircularBufferTest, RangeBasedLoopTest){
412451

413452

414453

454+

0 commit comments

Comments
 (0)