annotate win64-msvc/include/kj/io.h @ 169:223a55898ab9 tip default

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