summaryrefslogtreecommitdiff
path: root/zen/serialize.h
diff options
context:
space:
mode:
authorB Stack <bgstack15@gmail.com>2020-06-19 16:18:18 -0400
committerB Stack <bgstack15@gmail.com>2020-06-19 16:18:18 -0400
commitb2801fb887fe40875b3ec90619b011b45c1d2796 (patch)
treefbbe856cbc0ba5a5d3a831f3ec514563cc69ecb1 /zen/serialize.h
parentMerge branch '10.24' into 'master' (diff)
downloadFreeFileSync-b2801fb887fe40875b3ec90619b011b45c1d2796.tar.gz
FreeFileSync-b2801fb887fe40875b3ec90619b011b45c1d2796.tar.bz2
FreeFileSync-b2801fb887fe40875b3ec90619b011b45c1d2796.zip
add upstream 10.25
Diffstat (limited to 'zen/serialize.h')
-rw-r--r--zen/serialize.h11
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;
}
bgstack15