-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemIn.h
58 lines (51 loc) · 1.81 KB
/
MemIn.h
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
#pragma once
#include <istream> // std::basic_istream<>
#include <string_view> // std::basic_string_view<>
namespace bux {
//
// Types
//
template <class _CharT, class _Traits>
struct C_IMemBufAsMember
{
// Data
struct C_IMemBuf: std::basic_streambuf<_CharT,_Traits>
{
C_IMemBuf(const _CharT *buffer, size_t size)
{
const auto beg = const_cast<_CharT*>(buffer);
this->setg(beg, beg, beg+size);
}
C_IMemBuf(std::basic_string_view<_CharT,_Traits> buffer)
{
const auto beg = const_cast<_CharT*>(buffer.data());
this->setg(beg, beg, beg+buffer.size());
}
} m_Buffer;
// Ctor
C_IMemBufAsMember(const _CharT *buffer, size_t size): m_Buffer(buffer, size) {}
C_IMemBufAsMember(std::basic_string_view<_CharT,_Traits> buffer): m_Buffer(buffer) {}
C_IMemBufAsMember(std::basic_string<_CharT,_Traits> &&) = delete;
};
template <class _CharT, class _Traits = std::char_traits<_CharT>>
class C_IMemStreamT:
private C_IMemBufAsMember<_CharT,_Traits>,
public std::basic_istream<_CharT,_Traits> // Inheritance order matters
{
public:
// Ctors
C_IMemStreamT(const _CharT *buffer, size_t size):
C_IMemBufAsMember<_CharT,_Traits>(buffer, size),
std::basic_istream<_CharT,_Traits>(&this->m_Buffer)
{}
C_IMemStreamT(std::basic_string_view<_CharT,_Traits> buffer):
C_IMemBufAsMember<_CharT,_Traits>(buffer),
std::basic_istream<_CharT,_Traits>(&this->m_Buffer)
{}
C_IMemStreamT(const _CharT *str):
C_IMemStreamT(std::basic_string_view<_CharT,_Traits>{str})
{}
C_IMemStreamT(std::basic_string<_CharT,_Traits> &&) = delete;
};
using C_IMemStream = C_IMemStreamT<char>;
} // namespace bux