annotate osx/include/kj/std/iostream.h @ 83:ae30d91d2ffe

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