mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-10 20:03:34 -03:00
Add ChronoFormatter to serialize
This commit is contained in:
parent
fa253d385f
commit
fa5103a9f5
1 changed files with 23 additions and 0 deletions
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue