-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.hh
34 lines (26 loc) · 909 Bytes
/
stream.hh
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
#pragma once
#include <array>
#include <iostream>
// Create a shared buffer
extern char buffer[1024];
// Thanks to https://stackoverflow.com/a/8244052/6441345
template <typename CharT = char>
class null_buffer : public std::basic_streambuf<CharT> {
public:
int overflow (int c) override {
// Calculate borders of shared buffer
constexpr CharT* start = reinterpret_cast<CharT*>(buffer);
constexpr uintmax_t size = sizeof(buffer) / sizeof(CharT);
// Sets 'new buffer' borders
this->setp(start, start + size);
return c == std::char_traits<CharT>::eof() ? '\0' : c;
}
};
extern null_buffer<char> null_buff;
template <typename CharT = char>
class null_ostream : public std::basic_ostream<CharT> {
public:
null_ostream (void) : std::basic_ostream<CharT>{ &null_buff } {}
null_buffer<CharT>* rdbuf (void) const { return &null_buff; }
};
extern null_ostream<char> null_ostr;