Index: webrtc/call/ringbuffer.h |
diff --git a/webrtc/call/ringbuffer.h b/webrtc/call/ringbuffer.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..628ab3009f9a730c163d70a3e4cd1ff46dc1fcd0 |
--- /dev/null |
+++ b/webrtc/call/ringbuffer.h |
@@ -0,0 +1,76 @@ |
+/* |
+ * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ * |
+ */ |
+ |
+#ifndef WEBRTC_CALL_RINGBUFFER_H_ |
+#define WEBRTC_CALL_RINGBUFFER_H_ |
+ |
+#include "webrtc/base/checks.h" |
+ |
+namespace webrtc { |
+ |
+// A RingBuffer works like a fixed size queue which starts discarding |
+// the oldest elements when it becomes full. |
+template <typename T> |
+class RingBuffer { |
+ public: |
+ // Creates a RingBuffer with space for |capacity| elements. |
+ explicit RingBuffer(size_t capacity) { |
+ RTC_DCHECK(capacity > 0); |
+ 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.
|
+ end_ = data_ + (capacity + 1); |
+ front_ = data_; |
+ back_ = data_; |
+ } |
+ ~RingBuffer() { delete[] data_; } |
+ |
+ // Removes an element from the front of the queue. |
+ void pop_front() { |
+ RTC_DCHECK(!empty()); |
+ 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
|
+ if (front_ == end_) { |
+ front_ = data_; |
+ } |
+ } |
+ |
+ // Appends an element to the back of the queue (and removes an |
+ // element from the front if there is no space at the back of the queue). |
+ void push_back(const T& elem) { |
+ *back_ = elem; |
+ back_++; |
stefan-webrtc
2016/03/01 09:44:16
++back_
terelius
2016/03/09 19:49:39
Done.
|
+ if (back_ == end_) { |
+ back_ = data_; |
+ } |
+ if (back_ == front_) { |
+ front_++; |
stefan-webrtc
2016/03/01 09:44:16
++front_
terelius
2016/03/09 19:49:39
Done.
|
+ } |
+ if (front_ == end_) { |
+ front_ = data_; |
+ } |
+ } |
+ |
+ T& front() { return *front_; } |
+ |
+ const T& front() const { return *front_; } |
+ |
+ bool empty() const { return (front_ == back_); } |
+ |
+ private: |
+ 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.
|
+ T* end_; |
+ T* front_; |
+ T* back_; |
+ |
+ RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RingBuffer); |
+}; |
+ |
+} // namespace webrtc |
+ |
+#endif // WEBRTC_CALL_RINGBUFFER_H_ |