Commit 7868bf9f authored by Scott Purdy's avatar Scott Purdy

Response to comments on #144. Remove 'using namespace std' from header, added…

Response to comments on #144. Remove 'using namespace std' from header, added additional tests, updated comment style, and added self to CONTRIBUTORS.
parent 847ea2c0
...@@ -6,6 +6,7 @@ Kenton Varda <temporal@gmail.com>: Primary Author ...@@ -6,6 +6,7 @@ Kenton Varda <temporal@gmail.com>: Primary Author
Jason Choy <jjwchoy@gmail.com>: kj/threadlocal.h and other iOS tweaks, `name` annotation in C++ code generator Jason Choy <jjwchoy@gmail.com>: kj/threadlocal.h and other iOS tweaks, `name` annotation in C++ code generator
Remy Blank <rblank@google.com> (contributions copyright Google Inc.): KJ Timers Remy Blank <rblank@google.com> (contributions copyright Google Inc.): KJ Timers
Joshua Warner <joshuawarner32@gmail.com>: cmake build Joshua Warner <joshuawarner32@gmail.com>: cmake build
Scott Purdy <scott@fer.io>: kj/std iostream interface
This file does not list people who maintain their own Cap'n Proto This file does not list people who maintain their own Cap'n Proto
implementations as separate projects. Those people are awesome too! :) implementations as separate projects. Those people are awesome too! :)
...@@ -31,7 +31,7 @@ TEST(StdIoStream, WriteVec) { ...@@ -31,7 +31,7 @@ TEST(StdIoStream, WriteVec) {
// Check that writing an array of arrays works even when some of the arrays // Check that writing an array of arrays works even when some of the arrays
// are empty. (This used to not work in some cases.) // are empty. (This used to not work in some cases.)
stringstream ss; ::std::stringstream ss;
StdInputStream in(ss); StdInputStream in(ss);
StdOutputStream out(ss); StdOutputStream out(ss);
...@@ -53,6 +53,53 @@ TEST(StdIoStream, WriteVec) { ...@@ -53,6 +53,53 @@ TEST(StdIoStream, WriteVec) {
EXPECT_STREQ("foobar", buf); EXPECT_STREQ("foobar", buf);
} }
TEST(StdIoStream, TryReadToEndOfFile) {
// Check that tryRead works when eof is reached before minBytes.
::std::stringstream ss;
StdInputStream in(ss);
StdOutputStream out(ss);
const void* bytes = "foobar";
out.write(bytes, 6);
char buf[9];
in.tryRead(buf, 8, 8);
buf[6] = '\0';
EXPECT_STREQ("foobar", buf);
}
TEST(StdIoStream, ReadToEndOfFile) {
// Check that read throws an exception when eof is reached before specified
// bytes.
::std::stringstream ss;
StdInputStream in(ss);
StdOutputStream out(ss);
const void* bytes = "foobar";
out.write(bytes, 6);
char buf[9];
Maybe<Exception> e = kj::runCatchingExceptions([&]() {
in.read(buf, 8, 8);
});
buf[6] = '\0';
KJ_IF_MAYBE(ex, e) {
// Ensure that the value is still read up to the EOF.
EXPECT_STREQ("foobar", buf);
} else {
ADD_FAILURE() << "Expected exception";
}
}
} // namespace } // namespace
} // namespace std } // namespace std
} // namespace kj } // namespace kj
...@@ -29,57 +29,52 @@ ...@@ -29,57 +29,52 @@
#include <iostream> #include <iostream>
#include "../io.h" #include "../io.h"
using namespace std;
namespace kj { namespace kj {
namespace std { namespace std {
class StdOutputStream: public kj::OutputStream { class StdOutputStream: public kj::OutputStream {
public: public:
explicit StdOutputStream(ostream& stream) : stream_(stream) {} explicit StdOutputStream(::std::ostream& stream) : stream_(stream) {}
~StdOutputStream() noexcept(false) {} ~StdOutputStream() noexcept(false) {}
/*
* Always writes the full size.
*/
virtual void write(const void* src, size_t size) override { virtual void write(const void* src, size_t size) override {
// Always writes the full size.
stream_.write((char*)src, size); stream_.write((char*)src, size);
} }
/*
* Equivalent to write()ing each byte array in sequence, which is what the
* default implementation does. Override if you can do something better,
* e.g. use writev() to do the write in a single syscall.
*/
virtual void write(ArrayPtr<const ArrayPtr<const byte>> pieces) override { virtual void write(ArrayPtr<const ArrayPtr<const byte>> pieces) override {
// Equivalent to write()ing each byte array in sequence, which is what the
// default implementation does. Override if you can do something better,
// e.g. use writev() to do the write in a single syscall.
for (auto piece : pieces) { for (auto piece : pieces) {
write(piece.begin(), piece.size()); write(piece.begin(), piece.size());
} }
} }
private: private:
ostream& stream_; ::std::ostream& stream_;
}; };
class StdInputStream: public kj::InputStream { class StdInputStream: public kj::InputStream {
public: public:
explicit StdInputStream(istream& stream) : stream_(stream) {} explicit StdInputStream(::std::istream& stream) : stream_(stream) {}
~StdInputStream() noexcept(false) {} ~StdInputStream() noexcept(false) {}
/*
* Like read(), but may return fewer than minBytes on EOF.
*/
virtual size_t tryRead( virtual size_t tryRead(
void* buffer, size_t minBytes, size_t maxBytes) override { void* buffer, size_t minBytes, size_t maxBytes) override {
// Like read(), but may return fewer than minBytes on EOF.
stream_.read((char*)buffer, maxBytes); stream_.read((char*)buffer, maxBytes);
return stream_.gcount(); return stream_.gcount();
} }
private: private:
istream& stream_; ::std::istream& stream_;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment