capnproto

FORK: Cap'n Proto serialization/RPC system - core tools and C++ library
git clone https://git.neptards.moe/neptards/capnproto.git
Log | Files | Refs | README | LICENSE

iostream.h (2566B)


      1 // Copyright (c) 2014 Sandstorm Development Group, Inc. and contributors
      2 // Licensed under the MIT License:
      3 //
      4 // Permission is hereby granted, free of charge, to any person obtaining a copy
      5 // of this software and associated documentation files (the "Software"), to deal
      6 // in the Software without restriction, including without limitation the rights
      7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      8 // copies of the Software, and to permit persons to whom the Software is
      9 // furnished to do so, subject to the following conditions:
     10 //
     11 // The above copyright notice and this permission notice shall be included in
     12 // all copies or substantial portions of the Software.
     13 //
     14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     20 // THE SOFTWARE.
     21 
     22 /*
     23  * Compatibility layer for stdlib iostream
     24  */
     25 
     26 #pragma once
     27 
     28 #include "../io.h"
     29 #include <iostream>
     30 
     31 KJ_BEGIN_HEADER
     32 
     33 namespace kj {
     34 namespace std {
     35 
     36 class StdOutputStream: public kj::OutputStream {
     37 
     38 public:
     39   explicit StdOutputStream(::std::ostream& stream) : stream_(stream) {}
     40   ~StdOutputStream() noexcept(false) {}
     41 
     42   virtual void write(const void* src, size_t size) override {
     43     // Always writes the full size.
     44 
     45     stream_.write((char*)src, size);
     46   }
     47 
     48   virtual void write(ArrayPtr<const ArrayPtr<const byte>> pieces) override {
     49     // Equivalent to write()ing each byte array in sequence, which is what the
     50     // default implementation does. Override if you can do something better,
     51     // e.g. use writev() to do the write in a single syscall.
     52 
     53     for (auto piece : pieces) {
     54       write(piece.begin(), piece.size());
     55     }
     56   }
     57 
     58 private:
     59   ::std::ostream& stream_;
     60 
     61 };
     62 
     63 class StdInputStream: public kj::InputStream {
     64 
     65 public:
     66   explicit StdInputStream(::std::istream& stream) : stream_(stream) {}
     67   ~StdInputStream() noexcept(false) {}
     68 
     69   virtual size_t tryRead(
     70       void* buffer, size_t minBytes, size_t maxBytes) override {
     71     // Like read(), but may return fewer than minBytes on EOF.
     72 
     73     stream_.read((char*)buffer, maxBytes);
     74     return stream_.gcount();
     75   }
     76 
     77 private:
     78   ::std::istream& stream_;
     79 
     80 };
     81 
     82 }  // namespace std
     83 }  // namespace kj
     84 
     85 KJ_END_HEADER