annotate win64-msvc/include/kj/io.h @ 132:42a73082be24

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