字节流设计
class ByteStream {
private:
std::deque<char> _buffer{};
size_t _capacity;
size_t _bytes_written{0};
size_t _bytes_read{0};
bool _end_input{false};
bool _error{};
public:
...
};
函数实现
size_t ByteStream::write(const string &data) {
size_t length = min(remaining_capacity(), data.size());
for (size_t i = 0; i < length; i++)
_buffer.push_back(data[i]);
_bytes_written += length;
return length;
}
string ByteStream::peek_output(const size_t len) const {
string res;
size_t length = min(len, _buffer.size());
for (size_t i = 0; i < length; i++)
res += _buffer[i];
return res;
}
void ByteStream::pop_output(const size_t len) {
size_t length = min(len, _buffer.size());
for (size_t i = 0; i < length; i++) {
_buffer.pop_front();
}
_bytes_read += length;
}
std::string ByteStream::read(const size_t len) {
string res = peek_output(len);
pop_output(len);
return res;
}
void ByteStream::end_input() { _end_input = true; }
bool ByteStream::input_ended() const { return _end_input; }
size_t ByteStream::buffer_size() const { return _buffer.size(); }
bool ByteStream::buffer_empty() const { return _buffer.size() == 0; }
bool ByteStream::eof() const { return buffer_empty() && input_ended(); }
size_t ByteStream::bytes_written() const { return _bytes_written; }
size_t ByteStream::bytes_read() const { return _bytes_read; }
size_t ByteStream::remaining_capacity() const { return _capacity - _buffer.size(); }
标签:const,buffer,length,lab0,return,ByteStream,size
From: https://www.cnblogs.com/cong0221/p/17275710.html