byteme
C++ wrappers for buffered inputs
Loading...
Searching...
No Matches
OstreamWriter.hpp
Go to the documentation of this file.
1#ifndef BYTEME_OSTREAM_WRITER_HPP
2#define BYTEME_OSTREAM_WRITER_HPP
3
4#include <memory>
5#include <stdexcept>
6#include <cstddef>
7
8#include "Writer.hpp"
9
16namespace byteme {
17
27template<class Pointer_>
28class OstreamWriter final : public Writer {
29public:
33 OstreamWriter(Pointer_ output) : my_output(std::move(output)) {}
34
35public:
36 using Writer::write;
37
38 void write(const unsigned char* buffer, std::size_t n) {
39 safe_write<std::streamsize, false>(
40 reinterpret_cast<const char*>(buffer),
41 n,
42 [&](const char* ptr0, std::streamsize n0) -> void {
43 my_output->write(ptr0, n0);
44 if (!(my_output->good())) {
45 throw std::runtime_error("failed to write to arbitrary output stream");
46 }
47 }
48 );
49 }
50
51 void finish() {
52 my_output->flush();
53 if (my_output->fail() || my_output->bad()) {
54 throw std::runtime_error("failed to flush to arbitrary output stream");
55 }
56 }
57
58private:
59 Pointer_ my_output;
60};
61
62}
63
64#endif
Write to an output sink.
Read bytes from a std::ostream.
Definition OstreamWriter.hpp:28
OstreamWriter(Pointer_ output)
Definition OstreamWriter.hpp:33
void write(const unsigned char *buffer, std::size_t n)
Definition OstreamWriter.hpp:38
void finish()
Definition OstreamWriter.hpp:51
Virtual class for writing bytes to a sink.
Definition Writer.hpp:21
virtual void write(const unsigned char *buffer, std::size_t n)=0
Simple byte readers and writers.