Library Idea
Ring Buffer


by Robert Ramey


Boost has Circular Buffer. But I’m not happy with it. I creates and manages it’s own collection. I want less than that. I want a class which takes any pair of (forward?) iterators like this:

template<typename T> // TI is a forward iterator type
struct ring_buffer {
private:
    const TI m_begin;
    const TI m_end;
    const TI m_current;
    ring_buffer(T::iterator begin, T::iterator end) : m_begin(begin), m_end(end) {}
    TI push_back(const typename TI::value_type & t){ ...}
    TI push_front(const typename TI::value_type & t){ ...}
    TI pop_back(){ ...}
    TI pop_front(){ ...} // other useful operations
};

This would be very light weight and would permit the elimination of all dynamic memory allocation if the pair of iterators corresponded to std::array either on the stack or in static memory area. Very useful and simple. embellishments would be to use atomics or mutexes to create a bounded buffer


There are 0 comments

Comment on This Page