aboutsummaryrefslogtreecommitdiffstats
path: root/source/circular.hpp
blob: 4f49322e28ffd136c380d76faff1b1861b00430b (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
 * @file circular.hpp
 * @brief Small utility for filling a buffer in a circular manner.
 *
 * Copyright (C) 2021 Clyne Sullivan
 *
 * Distributed under the GNU GPL v3 or later. You should have received a copy of
 * the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 */

#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(m_begin) {}

    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);
    }

    void reset(const T& fill) noexcept {
        std::fill(m_begin, m_end, fill);
        m_current = m_begin;
    }

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

#endif // CIRCULAR_HPP