comparison osx/include/kj/std/iostream.h @ 62:0994c39f1e94

Cap'n Proto v0.6 + build for OSX
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 22 May 2017 10:01:37 +0100
parents
children
comparison
equal deleted inserted replaced
61:d101c4099725 62:0994c39f1e94
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 #ifndef KJ_STD_IOSTREAM_H_
27 #define KJ_STD_IOSTREAM_H_
28
29 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
30 #pragma GCC system_header
31 #endif
32
33 #include "../io.h"
34 #include <iostream>
35
36 namespace kj {
37 namespace std {
38
39 class StdOutputStream: public kj::OutputStream {
40
41 public:
42 explicit StdOutputStream(::std::ostream& stream) : stream_(stream) {}
43 ~StdOutputStream() noexcept(false) {}
44
45 virtual void write(const void* src, size_t size) override {
46 // Always writes the full size.
47
48 stream_.write((char*)src, size);
49 }
50
51 virtual void write(ArrayPtr<const ArrayPtr<const byte>> pieces) override {
52 // Equivalent to write()ing each byte array in sequence, which is what the
53 // default implementation does. Override if you can do something better,
54 // e.g. use writev() to do the write in a single syscall.
55
56 for (auto piece : pieces) {
57 write(piece.begin(), piece.size());
58 }
59 }
60
61 private:
62 ::std::ostream& stream_;
63
64 };
65
66 class StdInputStream: public kj::InputStream {
67
68 public:
69 explicit StdInputStream(::std::istream& stream) : stream_(stream) {}
70 ~StdInputStream() noexcept(false) {}
71
72 virtual size_t tryRead(
73 void* buffer, size_t minBytes, size_t maxBytes) override {
74 // Like read(), but may return fewer than minBytes on EOF.
75
76 stream_.read((char*)buffer, maxBytes);
77 return stream_.gcount();
78 }
79
80 private:
81 ::std::istream& stream_;
82
83 };
84
85 } // namespace std
86 } // namespace kj
87
88 #endif // KJ_STD_IOSTREAM_H_