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.
5 #ifndef CC_DEBUG_RING_BUFFER_H_
6 #define CC_DEBUG_RING_BUFFER_H_
8 #include "base/logging.h"
12 template<typename T
, size_t kSize
>
19 size_t BufferSize() const {
23 size_t CurrentIndex() const {
24 return current_index_
;
27 // tests if a value was saved to this index
28 bool IsFilledIndex(size_t n
) const {
29 return BufferIndex(n
) < current_index_
;
32 // n = 0 returns the oldest value and
33 // n = bufferSize() - 1 returns the most recent value.
34 const T
& ReadBuffer(size_t n
) const {
35 DCHECK(IsFilledIndex(n
));
36 return buffer_
[BufferIndex(n
)];
39 T
* MutableReadBuffer(size_t n
) {
40 DCHECK(IsFilledIndex(n
));
41 return &buffer_
[BufferIndex(n
)];
44 void SaveToBuffer(const T
& value
) {
45 buffer_
[BufferIndex(0)] = value
;
53 // Iterator has const access to the RingBuffer it got retrieved from.
56 size_t index() const { return index_
; }
58 const T
* operator->() const { return &buffer_
.ReadBuffer(index_
); }
59 const T
* operator*() const { return &buffer_
.ReadBuffer(index_
); }
61 Iterator
& operator++() {
68 Iterator
& operator--() {
75 operator bool() const {
76 return buffer_
.IsFilledIndex(index_
) && !out_of_range_
;
80 Iterator(const RingBuffer
<T
, kSize
>& buffer
, size_t index
)
83 out_of_range_(false) {
86 const RingBuffer
<T
, kSize
>& buffer_
;
90 friend class RingBuffer
<T
, kSize
>;
93 // Returns an Iterator pointing to the oldest value in the buffer.
94 // Example usage (iterate from oldest to newest value):
95 // for (RingBuffer<T, kSize>::Iterator it = ring_buffer.Begin(); it; ++it) {}
96 Iterator
Begin() const {
97 if (current_index_
< kSize
)
98 return Iterator(*this, kSize
- current_index_
);
99 return Iterator(*this, 0);
102 // Returns an Iterator pointing to the newest value in the buffer.
103 // Example usage (iterate backwards from newest to oldest value):
104 // for (RingBuffer<T, kSize>::Iterator it = ring_buffer.End(); it; --it) {}
105 Iterator
End() const {
106 return Iterator(*this, kSize
- 1);
110 inline size_t BufferIndex(size_t n
) const {
111 return (current_index_
+ n
) % kSize
;
115 size_t current_index_
;
117 DISALLOW_COPY_AND_ASSIGN(RingBuffer
);
122 #endif // CC_DEBUG_RING_BUFFER_H_