blob: 84283a9f863a293c1574673425f0dda1eb295cd2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// 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_
#define CC_RING_BUFFER_H_
#include "base/logging.h"
namespace cc {
template<typename T, size_t size>
class RingBuffer {
public:
explicit RingBuffer()
: current_index_(0) {
}
size_t BufferSize() const {
return size;
}
size_t CurrentIndex() const {
return current_index_;
}
// tests if a value was saved to this index
bool IsFilledIndex(size_t n) const {
return BufferIndex(n) < current_index_;
}
// n = 0 returns the oldest value and
// n = bufferSize() - 1 returns the most recent value.
T ReadBuffer(size_t n) const {
DCHECK(IsFilledIndex(n));
return buffer_[BufferIndex(n)];
}
void SaveToBuffer(T value) {
buffer_[BufferIndex(0)] = value;
current_index_++;
}
private:
inline size_t BufferIndex(size_t n) const {
return (current_index_ + n) % size;
}
T buffer_[size];
size_t current_index_;
};
} // namespace cc
#endif // CC_RING_BUFFER_H_
|