AWS IoT Device SDK C++ v2: crt/aws-crt-cpp/include/aws/crt/io/Stream.h Source File

AWS IoT Device SDK C++ v2 1.14.0 AWS IoT Device SDK C++ v2

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 */
38 class AWS_CRT_CPP_API InputStream
39 {
40 public:
41 virtual ~InputStream();
42
43 InputStream(const InputStream &) = delete;
44 InputStream &operator=(const InputStream &) = delete;
45 InputStream(InputStream &&) = delete;
46 InputStream &operator=(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:
97 Allocator *m_allocator;
98 aws_input_stream m_underlying_stream;
99
100 InputStream(Aws::Crt::Allocator *allocator = g_allocator);
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 */
147 class AWS_CRT_CPP_API StdIOStreamInputStream : public InputStream
148 {
149 public:
150 StdIOStreamInputStream(
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
Types.h
Aws::Crt::Io::InputStream
Definition: Stream.h:39
Aws::Crt::Io::InputStream::IsValid
virtual bool IsValid() const noexcept=0
Aws::Crt::Io::InputStream::operator=
InputStream & operator=(const InputStream &)=delete
Aws::Crt::Io::InputStream::ReadImpl
virtual bool ReadImpl(ByteBuf &buffer) noexcept=0
Aws::Crt::Io::InputStream::m_underlying_stream
aws_input_stream m_underlying_stream
Definition: Stream.h:98
Aws::Crt::Io::InputStream::operator=
InputStream & operator=(InputStream &&)=delete
Aws::Crt::Io::InputStream::m_allocator
Allocator * m_allocator
Definition: Stream.h:97
Aws::Crt::Io::InputStream::GetStatusImpl
virtual StreamStatus GetStatusImpl() const noexcept=0
Aws::Crt::Io::InputStream::InputStream
InputStream(InputStream &&)=delete
Aws::Crt::Io::InputStream::InputStream
InputStream(const InputStream &)=delete
Aws::Crt::Io::InputStream::Seek
bool Seek(int64_t offset, StreamSeekBasis seekBasis)
Definition: Stream.h:71
Aws::Crt::Io::InputStream::Read
bool Read(ByteBuf &dest)
Definition: Stream.h:63
Aws::Crt::Io::InputStream::GetStatus
bool GetStatus(StreamStatus &status)
Definition: Stream.h:81
Aws::Crt::Io::InputStream::GetLength
bool GetLength(int64_t &length)
Definition: Stream.h:91
Aws::Crt::Io::StdIOStreamInputStream
Definition: Stream.h:148
Exports.h
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Aws::Crt::Io::StreamStatus
aws_stream_status StreamStatus
Definition: Stream.h:17
Aws::Crt::Io::StreamSeekBasis
StreamSeekBasis
Definition: Stream.h:28
Aws::Crt::Io::StreamSeekBasis::Begin
@ Begin
Aws::Crt::Io::StreamSeekBasis::End
@ End
Aws::Crt::Io::OffsetType
aws_off_t OffsetType
Definition: Stream.h:22
Aws::Crt::Allocator
aws_allocator Allocator
Definition: StlAllocator.h:17
Aws::Crt::g_allocator
AWS_CRT_CPP_API Allocator * g_allocator
Definition: Api.cpp:21
Aws::Crt::ByteBuf
aws_byte_buf ByteBuf
Definition: Types.h:32
Aws
Definition: Api.h:17
Aws::offset
buffer offset
Definition: cJSON.cpp:1128
Aws::length
char const int length
Definition: cJSON.h:181
Aws::buffer
char * buffer
Definition: cJSON.h:181