aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2022-04-07 19:47:52 +0200
committerMacroFake <falke.marco@gmail.com>2022-07-26 11:05:04 +0200
commitfa5103a9f5f8559ab005c0b012d3d3a8057d81fb (patch)
tree7054107ef06e91e0d178d578393c0983e22c1c80 /src
parentfa253d385f9201ea10beacecf3e0e80ff69f3138 (diff)
downloadbitcoin-fa5103a9f5f8559ab005c0b012d3d3a8057d81fb.tar.xz
Add ChronoFormatter to serialize
Diffstat (limited to 'src')
-rw-r--r--src/serialize.h23
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: