this repo has no description
1/*
2This file is part of Darling.
3
4Copyright (C) 2020 Lubos Dolezel
5
6Darling is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
10
11Darling is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with Darling. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef _CA_CONSUMABLE_BUFFER_H
21#define _CA_CONSUMABLE_BUFFER_H
22#include <vector>
23#include <stdexcept>
24#include <cstring>
25#include <stdint.h>
26
27class ConsumableBuffer
28{
29public:
30 void push(const void* mem, size_t bytes)
31 {
32 const uint8_t* ptr = static_cast<const uint8_t*>(mem);
33 m_data.insert(m_data.end(), ptr, ptr + bytes);
34 }
35 size_t size() const
36 {
37 return m_data.size();
38 }
39 const uint8_t* data()
40 {
41 return m_data.data();
42 }
43 void consume(size_t bytes)
44 {
45 if (bytes > size())
46 throw std::logic_error("ConsumableBuffer::consume() bytes > size()");
47
48 std::memmove(m_data.data(), m_data.data() + bytes, m_data.size() - bytes);
49 m_data.resize(m_data.size() - bytes);
50 }
51private:
52 std::vector<uint8_t> m_data;
53};
54
55#endif