OGLplus  (0.59.0) a C++ wrapper for rendering APIs

istream_source.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_SERIALIZE_ISTREAM_SOURCE_HPP
10 #define EAGINE_SERIALIZE_ISTREAM_SOURCE_HPP
11 
12 #include "../assert.hpp"
13 #include "../memory/buffer.hpp"
14 #include "../memory/span_algo.hpp"
15 #include "data_source.hpp"
16 #include <istream>
17 
18 namespace eagine {
19 //------------------------------------------------------------------------------
24 public:
26  istream_data_source(std::istream& in) noexcept
27  : _in{in} {}
28 
29  auto top(span_size_t req_size) -> memory::const_block final {
30  if(_cur_size < req_size) {
31  while(_storage.size() < _cur_size + req_size) {
32  _storage.resize(_storage.size() + _chunk_size());
33  }
34  read_from_stream(
35  _in,
36  head(skip(cover(_storage), _cur_size), req_size - _cur_size));
37  _cur_size += span_size(_in.gcount());
38  }
39  return head(head(view(_storage), _cur_size), req_size);
40  }
41 
42  void pop(span_size_t del_size) final {
43  if(_cur_size <= del_size) {
44  _cur_size = 0;
45  if(_storage.size() > _chunk_size()) {
46  _storage.resize(_chunk_size());
47  }
48  } else {
49  auto sw = head(cover(_storage), _cur_size);
50  memory::copy(skip(sw, del_size), sw);
51  _cur_size -= del_size;
52  }
53  EAGINE_ASSERT(_cur_size >= 0);
54  }
55 
56 private:
57  static constexpr auto _chunk_size() noexcept -> span_size_t {
58  return 8 * 1024;
59  }
60 
61  std::istream& _in;
62  span_size_t _cur_size{0};
63  memory::buffer _storage{};
64 };
65 //------------------------------------------------------------------------------
66 } // namespace eagine
67 
68 #endif // EAGINE_SERIALIZE_ISTREAM_SOURCE_HPP
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
istream_data_source(std::istream &in) noexcept
Constructor setting the source stream.
Definition: istream_source.hpp:26
static constexpr auto span_size(T v) noexcept
Converts argument to span size type.
Definition: types.hpp:59
Common code is placed in this namespace.
Definition: eagine.hpp:21
static constexpr auto cover(T *addr, S size) noexcept -> span_if_mutable< T >
Creates a span starting at the specified pointer and specified length.
Definition: span.hpp:465
static constexpr auto view(T *addr, S size) noexcept -> const_span< T >
Creates a view starting at the specified pointer and specified length.
Definition: span.hpp:458
static constexpr auto head(basic_span< T, P, S > s, L l) noexcept -> basic_span< T, P, S >
Returns the first l elements from the front of a span.
Definition: span_algo.hpp:99
Non-owning view of a contiguous range of memory with ValueType elements.
Definition: flatten_fwd.hpp:16
static constexpr auto skip(basic_span< T, P, S > s, L l) noexcept -> basic_span< T, P, S >
Skips a specified count of elements from the front of a span.
Definition: span_algo.hpp:60
Abstract base class for deserialization data sources.
Definition: data_source.hpp:25
void pop(span_size_t del_size) final
Returns the specified amount of data of the top of the source.
Definition: istream_source.hpp:42
auto top(span_size_t req_size) -> memory::const_block final
Returns a block covering the specified amount of data of the top.
Definition: istream_source.hpp:29
auto size() const noexcept -> span_size_t
Returns the size of the buffer in bytes.
Definition: buffer.hpp:81
auto resize(span_size_t new_size) -> auto &
Resizes the buffer to the specified number of bytes.
Definition: buffer.hpp:114
static auto copy(const_block source, block dest) -> block
Copies the content of source block to destination block.
Definition: copy.hpp:23
Deserialization data source backed by an input stream.
Definition: istream_source.hpp:23

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).