diff options
author | MarcoFalke <falke.marco@gmail.com> | 2022-04-07 19:47:52 +0200 |
---|---|---|
committer | MacroFake <falke.marco@gmail.com> | 2022-07-26 11:05:04 +0200 |
commit | fa5103a9f5f8559ab005c0b012d3d3a8057d81fb (patch) | |
tree | 7054107ef06e91e0d178d578393c0983e22c1c80 | |
parent | fa253d385f9201ea10beacecf3e0e80ff69f3138 (diff) |
Add ChronoFormatter to serialize
-rw-r--r-- | src/serialize.h | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/serialize.h b/src/serialize.h index a1cce78451..89a9f32240 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -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: |