1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#ifndef BYTEME_WRITER_HPP
#define BYTEME_WRITER_HPP
#include <string>
#include <cstring>
/**
* @file Writer.hpp
*
* @brief Write to an output sink.
*/
namespace byteme {
/**
* @brief Virtual class for writing bytes to a sink.
*/
class Writer {
public:
virtual ~Writer() = default;
/**
* Write the next chunk of bytes to the output sink.
* This may or may not be internally buffered at the discretion of the concrete class.
*
* @param[in] buffer Pointer to the start of an array containing the bytes to be written.
* @param n Length of the array in `buffer`.
*/
virtual void write(const unsigned char* buffer, size_t n) = 0;
/**
* Indicate that the writing has finished and that the relevant clean-up (buffer flushing, file closing, etc.) should be performed.
* This should be called exactly once, after which no further calls to `write()` or `finish()` should be performed.
*/
virtual void finish() = 0;
/**
* Write a string to the output sink.
*
* @param x String to be written.
*/
void write(const std::string& x) {
write(reinterpret_cast<const unsigned char*>(x.c_str()), x.size());
}
/**
* Write a null-terminated C-string to the output sink.
*
* @param[in] Pointer to the string to be written.
*/
void write(const char* x) {
write(reinterpret_cast<const unsigned char*>(x), std::strlen(x));
}
/**
* Write a single character to the output sink.
*
* @param x Character to be written.
*/
void write(char x) {
write(reinterpret_cast<const unsigned char*>(&x), 1);
}
};
}
#endif
|