byteme
C++ wrappers for buffered inputs
Loading...
Searching...
No Matches
Writer.hpp
Go to the documentation of this file.
1#ifndef BYTEME_WRITER_HPP
2#define BYTEME_WRITER_HPP
3
4#include <string>
5#include <cstring>
6#include <cstddef>
7
8#include "check_buffer_size.hpp"
9
16namespace byteme {
17
21class Writer {
22public:
23 virtual ~Writer() = default;
24
32 virtual void write(const unsigned char* buffer, std::size_t n) = 0;
33
38 virtual void finish() = 0;
39
45 void write(const std::string& x) {
46 // Protect against cases where size_t < string::size_type.
47 safe_write<std::size_t, false>(
48 reinterpret_cast<const unsigned char*>(x.c_str()),
49 x.size(),
50 [&](const unsigned char* ptr0, std::size_t n0) -> void {
51 write(ptr0, n0);
52 }
53 );
54 }
55
61 void write(const char* x) {
62 write(reinterpret_cast<const unsigned char*>(x), std::strlen(x));
63 }
64
70 void write(char x) {
71 write(reinterpret_cast<const unsigned char*>(&x), 1);
72 }
73};
74
75}
76
77#endif
Virtual class for writing bytes to a sink.
Definition Writer.hpp:21
void write(const std::string &x)
Definition Writer.hpp:45
void write(char x)
Definition Writer.hpp:70
virtual void finish()=0
virtual void write(const unsigned char *buffer, std::size_t n)=0
void write(const char *x)
Definition Writer.hpp:61
Simple byte readers and writers.