|
16 | 16 |
|
17 | 17 | #include <aws/crt/Exports.h>
|
18 | 18 | #include <aws/crt/Types.h>
|
19 |
| - |
20 |
| -struct aws_input_stream; |
| 19 | +#include <aws/io/stream.h> |
21 | 20 |
|
22 | 21 | namespace Aws
|
23 | 22 | {
|
24 | 23 | namespace Crt
|
25 | 24 | {
|
26 | 25 | namespace Io
|
27 | 26 | {
|
28 |
| - /** |
29 |
| - * Factory to create a aws-c-io input stream subclass from a C++ stream |
| 27 | + using StreamStatus = aws_stream_status; |
| 28 | + using OffsetType = aws_off_t; |
| 29 | + |
| 30 | + enum class StreamSeekBasis |
| 31 | + { |
| 32 | + Begin = AWS_SSB_BEGIN, |
| 33 | + End = AWS_SSB_END, |
| 34 | + }; |
| 35 | + |
| 36 | + /*** |
| 37 | + * Interface for building an Object oriented stream that will be honored by the CRT's low-level |
| 38 | + * aws_input_stream interface. To use, create a subclass of InputStream and define the abstract |
| 39 | + * functions. |
| 40 | + */ |
| 41 | + class AWS_CRT_CPP_API InputStream |
| 42 | + { |
| 43 | + public: |
| 44 | + virtual ~InputStream(); |
| 45 | + |
| 46 | + InputStream(const InputStream &) = delete; |
| 47 | + InputStream &operator=(const InputStream &) = delete; |
| 48 | + InputStream(InputStream &&) = delete; |
| 49 | + InputStream &operator=(InputStream &&) = delete; |
| 50 | + |
| 51 | + explicit operator bool() const noexcept { return IsGood(); } |
| 52 | + |
| 53 | + virtual bool IsGood() const noexcept = 0; |
| 54 | + |
| 55 | + aws_input_stream *GetUnderlyingStream() noexcept { return &m_underlying_stream; } |
| 56 | + |
| 57 | + protected: |
| 58 | + Allocator *m_allocator; |
| 59 | + aws_input_stream m_underlying_stream; |
| 60 | + |
| 61 | + InputStream(Aws::Crt::Allocator *allocator = DefaultAllocator()); |
| 62 | + |
| 63 | + /*** |
| 64 | + * Read up-to buffer::capacity - buffer::len into buffer::buffer |
| 65 | + * Increment buffer::len by the amount you read in. |
| 66 | + * |
| 67 | + * @return true on success, false otherwise. Return false, when there is nothing left to read. |
| 68 | + * You SHOULD raise an error via aws_raise_error() |
| 69 | + * if an actual failure condition occurs. |
| 70 | + * If you return false, GetStatusImpl() will be called to determine |
| 71 | + * the validity of the stream. |
| 72 | + */ |
| 73 | + virtual bool ReadImpl(ByteBuf &buffer) noexcept = 0; |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns the current status of the stream. |
| 77 | + */ |
| 78 | + virtual StreamStatus GetStatusImpl() const noexcept = 0; |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the total length of the available data for the stream. |
| 82 | + * Returns -1 if not available. |
| 83 | + */ |
| 84 | + virtual int64_t GetLengthImpl() const noexcept = 0; |
| 85 | + |
| 86 | + /** |
| 87 | + * Seek's the stream to seekBasis based offset bytes. |
| 88 | + * |
| 89 | + * It is expected, that if seeking to the beginning of a stream, |
| 90 | + * all error's are cleared if possible. |
| 91 | + * |
| 92 | + * @return true on success, false otherwise. You SHOULD raise an error via aws_raise_error() |
| 93 | + * if a failure occurs. If you return false, the m_good flag will be set to false. |
| 94 | + */ |
| 95 | + virtual bool SeekImpl(OffsetType offset, StreamSeekBasis seekBasis) noexcept = 0; |
| 96 | + |
| 97 | + private: |
| 98 | + static int s_Seek(aws_input_stream *stream, aws_off_t offset, enum aws_stream_seek_basis basis); |
| 99 | + static int s_Read(aws_input_stream *stream, aws_byte_buf *dest); |
| 100 | + static int s_GetStatus(aws_input_stream *stream, aws_stream_status *status); |
| 101 | + static int s_GetLength(struct aws_input_stream *stream, int64_t *out_length); |
| 102 | + static void s_Destroy(struct aws_input_stream *stream); |
| 103 | + |
| 104 | + static aws_input_stream_vtable s_vtable; |
| 105 | + }; |
| 106 | + |
| 107 | + /*** |
| 108 | + * Implementation of Aws::Crt::Io::InputStream that wraps a std::input_stream. |
30 | 109 | */
|
31 |
| - AWS_CRT_CPP_API aws_input_stream *AwsInputStreamNewCpp( |
32 |
| - const std::shared_ptr<Aws::Crt::Io::IStream> &stream, |
33 |
| - Aws::Crt::Allocator *allocator = DefaultAllocator()) noexcept; |
| 110 | + class AWS_CRT_CPP_API StdIOStreamInputStream : public InputStream |
| 111 | + { |
| 112 | + public: |
| 113 | + StdIOStreamInputStream( |
| 114 | + std::shared_ptr<Aws::Crt::Io::IStream> stream, |
| 115 | + Aws::Crt::Allocator *allocator = DefaultAllocator()) noexcept; |
| 116 | + |
| 117 | + bool IsGood() const noexcept override; |
| 118 | + |
| 119 | + protected: |
| 120 | + bool ReadImpl(ByteBuf &buffer) noexcept override; |
| 121 | + StreamStatus GetStatusImpl() const noexcept override; |
| 122 | + int64_t GetLengthImpl() const noexcept override; |
| 123 | + bool SeekImpl(OffsetType offsetType, StreamSeekBasis seekBasis) noexcept override; |
| 124 | + |
| 125 | + private: |
| 126 | + std::shared_ptr<Aws::Crt::Io::IStream> m_stream; |
| 127 | + }; |
34 | 128 | } // namespace Io
|
35 | 129 | } // namespace Crt
|
36 | 130 | } // namespace Aws
|
0 commit comments