annotate win32-mingw/include/kj/io.h @ 150:0a1a4a299a5d

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