28 std::string
str()
const {
29 if (buffer_.empty()) {
32 return std::string(buffer_.data(), buffer_.size());
36 void str(
const std::string& s) {
37 buffer_.assign(s.begin(), s.end());
39 update_pointers(0, buffer_.size());
47 return traits_type::eof();
55 int_type
overflow(int_type c = traits_type::eof())
override {
56 if (c == traits_type::eof()) {
57 return traits_type::eof();
60 std::ptrdiff_t get_offset = eback() ? gptr() - eback() : 0;
62 buffer_.push_back(traits_type::to_char_type(c));
65 update_pointers(get_offset, buffer_.size());
73 pos_type
seekoff(off_type off, std::ios_base::seekdir dir,
74 std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
override {
80 case std::ios_base::beg:
83 case std::ios_base::cur: {
84 off_type current_pos = 0;
85 if (which & std::ios_base::in) {
87 if (gptr() !=
nullptr) {
88 current_pos = gptr() - eback();
90 }
else if (which & std::ios_base::out) {
92 if (pptr() !=
nullptr) {
93 current_pos = pptr() - pbase();
96 new_pos = current_pos + off;
99 case std::ios_base::end:
100 new_pos = buffer_.size() + off;
103 return pos_type(off_type(-1));
107 if (new_pos < 0 || new_pos >
static_cast<off_type
>(buffer_.size())) {
108 return pos_type(off_type(-1));
112 char* base = buffer_.data();
113 char* end = base + buffer_.size();
115 if (which & std::ios_base::in) {
116 setg(base, base + new_pos, end);
118 if (which & std::ios_base::out) {
120 pbump(
static_cast<int>(new_pos));
123 return pos_type(new_pos);
130 std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
override {
131 return seekoff(pos, std::ios_base::beg, which);
138 void update_pointers(std::ptrdiff_t get_offset = 0, std::ptrdiff_t put_offset = 0) {
140 char* base = buffer_.data();
141 char* end = base + buffer_.size();
144 setg(base, base + get_offset, end);
148 pbump(
static_cast<int>(put_offset));
151 std::vector<char> buffer_;
void str(const std::string &s)
Definition memory_buffer.hpp:36
memory_buffer()
Definition memory_buffer.hpp:18
std::string str() const
Definition memory_buffer.hpp:28
memory_buffer & operator=(const memory_buffer &)=delete
int_type underflow() override
Called when the get area is exhausted. For an in-memory buffer, this means EOF.
Definition memory_buffer.hpp:46
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
Implements seeking to an absolute or relative position. This is the corrected version.
Definition memory_buffer.hpp:73
memory_buffer(const memory_buffer &)=delete
pos_type seekpos(pos_type pos, std::ios_base::openmode which=std::ios_base::in|std::ios_base::out) override
Implements seeking to an absolute position by deferring to seekoff.
Definition memory_buffer.hpp:129
int_type overflow(int_type c=traits_type::eof()) override
Called when a write occurs at or past the end of the put area.
Definition memory_buffer.hpp:55