AWS IoT Device SDK C++ v2  1.33.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 
120  virtual StreamStatus GetStatusImpl() const noexcept = 0;
121 
126  virtual int64_t GetLengthImpl() const noexcept = 0;
127 
137  virtual bool SeekImpl(int64_t offset, StreamSeekBasis seekBasis) noexcept = 0;
138 
139  private:
140  static int s_Seek(aws_input_stream *stream, int64_t offset, enum aws_stream_seek_basis basis);
141  static int s_Read(aws_input_stream *stream, aws_byte_buf *dest);
142  static int s_GetStatus(aws_input_stream *stream, aws_stream_status *status);
143  static int s_GetLength(struct aws_input_stream *stream, int64_t *out_length);
144  static void s_Acquire(aws_input_stream *stream);
145  static void s_Release(aws_input_stream *stream);
146 
147  static aws_input_stream_vtable s_vtable;
148  };
149 
150  /***
151  * Implementation of Aws::Crt::Io::InputStream that wraps a std::input_stream.
152  */
154  {
155  public:
157  std::shared_ptr<Aws::Crt::Io::IStream> stream,
158  Aws::Crt::Allocator *allocator = ApiAllocator()) noexcept;
159 
160  bool IsValid() const noexcept override;
161 
162  protected:
163  bool ReadImpl(ByteBuf &buffer) noexcept override;
164  StreamStatus GetStatusImpl() const noexcept override;
165  int64_t GetLengthImpl() const noexcept override;
166  bool SeekImpl(OffsetType offsetType, StreamSeekBasis seekBasis) noexcept override;
167 
168  private:
169  std::shared_ptr<Aws::Crt::Io::IStream> m_stream;
170  };
171  } // namespace Io
172  } // namespace Crt
173 } // 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:154
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