From c95b3937fef3e2c63768f1b3b1dc2c898f23d91d Mon Sep 17 00:00:00 2001 From: B Stack Date: Wed, 22 Jul 2020 11:37:03 -0400 Subject: add upstream 11.0 --- zen/serialize.h | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'zen/serialize.h') diff --git a/zen/serialize.h b/zen/serialize.h index a34f91a7..6c57e4ee 100644 --- a/zen/serialize.h +++ b/zen/serialize.h @@ -11,21 +11,20 @@ #include #include #include "string_base.h" +#include "sys_error.h" //keep header clean from specific stream implementations! (e.g.file_io.h)! used by abstract.h! namespace zen { -//high-performance unformatted serialization (avoiding wxMemoryOutputStream/wxMemoryInputStream inefficiencies) +/* high-performance unformatted serialization (avoiding wxMemoryOutputStream/wxMemoryInputStream inefficiencies) -/* -------------------------- |Binary Container Concept| -------------------------- binary container for data storage: must support "basic" std::vector interface (e.g. std::vector, std::string, Zbase) -*/ -/* + ------------------------------- |Buffered Input Stream Concept| ------------------------------- @@ -49,8 +48,7 @@ struct BufferedOutputStream Optional: support stream-copying -------------------------------- const IOCallback& notifyUnbufferedIO -}; -*/ +}; */ using IOCallback = std::function; //throw X @@ -65,8 +63,12 @@ template void writeNumber (BufferedOutpu template void writeContainer(BufferedOutputStream& stream, const C& str); //noexcept template < class BufferedOutputStream> void writeArray (BufferedOutputStream& stream, const void* buffer, size_t len); // //---------------------------------------------------------------------- -class UnexpectedEndOfStreamError {}; -template N readNumber (BufferedInputStream& stream); //throw UnexpectedEndOfStreamError (corrupted data) +struct SysErrorUnexpectedEos : public SysError +{ + SysErrorUnexpectedEos() : SysError(_("File content is corrupted.") + L" (unexpected end of stream)") {} +}; + +template N readNumber (BufferedInputStream& stream); //throw SysErrorUnexpectedEos (corrupted data) template C readContainer(BufferedInputStream& stream); // template < class BufferedInputStream> void readArray (BufferedInputStream& stream, void* buffer, size_t len); // @@ -220,40 +222,40 @@ void writeContainer(BufferedOutputStream& stream, const C& cont) //don't even co template inline -void readArray(BufferedInputStream& stream, void* buffer, size_t len) //throw UnexpectedEndOfStreamError +void readArray(BufferedInputStream& stream, void* buffer, size_t len) //throw SysErrorUnexpectedEos { const size_t bytesRead = stream.read(buffer, len); assert(bytesRead <= len); //buffer overflow otherwise not always detected! if (bytesRead < len) - throw UnexpectedEndOfStreamError(); + throw SysErrorUnexpectedEos(); } template inline -N readNumber(BufferedInputStream& stream) //throw UnexpectedEndOfStreamError +N readNumber(BufferedInputStream& stream) //throw SysErrorUnexpectedEos { static_assert(IsArithmetic::value || std::is_same_v || std::is_enum_v); N num{}; - readArray(stream, &num, sizeof(N)); //throw UnexpectedEndOfStreamError + readArray(stream, &num, sizeof(N)); //throw SysErrorUnexpectedEos return num; } template inline -C readContainer(BufferedInputStream& stream) //throw UnexpectedEndOfStreamError +C readContainer(BufferedInputStream& stream) //throw SysErrorUnexpectedEos { C cont; - auto strLength = readNumber(stream); //throw UnexpectedEndOfStreamError + auto strLength = readNumber(stream); //throw SysErrorUnexpectedEos if (strLength > 0) { try { - cont.resize(strLength); //throw std::length_error + cont.resize(strLength); //throw std::length_error, std::bad_alloc } - catch (std::length_error&) { throw UnexpectedEndOfStreamError(); } //most likely this is due to data corruption! - catch ( std::bad_alloc&) { throw UnexpectedEndOfStreamError(); } // + catch (std::length_error&) { throw SysErrorUnexpectedEos(); } //most likely this is due to data corruption! + catch ( std::bad_alloc&) { throw SysErrorUnexpectedEos(); } // - readArray(stream, &cont[0], sizeof(typename C::value_type) * strLength); //throw UnexpectedEndOfStreamError + readArray(stream, &cont[0], sizeof(typename C::value_type) * strLength); //throw SysErrorUnexpectedEos } return cont; } -- cgit