forked from primihub/SEAL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreambuf.h
167 lines (107 loc) · 4.65 KB
/
streambuf.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include "seal/dynarray.h"
#include "seal/util/defines.h"
#include <cstddef>
#include <ios>
#include <streambuf>
namespace seal
{
namespace util
{
class SafeByteBuffer final : public std::streambuf
{
public:
SafeByteBuffer(std::streamsize size = 1, bool clear_buffers = true);
~SafeByteBuffer() override = default;
SafeByteBuffer(const SafeByteBuffer ©) = delete;
SafeByteBuffer &operator=(const SafeByteBuffer &assign) = delete;
SEAL_NODISCARD inline const seal_byte *data() const noexcept
{
return reinterpret_cast<const seal_byte *>(buf_.cbegin());
}
SEAL_NODISCARD inline seal_byte *data() noexcept
{
return reinterpret_cast<seal_byte *>(buf_.begin());
}
SEAL_NODISCARD inline std::size_t size() noexcept
{
return buf_.size();
}
SEAL_NODISCARD inline std::size_t capacity() noexcept
{
return buf_.capacity();
}
private:
void safe_gbump(std::streamsize count);
void safe_pbump(std::streamsize count);
int_type underflow() override;
int_type pbackfail(int_type ch) override;
std::streamsize showmanyc() override;
std::streamsize xsgetn(char_type *s, std::streamsize count) override;
pos_type seekpos(
pos_type pos, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) override;
pos_type seekoff(
off_type off, std::ios_base::seekdir dir,
std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) override;
void expand_size();
int_type overflow(int_type ch = traits_type::eof()) override;
std::streamsize xsputn(const char_type *s, std::streamsize count) override;
std::streamsize size_;
const bool clear_buffers_;
DynArray<char> buf_;
static constexpr double expansion_factor_ = 1.3;
int_type eof_ = traits_type::eof();
};
class ArrayGetBuffer final : public std::streambuf
{
public:
ArrayGetBuffer(const char_type *buf, std::streamsize size);
~ArrayGetBuffer() override = default;
ArrayGetBuffer(const ArrayGetBuffer ©) = delete;
ArrayGetBuffer &operator=(const ArrayGetBuffer &assign) = delete;
private:
int_type underflow() override;
int_type uflow() override;
int_type pbackfail(int_type ch) override;
std::streamsize showmanyc() override;
std::streamsize xsgetn(char_type *s, std::streamsize count) override;
pos_type seekpos(pos_type pos, std::ios_base::openmode which = std::ios_base::in) override;
pos_type seekoff(
off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which = std::ios_base::in) override;
const char_type *buf_;
std::streamsize size_;
using iterator_type = const char_type *;
int_type eof_ = traits_type::eof();
iterator_type begin_;
iterator_type end_;
iterator_type head_;
};
class ArrayPutBuffer final : public std::streambuf
{
public:
ArrayPutBuffer(char_type *buf, std::streamsize size);
~ArrayPutBuffer() override = default;
ArrayPutBuffer(const ArrayPutBuffer ©) = delete;
ArrayPutBuffer &operator=(const ArrayPutBuffer &assign) = delete;
SEAL_NODISCARD inline bool at_end() const noexcept
{
return head_ == end_;
}
private:
int_type overflow(int_type ch = traits_type::eof()) override;
std::streamsize xsputn(const char_type *s, std::streamsize count) override;
pos_type seekpos(pos_type pos, std::ios_base::openmode which = std::ios_base::out) override;
pos_type seekoff(
off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which = std::ios_base::out) override;
char_type *buf_;
std::streamsize size_;
using iterator_type = char_type *;
int_type eof_ = traits_type::eof();
iterator_type begin_;
iterator_type end_;
iterator_type head_;
};
} // namespace util
} // namespace seal