@@ -108,6 +108,8 @@ class CircularBuffer {
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
@@ -208,37 +211,37 @@ class CircularBuffer {
208
211
return *this ;
209
212
}
210
213
211
- bool operator ==(const BufferIterator& other){
214
+ bool operator ==(const BufferIterator& other) const {
212
215
if (!_comparable (other))
213
216
return false ;
214
217
return ((_index == other._index )&&(_offset == other._offset ));
215
218
}
216
219
217
- bool operator !=(const BufferIterator& other){
220
+ bool operator !=(const BufferIterator& other) const {
218
221
if (!_comparable (other))
219
222
return true ;
220
223
return ((_index != other._index )||(_offset != other._offset ));
221
224
}
222
225
223
- bool operator <(const BufferIterator& other){
226
+ bool operator <(const BufferIterator& other) const {
224
227
if (!_comparable (other))
225
228
return false ;
226
229
return ((_index + _offset)<(other._index +other._offset ));
227
230
}
228
231
229
- bool operator >(const BufferIterator& other){
232
+ bool operator >(const BufferIterator& other) const {
230
233
if (!_comparable (other))
231
234
return false ;
232
235
return ((_index + _offset)>(other._index +other._offset ));
233
236
}
234
237
235
- bool operator <=(const BufferIterator& other){
238
+ bool operator <=(const BufferIterator& other) const {
236
239
if (!_comparable (other))
237
240
return false ;
238
241
return ((_index + _offset)<=(other._index +other._offset ));
239
242
}
240
243
241
- bool operator >=(const BufferIterator& other){
244
+ bool operator >=(const BufferIterator& other) const {
242
245
if (!_comparable (other))
243
246
return false ;
244
247
return ((_index + _offset)>=(other._index +other._offset ));
@@ -452,4 +455,27 @@ typename CircularBuffer<T>::const_iterator CircularBuffer<T>::end() const{
452
455
return iter;
453
456
}
454
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
+ }
455
481
#endif /* CIRCULAR_BUFFER_H */
0 commit comments