@@ -20,13 +20,10 @@ class CircularBuffer {
20
20
typedef const T& const_reference;
21
21
typedef size_t size_type;
22
22
typedef ptrdiff_t difference_type;
23
- template <bool isConst>
24
- struct BufferIterator ;
25
- typedef BufferIterator<false > iterator;
26
- typedef BufferIterator<true > const_iterator;
23
+ template <bool isConst> struct BufferIterator ;
24
+
27
25
28
26
public:
29
-
30
27
explicit CircularBuffer (size_t size)
31
28
:_buff{std::unique_ptr<T[]>(new value_type[size])}, _max_size{size}{}
32
29
@@ -104,10 +101,15 @@ class CircularBuffer {
104
101
const_reference at (size_type index) const ;
105
102
reference at (size_type index);
106
103
104
+ typedef BufferIterator<false > iterator;
105
+ typedef BufferIterator<true > const_iterator;
106
+
107
107
iterator begin ();
108
108
const_iterator begin () const ;
109
109
iterator end ();
110
110
const_iterator end () const ;
111
+ const_iterator cbegin () const noexcept ;
112
+ const_iterator cend () const noexcept ;
111
113
112
114
private:
113
115
void _increment_bufferstate ();
@@ -127,14 +129,15 @@ class CircularBuffer {
127
129
typedef std::random_access_iterator_tag iterator_type;
128
130
typedef typename std::conditional<isConst, const value_type&, value_type&>::type reference;
129
131
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;
131
134
132
135
cbuf_pointer _ptrToBuffer;
133
136
size_type _offset;
134
137
size_type _index;
135
138
bool _reverse;
136
139
137
- bool _comparable (const BufferIterator& other){
140
+ bool _comparable (const BufferIterator<isConst> & other) const {
138
141
return (_ptrToBuffer == other._ptrToBuffer )&&(_reverse == other._reverse );
139
142
}
140
143
@@ -191,6 +194,7 @@ class CircularBuffer {
191
194
rhsiter._index += n;
192
195
return rhsiter;
193
196
}
197
+
194
198
195
199
BufferIterator& operator +=(difference_type n){
196
200
_index += n;
@@ -207,37 +211,37 @@ class CircularBuffer {
207
211
return *this ;
208
212
}
209
213
210
- bool operator ==(const BufferIterator& other){
214
+ bool operator ==(const BufferIterator& other) const {
211
215
if (!_comparable (other))
212
216
return false ;
213
217
return ((_index == other._index )&&(_offset == other._offset ));
214
218
}
215
219
216
- bool operator !=(const BufferIterator& other){
220
+ bool operator !=(const BufferIterator& other) const {
217
221
if (!_comparable (other))
218
222
return true ;
219
223
return ((_index != other._index )||(_offset != other._offset ));
220
224
}
221
225
222
- bool operator <(const BufferIterator& other){
226
+ bool operator <(const BufferIterator& other) const {
223
227
if (!_comparable (other))
224
228
return false ;
225
229
return ((_index + _offset)<(other._index +other._offset ));
226
230
}
227
231
228
- bool operator >(const BufferIterator& other){
232
+ bool operator >(const BufferIterator& other) const {
229
233
if (!_comparable (other))
230
234
return false ;
231
235
return ((_index + _offset)>(other._index +other._offset ));
232
236
}
233
237
234
- bool operator <=(const BufferIterator& other){
238
+ bool operator <=(const BufferIterator& other) const {
235
239
if (!_comparable (other))
236
240
return false ;
237
241
return ((_index + _offset)<=(other._index +other._offset ));
238
242
}
239
243
240
- bool operator >=(const BufferIterator& other){
244
+ bool operator >=(const BufferIterator& other) const {
241
245
if (!_comparable (other))
242
246
return false ;
243
247
return ((_index + _offset)>=(other._index +other._offset ));
@@ -268,7 +272,7 @@ template<typename T>
268
272
inline
269
273
void CircularBuffer<T>::clear(){
270
274
std::lock_guard<std::mutex> _lck (_mtx);
271
- _head = _tail = _size = _max_size = 0 ;
275
+ _head = _tail = _size = 0 ;
272
276
}
273
277
274
278
template <typename T>
@@ -363,8 +367,8 @@ template<typename T>
363
367
inline
364
368
typename CircularBuffer<T>::reference CircularBuffer<T>::operator [](size_t index) {
365
369
std::lock_guard<std::mutex> _lck (_mtx);
366
- if ((index<0 )||(index>=_max_size ))
367
- throw std::out_of_range (" Index is out of Range of buffer capacity " );
370
+ if ((index<0 )||(index>=_size ))
371
+ throw std::out_of_range (" Index is out of Range of buffer size " );
368
372
index += _tail;
369
373
index %= _max_size;
370
374
return _buff[index];
@@ -374,8 +378,8 @@ template<typename T>
374
378
inline
375
379
typename CircularBuffer<T>::const_reference CircularBuffer<T>::operator [](size_t index) const {
376
380
std::lock_guard<std::mutex> _lck (_mtx);
377
- if ((index<0 )||(index>=_max_size ))
378
- throw std::out_of_range (" Index is out of Range of buffer capacity " );
381
+ if ((index<0 )||(index>=_size ))
382
+ throw std::out_of_range (" Index is out of Range of buffer size " );
379
383
index += _tail;
380
384
index %= _max_size;
381
385
return _buff[index];
@@ -419,7 +423,7 @@ template<typename T>
419
423
inline
420
424
typename CircularBuffer<T>::const_iterator CircularBuffer<T>::begin() const {
421
425
std::lock_guard<std::mutex> _lck (_mtx);
422
- iterator iter;
426
+ const_iterator iter;
423
427
iter._ptrToBuffer = this ;
424
428
iter._offset = _tail;
425
429
iter._index = 0 ;
@@ -443,12 +447,35 @@ template<typename T>
443
447
inline
444
448
typename CircularBuffer<T>::const_iterator CircularBuffer<T>::end() const {
445
449
std::lock_guard<std::mutex> _lck (_mtx);
446
- iterator iter;
450
+ const_iterator iter;
447
451
iter._ptrToBuffer = this ;
448
452
iter._offset = _tail;
449
453
iter._index = _size;
450
454
iter._reverse = false ;
451
455
return iter;
452
456
}
453
457
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
+ }
454
481
#endif /* CIRCULAR_BUFFER_H */
0 commit comments