00001 #ifndef FCAM_CIRCULAR_BUFFER_H
00002 #define FCAM_CIRCULAR_BUFFER_H
00003
00004 #include <stdlib.h>
00005
00006 namespace FCam {
00007
00008
00009
00010
00011
00012 template<typename T>
00013 class CircularBuffer {
00014 public:
00015 CircularBuffer(size_t s) {
00016 allocated = s;
00017 memory = new T[s];
00018 start = end = 0;
00019 }
00020
00021 ~CircularBuffer() {
00022 delete[] memory;
00023 memory = NULL;
00024 start = end = allocated = 0;
00025 }
00026
00027 T &operator[](size_t i) {
00028 return memory[(end - 1 - i + allocated) % allocated];
00029 }
00030
00031 size_t size() {
00032 if (end >= start) return end-start;
00033 else return end - start + allocated;
00034 }
00035
00036 void push(T obj) {
00037 memory[end] = obj;
00038 end++;
00039 if (end == allocated) end = 0;
00040
00041
00042 if (end == start) {
00043 start++;
00044 if (start == allocated) start = 0;
00045 }
00046 }
00047
00048
00049 private:
00050 size_t start, end;
00051 size_t allocated;
00052 T *memory;
00053 };
00054
00055 }
00056
00057 #endif