annotate win32-mingw/include/capnp/serialize-packed.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 CAPNP_SERIALIZE_PACKED_H_
Chris@50 23 #define CAPNP_SERIALIZE_PACKED_H_
Chris@50 24
Chris@50 25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
Chris@50 26 #pragma GCC system_header
Chris@50 27 #endif
Chris@50 28
Chris@50 29 #include "serialize.h"
Chris@50 30
Chris@50 31 namespace capnp {
Chris@50 32
Chris@50 33 namespace _ { // private
Chris@50 34
Chris@50 35 class PackedInputStream: public kj::InputStream {
Chris@50 36 // An input stream that unpacks packed data with a picky constraint: The caller must read data
Chris@50 37 // in the exact same size and sequence as the data was written to PackedOutputStream.
Chris@50 38
Chris@50 39 public:
Chris@50 40 explicit PackedInputStream(kj::BufferedInputStream& inner);
Chris@50 41 KJ_DISALLOW_COPY(PackedInputStream);
Chris@50 42 ~PackedInputStream() noexcept(false);
Chris@50 43
Chris@50 44 // implements InputStream ------------------------------------------
Chris@50 45 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
Chris@50 46 void skip(size_t bytes) override;
Chris@50 47
Chris@50 48 private:
Chris@50 49 kj::BufferedInputStream& inner;
Chris@50 50 };
Chris@50 51
Chris@50 52 class PackedOutputStream: public kj::OutputStream {
Chris@50 53 public:
Chris@50 54 explicit PackedOutputStream(kj::BufferedOutputStream& inner);
Chris@50 55 KJ_DISALLOW_COPY(PackedOutputStream);
Chris@50 56 ~PackedOutputStream() noexcept(false);
Chris@50 57
Chris@50 58 // implements OutputStream -----------------------------------------
Chris@50 59 void write(const void* buffer, size_t bytes) override;
Chris@50 60
Chris@50 61 private:
Chris@50 62 kj::BufferedOutputStream& inner;
Chris@50 63 };
Chris@50 64
Chris@50 65 } // namespace _ (private)
Chris@50 66
Chris@50 67 class PackedMessageReader: private _::PackedInputStream, public InputStreamMessageReader {
Chris@50 68 public:
Chris@50 69 PackedMessageReader(kj::BufferedInputStream& inputStream, ReaderOptions options = ReaderOptions(),
Chris@50 70 kj::ArrayPtr<word> scratchSpace = nullptr);
Chris@50 71 KJ_DISALLOW_COPY(PackedMessageReader);
Chris@50 72 ~PackedMessageReader() noexcept(false);
Chris@50 73 };
Chris@50 74
Chris@50 75 class PackedFdMessageReader: private kj::FdInputStream, private kj::BufferedInputStreamWrapper,
Chris@50 76 public PackedMessageReader {
Chris@50 77 public:
Chris@50 78 PackedFdMessageReader(int fd, ReaderOptions options = ReaderOptions(),
Chris@50 79 kj::ArrayPtr<word> scratchSpace = nullptr);
Chris@50 80 // Read message from a file descriptor, without taking ownership of the descriptor.
Chris@50 81 // Note that if you want to reuse the descriptor after the reader is destroyed, you'll need to
Chris@50 82 // seek it, since otherwise the position is unspecified.
Chris@50 83
Chris@50 84 PackedFdMessageReader(kj::AutoCloseFd fd, ReaderOptions options = ReaderOptions(),
Chris@50 85 kj::ArrayPtr<word> scratchSpace = nullptr);
Chris@50 86 // Read a message from a file descriptor, taking ownership of the descriptor.
Chris@50 87
Chris@50 88 KJ_DISALLOW_COPY(PackedFdMessageReader);
Chris@50 89
Chris@50 90 ~PackedFdMessageReader() noexcept(false);
Chris@50 91 };
Chris@50 92
Chris@50 93 void writePackedMessage(kj::BufferedOutputStream& output, MessageBuilder& builder);
Chris@50 94 void writePackedMessage(kj::BufferedOutputStream& output,
Chris@50 95 kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
Chris@50 96 // Write a packed message to a buffered output stream.
Chris@50 97
Chris@50 98 void writePackedMessage(kj::OutputStream& output, MessageBuilder& builder);
Chris@50 99 void writePackedMessage(kj::OutputStream& output,
Chris@50 100 kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
Chris@50 101 // Write a packed message to an unbuffered output stream. If you intend to write multiple messages
Chris@50 102 // in succession, consider wrapping your output in a buffered stream in order to reduce system
Chris@50 103 // call overhead.
Chris@50 104
Chris@50 105 void writePackedMessageToFd(int fd, MessageBuilder& builder);
Chris@50 106 void writePackedMessageToFd(int fd, kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
Chris@50 107 // Write a single packed message to the file descriptor.
Chris@50 108
Chris@50 109 size_t computeUnpackedSizeInWords(kj::ArrayPtr<const byte> packedBytes);
Chris@50 110 // Computes the number of words to which the given packed bytes will unpack. Not intended for use
Chris@50 111 // in performance-sensitive situations.
Chris@50 112
Chris@50 113 // =======================================================================================
Chris@50 114 // inline stuff
Chris@50 115
Chris@50 116 inline void writePackedMessage(kj::BufferedOutputStream& output, MessageBuilder& builder) {
Chris@50 117 writePackedMessage(output, builder.getSegmentsForOutput());
Chris@50 118 }
Chris@50 119
Chris@50 120 inline void writePackedMessage(kj::OutputStream& output, MessageBuilder& builder) {
Chris@50 121 writePackedMessage(output, builder.getSegmentsForOutput());
Chris@50 122 }
Chris@50 123
Chris@50 124 inline void writePackedMessageToFd(int fd, MessageBuilder& builder) {
Chris@50 125 writePackedMessageToFd(fd, builder.getSegmentsForOutput());
Chris@50 126 }
Chris@50 127
Chris@50 128 } // namespace capnp
Chris@50 129
Chris@50 130 #endif // CAPNP_SERIALIZE_PACKED_H_