-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathcallback_streambuf.hpp
More file actions
47 lines (40 loc) · 1.14 KB
/
callback_streambuf.hpp
File metadata and controls
47 lines (40 loc) · 1.14 KB
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
#ifndef CALLBACK_STREAMBUF_H
#define CALLBACK_STREAMBUF_H
#include <array>
#include <iostream>
template<typename Callback, class Char = char>
class callback_streambuf : public std::basic_streambuf<Char>
{
public:
using base_type = std::streambuf;
using char_type = typename base_type::char_type;
using int_type = typename base_type::int_type;
callback_streambuf(Callback callback, std::size_t buffer_size)
: callback_(callback),
buffer_(buffer_size)
{
base_type::setp(buffer_.data(), buffer_.data() + buffer_.size());
}
protected:
int sync()
{
bool ok = callback_(base_type::pbase(),
base_type::pptr() - base_type::pbase());
base_type::setp(buffer_.data(), buffer_.data() + buffer_.size());
return ok ? 0 : -1;
}
int_type overflow(int_type ch)
{
int ret = sync();
if (ch == base_type::traits_type::eof())
{
return ch;
}
base_type::sputc(ch);
return ret == 0 ? 0 : base_type::traits_type::eof();
}
private:
Callback callback_;
std::vector<char_type> buffer_;
};
#endif