#pragma once #include template class WrappedBuffer { public: WrappedBuffer(); virtual ~WrappedBuffer(); void put(T item); void pop_back(); unsigned int size() const; std::basic_string toString() const; T* toArray(T arr[SIZE]) const; protected: T buffer[SIZE]; int curR; int curW; }; template WrappedBuffer::WrappedBuffer(): curR(0), curW(-1) { } template WrappedBuffer::~WrappedBuffer() { } template void WrappedBuffer::put(T item) { if (curW +1 == SIZE) { curR = 1; curW = 0; buffer[0] = item; } else if (curW == -1) { curR = SIZE; buffer[curW = 0] = item; } else { buffer[++curW] = item; if (curR == curW) { if (++curR > SIZE) curR = 0; } } } template void WrappedBuffer::pop_back() { unsigned int oldSize = size(); if (oldSize == 0) return; else if (oldSize == 1) { curW = -1; curR = 0; } else if (--curW < 0) { curW += SIZE; } } template unsigned int WrappedBuffer::size() const { if (curW == -1) return 0; return (curR > curW) ? (SIZE - curR + curW +1) : (curW - curR +1); } template std::basic_string WrappedBuffer::toString() const { std::basic_string result(size(), (T) 0); const unsigned int size = this->size(); int from = (curR == SIZE) ? 0 : curR; unsigned int j = 0; for (int i = from; (curW > curR && i <= curW) || (curW <= curR && i < SIZE); ++i) result[j++] = buffer[i]; for (int i = 0; j < size; ++i) result[j++] = buffer[i]; return result; } template T* WrappedBuffer::toArray(T arr[SIZE]) const { if (!arr) arr = new T[SIZE](); //TODO return arr; }