aws-crt-cpp
C++ wrapper around the aws-c-* libraries. Provides Cross-Platform Transport Protocols and SSL/TLS implementations for C++.
Stream.h
Go to the documentation of this file.
1#pragma once
7#include <aws/crt/Exports.h>
8#include <aws/crt/Types.h>
9#include <aws/io/stream.h>
10
11namespace Aws
12{
13 namespace Crt
14 {
15 namespace Io
16 {
17 using StreamStatus = aws_stream_status;
18
22 using OffsetType = aws_off_t;
23
27 enum class StreamSeekBasis
28 {
29 Begin = AWS_SSB_BEGIN,
30 End = AWS_SSB_END,
31 };
32
33 /***
34 * Interface for building an Object oriented stream that will be honored by the CRT's low-level
35 * aws_input_stream interface. To use, create a subclass of InputStream and define the abstract
36 * functions.
37 */
39 {
40 public:
41 virtual ~InputStream();
42
43 InputStream(const InputStream &) = delete;
44 InputStream &operator=(const InputStream &) = delete;
47
48 explicit operator bool() const noexcept { return IsValid(); }
49
53 virtual bool IsValid() const noexcept = 0;
54
56 aws_input_stream *GetUnderlyingStream() noexcept { return &m_underlying_stream; }
57
63 bool Read(ByteBuf &dest) { return aws_input_stream_read(&m_underlying_stream, &dest) == 0; }
64
71 bool Seek(int64_t offset, StreamSeekBasis seekBasis)
72 {
73 return aws_input_stream_seek(&m_underlying_stream, offset, (aws_stream_seek_basis)seekBasis) == 0;
74 }
75
81 bool GetStatus(StreamStatus &status)
82 {
83 return aws_input_stream_get_status(&m_underlying_stream, &status) == 0;
84 }
85
91 bool GetLength(int64_t &length)
92 {
93 return aws_input_stream_get_length(&m_underlying_stream, &length) == 0;
94 }
95
96 protected:
98 aws_input_stream m_underlying_stream;
99
101
102 /***
103 * Read up-to buffer::capacity - buffer::len into buffer::buffer
104 * Increment buffer::len by the amount you read in.
105 *
106 * @return true on success, false otherwise. Return false, when there is nothing left to read.
107 * You SHOULD raise an error via aws_raise_error()
108 * if an actual failure condition occurs.
109 */
110 virtual bool ReadImpl(ByteBuf &buffer) noexcept = 0;
111
115 virtual StreamStatus GetStatusImpl() const noexcept = 0;
116
121 virtual int64_t GetLengthImpl() const noexcept = 0;
122
132 virtual bool SeekImpl(int64_t offset, StreamSeekBasis seekBasis) noexcept = 0;
133
134 private:
135 static int s_Seek(aws_input_stream *stream, int64_t offset, enum aws_stream_seek_basis basis);
136 static int s_Read(aws_input_stream *stream, aws_byte_buf *dest);
137 static int s_GetStatus(aws_input_stream *stream, aws_stream_status *status);
138 static int s_GetLength(struct aws_input_stream *stream, int64_t *out_length);
139 static void s_Destroy(struct aws_input_stream *stream);
140
141 static aws_input_stream_vtable s_vtable;
142 };
143
144 /***
145 * Implementation of Aws::Crt::Io::InputStream that wraps a std::input_stream.
146 */
148 {
149 public:
151 std::shared_ptr<Aws::Crt::Io::IStream> stream,
152 Aws::Crt::Allocator *allocator = g_allocator) noexcept;
153
154 bool IsValid() const noexcept override;
155
156 protected:
157 bool ReadImpl(ByteBuf &buffer) noexcept override;
158 StreamStatus GetStatusImpl() const noexcept override;
159 int64_t GetLengthImpl() const noexcept override;
160 bool SeekImpl(OffsetType offsetType, StreamSeekBasis seekBasis) noexcept override;
161
162 private:
163 std::shared_ptr<Aws::Crt::Io::IStream> m_stream;
164 };
165 } // namespace Io
166 } // namespace Crt
167} // namespace Aws
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Definition: Stream.h:39
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:98
InputStream & operator=(InputStream &&)=delete
Allocator * m_allocator
Definition: Stream.h:97
virtual StreamStatus GetStatusImpl() const noexcept=0
InputStream(InputStream &&)=delete
InputStream(const InputStream &)=delete
bool Seek(int64_t offset, StreamSeekBasis seekBasis)
Definition: Stream.h:71
bool Read(ByteBuf &dest)
Definition: Stream.h:63
bool GetStatus(StreamStatus &status)
Definition: Stream.h:81
bool GetLength(int64_t &length)
Definition: Stream.h:91
Definition: Stream.h:148
aws_stream_status StreamStatus
Definition: Stream.h:17
StreamSeekBasis
Definition: Stream.h:28
aws_off_t OffsetType
Definition: Stream.h:22
aws_allocator Allocator
Definition: StlAllocator.h:17
AWS_CRT_CPP_API Allocator * g_allocator
Definition: Api.cpp:23
aws_byte_buf ByteBuf
Definition: Types.h:32
Definition: Api.h:14
buffer offset
Definition: cJSON.cpp:1128
char const int length
Definition: cJSON.h:181
char * buffer
Definition: cJSON.h:181