Commit 8ebaa614 authored by Kenton Varda's avatar Kenton Varda

Merge pull request #144 from scottpurdy/std-compat

Adds kj::std as header-only compat layer
parents 01c05f5e 7868bf9f
...@@ -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! :)
...@@ -117,6 +117,7 @@ capnpc_outputs = \ ...@@ -117,6 +117,7 @@ capnpc_outputs = \
includecapnpdir = $(includedir)/capnp includecapnpdir = $(includedir)/capnp
includekjdir = $(includedir)/kj includekjdir = $(includedir)/kj
includekjparsedir = $(includekjdir)/parse includekjparsedir = $(includekjdir)/parse
includekjstddir = $(includekjdir)/std
dist_includecapnp_DATA = $(public_capnpc_inputs) dist_includecapnp_DATA = $(public_capnpc_inputs)
...@@ -154,6 +155,9 @@ includekjparse_HEADERS = \ ...@@ -154,6 +155,9 @@ includekjparse_HEADERS = \
src/kj/parse/common.h \ src/kj/parse/common.h \
src/kj/parse/char.h src/kj/parse/char.h
includekjstd_HEADERS = \
src/kj/std/iostream.h
includecapnp_HEADERS = \ includecapnp_HEADERS = \
src/capnp/c++.capnp.h \ src/capnp/c++.capnp.h \
src/capnp/common.h \ src/capnp/common.h \
...@@ -372,6 +376,7 @@ heavy_tests = \ ...@@ -372,6 +376,7 @@ heavy_tests = \
src/kj/async-io-test.c++ \ src/kj/async-io-test.c++ \
src/kj/parse/common-test.c++ \ src/kj/parse/common-test.c++ \
src/kj/parse/char-test.c++ \ src/kj/parse/char-test.c++ \
src/kj/std/iostream-test.c++ \
src/capnp/capability-test.c++ \ src/capnp/capability-test.c++ \
src/capnp/schema-test.c++ \ src/capnp/schema-test.c++ \
src/capnp/schema-loader-test.c++ \ src/capnp/schema-loader-test.c++ \
......
...@@ -45,10 +45,14 @@ set(kj-parse_headers ...@@ -45,10 +45,14 @@ set(kj-parse_headers
parse/common.h parse/common.h
parse/char.h parse/char.h
) )
set(kj-std_headers
std/iostream.h
)
add_library(kj ${kj_sources}) add_library(kj ${kj_sources})
install(TARGETS kj ARCHIVE DESTINATION "${LIB_INSTALL_DIR}") install(TARGETS kj ARCHIVE DESTINATION "${LIB_INSTALL_DIR}")
install(FILES ${kj_headers} DESTINATION "${INCLUDE_INSTALL_DIR}/kj") install(FILES ${kj_headers} DESTINATION "${INCLUDE_INSTALL_DIR}/kj")
install(FILES ${kj-parse_headers} DESTINATION "${INCLUDE_INSTALL_DIR}/kj/parse") install(FILES ${kj-parse_headers} DESTINATION "${INCLUDE_INSTALL_DIR}/kj/parse")
install(FILES ${kj-std_headers} DESTINATION "${INCLUDE_INSTALL_DIR}/kj/std")
set(kj-async_sources set(kj-async_sources
async.c++ async.c++
...@@ -103,6 +107,7 @@ if(BUILD_TESTING) ...@@ -103,6 +107,7 @@ if(BUILD_TESTING)
async-io-test.c++ async-io-test.c++
parse/common-test.c++ parse/common-test.c++
parse/char-test.c++ parse/char-test.c++
std/iostream-test.c++
) )
target_link_libraries(kj-heavy-tests kj kj-async gtest gtest_main) target_link_libraries(kj-heavy-tests kj kj-async gtest gtest_main)
add_dependencies(check kj-heavy-tests) add_dependencies(check kj-heavy-tests)
......
// Copyright (c) 2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "iostream.h"
#include <sstream>
#include <gtest/gtest.h>
namespace kj {
namespace std {
namespace {
TEST(StdIoStream, WriteVec) {
// Check that writing an array of arrays works even when some of the arrays
// are empty. (This used to not work in some cases.)
::std::stringstream ss;
StdInputStream in(ss);
StdOutputStream out(ss);
ArrayPtr<const byte> pieces[5] = {
arrayPtr(implicitCast<const byte*>(nullptr), 0),
arrayPtr(reinterpret_cast<const byte*>("foo"), 3),
arrayPtr(implicitCast<const byte*>(nullptr), 0),
arrayPtr(reinterpret_cast<const byte*>("bar"), 3),
arrayPtr(implicitCast<const byte*>(nullptr), 0)
};
out.write(pieces);
char buf[7];
in.read(buf, 6);
buf[6] = '\0';
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 std
} // namespace kj
// Copyright (c) 2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
/*
* Compatibility layer for stdlib iostream
*/
#ifndef KJ_STD_IOSTREAM_H_
#define KJ_STD_IOSTREAM_H_
#include <iostream>
#include "../io.h"
namespace kj {
namespace std {
class StdOutputStream: public kj::OutputStream {
public:
explicit StdOutputStream(::std::ostream& stream) : stream_(stream) {}
~StdOutputStream() noexcept(false) {}
virtual void write(const void* src, size_t size) override {
// Always writes the full size.
stream_.write((char*)src, size);
}
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) {
write(piece.begin(), piece.size());
}
}
private:
::std::ostream& stream_;
};
class StdInputStream: public kj::InputStream {
public:
explicit StdInputStream(::std::istream& stream) : stream_(stream) {}
~StdInputStream() noexcept(false) {}
virtual size_t tryRead(
void* buffer, size_t minBytes, size_t maxBytes) override {
// Like read(), but may return fewer than minBytes on EOF.
stream_.read((char*)buffer, maxBytes);
return stream_.gcount();
}
private:
::std::istream& stream_;
};
} // namespace std
} // namespace kj
#endif // KJ_STD_IOSTREAM_H_
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