aws-crt-cpp
C++ wrapper around the aws-c-* libraries. Provides Cross-Platform Transport Protocols and SSL/TLS implementations for C++.
Loading...
Searching...
No Matches
Stream.h
Go to the documentation of this file.
1#pragma once
7#include <aws/crt/Exports.h>
9#include <aws/crt/Types.h>
10#include <aws/io/stream.h>
11
12namespace Aws
13{
14 namespace Crt
15 {
16 namespace Io
17 {
18 using StreamStatus = aws_stream_status;
19
23 using OffsetType = aws_off_t;
24
28 enum class StreamSeekBasis
29 {
30 Begin = AWS_SSB_BEGIN,
31 End = AWS_SSB_END,
32 };
33
34 /***
35 * Interface for building an Object oriented stream that will be honored by the CRT's low-level
36 * aws_input_stream interface. To use, create a subclass of InputStream and define the abstract
37 * functions.
38 */
39 class AWS_CRT_CPP_API InputStream : public std::enable_shared_from_this<InputStream>,
40 public RefCounted<InputStream>
41 {
42 public:
43 virtual ~InputStream();
44
45 InputStream(const InputStream &) = delete;
46 InputStream &operator=(const InputStream &) = delete;
49
50 explicit operator bool() const noexcept { return IsValid(); }
51
55 virtual bool IsValid() const noexcept = 0;
56
58 aws_input_stream *GetUnderlyingStream() noexcept { return &m_underlying_stream; }
59
65 bool Read(ByteBuf &dest) { return aws_input_stream_read(&m_underlying_stream, &dest) == 0; }
66
73 bool Seek(int64_t offset, StreamSeekBasis seekBasis)
74 {
75 return aws_input_stream_seek(&m_underlying_stream, offset, (aws_stream_seek_basis)seekBasis) == 0;
76 }
77
83 bool GetStatus(StreamStatus &status)
84 {
85 return aws_input_stream_get_status(&m_underlying_stream, &status) == 0;
86 }
87
93 bool GetLength(int64_t &length)
94 {
95 return aws_input_stream_get_length(&m_underlying_stream, &length) == 0;
96 }
97
98 protected:
100 aws_input_stream m_underlying_stream;
101
103
104 /***
105 * Read up-to buffer::capacity - buffer::len into buffer::buffer
106 * Increment buffer::len by the amount you read in.
107 *
108 * @return true if nothing went wrong.
109 * Return true even if you read 0 bytes because the end-of-file has been reached.
110 * Return true even if you read 0 bytes because data is not currently available.
111 *
112 * Return false if an actual failure condition occurs,
113 * you SHOULD also raise an error via aws_raise_error().
114 */
115 virtual bool ReadImpl(ByteBuf &buffer) noexcept = 0;
116
117 /***
118 * Read up-to buffer::capacity - buffer::len immediately available bytes into buffer::buffer
119 * Increment buffer::len by the amount you read in.
120 *
121 * @return true if nothing went wrong.
122 * Return true even if you read 0 bytes because the end-of-file has been reached.
123 * Return true even if you read 0 bytes because data is not currently available.
124 *
125 * Return false if an actual failure condition occurs,
126 * you SHOULD also raise an error via aws_raise_error().
127 */
128 virtual bool ReadSomeImpl(ByteBuf &buffer) noexcept = 0;
129
133 virtual StreamStatus GetStatusImpl() const noexcept = 0;
134
139 virtual int64_t GetLengthImpl() const noexcept = 0;
140
150 virtual bool SeekImpl(int64_t offset, StreamSeekBasis seekBasis) noexcept = 0;
151
159 virtual int64_t PeekImpl() const noexcept = 0;
160
161 private:
162 static int s_Seek(aws_input_stream *stream, int64_t offset, enum aws_stream_seek_basis basis);
163 static int s_Read(aws_input_stream *stream, aws_byte_buf *dest);
164 static int s_GetStatus(aws_input_stream *stream, aws_stream_status *status);
165 static int s_GetLength(struct aws_input_stream *stream, int64_t *out_length);
166 static void s_Acquire(aws_input_stream *stream);
167 static void s_Release(aws_input_stream *stream);
168
169 static aws_input_stream_vtable s_vtable;
170 };
171
172 /***
173 * Implementation of Aws::Crt::Io::InputStream that wraps a std::input_stream.
174 */
176 {
177 public:
179 std::shared_ptr<Aws::Crt::Io::IStream> stream,
180 Aws::Crt::Allocator *allocator = ApiAllocator()) noexcept;
181
182 bool IsValid() const noexcept override;
183
184 protected:
185 bool ReadImpl(ByteBuf &buffer) noexcept override;
186 bool ReadSomeImpl(ByteBuf &buffer) noexcept override;
187 StreamStatus GetStatusImpl() const noexcept override;
188 int64_t GetLengthImpl() const noexcept override;
189 bool SeekImpl(OffsetType offsetType, StreamSeekBasis seekBasis) noexcept override;
190 int64_t PeekImpl() const noexcept override;
191
192 private:
193 std::shared_ptr<Aws::Crt::Io::IStream> m_stream;
194 };
195 } // namespace Io
196 } // namespace Crt
197} // namespace Aws
#define AWS_CRT_CPP_API
Definition Exports.h:36
Definition Stream.h:41
virtual bool IsValid() const noexcept=0
InputStream & operator=(const InputStream &)=delete
virtual bool ReadImpl(ByteBuf &buffer) noexcept=0
aws_input_stream m_underlying_stream
Definition Stream.h:100
InputStream & operator=(InputStream &&)=delete
Allocator * m_allocator
Definition Stream.h:99
virtual StreamStatus GetStatusImpl() const noexcept=0
InputStream(InputStream &&)=delete
InputStream(const InputStream &)=delete
bool Seek(int64_t offset, StreamSeekBasis seekBasis)
Definition Stream.h:73
bool Read(ByteBuf &dest)
Definition Stream.h:65
bool GetStatus(StreamStatus &status)
Definition Stream.h:83
virtual bool ReadSomeImpl(ByteBuf &buffer) noexcept=0
bool GetLength(int64_t &length)
Definition Stream.h:93
Definition RefCounted.h:29
aws_stream_status StreamStatus
Definition Stream.h:18
StreamSeekBasis
Definition Stream.h:29
aws_off_t OffsetType
Definition Stream.h:23
aws_allocator Allocator
Definition Allocator.h:14
AWS_CRT_CPP_API Allocator * ApiAllocator() noexcept
Definition Allocator.cpp:24
aws_byte_buf ByteBuf
Definition Types.h:30
Definition Allocator.h:11