Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: webrtc/call/ringbuffer.h

Issue 2380683005: Moved RtcEventLog files from call/ to logging/ (new top level dir) (Closed)
Patch Set: Rebase to master Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/call/mock/mock_rtc_event_log.h ('k') | webrtc/call/ringbuffer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 <memory>
16 #include <utility>
17
18 #include "webrtc/base/checks.h"
19 #include "webrtc/base/constructormagic.h"
20
21 namespace webrtc {
22
23 // A RingBuffer works like a fixed size queue which starts discarding
24 // the oldest elements when it becomes full.
25 template <typename T>
26 class RingBuffer {
27 public:
28 // Creates a RingBuffer with space for |capacity| elements.
29 explicit RingBuffer(size_t capacity)
30 : // We allocate space for one extra sentinel element.
31 data_(new T[capacity + 1]) {
32 RTC_DCHECK(capacity > 0);
33 end_ = data_.get() + (capacity + 1);
34 front_ = data_.get();
35 back_ = data_.get();
36 }
37
38 ~RingBuffer() {
39 // The unique_ptr will free the memory.
40 }
41
42 // Removes an element from the front of the queue.
43 void pop_front() {
44 RTC_DCHECK(!empty());
45 ++front_;
46 if (front_ == end_) {
47 front_ = data_.get();
48 }
49 }
50
51 // Appends an element to the back of the queue (and removes an
52 // element from the front if there is no space at the back of the queue).
53 void push_back(const T& elem) {
54 *back_ = elem;
55 ++back_;
56 if (back_ == end_) {
57 back_ = data_.get();
58 }
59 if (back_ == front_) {
60 ++front_;
61 }
62 if (front_ == end_) {
63 front_ = data_.get();
64 }
65 }
66
67 // Appends an element to the back of the queue (and removes an
68 // element from the front if there is no space at the back of the queue).
69 void push_back(T&& elem) {
70 *back_ = std::move(elem);
71 ++back_;
72 if (back_ == end_) {
73 back_ = data_.get();
74 }
75 if (back_ == front_) {
76 ++front_;
77 }
78 if (front_ == end_) {
79 front_ = data_.get();
80 }
81 }
82
83 T& front() { return *front_; }
84
85 const T& front() const { return *front_; }
86
87 bool empty() const { return (front_ == back_); }
88
89 private:
90 std::unique_ptr<T[]> data_;
91 T* end_;
92 T* front_;
93 T* back_;
94
95 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RingBuffer);
96 };
97
98 } // namespace webrtc
99
100 #endif // WEBRTC_CALL_RINGBUFFER_H_
OLDNEW
« no previous file with comments | « webrtc/call/mock/mock_rtc_event_log.h ('k') | webrtc/call/ringbuffer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698