@@ -20,13 +20,10 @@ class CircularBuffer {
2020 typedef const T& const_reference;
2121 typedef size_t size_type;
2222 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+
2725
2826public:
29-
3027 explicit CircularBuffer (size_t size)
3128 :_buff{std::unique_ptr<T[]>(new value_type[size])}, _max_size{size}{}
3229
@@ -104,10 +101,15 @@ class CircularBuffer {
104101 const_reference at (size_type index) const ;
105102 reference at (size_type index);
106103
104+ typedef BufferIterator<false > iterator;
105+ typedef BufferIterator<true > const_iterator;
106+
107107 iterator begin ();
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
112114private:
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
@@ -191,6 +194,7 @@ class CircularBuffer {
191194 rhsiter._index += n;
192195 return rhsiter;
193196 }
197+
194198
195199 BufferIterator& operator +=(difference_type n){
196200 _index += n;
@@ -207,37 +211,37 @@ class CircularBuffer {
207211 return *this ;
208212 }
209213
210- bool operator ==(const BufferIterator& other){
214+ bool operator ==(const BufferIterator& other) const {
211215 if (!_comparable (other))
212216 return false ;
213217 return ((_index == other._index )&&(_offset == other._offset ));
214218 }
215219
216- bool operator !=(const BufferIterator& other){
220+ bool operator !=(const BufferIterator& other) const {
217221 if (!_comparable (other))
218222 return true ;
219223 return ((_index != other._index )||(_offset != other._offset ));
220224 }
221225
222- bool operator <(const BufferIterator& other){
226+ bool operator <(const BufferIterator& other) const {
223227 if (!_comparable (other))
224228 return false ;
225229 return ((_index + _offset)<(other._index +other._offset ));
226230 }
227231
228- bool operator >(const BufferIterator& other){
232+ bool operator >(const BufferIterator& other) const {
229233 if (!_comparable (other))
230234 return false ;
231235 return ((_index + _offset)>(other._index +other._offset ));
232236 }
233237
234- bool operator <=(const BufferIterator& other){
238+ bool operator <=(const BufferIterator& other) const {
235239 if (!_comparable (other))
236240 return false ;
237241 return ((_index + _offset)<=(other._index +other._offset ));
238242 }
239243
240- bool operator >=(const BufferIterator& other){
244+ bool operator >=(const BufferIterator& other) const {
241245 if (!_comparable (other))
242246 return false ;
243247 return ((_index + _offset)>=(other._index +other._offset ));
@@ -268,7 +272,7 @@ template<typename T>
268272inline
269273void CircularBuffer<T>::clear(){
270274 std::lock_guard<std::mutex> _lck (_mtx);
271- _head = _tail = _size = _max_size = 0 ;
275+ _head = _tail = _size = 0 ;
272276}
273277
274278template <typename T>
@@ -363,8 +367,8 @@ template<typename T>
363367inline
364368typename CircularBuffer<T>::reference CircularBuffer<T>::operator [](size_t index) {
365369 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 " );
368372 index += _tail;
369373 index %= _max_size;
370374 return _buff[index];
@@ -374,8 +378,8 @@ template<typename T>
374378inline
375379typename CircularBuffer<T>::const_reference CircularBuffer<T>::operator [](size_t index) const {
376380 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 " );
379383 index += _tail;
380384 index %= _max_size;
381385 return _buff[index];
@@ -419,7 +423,7 @@ template<typename T>
419423inline
420424typename CircularBuffer<T>::const_iterator CircularBuffer<T>::begin() const {
421425 std::lock_guard<std::mutex> _lck (_mtx);
422- iterator iter;
426+ const_iterator iter;
423427 iter._ptrToBuffer = this ;
424428 iter._offset = _tail;
425429 iter._index = 0 ;
@@ -443,12 +447,35 @@ template<typename T>
443447inline
444448typename CircularBuffer<T>::const_iterator CircularBuffer<T>::end() const {
445449 std::lock_guard<std::mutex> _lck (_mtx);
446- iterator iter;
450+ const_iterator iter;
447451 iter._ptrToBuffer = this ;
448452 iter._offset = _tail;
449453 iter._index = _size;
450454 iter._reverse = false ;
451455 return iter;
452456}
453457
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+ }
454481#endif /* CIRCULAR_BUFFER_H */
0 commit comments