annotate win64-msvc/include/capnp/serialize-packed.h @ 74:2f2b27544483

Rebuild win32 Opus using mingw 5 rather than 7 to avoid runtime incompatibility
author Chris Cannam
date Wed, 30 Jan 2019 10:30:56 +0000
parents 0f2d93caa50c
children
rev   line source
Chris@63 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@63 2 // Licensed under the MIT License:
Chris@63 3 //
Chris@63 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@63 5 // of this software and associated documentation files (the "Software"), to deal
Chris@63 6 // in the Software without restriction, including without limitation the rights
Chris@63 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@63 8 // copies of the Software, and to permit persons to whom the Software is
Chris@63 9 // furnished to do so, subject to the following conditions:
Chris@63 10 //
Chris@63 11 // The above copyright notice and this permission notice shall be included in
Chris@63 12 // all copies or substantial portions of the Software.
Chris@63 13 //
Chris@63 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@63 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@63 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@63 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@63 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@63 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@63 20 // THE SOFTWARE.
Chris@63 21
Chris@63 22 #ifndef CAPNP_SERIALIZE_PACKED_H_
Chris@63 23 #define CAPNP_SERIALIZE_PACKED_H_
Chris@63 24
Chris@63 25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
Chris@63 26 #pragma GCC system_header
Chris@63 27 #endif
Chris@63 28
Chris@63 29 #include "serialize.h"
Chris@63 30
Chris@63 31 namespace capnp {
Chris@63 32
Chris@63 33 namespace _ { // private
Chris@63 34
Chris@63 35 class PackedInputStream: public kj::InputStream {
Chris@63 36 // An input stream that unpacks packed data with a picky constraint: The caller must read data
Chris@63 37 // in the exact same size and sequence as the data was written to PackedOutputStream.
Chris@63 38
Chris@63 39 public:
Chris@63 40 explicit PackedInputStream(kj::BufferedInputStream& inner);
Chris@63 41 KJ_DISALLOW_COPY(PackedInputStream);
Chris@63 42 ~PackedInputStream() noexcept(false);
Chris@63 43
Chris@63 44 // implements InputStream ------------------------------------------
Chris@63 45 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
Chris@63 46 void skip(size_t bytes) override;
Chris@63 47
Chris@63 48 private:
Chris@63 49 kj::BufferedInputStream& inner;
Chris@63 50 };
Chris@63 51
Chris@63 52 class PackedOutputStream: public kj::OutputStream {
Chris@63 53 public:
Chris@63 54 explicit PackedOutputStream(kj::BufferedOutputStream& inner);
Chris@63 55 KJ_DISALLOW_COPY(PackedOutputStream);
Chris@63 56 ~PackedOutputStream() noexcept(false);
Chris@63 57
Chris@63 58 // implements OutputStream -----------------------------------------
Chris@63 59 void write(const void* buffer, size_t bytes) override;
Chris@63 60
Chris@63 61 private:
Chris@63 62 kj::BufferedOutputStream& inner;
Chris@63 63 };
Chris@63 64
Chris@63 65 } // namespace _ (private)
Chris@63 66
Chris@63 67 class PackedMessageReader: private _::PackedInputStream, public InputStreamMessageReader {
Chris@63 68 public:
Chris@63 69 PackedMessageReader(kj::BufferedInputStream& inputStream, ReaderOptions options = ReaderOptions(),
Chris@63 70 kj::ArrayPtr<word> scratchSpace = nullptr);
Chris@63 71 KJ_DISALLOW_COPY(PackedMessageReader);
Chris@63 72 ~PackedMessageReader() noexcept(false);
Chris@63 73 };
Chris@63 74
Chris@63 75 class PackedFdMessageReader: private kj::FdInputStream, private kj::BufferedInputStreamWrapper,
Chris@63 76 public PackedMessageReader {
Chris@63 77 public:
Chris@63 78 PackedFdMessageReader(int fd, ReaderOptions options = ReaderOptions(),
Chris@63 79 kj::ArrayPtr<word> scratchSpace = nullptr);
Chris@63 80 // Read message from a file descriptor, without taking ownership of the descriptor.
Chris@63 81 // Note that if you want to reuse the descriptor after the reader is destroyed, you'll need to
Chris@63 82 // seek it, since otherwise the position is unspecified.
Chris@63 83
Chris@63 84 PackedFdMessageReader(kj::AutoCloseFd fd, ReaderOptions options = ReaderOptions(),
Chris@63 85 kj::ArrayPtr<word> scratchSpace = nullptr);
Chris@63 86 // Read a message from a file descriptor, taking ownership of the descriptor.
Chris@63 87
Chris@63 88 KJ_DISALLOW_COPY(PackedFdMessageReader);
Chris@63 89
Chris@63 90 ~PackedFdMessageReader() noexcept(false);
Chris@63 91 };
Chris@63 92
Chris@63 93 void writePackedMessage(kj::BufferedOutputStream& output, MessageBuilder& builder);
Chris@63 94 void writePackedMessage(kj::BufferedOutputStream& output,
Chris@63 95 kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
Chris@63 96 // Write a packed message to a buffered output stream.
Chris@63 97
Chris@63 98 void writePackedMessage(kj::OutputStream& output, MessageBuilder& builder);
Chris@63 99 void writePackedMessage(kj::OutputStream& output,
Chris@63 100 kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
Chris@63 101 // Write a packed message to an unbuffered output stream. If you intend to write multiple messages
Chris@63 102 // in succession, consider wrapping your output in a buffered stream in order to reduce system
Chris@63 103 // call overhead.
Chris@63 104
Chris@63 105 void writePackedMessageToFd(int fd, MessageBuilder& builder);
Chris@63 106 void writePackedMessageToFd(int fd, kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
Chris@63 107 // Write a single packed message to the file descriptor.
Chris@63 108
Chris@63 109 size_t computeUnpackedSizeInWords(kj::ArrayPtr<const byte> packedBytes);
Chris@63 110 // Computes the number of words to which the given packed bytes will unpack. Not intended for use
Chris@63 111 // in performance-sensitive situations.
Chris@63 112
Chris@63 113 // =======================================================================================
Chris@63 114 // inline stuff
Chris@63 115
Chris@63 116 inline void writePackedMessage(kj::BufferedOutputStream& output, MessageBuilder& builder) {
Chris@63 117 writePackedMessage(output, builder.getSegmentsForOutput());
Chris@63 118 }
Chris@63 119
Chris@63 120 inline void writePackedMessage(kj::OutputStream& output, MessageBuilder& builder) {
Chris@63 121 writePackedMessage(output, builder.getSegmentsForOutput());
Chris@63 122 }
Chris@63 123
Chris@63 124 inline void writePackedMessageToFd(int fd, MessageBuilder& builder) {
Chris@63 125 writePackedMessageToFd(fd, builder.getSegmentsForOutput());
Chris@63 126 }
Chris@63 127
Chris@63 128 } // namespace capnp
Chris@63 129
Chris@63 130 #endif // CAPNP_SERIALIZE_PACKED_H_