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 namespace cc { | |
9 | |
10 template<typename T, unsigned int size> | |
11 class RingBuffer { | |
12 public: | |
13 explicit RingBuffer() | |
14 : current_index_(0) { | |
15 } | |
16 | |
17 unsigned int BufferSize() const { | |
18 return size; | |
19 } | |
20 | |
21 unsigned int CurrentIndex() const { | |
22 return current_index_; | |
23 } | |
24 | |
25 // tests if a value was saved to this index | |
26 bool IsFilledIndex(unsigned int n) const { | |
shawnsingh
2013/01/10 21:13:01
I just noticed that the style guide asks us never
danakj
2013/01/10 23:31:40
What, are you sure it doesn't mean to use "unsigne
| |
27 return BufferIndex(n) < current_index_; | |
28 } | |
29 | |
30 // n = 0 returns the oldest value and | |
31 // n = bufferSize() - 1 returns the most recent value. | |
32 T ReadBuffer(unsigned int n) const { | |
33 return buffer_[BufferIndex(n)]; | |
shawnsingh
2013/01/10 21:13:01
Can we DCHECK here that n is always >= 0 and stric
| |
34 } | |
35 | |
36 void SaveToBuffer(T value) | |
37 { | |
38 buffer_[BufferIndex(0)] = value; | |
39 current_index_++; | |
40 } | |
41 | |
42 private: | |
43 inline int BufferIndex(int n) const { | |
44 return (current_index_ + n) % size; | |
45 } | |
46 | |
47 T buffer_[size]; | |
48 unsigned int current_index_; | |
49 }; | |
50 | |
51 } // namespace cc | |
52 | |
53 #endif // CC_RING_BUFFER_H_ | |
OLD | NEW |