OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
shawnsingh
2013/01/09 20:55:09
2013 now =)
| |
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_ | |
shawnsingh
2013/01/09 20:55:09
As I understand, we are trying to make all new fil
| |
6 #define CC_RING_BUFFER_H_ | |
7 | |
8 namespace cc { | |
9 | |
10 template<typename T, int size> | |
11 class RingBuffer { | |
12 public: | |
13 int bufferSize() const | |
14 { | |
15 return size; | |
16 } | |
17 | |
18 // n = 0 returns the oldest value and | |
19 // n = bufferSize() - 1 returns the most recent value. | |
20 T readBuffer(int n) const | |
21 { | |
22 return m_buffer[bufferIndex(m_currentIndex + n)]; | |
23 } | |
24 | |
25 protected: | |
26 explicit RingBuffer() | |
27 : m_currentIndex(0) | |
28 { | |
29 } | |
30 | |
31 void saveToBuffer(T value) | |
32 { | |
33 m_buffer[bufferIndex(m_currentIndex)] = value; | |
34 m_currentIndex++; | |
35 } | |
36 | |
37 inline int bufferIndex(int n) const | |
38 { | |
39 return (n % size + size) % size; | |
shawnsingh
2013/01/09 20:55:09
Wouldn't this be equivalent to (n+size) % size, w
| |
40 } | |
41 | |
42 T m_buffer[size]; | |
43 int m_currentIndex; | |
44 }; | |
45 | |
46 } // namespace cc | |
47 | |
48 #endif // CC_RING_BUFFER_H_ | |
OLD | NEW |