Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CC_DEBUG_RING_BUFFER_H_ | 5 #ifndef CC_DEBUG_RING_BUFFER_H_ |
| 6 #define CC_DEBUG_RING_BUFFER_H_ | 6 #define CC_DEBUG_RING_BUFFER_H_ |
| 7 | 7 |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 | 9 |
| 10 namespace cc { | 10 namespace cc { |
| 11 | 11 |
| 12 template<typename T, size_t size> | 12 template<typename T, size_t kSize> |
|
danakj
2013/03/25 17:23:31
Ooh.. informative.
| |
| 13 class RingBuffer { | 13 class RingBuffer { |
| 14 public: | 14 public: |
| 15 explicit RingBuffer() | 15 explicit RingBuffer() |
| 16 : current_index_(0) { | 16 : current_index_(0) { |
| 17 } | 17 } |
| 18 | 18 |
| 19 size_t BufferSize() const { | 19 size_t BufferSize() const { |
| 20 return size; | 20 return kSize; |
| 21 } | 21 } |
| 22 | 22 |
| 23 size_t CurrentIndex() const { | 23 size_t CurrentIndex() const { |
| 24 return current_index_; | 24 return current_index_; |
| 25 } | 25 } |
| 26 | 26 |
| 27 // tests if a value was saved to this index | 27 // tests if a value was saved to this index |
| 28 bool IsFilledIndex(size_t n) const { | 28 bool IsFilledIndex(size_t n) const { |
| 29 return BufferIndex(n) < current_index_; | 29 return BufferIndex(n) < current_index_; |
| 30 } | 30 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 53 // Iterator has const access to the RingBuffer it got retrieved from. | 53 // Iterator has const access to the RingBuffer it got retrieved from. |
| 54 class Iterator { | 54 class Iterator { |
| 55 public: | 55 public: |
| 56 size_t index() const { return index_; } | 56 size_t index() const { return index_; } |
| 57 | 57 |
| 58 const T* operator->() const { return &buffer_.ReadBuffer(index_); } | 58 const T* operator->() const { return &buffer_.ReadBuffer(index_); } |
| 59 const T* operator*() const { return &buffer_.ReadBuffer(index_); } | 59 const T* operator*() const { return &buffer_.ReadBuffer(index_); } |
| 60 | 60 |
| 61 Iterator& operator++() { | 61 Iterator& operator++() { |
| 62 index_++; | 62 index_++; |
| 63 if (index_ == size) | 63 if (index_ == kSize) |
| 64 out_of_range_ = true; | 64 out_of_range_ = true; |
| 65 return *this; | 65 return *this; |
| 66 } | 66 } |
| 67 | 67 |
| 68 Iterator& operator--() { | 68 Iterator& operator--() { |
| 69 if (index_ == 0) | 69 if (index_ == 0) |
| 70 out_of_range_ = true; | 70 out_of_range_ = true; |
| 71 index_--; | 71 index_--; |
| 72 return *this; | 72 return *this; |
| 73 } | 73 } |
| 74 | 74 |
| 75 operator bool() const { | 75 operator bool() const { |
| 76 return buffer_.IsFilledIndex(index_) && !out_of_range_; | 76 return buffer_.IsFilledIndex(index_) && !out_of_range_; |
| 77 } | 77 } |
| 78 | 78 |
| 79 private: | 79 private: |
| 80 Iterator(const RingBuffer<T, size>& buffer, size_t index) | 80 Iterator(const RingBuffer<T, kSize>& buffer, size_t index) |
| 81 : buffer_(buffer), | 81 : buffer_(buffer), |
| 82 index_(index), | 82 index_(index), |
| 83 out_of_range_(false) { | 83 out_of_range_(false) { |
| 84 } | 84 } |
| 85 | 85 |
| 86 const RingBuffer<T, size>& buffer_; | 86 const RingBuffer<T, kSize>& buffer_; |
| 87 size_t index_; | 87 size_t index_; |
| 88 bool out_of_range_; | 88 bool out_of_range_; |
| 89 | 89 |
| 90 friend class RingBuffer<T, size>; | 90 friend class RingBuffer<T, kSize>; |
| 91 }; | 91 }; |
| 92 | 92 |
| 93 // Returns an Iterator pointing to the oldest value in the buffer. | 93 // Returns an Iterator pointing to the oldest value in the buffer. |
| 94 // Example usage (iterate from oldest to newest value): | 94 // Example usage (iterate from oldest to newest value): |
| 95 // for (RingBuffer<T, size>::Iterator it = ring_buffer.Begin(); it; ++it) ... | 95 // for (RingBuffer<T, kSize>::Iterator it = ring_buffer.Begin(); it; ++it) {} |
| 96 Iterator Begin() const { | 96 Iterator Begin() const { |
| 97 if (current_index_ < size) | 97 if (current_index_ < kSize) |
| 98 return Iterator(*this, size - current_index_); | 98 return Iterator(*this, kSize - current_index_); |
| 99 return Iterator(*this, 0); | 99 return Iterator(*this, 0); |
| 100 } | 100 } |
| 101 | 101 |
| 102 // Returns an Iterator pointing to the newest value in the buffer. | 102 // Returns an Iterator pointing to the newest value in the buffer. |
| 103 // Example usage (iterate backwards from newest to oldest value): | 103 // Example usage (iterate backwards from newest to oldest value): |
| 104 // for (RingBuffer<T, size>::Iterator it = ring_buffer.End(); it; --it) ... | 104 // for (RingBuffer<T, kSize>::Iterator it = ring_buffer.End(); it; --it) {} |
| 105 Iterator End() const { | 105 Iterator End() const { |
| 106 return Iterator(*this, size - 1); | 106 return Iterator(*this, kSize - 1); |
| 107 } | 107 } |
| 108 | 108 |
| 109 private: | 109 private: |
| 110 inline size_t BufferIndex(size_t n) const { | 110 inline size_t BufferIndex(size_t n) const { |
| 111 return (current_index_ + n) % size; | 111 return (current_index_ + n) % kSize; |
| 112 } | 112 } |
| 113 | 113 |
| 114 T buffer_[size]; | 114 T buffer_[kSize]; |
| 115 size_t current_index_; | 115 size_t current_index_; |
| 116 }; | 116 }; |
| 117 | 117 |
| 118 } // namespace cc | 118 } // namespace cc |
| 119 | 119 |
| 120 #endif // CC_DEBUG_RING_BUFFER_H_ | 120 #endif // CC_DEBUG_RING_BUFFER_H_ |
| OLD | NEW |