diff options
author | B Stack <bgstack15@gmail.com> | 2020-06-20 18:42:30 +0000 |
---|---|---|
committer | B Stack <bgstack15@gmail.com> | 2020-06-20 18:42:30 +0000 |
commit | a2c5f84d26cba5401bd89978de7a1e67e0f60ea8 (patch) | |
tree | fbbe856cbc0ba5a5d3a831f3ec514563cc69ecb1 /zen/serialize.h | |
parent | Merge branch '10.24' into 'master' (diff) | |
parent | add upstream 10.25 (diff) | |
download | FreeFileSync-a2c5f84d26cba5401bd89978de7a1e67e0f60ea8.tar.gz FreeFileSync-a2c5f84d26cba5401bd89978de7a1e67e0f60ea8.tar.bz2 FreeFileSync-a2c5f84d26cba5401bd89978de7a1e67e0f60ea8.zip |
Merge branch '10.25' into 'master'10.25
add upstream 10.25
See merge request opensource-tracking/FreeFileSync!23
Diffstat (limited to 'zen/serialize.h')
-rw-r--r-- | zen/serialize.h | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/zen/serialize.h b/zen/serialize.h index 1eabcdec..a34f91a7 100644 --- a/zen/serialize.h +++ b/zen/serialize.h @@ -183,6 +183,11 @@ BinContainer bufferedLoad(BufferedInputStream& streamIn) //throw X if (bytesRead < blockSize) //end of file { buffer.resize(buffer.size() - (blockSize - bytesRead)); //caveat: unsigned arithmetics + + //caveat: memory consumption of returned string! + if (buffer.capacity() > buffer.size() * 3 / 2) //reference: in worst case, std::vector with growth factor 1.5 "wastes" 50% of its size as unused capacity + buffer.shrink_to_fit(); //=> shrink if buffer is wasting more than that! + return buffer; } } @@ -199,7 +204,7 @@ void writeArray(BufferedOutputStream& stream, const void* buffer, size_t len) template <class N, class BufferedOutputStream> inline void writeNumber(BufferedOutputStream& stream, const N& num) { - static_assert(IsArithmetic<N>::value || std::is_same_v<N, bool>); + static_assert(IsArithmetic<N>::value || std::is_same_v<N, bool> || std::is_enum_v<N>); writeArray(stream, &num, sizeof(N)); } @@ -227,8 +232,8 @@ void readArray(BufferedInputStream& stream, void* buffer, size_t len) //throw Un template <class N, class BufferedInputStream> inline N readNumber(BufferedInputStream& stream) //throw UnexpectedEndOfStreamError { - static_assert(IsArithmetic<N>::value || std::is_same_v<N, bool>); - N num = 0; + static_assert(IsArithmetic<N>::value || std::is_same_v<N, bool> || std::is_enum_v<N>); + N num{}; readArray(stream, &num, sizeof(N)); //throw UnexpectedEndOfStreamError return num; } |