AWS IoT Device SDK C++ v2  1.34.0
AWS IoT Device SDK C++ v2
Stream.h
Go to the documentation of this file.
1 #pragma once
2 
7 #include <aws/crt/Exports.h>
8 #include <aws/crt/RefCounted.h>
9 #include <aws/crt/Types.h>
10 #include <aws/io/stream.h>
11 
12 namespace 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;
47  InputStream(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
Aws::Crt::Io::InputStream::operator=
InputStream & operator=(InputStream &&)=delete
Aws::Crt::Io::InputStream::GetStatusImpl
virtual StreamStatus GetStatusImpl() const noexcept=0
Aws::Crt::ApiAllocator
AWS_CRT_CPP_API Allocator * ApiAllocator() noexcept
Definition: Allocator.cpp:24
Aws::Crt::Io::InputStream::GetLength
bool GetLength(int64_t &length)
Definition: Stream.h:93
Aws::Crt::Io::InputStream::Read
bool Read(ByteBuf &dest)
Definition: Stream.h:65
Aws::Crt::Io::InputStream::Seek
bool Seek(int64_t offset, StreamSeekBasis seekBasis)
Definition: Stream.h:73
Aws::Crt::Io::InputStream::GetStatus
bool GetStatus(StreamStatus &status)
Definition: Stream.h:83
Aws::Crt::Io::StreamSeekBasis::Begin
@ Begin
Aws::Crt::Io::InputStream::m_underlying_stream
aws_input_stream m_underlying_stream
Definition: Stream.h:100
Aws::Crt::Io::InputStream::InputStream
InputStream(const InputStream &)=delete
Aws::Crt::Io::InputStream::ReadImpl
virtual bool ReadImpl(ByteBuf &buffer) noexcept=0
Aws::Crt::Io::StdIOStreamInputStream
Definition: Stream.h:176
RefCounted.h
Aws::Crt::RefCounted
Definition: RefCounted.h:29
Aws::Crt::Io::StreamSeekBasis
StreamSeekBasis
Definition: Stream.h:29
Aws::Crt::Io::InputStream
Definition: Stream.h:41
Aws::Crt::Io::InputStream::m_allocator
Allocator * m_allocator
Definition: Stream.h:99
Types.h
Aws::Crt::Io::InputStream::IsValid
virtual bool IsValid() const noexcept=0
Aws::Crt::ByteBuf
aws_byte_buf ByteBuf
Definition: Types.h:30
Aws::Crt::Io::InputStream::operator=
InputStream & operator=(const InputStream &)=delete
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Aws
Definition: Allocator.h:11
Aws::Crt::Allocator
aws_allocator Allocator
Definition: Allocator.h:14
Exports.h
Aws::Crt::Io::StreamStatus
aws_stream_status StreamStatus
Definition: Stream.h:18
Aws::Crt::Io::InputStream::InputStream
InputStream(InputStream &&)=delete
Aws::Crt::Io::OffsetType
aws_off_t OffsetType
Definition: Stream.h:23
Aws::Crt::Io::InputStream::ReadSomeImpl
virtual bool ReadSomeImpl(ByteBuf &buffer) noexcept=0