Skip to content

Instantly share code, notes, and snippets.

@AndreaCrotti
Created October 31, 2010 13:16
Show Gist options
  • Save AndreaCrotti/656576 to your computer and use it in GitHub Desktop.
Save AndreaCrotti/656576 to your computer and use it in GitHub Desktop.
#ifndef RINGBUFFER_H
#define RINGBUFFER_H
#include <deque>
template<typename T>
class RingBuffer
{
// TODO: see how to make it also iterable
private:
size_t max_size;
std::deque<T> buffer;
public:
RingBuffer(size_t max_size);
void push(T el);
size_t size() { return buffer.size(); }
T operator[](int index) { return buffer[index]; }
// TODO: move in the implementation below if possible
friend ostream& operator<<(ostream& s, const RingBuffer<T>& c) {
s << c;
return s;
}
};
template<typename T>
RingBuffer<T>::RingBuffer(size_t max_size) : max_size(max_size)
{}
template<typename T>
void RingBuffer<T>::push(T el)
{
if (buffer.size() == max_size) {
buffer.pop_front();
}
buffer.push_back(el);
cout << "after push" << endl;
}
#endif /* RINGBUFFER_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment