annotate win64-msvc/include/kj/io.h @ 47:d93140aac40b

Current Capnp libs and headers from git
author Chris Cannam
date Thu, 20 Oct 2016 18:15:38 +0100
parents
children 0f2d93caa50c
rev   line source
Chris@47 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@47 2 // Licensed under the MIT License:
Chris@47 3 //
Chris@47 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@47 5 // of this software and associated documentation files (the "Software"), to deal
Chris@47 6 // in the Software without restriction, including without limitation the rights
Chris@47 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@47 8 // copies of the Software, and to permit persons to whom the Software is
Chris@47 9 // furnished to do so, subject to the following conditions:
Chris@47 10 //
Chris@47 11 // The above copyright notice and this permission notice shall be included in
Chris@47 12 // all copies or substantial portions of the Software.
Chris@47 13 //
Chris@47 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@47 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@47 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@47 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@47 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@47 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@47 20 // THE SOFTWARE.
Chris@47 21
Chris@47 22 #ifndef KJ_IO_H_
Chris@47 23 #define KJ_IO_H_
Chris@47 24
Chris@47 25 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
Chris@47 26 #pragma GCC system_header
Chris@47 27 #endif
Chris@47 28
Chris@47 29 #include <stddef.h>
Chris@47 30 #include "common.h"
Chris@47 31 #include "array.h"
Chris@47 32 #include "exception.h"
Chris@47 33
Chris@47 34 namespace kj {
Chris@47 35
Chris@47 36 // =======================================================================================
Chris@47 37 // Abstract interfaces
Chris@47 38
Chris@47 39 class InputStream {
Chris@47 40 public:
Chris@47 41 virtual ~InputStream() noexcept(false);
Chris@47 42
Chris@47 43 size_t read(void* buffer, size_t minBytes, size_t maxBytes);
Chris@47 44 // Reads at least minBytes and at most maxBytes, copying them into the given buffer. Returns
Chris@47 45 // the size read. Throws an exception on errors. Implemented in terms of tryRead().
Chris@47 46 //
Chris@47 47 // maxBytes is the number of bytes the caller really wants, but minBytes is the minimum amount
Chris@47 48 // needed by the caller before it can start doing useful processing. If the stream returns less
Chris@47 49 // than maxBytes, the caller will usually call read() again later to get the rest. Returning
Chris@47 50 // less than maxBytes is useful when it makes sense for the caller to parallelize processing
Chris@47 51 // with I/O.
Chris@47 52 //
Chris@47 53 // Never blocks if minBytes is zero. If minBytes is zero and maxBytes is non-zero, this may
Chris@47 54 // attempt a non-blocking read or may just return zero. To force a read, use a non-zero minBytes.
Chris@47 55 // To detect EOF without throwing an exception, use tryRead().
Chris@47 56 //
Chris@47 57 // If the InputStream can't produce minBytes, it MUST throw an exception, as the caller is not
Chris@47 58 // expected to understand how to deal with partial reads.
Chris@47 59
Chris@47 60 virtual size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) = 0;
Chris@47 61 // Like read(), but may return fewer than minBytes on EOF.
Chris@47 62
Chris@47 63 inline void read(void* buffer, size_t bytes) { read(buffer, bytes, bytes); }
Chris@47 64 // Convenience method for reading an exact number of bytes.
Chris@47 65
Chris@47 66 virtual void skip(size_t bytes);
Chris@47 67 // Skips past the given number of bytes, discarding them. The default implementation read()s
Chris@47 68 // into a scratch buffer.
Chris@47 69 };
Chris@47 70
Chris@47 71 class OutputStream {
Chris@47 72 public:
Chris@47 73 virtual ~OutputStream() noexcept(false);
Chris@47 74
Chris@47 75 virtual void write(const void* buffer, size_t size) = 0;
Chris@47 76 // Always writes the full size. Throws exception on error.
Chris@47 77
Chris@47 78 virtual void write(ArrayPtr<const ArrayPtr<const byte>> pieces);
Chris@47 79 // Equivalent to write()ing each byte array in sequence, which is what the default implementation
Chris@47 80 // does. Override if you can do something better, e.g. use writev() to do the write in a single
Chris@47 81 // syscall.
Chris@47 82 };
Chris@47 83
Chris@47 84 class BufferedInputStream: public InputStream {
Chris@47 85 // An input stream which buffers some bytes in memory to reduce system call overhead.
Chris@47 86 // - OR -
Chris@47 87 // An input stream that actually reads from some in-memory data structure and wants to give its
Chris@47 88 // caller a direct pointer to that memory to potentially avoid a copy.
Chris@47 89
Chris@47 90 public:
Chris@47 91 virtual ~BufferedInputStream() noexcept(false);
Chris@47 92
Chris@47 93 ArrayPtr<const byte> getReadBuffer();
Chris@47 94 // Get a direct pointer into the read buffer, which contains the next bytes in the input. If the
Chris@47 95 // caller consumes any bytes, it should then call skip() to indicate this. This always returns a
Chris@47 96 // non-empty buffer or throws an exception. Implemented in terms of tryGetReadBuffer().
Chris@47 97
Chris@47 98 virtual ArrayPtr<const byte> tryGetReadBuffer() = 0;
Chris@47 99 // Like getReadBuffer() but may return an empty buffer on EOF.
Chris@47 100 };
Chris@47 101
Chris@47 102 class BufferedOutputStream: public OutputStream {
Chris@47 103 // An output stream which buffers some bytes in memory to reduce system call overhead.
Chris@47 104 // - OR -
Chris@47 105 // An output stream that actually writes into some in-memory data structure and wants to give its
Chris@47 106 // caller a direct pointer to that memory to potentially avoid a copy.
Chris@47 107
Chris@47 108 public:
Chris@47 109 virtual ~BufferedOutputStream() noexcept(false);
Chris@47 110
Chris@47 111 virtual ArrayPtr<byte> getWriteBuffer() = 0;
Chris@47 112 // Get a direct pointer into the write buffer. The caller may choose to fill in some prefix of
Chris@47 113 // this buffer and then pass it to write(), in which case write() may avoid a copy. It is
Chris@47 114 // incorrect to pass to write any slice of this buffer which is not a prefix.
Chris@47 115 };
Chris@47 116
Chris@47 117 // =======================================================================================
Chris@47 118 // Buffered streams implemented as wrappers around regular streams
Chris@47 119
Chris@47 120 class BufferedInputStreamWrapper: public BufferedInputStream {
Chris@47 121 // Implements BufferedInputStream in terms of an InputStream.
Chris@47 122 //
Chris@47 123 // Note that the underlying stream's position is unpredictable once the wrapper is destroyed,
Chris@47 124 // unless the entire stream was consumed. To read a predictable number of bytes in a buffered
Chris@47 125 // way without going over, you'd need this wrapper to wrap some other wrapper which itself
Chris@47 126 // implements an artificial EOF at the desired point. Such a stream should be trivial to write
Chris@47 127 // but is not provided by the library at this time.
Chris@47 128
Chris@47 129 public:
Chris@47 130 explicit BufferedInputStreamWrapper(InputStream& inner, ArrayPtr<byte> buffer = nullptr);
Chris@47 131 // Creates a buffered stream wrapping the given non-buffered stream. No guarantee is made about
Chris@47 132 // the position of the inner stream after a buffered wrapper has been created unless the entire
Chris@47 133 // input is read.
Chris@47 134 //
Chris@47 135 // If the second parameter is non-null, the stream uses the given buffer instead of allocating
Chris@47 136 // its own. This may improve performance if the buffer can be reused.
Chris@47 137
Chris@47 138 KJ_DISALLOW_COPY(BufferedInputStreamWrapper);
Chris@47 139 ~BufferedInputStreamWrapper() noexcept(false);
Chris@47 140
Chris@47 141 // implements BufferedInputStream ----------------------------------
Chris@47 142 ArrayPtr<const byte> tryGetReadBuffer() override;
Chris@47 143 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
Chris@47 144 void skip(size_t bytes) override;
Chris@47 145
Chris@47 146 private:
Chris@47 147 InputStream& inner;
Chris@47 148 Array<byte> ownedBuffer;
Chris@47 149 ArrayPtr<byte> buffer;
Chris@47 150 ArrayPtr<byte> bufferAvailable;
Chris@47 151 };
Chris@47 152
Chris@47 153 class BufferedOutputStreamWrapper: public BufferedOutputStream {
Chris@47 154 // Implements BufferedOutputStream in terms of an OutputStream. Note that writes to the
Chris@47 155 // underlying stream may be delayed until flush() is called or the wrapper is destroyed.
Chris@47 156
Chris@47 157 public:
Chris@47 158 explicit BufferedOutputStreamWrapper(OutputStream& inner, ArrayPtr<byte> buffer = nullptr);
Chris@47 159 // Creates a buffered stream wrapping the given non-buffered stream.
Chris@47 160 //
Chris@47 161 // If the second parameter is non-null, the stream uses the given buffer instead of allocating
Chris@47 162 // its own. This may improve performance if the buffer can be reused.
Chris@47 163
Chris@47 164 KJ_DISALLOW_COPY(BufferedOutputStreamWrapper);
Chris@47 165 ~BufferedOutputStreamWrapper() noexcept(false);
Chris@47 166
Chris@47 167 void flush();
Chris@47 168 // Force the wrapper to write any remaining bytes in its buffer to the inner stream. Note that
Chris@47 169 // this only flushes this object's buffer; this object has no idea how to flush any other buffers
Chris@47 170 // that may be present in the underlying stream.
Chris@47 171
Chris@47 172 // implements BufferedOutputStream ---------------------------------
Chris@47 173 ArrayPtr<byte> getWriteBuffer() override;
Chris@47 174 void write(const void* buffer, size_t size) override;
Chris@47 175
Chris@47 176 private:
Chris@47 177 OutputStream& inner;
Chris@47 178 Array<byte> ownedBuffer;
Chris@47 179 ArrayPtr<byte> buffer;
Chris@47 180 byte* bufferPos;
Chris@47 181 UnwindDetector unwindDetector;
Chris@47 182 };
Chris@47 183
Chris@47 184 // =======================================================================================
Chris@47 185 // Array I/O
Chris@47 186
Chris@47 187 class ArrayInputStream: public BufferedInputStream {
Chris@47 188 public:
Chris@47 189 explicit ArrayInputStream(ArrayPtr<const byte> array);
Chris@47 190 KJ_DISALLOW_COPY(ArrayInputStream);
Chris@47 191 ~ArrayInputStream() noexcept(false);
Chris@47 192
Chris@47 193 // implements BufferedInputStream ----------------------------------
Chris@47 194 ArrayPtr<const byte> tryGetReadBuffer() override;
Chris@47 195 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
Chris@47 196 void skip(size_t bytes) override;
Chris@47 197
Chris@47 198 private:
Chris@47 199 ArrayPtr<const byte> array;
Chris@47 200 };
Chris@47 201
Chris@47 202 class ArrayOutputStream: public BufferedOutputStream {
Chris@47 203 public:
Chris@47 204 explicit ArrayOutputStream(ArrayPtr<byte> array);
Chris@47 205 KJ_DISALLOW_COPY(ArrayOutputStream);
Chris@47 206 ~ArrayOutputStream() noexcept(false);
Chris@47 207
Chris@47 208 ArrayPtr<byte> getArray() {
Chris@47 209 // Get the portion of the array which has been filled in.
Chris@47 210 return arrayPtr(array.begin(), fillPos);
Chris@47 211 }
Chris@47 212
Chris@47 213 // implements BufferedInputStream ----------------------------------
Chris@47 214 ArrayPtr<byte> getWriteBuffer() override;
Chris@47 215 void write(const void* buffer, size_t size) override;
Chris@47 216
Chris@47 217 private:
Chris@47 218 ArrayPtr<byte> array;
Chris@47 219 byte* fillPos;
Chris@47 220 };
Chris@47 221
Chris@47 222 class VectorOutputStream: public BufferedOutputStream {
Chris@47 223 public:
Chris@47 224 explicit VectorOutputStream(size_t initialCapacity = 4096);
Chris@47 225 KJ_DISALLOW_COPY(VectorOutputStream);
Chris@47 226 ~VectorOutputStream() noexcept(false);
Chris@47 227
Chris@47 228 ArrayPtr<byte> getArray() {
Chris@47 229 // Get the portion of the array which has been filled in.
Chris@47 230 return arrayPtr(vector.begin(), fillPos);
Chris@47 231 }
Chris@47 232
Chris@47 233 // implements BufferedInputStream ----------------------------------
Chris@47 234 ArrayPtr<byte> getWriteBuffer() override;
Chris@47 235 void write(const void* buffer, size_t size) override;
Chris@47 236
Chris@47 237 private:
Chris@47 238 Array<byte> vector;
Chris@47 239 byte* fillPos;
Chris@47 240
Chris@47 241 void grow(size_t minSize);
Chris@47 242 };
Chris@47 243
Chris@47 244 // =======================================================================================
Chris@47 245 // File descriptor I/O
Chris@47 246
Chris@47 247 class AutoCloseFd {
Chris@47 248 // A wrapper around a file descriptor which automatically closes the descriptor when destroyed.
Chris@47 249 // The wrapper supports move construction for transferring ownership of the descriptor. If
Chris@47 250 // close() returns an error, the destructor throws an exception, UNLESS the destructor is being
Chris@47 251 // called during unwind from another exception, in which case the close error is ignored.
Chris@47 252 //
Chris@47 253 // If your code is not exception-safe, you should not use AutoCloseFd. In this case you will
Chris@47 254 // have to call close() yourself and handle errors appropriately.
Chris@47 255
Chris@47 256 public:
Chris@47 257 inline AutoCloseFd(): fd(-1) {}
Chris@47 258 inline AutoCloseFd(decltype(nullptr)): fd(-1) {}
Chris@47 259 inline explicit AutoCloseFd(int fd): fd(fd) {}
Chris@47 260 inline AutoCloseFd(AutoCloseFd&& other) noexcept: fd(other.fd) { other.fd = -1; }
Chris@47 261 KJ_DISALLOW_COPY(AutoCloseFd);
Chris@47 262 ~AutoCloseFd() noexcept(false);
Chris@47 263
Chris@47 264 inline AutoCloseFd& operator=(AutoCloseFd&& other) {
Chris@47 265 AutoCloseFd old(kj::mv(*this));
Chris@47 266 fd = other.fd;
Chris@47 267 other.fd = -1;
Chris@47 268 return *this;
Chris@47 269 }
Chris@47 270
Chris@47 271 inline AutoCloseFd& operator=(decltype(nullptr)) {
Chris@47 272 AutoCloseFd old(kj::mv(*this));
Chris@47 273 return *this;
Chris@47 274 }
Chris@47 275
Chris@47 276 inline operator int() const { return fd; }
Chris@47 277 inline int get() const { return fd; }
Chris@47 278
Chris@47 279 operator bool() const = delete;
Chris@47 280 // Deleting this operator prevents accidental use in boolean contexts, which
Chris@47 281 // the int conversion operator above would otherwise allow.
Chris@47 282
Chris@47 283 inline bool operator==(decltype(nullptr)) { return fd < 0; }
Chris@47 284 inline bool operator!=(decltype(nullptr)) { return fd >= 0; }
Chris@47 285
Chris@47 286 private:
Chris@47 287 int fd;
Chris@47 288 UnwindDetector unwindDetector;
Chris@47 289 };
Chris@47 290
Chris@47 291 inline auto KJ_STRINGIFY(const AutoCloseFd& fd)
Chris@47 292 -> decltype(kj::toCharSequence(implicitCast<int>(fd))) {
Chris@47 293 return kj::toCharSequence(implicitCast<int>(fd));
Chris@47 294 }
Chris@47 295
Chris@47 296 class FdInputStream: public InputStream {
Chris@47 297 // An InputStream wrapping a file descriptor.
Chris@47 298
Chris@47 299 public:
Chris@47 300 explicit FdInputStream(int fd): fd(fd) {}
Chris@47 301 explicit FdInputStream(AutoCloseFd fd): fd(fd), autoclose(mv(fd)) {}
Chris@47 302 KJ_DISALLOW_COPY(FdInputStream);
Chris@47 303 ~FdInputStream() noexcept(false);
Chris@47 304
Chris@47 305 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
Chris@47 306
Chris@47 307 private:
Chris@47 308 int fd;
Chris@47 309 AutoCloseFd autoclose;
Chris@47 310 };
Chris@47 311
Chris@47 312 class FdOutputStream: public OutputStream {
Chris@47 313 // An OutputStream wrapping a file descriptor.
Chris@47 314
Chris@47 315 public:
Chris@47 316 explicit FdOutputStream(int fd): fd(fd) {}
Chris@47 317 explicit FdOutputStream(AutoCloseFd fd): fd(fd), autoclose(mv(fd)) {}
Chris@47 318 KJ_DISALLOW_COPY(FdOutputStream);
Chris@47 319 ~FdOutputStream() noexcept(false);
Chris@47 320
Chris@47 321 void write(const void* buffer, size_t size) override;
Chris@47 322 void write(ArrayPtr<const ArrayPtr<const byte>> pieces) override;
Chris@47 323
Chris@47 324 private:
Chris@47 325 int fd;
Chris@47 326 AutoCloseFd autoclose;
Chris@47 327 };
Chris@47 328
Chris@47 329 } // namespace kj
Chris@47 330
Chris@47 331 #endif // KJ_IO_H_