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

ostream_sink.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_SERIALIZE_OSTREAM_SINK_HPP
10 #define EAGINE_SERIALIZE_OSTREAM_SINK_HPP
11 
12 #include "data_sink.hpp"
13 #include <limits>
14 #include <ostream>
15 #include <sstream>
16 #include <stack>
17 
18 namespace eagine {
19 //------------------------------------------------------------------------------
24 public:
26  ostream_data_sink(std::ostream& out) noexcept
27  : _out{out} {}
28 
29  auto remaining_size() -> span_size_t final {
30  return std::numeric_limits<span_size_t>::max();
31  }
32 
34 
36  write_to_stream(current(), blk);
37  if(current().eof()) {
39  }
40  if(!current().good()) {
42  }
43  return {};
44  }
45 
46  auto begin_work() -> transaction_handle final {
47  _subs.emplace();
48  return transaction_handle(_subs.size());
49  }
50 
51  void commit(transaction_handle th) final {
52  EAGINE_ASSERT(th == transaction_handle(_subs.size()));
53  EAGINE_MAYBE_UNUSED(th);
54  std::stringstream top{std::move(_subs.top())};
55  _subs.pop();
56  current() << top.rdbuf();
57  }
58 
59  void rollback(transaction_handle th) final {
60  EAGINE_ASSERT(th == transaction_handle(_subs.size()));
61  EAGINE_MAYBE_UNUSED(th);
62  _subs.pop();
63  }
64 
65  auto finalize() -> serialization_errors final {
66  return {};
67  }
68 
69 private:
70  auto current() noexcept -> std::ostream& {
71  if(_subs.empty()) {
72  return _out;
73  }
74  return _subs.top();
75  }
76 
77  std::ostream& _out;
78  std::stack<std::stringstream> _subs;
79 };
80 //------------------------------------------------------------------------------
81 } // namespace eagine
82 
83 #endif // EAGINE_SERIALIZE_OSTREAM_SINK_HPP
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
auto begin_work() -> transaction_handle final
Begins a write transaction on this data sink.
Definition: ostream_sink.hpp:46
@ data_sink_error
Internal error in the serialization data sink.
void rollback(transaction_handle th) final
Rolls-back writes done as a part of transaction identified by argument.
Definition: ostream_sink.hpp:59
Common code is placed in this namespace.
Definition: eagine.hpp:21
Class for manipulating and testing a group of enumeration-based bits.
Definition: bitfield.hpp:19
Abstract base class for serialization data sinks.
Definition: data_sink.hpp:24
std::uintptr_t transaction_handle
Alias for type indentifying sink transaction that can be rolled back.
Definition: data_sink.hpp:60
auto finalize() -> serialization_errors final
Does additional finalization, like compression after serialization operation.
Definition: ostream_sink.hpp:65
virtual auto write(memory::const_block data) -> result=0
Writes a block of data into this sink.
Non-owning view of a contiguous range of memory with ValueType elements.
Definition: flatten_fwd.hpp:16
auto write(memory::const_block blk) -> serialization_errors final
Writes a block of data into this sink.
Definition: ostream_sink.hpp:35
Serialization data sink backed by an output stream.
Definition: ostream_sink.hpp:23
void commit(transaction_handle th) final
Commits writes done as a part of transaction identified by argument.
Definition: ostream_sink.hpp:51
ostream_data_sink(std::ostream &out) noexcept
Constructor setting the target stream.
Definition: ostream_sink.hpp:26
@ too_much_data
Too much data to fit into serialization data sink.
auto remaining_size() -> span_size_t final
Returns the remaining available size for data in this sink.
Definition: ostream_sink.hpp:29

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