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
24 enum class StreamSeekBasis
25 {
26 Begin = AWS_SSB_BEGIN,
27 End = AWS_SSB_END,
28 };
29
30 /***
31 * Interface for building an Object oriented stream that will be honored by the CRT's low-level
32 * aws_input_stream interface. To use, create a subclass of InputStream and define the abstract
33 * functions.
34 */
36 {
37 public:
38 virtual ~InputStream();
39
40 InputStream(const InputStream &) = delete;
41 InputStream &operator=(const InputStream &) = delete;
44
45 explicit operator bool() const noexcept { return IsValid(); }
46 virtual bool IsValid() const noexcept = 0;
47
48 aws_input_stream *GetUnderlyingStream() noexcept { return &m_underlying_stream; }
49
50 bool Read(ByteBuf &dest) { return aws_input_stream_read(&m_underlying_stream, &dest) == 0; }
51 bool Seek(int64_t offset, StreamSeekBasis seekBasis)
52 {
53 return aws_input_stream_seek(&m_underlying_stream, offset, (aws_stream_seek_basis)seekBasis) == 0;
54 }
55 bool GetStatus(StreamStatus &status)
56 {
57 return aws_input_stream_get_status(&m_underlying_stream, &status) == 0;
58 }
59 bool GetLength(int64_t &length)
60 {
61 return aws_input_stream_get_length(&m_underlying_stream, &length) == 0;
62 }
63
64 protected:
66 aws_input_stream m_underlying_stream;
67
69
70 /***
71 * Read up-to buffer::capacity - buffer::len into buffer::buffer
72 * Increment buffer::len by the amount you read in.
73 *
74 * @return true on success, false otherwise. Return false, when there is nothing left to read.
75 * You SHOULD raise an error via aws_raise_error()
76 * if an actual failure condition occurs.
77 */
78 virtual bool ReadImpl(ByteBuf &buffer) noexcept = 0;
79
83 virtual StreamStatus GetStatusImpl() const noexcept = 0;
84
89 virtual int64_t GetLengthImpl() const noexcept = 0;
90
100 virtual bool SeekImpl(int64_t offset, StreamSeekBasis seekBasis) noexcept = 0;
101
102 private:
103 static int s_Seek(aws_input_stream *stream, int64_t offset, enum aws_stream_seek_basis basis);
104 static int s_Read(aws_input_stream *stream, aws_byte_buf *dest);
105 static int s_GetStatus(aws_input_stream *stream, aws_stream_status *status);
106 static int s_GetLength(struct aws_input_stream *stream, int64_t *out_length);
107 static void s_Destroy(struct aws_input_stream *stream);
108
109 static aws_input_stream_vtable s_vtable;
110 };
111
112 /***
113 * Implementation of Aws::Crt::Io::InputStream that wraps a std::input_stream.
114 */
116 {
117 public:
119 std::shared_ptr<Aws::Crt::Io::IStream> stream,
120 Aws::Crt::Allocator *allocator = g_allocator) noexcept;
121
122 bool IsValid() const noexcept override;
123
124 protected:
125 bool ReadImpl(ByteBuf &buffer) noexcept override;
126 StreamStatus GetStatusImpl() const noexcept override;
127 int64_t GetLengthImpl() const noexcept override;
128 bool SeekImpl(OffsetType offsetType, StreamSeekBasis seekBasis) noexcept override;
129
130 private:
131 std::shared_ptr<Aws::Crt::Io::IStream> m_stream;
132 };
133 } // namespace Io
134 } // namespace Crt
135} // namespace Aws
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Definition: Stream.h:36
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:66
InputStream & operator=(InputStream &&)=delete
Allocator * m_allocator
Definition: Stream.h:65
virtual StreamStatus GetStatusImpl() const noexcept=0
InputStream(InputStream &&)=delete
InputStream(const InputStream &)=delete
bool Seek(int64_t offset, StreamSeekBasis seekBasis)
Definition: Stream.h:51
bool Read(ByteBuf &dest)
Definition: Stream.h:50
bool GetStatus(StreamStatus &status)
Definition: Stream.h:55
bool GetLength(int64_t &length)
Definition: Stream.h:59
Definition: Stream.h:116
aws_stream_status StreamStatus
Definition: Stream.h:17
StreamSeekBasis
Definition: Stream.h:25
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:21
aws_byte_buf ByteBuf
Definition: Types.h:32
Definition: Api.h:17
buffer offset
Definition: cJSON.cpp:1128
char const int length
Definition: cJSON.h:181
char * buffer
Definition: cJSON.h:181