Some changes to the design of base-writer.hpp
This commit is contained in:
@@ -118,8 +118,21 @@ public:
|
||||
//
|
||||
// using read_string_type = std::string; // or compatible
|
||||
// void read_bytes_into(char *n, size_t size)
|
||||
// void raise_string_too_long();
|
||||
// bool read_beyond_eof();
|
||||
// void handle_string_too_long();
|
||||
//
|
||||
// Error Handling:
|
||||
//
|
||||
// It is up to the derived class whether it wants
|
||||
// to report errors using exceptions or flags.
|
||||
//
|
||||
// If read_bytes_into discovers there's not enough bytes,
|
||||
// there are two valid options: throw an exception, OR,
|
||||
// set an error flag and fill the buffer with zeros.
|
||||
//
|
||||
// If read_string discovers that the string is longer than
|
||||
// the allowed limit, it will call handle_string_too_long.
|
||||
// This function may either throw an exception, or set an
|
||||
// error flag.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -131,7 +144,6 @@ protected:
|
||||
T result;
|
||||
Derived *dthis = static_cast<Derived*>(this);
|
||||
dthis->read_bytes_into((char *)(&result), sizeof(result));
|
||||
if (dthis->read_beyond_eof()) result = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -146,7 +158,7 @@ public:
|
||||
int32_t read_int32() { return read_value_core<int32_t>(); }
|
||||
int64_t read_int64() { return read_value_core<int64_t>(); }
|
||||
|
||||
bool read_bool() { return read_uint8(); }
|
||||
bool read_bool() { return (bool)read_uint8(); }
|
||||
char read_char() { return read_value_core<char>(); }
|
||||
float read_float() { return read_value_core<float>(); }
|
||||
double read_double() { return read_value_core<double>(); }
|
||||
@@ -162,10 +174,12 @@ public:
|
||||
auto read_string_limit(uint64_t limit) {
|
||||
size_t len = read_length();
|
||||
Derived *dthis = static_cast<Derived*>(this);
|
||||
if (len > limit) dthis->raise_string_too_long();
|
||||
if (len > limit) {
|
||||
dthis->raise_string_too_long();
|
||||
len = 0;
|
||||
}
|
||||
typename Derived::read_string_type result(len, ' ');
|
||||
dthis->read_bytes_into(&(result[0]), len);
|
||||
if (dthis->read_beyond_eof()) result.clear();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user