@@ -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(){
287290template <typename T>
288291inline
289292void CircularBuffer<T>::_decrement_bufferstate(){
290- // _full = false;
291293 --_size;
292294 _tail = (_tail + 1 )%_max_size;
293295}
294296
295297template <typename T>
296298inline
297299typename 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
305308template <typename T>
306309inline
307310typename 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;
0 commit comments