Chromium Code Reviews| Index: cc/ring_buffer.h |
| diff --git a/cc/ring_buffer.h b/cc/ring_buffer.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2de3475b930f39f7155cee933cf4ab1d61b47fea |
| --- /dev/null |
| +++ b/cc/ring_buffer.h |
| @@ -0,0 +1,48 @@ |
| +// Copyright 2012 The Chromium Authors. All rights reserved. |
|
shawnsingh
2013/01/09 20:55:09
2013 now =)
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CC_RING_BUFFER_H_ |
|
shawnsingh
2013/01/09 20:55:09
As I understand, we are trying to make all new fil
|
| +#define CC_RING_BUFFER_H_ |
| + |
| +namespace cc { |
| + |
| +template<typename T, int size> |
| +class RingBuffer { |
| +public: |
| + int bufferSize() const |
| + { |
| + return size; |
| + } |
| + |
| + // n = 0 returns the oldest value and |
| + // n = bufferSize() - 1 returns the most recent value. |
| + T readBuffer(int n) const |
| + { |
| + return m_buffer[bufferIndex(m_currentIndex + n)]; |
| + } |
| + |
| +protected: |
| + explicit RingBuffer() |
| + : m_currentIndex(0) |
| + { |
| + } |
| + |
| + void saveToBuffer(T value) |
| + { |
| + m_buffer[bufferIndex(m_currentIndex)] = value; |
| + m_currentIndex++; |
| + } |
| + |
| + inline int bufferIndex(int n) const |
| + { |
| + return (n % size + size) % size; |
|
shawnsingh
2013/01/09 20:55:09
Wouldn't this be equivalent to (n+size) % size, w
|
| + } |
| + |
| + T m_buffer[size]; |
| + int m_currentIndex; |
| +}; |
| + |
| +} // namespace cc |
| + |
| +#endif // CC_RING_BUFFER_H_ |