-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadbuffer.hpp
61 lines (50 loc) · 1.1 KB
/
readbuffer.hpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
//@ {"targets":[{"name":"readbuffer.hpp","type":"include"}]}
#ifndef MAIKE_READBUFFER_HPP
#define MAIKE_READBUFFER_HPP
#include "datasource.hpp"
#include "visibility.hpp"
#include <cstdint>
namespace Maike
{
class PRIVATE ReadBuffer
{
public:
ReadBuffer(const ReadBuffer&)=delete;
ReadBuffer& operator=(const ReadBuffer&)=delete;
explicit ReadBuffer(DataSource& source):r_source(source)
{
r_read_pos=m_buffer;
m_n_valid=0;
}
uint8_t byteRead()
{
if(bufferEnd())
{fetch();}
auto ret=*r_read_pos;
++r_read_pos;
return ret;
}
bool eof() const
{
if(bufferEnd())
{fetch();}
return bufferEnd();
}
const char* nameGet() const noexcept
{return r_source.nameGet();}
private:
DataSource& r_source;
static constexpr size_t N=4096;
mutable uint8_t m_buffer[N];
mutable uint8_t* r_read_pos;
mutable size_t m_n_valid;
bool bufferEnd() const noexcept
{return r_read_pos==m_buffer + m_n_valid;}
void fetch() const
{
m_n_valid=r_source.read(m_buffer,N);
r_read_pos=m_buffer;
}
};
}
#endif