···11+/*
22+This file is part of Darling.
33+44+Copyright (C) 2020 Lubos Dolezel
55+66+Darling is free software: you can redistribute it and/or modify
77+it under the terms of the GNU General Public License as published by
88+the Free Software Foundation, either version 3 of the License, or
99+(at your option) any later version.
1010+1111+Darling is distributed in the hope that it will be useful,
1212+but WITHOUT ANY WARRANTY; without even the implied warranty of
1313+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414+GNU General Public License for more details.
1515+1616+You should have received a copy of the GNU General Public License
1717+along with Darling. If not, see <http://www.gnu.org/licenses/>.
1818+*/
1919+2020+#ifndef _CA_CONSUMABLE_BUFFER_H
2121+#define _CA_CONSUMABLE_BUFFER_H
2222+#include <vector>
2323+#include <stdexcept>
2424+#include <cstring>
2525+#include <stdint.h>
2626+2727+class ConsumableBuffer
2828+{
2929+public:
3030+ void push(const void* mem, size_t bytes)
3131+ {
3232+ const uint8_t* ptr = static_cast<const uint8_t*>(mem);
3333+ m_data.insert(m_data.end(), ptr, ptr + bytes);
3434+ }
3535+ size_t size() const
3636+ {
3737+ return m_data.size();
3838+ }
3939+ const uint8_t* data()
4040+ {
4141+ return m_data.data();
4242+ }
4343+ void consume(size_t bytes)
4444+ {
4545+ if (bytes > size())
4646+ throw std::logic_error("ConsumableBuffer::consume() bytes > size()");
4747+4848+ std::memmove(m_data.data(), m_data.data() + bytes, m_data.size() - bytes);
4949+ m_data.resize(m_data.size() - bytes);
5050+ }
5151+private:
5252+ std::vector<uint8_t> m_data;
5353+};
5454+5555+#endif