• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef RINGBUFFER_H_
17 #define RINGBUFFER_H_
18 
19 #include "utils/Macros.h"
20 
21 #include <stddef.h>
22 
23 namespace android {
24 namespace uirenderer {
25 
26 template <class T, size_t SIZE>
27 class RingBuffer {
28     PREVENT_COPY_AND_ASSIGN(RingBuffer);
29 
30 public:
RingBuffer()31     RingBuffer() {}
~RingBuffer()32     ~RingBuffer() {}
33 
capacity()34     constexpr size_t capacity() const { return SIZE; }
size()35     size_t size() const { return mCount; }
36 
next()37     T& next() {
38         mHead = (mHead + 1) % SIZE;
39         if (mCount < SIZE) {
40             mCount++;
41         }
42         return mBuffer[mHead];
43     }
44 
front()45     T& front() { return (*this)[0]; }
46 
back()47     T& back() { return (*this)[size() - 1]; }
48 
49     T& operator[](size_t index) { return mBuffer[(mHead + index + 1) % mCount]; }
50 
51     const T& operator[](size_t index) const { return mBuffer[(mHead + index + 1) % mCount]; }
52 
clear()53     void clear() {
54         mCount = 0;
55         mHead = -1;
56     }
57 
58 private:
59     T mBuffer[SIZE];
60     int mHead = -1;
61     size_t mCount = 0;
62 };
63 
64 }  // namespace uirenderer
65 }  // namespace android
66 
67 #endif /* RINGBUFFER_H_ */
68