jpayne@69: // Copyright (c) 2014 Sandstorm Development Group, Inc. and contributors jpayne@69: // Licensed under the MIT License: jpayne@69: // jpayne@69: // Permission is hereby granted, free of charge, to any person obtaining a copy jpayne@69: // of this software and associated documentation files (the "Software"), to deal jpayne@69: // in the Software without restriction, including without limitation the rights jpayne@69: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell jpayne@69: // copies of the Software, and to permit persons to whom the Software is jpayne@69: // furnished to do so, subject to the following conditions: jpayne@69: // jpayne@69: // The above copyright notice and this permission notice shall be included in jpayne@69: // all copies or substantial portions of the Software. jpayne@69: // jpayne@69: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR jpayne@69: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, jpayne@69: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE jpayne@69: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER jpayne@69: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, jpayne@69: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN jpayne@69: // THE SOFTWARE. jpayne@69: jpayne@69: /* jpayne@69: * Compatibility layer for stdlib iostream jpayne@69: */ jpayne@69: jpayne@69: #pragma once jpayne@69: jpayne@69: #include "../io.h" jpayne@69: #include jpayne@69: jpayne@69: KJ_BEGIN_HEADER jpayne@69: jpayne@69: namespace kj { jpayne@69: namespace std { jpayne@69: jpayne@69: class StdOutputStream: public kj::OutputStream { jpayne@69: jpayne@69: public: jpayne@69: explicit StdOutputStream(::std::ostream& stream) : stream_(stream) {} jpayne@69: ~StdOutputStream() noexcept(false) {} jpayne@69: jpayne@69: virtual void write(const void* src, size_t size) override { jpayne@69: // Always writes the full size. jpayne@69: jpayne@69: stream_.write((char*)src, size); jpayne@69: } jpayne@69: jpayne@69: virtual void write(ArrayPtr> pieces) override { jpayne@69: // Equivalent to write()ing each byte array in sequence, which is what the jpayne@69: // default implementation does. Override if you can do something better, jpayne@69: // e.g. use writev() to do the write in a single syscall. jpayne@69: jpayne@69: for (auto piece : pieces) { jpayne@69: write(piece.begin(), piece.size()); jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: private: jpayne@69: ::std::ostream& stream_; jpayne@69: jpayne@69: }; jpayne@69: jpayne@69: class StdInputStream: public kj::InputStream { jpayne@69: jpayne@69: public: jpayne@69: explicit StdInputStream(::std::istream& stream) : stream_(stream) {} jpayne@69: ~StdInputStream() noexcept(false) {} jpayne@69: jpayne@69: virtual size_t tryRead( jpayne@69: void* buffer, size_t minBytes, size_t maxBytes) override { jpayne@69: // Like read(), but may return fewer than minBytes on EOF. jpayne@69: jpayne@69: stream_.read((char*)buffer, maxBytes); jpayne@69: return stream_.gcount(); jpayne@69: } jpayne@69: jpayne@69: private: jpayne@69: ::std::istream& stream_; jpayne@69: jpayne@69: }; jpayne@69: jpayne@69: } // namespace std jpayne@69: } // namespace kj jpayne@69: jpayne@69: KJ_END_HEADER