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

block_sink.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_SERIALIZE_BLOCK_SINK_HPP
10 #define EAGINE_SERIALIZE_BLOCK_SINK_HPP
11 
12 #include "../memory/span_algo.hpp"
13 #include "data_sink.hpp"
14 #include <type_traits>
15 #include <vector>
16 
17 namespace eagine {
18 //------------------------------------------------------------------------------
24 public:
27  constexpr block_data_sink() noexcept = default;
28 
31  : _dst{dst} {}
32 
35  void reset(memory::block dst) {
36  _dst = dst;
37  _done = 0;
38  _save_points.clear();
39  }
40 
43  auto done() const noexcept -> memory::block {
44  return head(_dst, _done);
45  }
46 
50  auto free() const noexcept -> memory::block {
51  return skip(_dst, _done);
52  }
53 
57  auto remaining_size() -> span_size_t final {
58  return free().size();
59  }
60 
62 
64  auto dst = free();
65  if(dst.size() < blk.size()) {
66  copy(head(blk, dst.size()), dst);
67  _done += dst.size();
69  }
70  copy(blk, dst);
71  _done += blk.size();
72  return {};
73  }
74 
78  if(_dst.size() < blk.size()) {
80  }
81  copy(blk, _dst);
82  _done = blk.size();
83  return {};
84  }
85 
86  auto begin_work() -> transaction_handle final {
87  _save_points.push_back(_done);
88  return transaction_handle(_save_points.size());
89  }
90 
91  void commit(transaction_handle th) final {
92  EAGINE_ASSERT(th == transaction_handle(_save_points.size()));
93  EAGINE_MAYBE_UNUSED(th);
94  _save_points.pop_back();
95  }
96 
97  void rollback(transaction_handle th) final {
98  EAGINE_ASSERT(th == transaction_handle(_save_points.size()));
99  EAGINE_MAYBE_UNUSED(th);
100  _done = _save_points.back();
101  _save_points.pop_back();
102  }
103 
104  auto finalize() -> serialization_errors override {
105  return {};
106  }
107 
108 private:
109  memory::block _dst{};
110  span_size_t _done{0};
111  std::vector<span_size_t> _save_points{};
112 };
113 //------------------------------------------------------------------------------
114 } // namespace eagine
115 
116 #endif // EAGINE_SERIALIZE_BLOCK_SINK_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
auto write(memory::const_block blk) -> serialization_errors final
Writes a block of data into this sink.
Definition: block_sink.hpp:63
Class for manipulating and testing a group of enumeration-based bits.
Definition: bitfield.hpp:19
Serialization data sink backed by a pre-allocated memory block.
Definition: block_sink.hpp:23
Abstract base class for serialization data sinks.
Definition: data_sink.hpp:24
auto done() const noexcept -> memory::block
Returns the part of the backing block already written to.
Definition: block_sink.hpp:43
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
basic_block< false > block
Alias for non-const byte memory span.
Definition: block.hpp:27
constexpr block_data_sink() noexcept=default
Default constructor.
std::uintptr_t transaction_handle
Alias for type indentifying sink transaction that can be rolled back.
Definition: data_sink.hpp:60
auto begin_work() -> transaction_handle final
Begins a write transaction on this data sink.
Definition: block_sink.hpp:86
void commit(transaction_handle th) final
Commits writes done as a part of transaction identified by argument.
Definition: block_sink.hpp:91
virtual auto write(memory::const_block data) -> result=0
Writes a block of data into this sink.
auto finalize() -> serialization_errors override
Does additional finalization, like compression after serialization operation.
Definition: block_sink.hpp:104
auto replace_with(memory::const_block blk) -> serialization_errors
Replaces the content of the backing block with the content of the argument.
Definition: block_sink.hpp:77
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
@ incomplete_write
Incomplete write, remaining data should be written later.
auto remaining_size() -> span_size_t final
Returns the size of the free part of the backing block.
Definition: block_sink.hpp:57
constexpr auto size() const noexcept -> size_type
Returns the number of elements in the span.
Definition: span.hpp:246
void rollback(transaction_handle th) final
Rolls-back writes done as a part of transaction identified by argument.
Definition: block_sink.hpp:97
void reset(memory::block dst)
Resets the backing block.
Definition: block_sink.hpp:35
@ too_much_data
Too much data to fit into serialization data sink.
auto free() const noexcept -> memory::block
Returns the free part of the backing block.
Definition: block_sink.hpp:50
block_data_sink(memory::block dst) noexcept
Constructor setting the backing block.
Definition: block_sink.hpp:30

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