aboutsummaryrefslogtreecommitdiffstats
path: root/source/circular.hpp
blob: 33b8ee0a847c136571f3bae7d3133faf45ae4644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef CIRCULAR_HPP
#define CIRCULAR_HPP

#include <iterator>

template<template<typename> class Container, typename T>
class CircularBuffer
{
public:
    CircularBuffer(Container<T>& container) :
        m_begin(std::begin(container)),
        m_end(std::end(container)),
        m_current(std::begin(container)) {}

    void put(const T& value) noexcept {
        *m_current = value;
        if (++m_current == m_end)
            m_current = m_begin;
    }

    std::size_t size() const noexcept {
        return std::distance(m_begin, m_end);
    }

private:
    Container<T>::iterator m_begin;
    Container<T>::iterator m_end;
    Container<T>::iterator m_current;
};

#endif // CIRCULAR_HPP