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 <ostream>
5#include <stdexcept>
6#include "Writer.hpp"
7
14namespace byteme {
15
23template<class Pointer_ = std::ostream*>
24class OstreamWriter : public Writer {
25public:
30 OstreamWriter(Pointer_ output) : my_output(std::move(output)) {}
31
32 using Writer::write;
33
34 void write(const unsigned char* buffer, size_t n) {
35 my_output->write(reinterpret_cast<const char*>(buffer), n);
36 if (!(my_output->good())) {
37 throw std::runtime_error("failed to write to arbitrary output stream");
38 }
39 }
40
41 void finish() {
42 my_output->flush();
43 if (my_output->fail() || my_output->bad()) {
44 throw std::runtime_error("failed to flush to arbitrary output stream");
45 }
46 }
47
48private:
49 Pointer_ my_output;
50};
51
52}
53
54#endif
Write to an output sink.
Read bytes from a std::ostream.
Definition OstreamWriter.hpp:24
OstreamWriter(Pointer_ output)
Definition OstreamWriter.hpp:30
void finish()
Definition OstreamWriter.hpp:41
void write(const unsigned char *buffer, size_t n)
Definition OstreamWriter.hpp:34
Virtual class for writing bytes to a sink.
Definition Writer.hpp:18
virtual void write(const unsigned char *buffer, size_t n)=0
Simple byte readers and writers.