OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CC_RING_BUFFER_H_ | |
6 #define CC_RING_BUFFER_H_ | |
7 | |
8 #include "base/logging.h" | |
9 | |
10 namespace cc { | |
11 | |
12 template<typename T, int size> | |
13 class RingBuffer { | |
14 public: | |
15 explicit RingBuffer() | |
16 : current_index_(0) { | |
17 } | |
18 | |
19 int BufferSize() const { | |
20 return size; | |
21 } | |
22 | |
23 int CurrentIndex() const { | |
24 return current_index_; | |
25 } | |
26 | |
27 // tests if a value was saved to this index | |
28 bool IsFilledIndex(int n) const { | |
29 DCHECK(n >= 0); | |
egraether
2013/01/10 23:38:29
Prevents using negative indices.
| |
30 return BufferIndex(n) < current_index_; | |
31 } | |
32 | |
33 // n = 0 returns the oldest value and | |
34 // n = bufferSize() - 1 returns the most recent value. | |
35 T ReadBuffer(int n) const { | |
36 DCHECK(IsFilledIndex(n)); | |
egraether
2013/01/10 23:38:29
Forbids access to uninitialized indices.
| |
37 return buffer_[BufferIndex(n)]; | |
38 } | |
39 | |
40 void SaveToBuffer(T value) { | |
41 buffer_[BufferIndex(0)] = value; | |
42 current_index_++; | |
43 } | |
44 | |
45 private: | |
46 inline int BufferIndex(int n) const { | |
47 return (current_index_ + n) % size; | |
48 } | |
49 | |
50 T buffer_[size]; | |
51 int current_index_; | |
52 }; | |
53 | |
54 } // namespace cc | |
55 | |
56 #endif // CC_RING_BUFFER_H_ | |
OLD | NEW |