aws-crt-cpp
Stream.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License").
6  * You may not use this file except in compliance with the License.
7  * A copy of the License is located at
8  *
9  * http://aws.amazon.com/apache2.0
10  *
11  * or in the "license" file accompanying this file. This file is distributed
12  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 
17 #include <aws/crt/Exports.h>
18 #include <aws/crt/Types.h>
19 #include <aws/io/stream.h>
20 
21 namespace Aws
22 {
23  namespace Crt
24  {
25  namespace Io
26  {
27  using StreamStatus = aws_stream_status;
28  using OffsetType = aws_off_t;
29 
30  enum class StreamSeekBasis
31  {
32  Begin = AWS_SSB_BEGIN,
33  End = AWS_SSB_END,
34  };
35 
36  /***
37  * Interface for building an Object oriented stream that will be honored by the CRT's low-level
38  * aws_input_stream interface. To use, create a subclass of InputStream and define the abstract
39  * functions.
40  */
42  {
43  public:
44  virtual ~InputStream();
45 
46  InputStream(const InputStream &) = delete;
47  InputStream &operator=(const InputStream &) = delete;
48  InputStream(InputStream &&) = delete;
49  InputStream &operator=(InputStream &&) = delete;
50 
51  explicit operator bool() const noexcept { return IsValid(); }
52  virtual bool IsValid() const noexcept = 0;
53 
54  aws_input_stream *GetUnderlyingStream() noexcept { return &m_underlying_stream; }
55 
56  bool Read(ByteBuf &dest) { return aws_input_stream_read(&m_underlying_stream, &dest) == 0; }
57  bool Seek(OffsetType offset, StreamSeekBasis seekBasis)
58  {
59  return aws_input_stream_seek(&m_underlying_stream, offset, (aws_stream_seek_basis)seekBasis) == 0;
60  }
61  bool GetStatus(StreamStatus &status)
62  {
63  return aws_input_stream_get_status(&m_underlying_stream, &status) == 0;
64  }
65  bool GetLength(int64_t &length)
66  {
67  return aws_input_stream_get_length(&m_underlying_stream, &length) == 0;
68  }
69 
70  protected:
71  Allocator *m_allocator;
72  aws_input_stream m_underlying_stream;
73 
75 
76  /***
77  * Read up-to buffer::capacity - buffer::len into buffer::buffer
78  * Increment buffer::len by the amount you read in.
79  *
80  * @return true on success, false otherwise. Return false, when there is nothing left to read.
81  * You SHOULD raise an error via aws_raise_error()
82  * if an actual failure condition occurs.
83  */
84  virtual bool ReadImpl(ByteBuf &buffer) noexcept = 0;
85 
89  virtual StreamStatus GetStatusImpl() const noexcept = 0;
90 
95  virtual int64_t GetLengthImpl() const noexcept = 0;
96 
106  virtual bool SeekImpl(OffsetType offset, StreamSeekBasis seekBasis) noexcept = 0;
107 
108  private:
109  static int s_Seek(aws_input_stream *stream, aws_off_t offset, enum aws_stream_seek_basis basis);
110  static int s_Read(aws_input_stream *stream, aws_byte_buf *dest);
111  static int s_GetStatus(aws_input_stream *stream, aws_stream_status *status);
112  static int s_GetLength(struct aws_input_stream *stream, int64_t *out_length);
113  static void s_Destroy(struct aws_input_stream *stream);
114 
115  static aws_input_stream_vtable s_vtable;
116  };
117 
118  /***
119  * Implementation of Aws::Crt::Io::InputStream that wraps a std::input_stream.
120  */
122  {
123  public:
125  std::shared_ptr<Aws::Crt::Io::IStream> stream,
126  Aws::Crt::Allocator *allocator = g_allocator) noexcept;
127 
128  bool IsValid() const noexcept override;
129 
130  protected:
131  bool ReadImpl(ByteBuf &buffer) noexcept override;
132  StreamStatus GetStatusImpl() const noexcept override;
133  int64_t GetLengthImpl() const noexcept override;
134  bool SeekImpl(OffsetType offsetType, StreamSeekBasis seekBasis) noexcept override;
135 
136  private:
137  std::shared_ptr<Aws::Crt::Io::IStream> m_stream;
138  };
139  } // namespace Io
140  } // namespace Crt
141 } // namespace Aws
Aws::Crt::Allocator
aws_allocator Allocator
Definition: StlAllocator.h:25
Aws::length
size_t length
Definition: cJSON.h:159
Types.h
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:34
Aws::Crt::Io::StreamSeekBasis
StreamSeekBasis
Definition: Stream.h:31
Aws::Crt::g_allocator
Allocator * g_allocator
Definition: Api.cpp:28
Aws::buffer
p buffer
Definition: cJSON.cpp:1033
Aws::Crt::ByteBuf
aws_byte_buf ByteBuf
Definition: Types.h:43
Aws::Crt::Io::StreamSeekBasis::End
@ End
Aws::Crt::Io::OffsetType
aws_off_t OffsetType
Definition: Stream.h:28
Aws::Crt::Io::StreamSeekBasis::Begin
@ Begin
Aws::Crt::Io::StdIOStreamInputStream
Definition: Stream.h:122
Aws
Definition: Api.h:25
Aws::offset
buffer offset
Definition: cJSON.cpp:908
Exports.h
Aws::Crt::Io::InputStream
Definition: Stream.h:42
Aws::Crt::Io::StreamStatus
aws_stream_status StreamStatus
Definition: Stream.h:27