From fa5103a9f5f8559ab005c0b012d3d3a8057d81fb Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Thu, 7 Apr 2022 19:47:52 +0200 Subject: [PATCH] Add ChronoFormatter to serialize --- src/serialize.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/serialize.h b/src/serialize.h index a1cce78451c..89a9f322408 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -520,6 +520,29 @@ struct CompactSizeFormatter } }; +template +struct ChronoFormatter { + template + 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 + 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 +using LossyChronoFormatter = ChronoFormatter; + class CompactSizeWriter { protected: