Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 * | |
| 10 */ | |
| 11 | |
| 12 #ifndef WEBRTC_CALL_RINGBUFFER_H_ | |
| 13 #define WEBRTC_CALL_RINGBUFFER_H_ | |
| 14 | |
| 15 #include "webrtc/base/checks.h" | |
| 16 | |
| 17 namespace webrtc { | |
| 18 | |
| 19 // A RingBuffer works like a fixed size queue which starts discarding | |
| 20 // the oldest elements when it becomes full. | |
| 21 template <typename T> | |
| 22 class RingBuffer { | |
| 23 public: | |
| 24 // Creates a RingBuffer with space for |capacity| elements. | |
| 25 explicit RingBuffer(size_t capacity) { | |
| 26 RTC_DCHECK(capacity > 0); | |
| 27 data_ = new T[capacity + 1]; | |
|
stefan-webrtc
2016/03/01 09:44:16
Comment on "capacity + 1", and/or make it named va
terelius
2016/03/09 19:49:39
Done.
| |
| 28 end_ = data_ + (capacity + 1); | |
| 29 front_ = data_; | |
| 30 back_ = data_; | |
| 31 } | |
| 32 ~RingBuffer() { delete[] data_; } | |
| 33 | |
| 34 // Removes an element from the front of the queue. | |
| 35 void pop_front() { | |
| 36 RTC_DCHECK(!empty()); | |
| 37 front_++; | |
|
stefan-webrtc
2016/03/01 09:44:16
++front_
terelius
2016/03/09 19:49:39
Done. However, the preincrement form has no real a
| |
| 38 if (front_ == end_) { | |
| 39 front_ = data_; | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 // Appends an element to the back of the queue (and removes an | |
| 44 // element from the front if there is no space at the back of the queue). | |
| 45 void push_back(const T& elem) { | |
| 46 *back_ = elem; | |
| 47 back_++; | |
|
stefan-webrtc
2016/03/01 09:44:16
++back_
terelius
2016/03/09 19:49:39
Done.
| |
| 48 if (back_ == end_) { | |
| 49 back_ = data_; | |
| 50 } | |
| 51 if (back_ == front_) { | |
| 52 front_++; | |
|
stefan-webrtc
2016/03/01 09:44:16
++front_
terelius
2016/03/09 19:49:39
Done.
| |
| 53 } | |
| 54 if (front_ == end_) { | |
| 55 front_ = data_; | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 T& front() { return *front_; } | |
| 60 | |
| 61 const T& front() const { return *front_; } | |
| 62 | |
| 63 bool empty() const { return (front_ == back_); } | |
| 64 | |
| 65 private: | |
| 66 T* data_; | |
|
stefan-webrtc
2016/03/01 09:44:16
Can you make data_ a unique_ptr?
terelius
2016/03/09 19:49:39
Done.
| |
| 67 T* end_; | |
| 68 T* front_; | |
| 69 T* back_; | |
| 70 | |
| 71 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RingBuffer); | |
| 72 }; | |
| 73 | |
| 74 } // namespace webrtc | |
| 75 | |
| 76 #endif // WEBRTC_CALL_RINGBUFFER_H_ | |
| OLD | NEW |