Added base-writer class
This commit is contained in:
@@ -219,6 +219,7 @@
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
|
||||
#include "base-writer.hpp"
|
||||
#include "luastack.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
@@ -240,8 +241,8 @@ public:
|
||||
virtual char const *what() const { return "Stream contained invalid data"; }
|
||||
};
|
||||
|
||||
class StreamBuffer : public eng::nevernew {
|
||||
public:
|
||||
class StreamBuffer : public eng::nevernew, public BaseReader<StreamBuffer, eng::string>, public BaseWriter<StreamBuffer> {
|
||||
public:
|
||||
// Construct an empty buffer.
|
||||
StreamBuffer();
|
||||
|
||||
@@ -287,8 +288,12 @@ public:
|
||||
// It just writes the bytes.
|
||||
//
|
||||
void write_bytes(const char *bytes, int64_t len);
|
||||
void write_bytes(std::string_view bytes);
|
||||
void write_bytes(std::string_view s);
|
||||
|
||||
// Copy bytes from the StreamBuffer into an external buffer.
|
||||
//
|
||||
void read_bytes_into(char *target, int64_t len);
|
||||
|
||||
// Read a block of bytes from the buffer.
|
||||
//
|
||||
// Caution: the pointer returned is a pointer to the stream's buffer. It is
|
||||
@@ -297,63 +302,19 @@ public:
|
||||
//
|
||||
const char *read_bytes(int64_t bytes);
|
||||
|
||||
// Write integers and floats into the buffer.
|
||||
//
|
||||
// Note that integral parameters are all 64 bits. That's so that I can do
|
||||
// runtime error checking to verify that the numbers are all in-range.
|
||||
//
|
||||
void write_int8(int64_t v);
|
||||
void write_int16(int64_t v);
|
||||
void write_int32(int64_t v);
|
||||
void write_int64(int64_t v);
|
||||
void write_uint8(uint64_t v);
|
||||
void write_uint16(uint64_t v);
|
||||
void write_uint32(uint64_t v);
|
||||
void write_uint64(uint64_t v);
|
||||
void write_char(char c);
|
||||
void write_float(float f);
|
||||
void write_double(double d);
|
||||
void write_xyz(const util::XYZ &xyz);
|
||||
void write_dxyz(const util::DXYZ &xyz);
|
||||
|
||||
// Read fixed-size integers from the buffer.
|
||||
//
|
||||
// May throw StreamEof if the specified number of bytes aren't present.
|
||||
//
|
||||
int8_t read_int8();
|
||||
int16_t read_int16();
|
||||
int32_t read_int32();
|
||||
int64_t read_int64();
|
||||
uint8_t read_uint8() { return read_int8(); }
|
||||
uint16_t read_uint16() { return read_int16(); }
|
||||
uint32_t read_uint32() { return read_int32(); }
|
||||
uint64_t read_uint64() { return read_int64(); }
|
||||
char read_char();
|
||||
float read_float();
|
||||
double read_double();
|
||||
util::XYZ read_xyz();
|
||||
util::DXYZ read_dxyz();
|
||||
|
||||
// Write other types into the buffer.
|
||||
//
|
||||
// Note that strings are preceded by a length field. Reading
|
||||
// a string works by reading the length field, and then reading
|
||||
// the correct number of bytes.
|
||||
//
|
||||
void write_bool(bool b) { write_int8(b ? 1 : 0); }
|
||||
void write_hashvalue(const util::HashValue &hv);
|
||||
void write_string(std::string_view s);
|
||||
|
||||
// Read other types from the buffer.
|
||||
// Read and write larger types.
|
||||
//
|
||||
// Throws StreamEof if the specified number of bytes aren't present.
|
||||
// Read string with a length limit will throw 'StreamCorruption' if the
|
||||
// length is too long.
|
||||
//
|
||||
bool read_bool() { return read_int8(); }
|
||||
void write_xyz(const util::XYZ &xyz);
|
||||
void write_dxyz(const util::DXYZ &xyz);
|
||||
util::XYZ read_xyz();
|
||||
util::DXYZ read_dxyz();
|
||||
|
||||
void write_hashvalue(const util::HashValue &hv);
|
||||
util::HashValue read_hashvalue();
|
||||
eng::string read_string();
|
||||
eng::string read_string_limit(int64_t max_allowed);
|
||||
|
||||
// Read the entire contents of the buffer as a string.
|
||||
//
|
||||
@@ -415,6 +376,10 @@ public:
|
||||
// Get an ostream that writes into the StreamBuffer.
|
||||
std::ostream &ostream();
|
||||
|
||||
// Throw a StreamCorruption exception.
|
||||
void raise_truncated() { throw StreamCorruption(); }
|
||||
void raise_string_too_long() { throw StreamCorruption(); }
|
||||
|
||||
private:
|
||||
// Start and end of the allocated block.
|
||||
char *buf_lo_;
|
||||
|
||||
Reference in New Issue
Block a user