blob: 6771cadad31e9ff3655568bd94c9a42f812664ba (
plain)
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
|
#include "common.hh"
#include "str_buffer.hh"
#include <utility>
namespace {
template<typename T>
class StringBuffer : public RoBuffer {
public:
explicit StringBuffer(T str)
: str_(std::move(str)), offset_(0) {}
bool empty() const override {
return offset_ >= str_.size();
}
char const* rbuf(size_t /* want */, size_t& avail) override {
avail = str_.size() - offset_;
return str_.data() + offset_;
}
void rcommit(size_t bytes) override {
assert(str_.size() - offset_ >= bytes);
offset_ += bytes;
}
private:
T str_;
size_t offset_;
};
class SharedStringBuffer : public Buffer {
public:
explicit SharedStringBuffer(std::shared_ptr<std::string> content)
: content_(std::move(content)), read_ptr_(0), write_ptr_(content_->size()) {
}
bool empty() const override {
return read_ptr_ >= content_->size();
}
char const* rbuf(size_t /* want */, size_t& avail) override {
avail = content_->size() - read_ptr_;
return content_->data() + read_ptr_;
}
void rcommit(size_t bytes) override {
assert(content_->size() - read_ptr_ >= bytes);
read_ptr_ += bytes;
}
bool full() const override {
return false;
}
void clear() override {
content_->clear();
read_ptr_ = write_ptr_ = 0;
}
char* wbuf(size_t request, size_t& avail) override {
avail = request;
content_->resize(write_ptr_ + request);
return content_->data() + write_ptr_;
}
void wcommit(size_t bytes) override {
assert(content_->size() - write_ptr_ >= bytes);
write_ptr_ += bytes;
}
private:
std::shared_ptr<std::string> content_;
size_t read_ptr_;
size_t write_ptr_;
};
} // namespace
std::unique_ptr<RoBuffer> make_strbuffer(std::string content) {
return std::make_unique<StringBuffer<std::string>>(std::move(content));
}
std::unique_ptr<RoBuffer> make_strbuffer(std::string_view content) {
return std::make_unique<StringBuffer<std::string_view>>(content);
}
std::unique_ptr<Buffer> make_strbuffer(std::shared_ptr<std::string> content) {
return std::make_unique<SharedStringBuffer>(std::move(content));
}
|