1 #include <vector> 2 3 template <class T> 4 class CircularVector 5 { 6 public: 7 explicit CircularVector(std::vector<T>& externalVec, size_t maxSizeIn) : 8 vec(externalVec), maxSize(maxSizeIn) 9 {} 10 11 template <class... Args> 12 void emplace(Args&&... args) 13 { 14 if (maxSize == 0) 15 { 16 return; 17 } 18 if (vec.size() == maxSize) 19 { 20 vec.at(idx) = T(std::forward<Args>(args)...); 21 } 22 else 23 { 24 vec.emplace_back(std::forward<Args>(args)...); 25 } 26 idx = (idx + 1) == maxSize ? 0 : (idx + 1); 27 } 28 29 void clear() 30 { 31 vec.clear(); 32 idx = 0; 33 } 34 35 void clearAndResize(size_t newMaxSize) 36 { 37 clear(); 38 maxSize = newMaxSize; 39 } 40 41 bool isFull() const 42 { 43 return vec.size() == maxSize; 44 } 45 46 typename std::vector<T>::const_iterator begin() const 47 { 48 return vec.begin(); 49 } 50 51 typename std::vector<T>::const_iterator end() const 52 { 53 return vec.end(); 54 } 55 56 private: 57 std::vector<T>& vec; 58 size_t maxSize = 0; 59 size_t idx = 0; 60 }; 61