A small refactor in BaseBuffer, in preparation for a bigger one
This commit is contained in:
@@ -230,7 +230,7 @@ class PlayLogfile : public BaseWriter<PlayLogfile>, public std::ofstream {
|
|||||||
using std::ofstream::ofstream;
|
using std::ofstream::ofstream;
|
||||||
public:
|
public:
|
||||||
void write_bytes(const char *n, size_t size) { write(n, size); }
|
void write_bytes(const char *n, size_t size) { write(n, size); }
|
||||||
void raise_truncated() {
|
void raise_integer_truncated() {
|
||||||
fprintf(stderr, "number exceeds allowable size\n");
|
fprintf(stderr, "number exceeds allowable size\n");
|
||||||
std::abort();
|
std::abort();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ struct LuaValueHolder {
|
|||||||
// You must provide two methods in the derived class:
|
// You must provide two methods in the derived class:
|
||||||
//
|
//
|
||||||
// write_bytes(const char *n, size_t size)
|
// write_bytes(const char *n, size_t size)
|
||||||
// raise_truncated()
|
// raise_integer_truncated()
|
||||||
//
|
//
|
||||||
///////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@@ -141,7 +141,7 @@ protected:
|
|||||||
template<class T, class XT>
|
template<class T, class XT>
|
||||||
void write_int_core(XT arg) {
|
void write_int_core(XT arg) {
|
||||||
T reduced = arg;
|
T reduced = arg;
|
||||||
if (XT(reduced) != arg) static_cast<Derived*>(this)->raise_truncated();
|
if (XT(reduced) != arg) static_cast<Derived*>(this)->raise_integer_truncated();
|
||||||
write_value_core(reduced);
|
write_value_core(reduced);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,7 +207,7 @@ public:
|
|||||||
//
|
//
|
||||||
// using read_string_type = std::string; // or compatible
|
// using read_string_type = std::string; // or compatible
|
||||||
// void read_bytes_into(char *n, size_t size)
|
// void read_bytes_into(char *n, size_t size)
|
||||||
// void handle_string_too_long();
|
// void raise_string_too_long();
|
||||||
//
|
//
|
||||||
// Error Handling:
|
// Error Handling:
|
||||||
//
|
//
|
||||||
@@ -219,7 +219,7 @@ public:
|
|||||||
// set an error flag and fill the buffer with zeros.
|
// set an error flag and fill the buffer with zeros.
|
||||||
//
|
//
|
||||||
// If read_string discovers that the string is longer than
|
// If read_string discovers that the string is longer than
|
||||||
// the allowed limit, it will call handle_string_too_long.
|
// the allowed limit, it will call raise_string_too_long.
|
||||||
// This function may either throw an exception, or set an
|
// This function may either throw an exception, or set an
|
||||||
// error flag.
|
// error flag.
|
||||||
//
|
//
|
||||||
@@ -447,23 +447,26 @@ public:
|
|||||||
|
|
||||||
// Write block of bytes into the buffer.
|
// Write block of bytes into the buffer.
|
||||||
//
|
//
|
||||||
|
void write_bytes(const char *data, size_t size) {
|
||||||
|
make_space(size);
|
||||||
|
memcpy(write_cursor_, data, size);
|
||||||
|
write_cursor_ += size;
|
||||||
|
}
|
||||||
|
|
||||||
void write_bytes(std::string_view s) {
|
void write_bytes(std::string_view s) {
|
||||||
int64_t len = s.size();
|
write_bytes(s.data(), s.size());
|
||||||
make_space(len);
|
|
||||||
memcpy(write_cursor_, s.data(), len);
|
|
||||||
write_cursor_ += len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write integers.
|
// Write integers.
|
||||||
//
|
//
|
||||||
void write_uint8(uint64_t data) { write_uint_core<uint8_t>(data); }
|
void write_uint8(uint64_t data) { write_int_core<uint8_t, uint64_t>(data); }
|
||||||
void write_uint16(uint64_t data) { write_uint_core<uint16_t>(data); }
|
void write_uint16(uint64_t data) { write_int_core<uint16_t, uint64_t>(data); }
|
||||||
void write_uint32(uint64_t data) { write_uint_core<uint32_t>(data); }
|
void write_uint32(uint64_t data) { write_int_core<uint32_t, uint64_t>(data); }
|
||||||
void write_uint64(uint64_t data) { write_uint_core<uint64_t>(data); }
|
void write_uint64(uint64_t data) { write_int_core<uint64_t, uint64_t>(data); }
|
||||||
void write_int8(int64_t data) { write_int_core<int8_t>(data); }
|
void write_int8(int64_t data) { write_int_core<int8_t, int64_t>(data); }
|
||||||
void write_int16(int64_t data) { write_int_core<int16_t>(data); }
|
void write_int16(int64_t data) { write_int_core<int16_t, int64_t>(data); }
|
||||||
void write_int32(int64_t data) { write_int_core<int32_t>(data); }
|
void write_int32(int64_t data) { write_int_core<int32_t, int64_t>(data); }
|
||||||
void write_int64(int64_t data) { write_int_core<int64_t>(data); }
|
void write_int64(int64_t data) { write_int_core<int64_t, int64_t>(data); }
|
||||||
|
|
||||||
// Write other primitive types.
|
// Write other primitive types.
|
||||||
//
|
//
|
||||||
@@ -665,14 +668,14 @@ public:
|
|||||||
//
|
//
|
||||||
// See the comment at the top of this file for an explanation.
|
// See the comment at the top of this file for an explanation.
|
||||||
//
|
//
|
||||||
void overwrite_int8(int64_t write_count_after, int64_t v) { overwrite_int_core<int8_t>(write_count_after, v); }
|
void overwrite_int8(int64_t write_count_after, int64_t v) { overwrite_int_core<int8_t, int64_t>(write_count_after, v); }
|
||||||
void overwrite_int16(int64_t write_count_after, int64_t v) { overwrite_int_core<int16_t>(write_count_after, v); }
|
void overwrite_int16(int64_t write_count_after, int64_t v) { overwrite_int_core<int16_t, int64_t>(write_count_after, v); }
|
||||||
void overwrite_int32(int64_t write_count_after, int64_t v) { overwrite_int_core<int32_t>(write_count_after, v); }
|
void overwrite_int32(int64_t write_count_after, int64_t v) { overwrite_int_core<int32_t, int64_t>(write_count_after, v); }
|
||||||
void overwrite_int64(int64_t write_count_after, int64_t v) { overwrite_int_core<int64_t>(write_count_after, v); }
|
void overwrite_int64(int64_t write_count_after, int64_t v) { overwrite_int_core<int64_t, int64_t>(write_count_after, v); }
|
||||||
void overwrite_uint8(int64_t write_count_after, uint64_t v) { overwrite_uint_core<uint8_t>(write_count_after, v); }
|
void overwrite_uint8(int64_t write_count_after, uint64_t v) { overwrite_int_core<uint8_t, uint64_t>(write_count_after, v); }
|
||||||
void overwrite_uint16(int64_t write_count_after, uint64_t v) { overwrite_uint_core<uint16_t>(write_count_after, v); }
|
void overwrite_uint16(int64_t write_count_after, uint64_t v) { overwrite_int_core<uint16_t, uint64_t>(write_count_after, v); }
|
||||||
void overwrite_uint32(int64_t write_count_after, uint64_t v) { overwrite_uint_core<uint32_t>(write_count_after, v); }
|
void overwrite_uint32(int64_t write_count_after, uint64_t v) { overwrite_int_core<uint32_t, uint64_t>(write_count_after, v); }
|
||||||
void overwrite_uint64(int64_t write_count_after, uint64_t v) { overwrite_uint_core<uint64_t>(write_count_after, v); }
|
void overwrite_uint64(int64_t write_count_after, uint64_t v) { overwrite_int_core<uint64_t, uint64_t>(write_count_after, v); }
|
||||||
|
|
||||||
// This is for unit testing.
|
// This is for unit testing.
|
||||||
//
|
//
|
||||||
@@ -722,17 +725,10 @@ private:
|
|||||||
write_cursor_ += sizeof(arg);
|
write_cursor_ += sizeof(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T, class XT>
|
||||||
void write_int_core(int64_t arg) {
|
void write_int_core(XT arg) {
|
||||||
T reduced = arg;
|
T reduced = arg;
|
||||||
if (int64_t(reduced) != arg) CoreHandler::raise_integer_truncated();
|
if (XT(reduced) != arg) CoreHandler::raise_integer_truncated();
|
||||||
write_value_core(reduced);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
void write_uint_core(uint64_t arg) {
|
|
||||||
T reduced = arg;
|
|
||||||
if (uint64_t(reduced) != arg) CoreHandler::raise_integer_truncated();
|
|
||||||
write_value_core(reduced);
|
write_value_core(reduced);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -749,21 +745,10 @@ private:
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T, class XT>
|
||||||
void overwrite_int_core(int64_t write_count_after, int64_t vv) {
|
void overwrite_int_core(int64_t write_count_after, XT vv) {
|
||||||
T v = vv;
|
T v = vv;
|
||||||
assert(int64_t(v) == vv);
|
assert(XT(v) == vv);
|
||||||
int64_t write_count_before = write_count_after - sizeof(v);
|
|
||||||
assert(write_count_before >= total_reads());
|
|
||||||
assert(write_count_after <= total_writes());
|
|
||||||
void *target = buf_lo_ + (write_count_before - pre_read_count_);
|
|
||||||
memcpy(target, &v, sizeof(v));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
void overwrite_uint_core(int64_t write_count_after, uint64_t vv) {
|
|
||||||
T v = vv;
|
|
||||||
assert(uint64_t(v) == vv);
|
|
||||||
int64_t write_count_before = write_count_after - sizeof(v);
|
int64_t write_count_before = write_count_after - sizeof(v);
|
||||||
assert(write_count_before >= total_reads());
|
assert(write_count_before >= total_reads());
|
||||||
assert(write_count_after <= total_writes());
|
assert(write_count_after <= total_writes());
|
||||||
|
|||||||
Reference in New Issue
Block a user