annotate osx/include/kj/io.h @ 140:59a8758c56b1

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