annotate win32-mingw/include/kj/io.h @ 50:37d53a7e8262

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