mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-08-31 22:51:27 +02:00
Add ChronoFormatter to serialize
This commit is contained in:
@@ -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:
|
||||
|
Reference in New Issue
Block a user