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

data_source.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_SERIALIZE_DATA_SOURCE_HPP
10 #define EAGINE_SERIALIZE_DATA_SOURCE_HPP
11 
12 #include "../interface.hpp"
13 #include "../memory/buffer.hpp"
14 #include "../memory/span_algo.hpp"
15 #include "../string_span.hpp"
16 #include "../valid_if/nonnegative.hpp"
17 #include "../valid_if/positive.hpp"
18 
19 namespace eagine {
20 //------------------------------------------------------------------------------
25 struct deserializer_data_source : interface<deserializer_data_source> {
26 
31  virtual auto top(span_size_t size) -> memory::const_block = 0;
32 
35  virtual void pop(span_size_t size) = 0;
36 
42  template <typename Function>
43  auto scan_until(
44  Function predicate,
46  const valid_if_positive<span_size_t>& step = {256})
48  const auto inc{extract(step)};
49  span_size_t start{0};
50  span_size_t total{inc};
51  while(auto blk = top(total)) {
52  if(auto found = find_element_if(skip(blk, start), predicate)) {
53  return {start + extract(found)};
54  }
55  if(blk.size() < total) {
56  break;
57  }
58  if(extract(max) < total) {
59  return extract(max);
60  }
61  start += inc;
62  total += inc;
63  }
64  return {-1};
65  }
66 
72  auto scan_for(
73  byte what,
75  const valid_if_positive<span_size_t>& step = {256})
77  return scan_until([what](byte b) { return b == what; }, max, step);
78  }
79 
81  void fetch_all(
82  memory::buffer& dst,
83  valid_if_positive<span_size_t> step = {256}) {
84  span_size_t offs{dst.size()};
85  while(auto blk = top(extract(step))) {
86  dst.enlarge_by(blk.size());
87  copy(blk, skip(cover(dst), offs));
88  offs += blk.size();
89  pop(blk.size());
90  }
91  }
92 };
93 //------------------------------------------------------------------------------
94 } // namespace eagine
95 
96 #endif // EAGINE_SERIALIZE_DATA_SOURCE_HPP
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
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 extract(api_result_value< Result, api_result_validity::never > &) noexcept -> Result &
Overload of extract for api_result_value.
Definition: c_api_wrap.hpp:270
virtual void pop(span_size_t size)=0
Returns the specified amount of data of the top of the source.
Primary template for conditionally valid values.
Definition: decl.hpp:49
static constexpr auto find_element_if(basic_span< T, P, S > spn, F predicate) noexcept -> optionally_valid< S >
Finds the position of the first element satisfying predicate in a span.
Definition: span_algo.hpp:299
Base template for abstract interfaces, implements common functionality.
Definition: interface.hpp:18
auto enlarge_by(span_size_t inc_size) -> auto &
Enlarges the buffer by the specified number of bytes.
Definition: buffer.hpp:139
auto scan_until(Function predicate, const valid_if_positive< span_size_t > &max, const valid_if_positive< span_size_t > &step={256}) -> valid_if_nonnegative< span_size_t >
Returns the position of the first byte where predicate is true.
Definition: data_source.hpp:43
Reallocatable owning byte buffer.
Definition: buffer.hpp:22
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
auto scan_for(byte what, const valid_if_positive< span_size_t > &max, const valid_if_positive< span_size_t > &step={256}) -> valid_if_nonnegative< span_size_t >
Returns the position of the first occurrence of the specified byte.
Definition: data_source.hpp:72
virtual auto top(span_size_t size) -> memory::const_block=0
Returns a block covering the specified amount of data of the top.
auto size() const noexcept -> span_size_t
Returns the size of the buffer in bytes.
Definition: buffer.hpp:81
void fetch_all(memory::buffer &dst, valid_if_positive< span_size_t > step={256})
Fetches all the remaining data into a buffer.
Definition: data_source.hpp:81

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