comparison include/llvm/Support/BinaryStream.h @ 147:c2174574ed3a

LLVM 10
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 16:55:33 +0900
parents 3a76565eade5
children
comparison
equal deleted inserted replaced
134:3a76565eade5 147:c2174574ed3a
1 //===- BinaryStream.h - Base interface for a stream of data -----*- C++ -*-===// 1 //===- BinaryStream.h - Base interface for a stream of data -----*- C++ -*-===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 4 // See https://llvm.org/LICENSE.txt for license information.
5 // This file is distributed under the University of Illinois Open Source 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // License. See LICENSE.TXT for details.
7 // 6 //
8 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
9 8
10 #ifndef LLVM_SUPPORT_BINARYSTREAM_H 9 #ifndef LLVM_SUPPORT_BINARYSTREAM_H
11 #define LLVM_SUPPORT_BINARYSTREAM_H 10 #define LLVM_SUPPORT_BINARYSTREAM_H
24 BSF_Write = 1, // Stream supports writing. 23 BSF_Write = 1, // Stream supports writing.
25 BSF_Append = 2, // Writing can occur at offset == length. 24 BSF_Append = 2, // Writing can occur at offset == length.
26 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ BSF_Append) 25 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ BSF_Append)
27 }; 26 };
28 27
29 /// \brief An interface for accessing data in a stream-like format, but which 28 /// An interface for accessing data in a stream-like format, but which
30 /// discourages copying. Instead of specifying a buffer in which to copy 29 /// discourages copying. Instead of specifying a buffer in which to copy
31 /// data on a read, the API returns an ArrayRef to data owned by the stream's 30 /// data on a read, the API returns an ArrayRef to data owned by the stream's
32 /// implementation. Since implementations may not necessarily store data in a 31 /// implementation. Since implementations may not necessarily store data in a
33 /// single contiguous buffer (or even in memory at all), in such cases a it may 32 /// single contiguous buffer (or even in memory at all), in such cases a it may
34 /// be necessary for an implementation to cache such a buffer so that it can 33 /// be necessary for an implementation to cache such a buffer so that it can
37 public: 36 public:
38 virtual ~BinaryStream() = default; 37 virtual ~BinaryStream() = default;
39 38
40 virtual llvm::support::endianness getEndian() const = 0; 39 virtual llvm::support::endianness getEndian() const = 0;
41 40
42 /// \brief Given an offset into the stream and a number of bytes, attempt to 41 /// Given an offset into the stream and a number of bytes, attempt to
43 /// read the bytes and set the output ArrayRef to point to data owned by the 42 /// read the bytes and set the output ArrayRef to point to data owned by the
44 /// stream. 43 /// stream.
45 virtual Error readBytes(uint32_t Offset, uint32_t Size, 44 virtual Error readBytes(uint32_t Offset, uint32_t Size,
46 ArrayRef<uint8_t> &Buffer) = 0; 45 ArrayRef<uint8_t> &Buffer) = 0;
47 46
48 /// \brief Given an offset into the stream, read as much as possible without 47 /// Given an offset into the stream, read as much as possible without
49 /// copying any data. 48 /// copying any data.
50 virtual Error readLongestContiguousChunk(uint32_t Offset, 49 virtual Error readLongestContiguousChunk(uint32_t Offset,
51 ArrayRef<uint8_t> &Buffer) = 0; 50 ArrayRef<uint8_t> &Buffer) = 0;
52 51
53 /// \brief Return the number of bytes of data in this stream. 52 /// Return the number of bytes of data in this stream.
54 virtual uint32_t getLength() = 0; 53 virtual uint32_t getLength() = 0;
55 54
56 /// \brief Return the properties of this stream. 55 /// Return the properties of this stream.
57 virtual BinaryStreamFlags getFlags() const { return BSF_None; } 56 virtual BinaryStreamFlags getFlags() const { return BSF_None; }
58 57
59 protected: 58 protected:
60 Error checkOffsetForRead(uint32_t Offset, uint32_t DataSize) { 59 Error checkOffsetForRead(uint32_t Offset, uint32_t DataSize) {
61 if (Offset > getLength()) 60 if (Offset > getLength())
64 return make_error<BinaryStreamError>(stream_error_code::stream_too_short); 63 return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
65 return Error::success(); 64 return Error::success();
66 } 65 }
67 }; 66 };
68 67
69 /// \brief A BinaryStream which can be read from as well as written to. Note 68 /// A BinaryStream which can be read from as well as written to. Note
70 /// that writing to a BinaryStream always necessitates copying from the input 69 /// that writing to a BinaryStream always necessitates copying from the input
71 /// buffer to the stream's backing store. Streams are assumed to be buffered 70 /// buffer to the stream's backing store. Streams are assumed to be buffered
72 /// so that to be portable it is necessary to call commit() on the stream when 71 /// so that to be portable it is necessary to call commit() on the stream when
73 /// all data has been written. 72 /// all data has been written.
74 class WritableBinaryStream : public BinaryStream { 73 class WritableBinaryStream : public BinaryStream {
75 public: 74 public:
76 ~WritableBinaryStream() override = default; 75 ~WritableBinaryStream() override = default;
77 76
78 /// \brief Attempt to write the given bytes into the stream at the desired 77 /// Attempt to write the given bytes into the stream at the desired
79 /// offset. This will always necessitate a copy. Cannot shrink or grow the 78 /// offset. This will always necessitate a copy. Cannot shrink or grow the
80 /// stream, only writes into existing allocated space. 79 /// stream, only writes into existing allocated space.
81 virtual Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) = 0; 80 virtual Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) = 0;
82 81
83 /// \brief For buffered streams, commits changes to the backing store. 82 /// For buffered streams, commits changes to the backing store.
84 virtual Error commit() = 0; 83 virtual Error commit() = 0;
85 84
86 /// \brief Return the properties of this stream. 85 /// Return the properties of this stream.
87 BinaryStreamFlags getFlags() const override { return BSF_Write; } 86 BinaryStreamFlags getFlags() const override { return BSF_Write; }
88 87
89 protected: 88 protected:
90 Error checkOffsetForWrite(uint32_t Offset, uint32_t DataSize) { 89 Error checkOffsetForWrite(uint32_t Offset, uint32_t DataSize) {
91 if (!(getFlags() & BSF_Append)) 90 if (!(getFlags() & BSF_Append))