File size: 1,039 Bytes
d8d559a | 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 | #ifndef UTIL_STRING_STREAM_H
#define UTIL_STRING_STREAM_H
#include "fake_ostream.hh"
#include <cassert>
#include <string>
namespace util {
class StringStream : public FakeOStream<StringStream> {
public:
StringStream() {}
StringStream &flush() { return *this; }
StringStream &write(const void *data, std::size_t length) {
out_.append(static_cast<const char*>(data), length);
return *this;
}
const std::string &str() const { return out_; }
void str(const std::string &val) { out_ = val; }
void swap(std::string &str) { std::swap(out_, str); }
protected:
friend class FakeOStream<StringStream>;
char *Ensure(std::size_t amount) {
std::size_t current = out_.size();
out_.resize(out_.size() + amount);
return &out_[current];
}
void AdvanceTo(char *to) {
assert(to <= &*out_.end());
assert(to >= &*out_.begin());
out_.resize(to - &*out_.begin());
}
private:
std::string out_;
};
} // namespace
#endif // UTIL_STRING_STREAM_H
|