Add ChronoFormatter to serialize

This commit is contained in:
MarcoFalke 2022-04-07 19:47:52 +02:00 committed by MacroFake
parent fa253d385f
commit fa5103a9f5
No known key found for this signature in database
GPG key ID: CE2B75697E69A548

View file

@ -520,6 +520,29 @@ struct CompactSizeFormatter
}
};
template <typename U, bool LOSSY = false>
struct ChronoFormatter {
template <typename Stream, typename Tp>
void Unser(Stream& s, Tp& tp)
{
U u;
s >> u;
// Lossy deserialization does not make sense, so force Wnarrowing
tp = Tp{typename Tp::duration{typename Tp::duration::rep{u}}};
}
template <typename Stream, typename Tp>
void Ser(Stream& s, Tp tp)
{
if constexpr (LOSSY) {
s << U(tp.time_since_epoch().count());
} else {
s << U{tp.time_since_epoch().count()};
}
}
};
template <typename U>
using LossyChronoFormatter = ChronoFormatter<U, true>;
class CompactSizeWriter
{
protected: