Add 'ostream' method to class StreamBuffer, and use it.
This commit is contained in:
@@ -477,6 +477,38 @@ void *StreamBuffer::lua_reader_ud(int64_t size) {
|
||||
return this;
|
||||
}
|
||||
|
||||
class StreamBufferWriter : public std::streambuf {
|
||||
private:
|
||||
StreamBuffer *target_;
|
||||
public:
|
||||
StreamBufferWriter(StreamBuffer *t) : target_(t) {}
|
||||
|
||||
virtual int_type overflow(int_type c) {
|
||||
if (c != EOF) {
|
||||
target_->write_uint8(c);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
};
|
||||
|
||||
class StreamBufferOStream : public std::ostream {
|
||||
private:
|
||||
StreamBufferWriter writer_;
|
||||
public:
|
||||
StreamBufferOStream(StreamBuffer *t) : std::ostream(nullptr), writer_(t) {
|
||||
rdbuf(&writer_);
|
||||
}
|
||||
virtual ~StreamBufferOStream() {
|
||||
}
|
||||
};
|
||||
|
||||
std::ostream &StreamBuffer::ostream() {
|
||||
if (ostream_ == nullptr) {
|
||||
ostream_.reset(new StreamBufferOStream(this));
|
||||
}
|
||||
return *ostream_;
|
||||
}
|
||||
|
||||
static bool streq(const char *str, const char *data) {
|
||||
int len = strlen(str);
|
||||
return memcmp(str, data, len) == 0;
|
||||
@@ -611,5 +643,11 @@ LuaDefine(unittests_streambuffer, "c") {
|
||||
eqsb1.write_int32(34);
|
||||
LuaAssert(L, !eqsb1.contents_equal(&eqsb2));
|
||||
|
||||
// Check the OStream functionality.
|
||||
StreamBuffer ossb;
|
||||
ossb.ostream() << "Testing.";
|
||||
ossb.ostream() << "Foo.";
|
||||
LuaAssertStrEq(L, ossb.read_entire_contents(), "Testing.Foo.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user