From 17d6a993fc17d533460c5f40f3908c708e057c18 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 23 May 2024 18:45:17 +0200 Subject: Merging upstream version 18.2.3. Signed-off-by: Daniel Baumann --- src/include/encoding.h | 49 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) (limited to 'src/include/encoding.h') diff --git a/src/include/encoding.h b/src/include/encoding.h index 40ba9d39c..08c67c33e 100644 --- a/src/include/encoding.h +++ b/src/include/encoding.h @@ -14,6 +14,7 @@ #ifndef CEPH_ENCODING_H #define CEPH_ENCODING_H +#include #include #include #include @@ -322,10 +323,11 @@ inline void decode_nohead(int len, bufferlist& s, bufferlist::const_iterator& p) p.copy(len, s); } -// Time, since the templates are defined in std::chrono +// Time, since the templates are defined in std::chrono. The default encodings +// for time_point and duration are backward-compatible with utime_t, but +// truncate seconds to 32 bits so are not guaranteed to round-trip. -template>* = nullptr> +template void encode(const std::chrono::time_point& t, ceph::bufferlist &bl) { auto ts = Clock::to_timespec(t); @@ -336,8 +338,7 @@ void encode(const std::chrono::time_point& t, encode(ns, bl); } -template>* = nullptr> +template void decode(std::chrono::time_point& t, bufferlist::const_iterator& p) { uint32_t s; @@ -351,8 +352,7 @@ void decode(std::chrono::time_point& t, t = Clock::from_timespec(ts); } -template>* = nullptr> +template void encode(const std::chrono::duration& d, ceph::bufferlist &bl) { using namespace std::chrono; @@ -362,8 +362,7 @@ void encode(const std::chrono::duration& d, encode(ns, bl); } -template>* = nullptr> +template void decode(std::chrono::duration& d, bufferlist::const_iterator& p) { int32_t s; @@ -373,6 +372,38 @@ void decode(std::chrono::duration& d, d = std::chrono::seconds(s) + std::chrono::nanoseconds(ns); } +// Provide encodings for chrono::time_point and duration that use +// the underlying representation so are guaranteed to round-trip. + +template +void round_trip_encode(const std::chrono::duration& d, + ceph::bufferlist &bl) { + const Rep r = d.count(); + encode(r, bl); +} + +template +void round_trip_decode(std::chrono::duration& d, + bufferlist::const_iterator& p) { + Rep r; + decode(r, p); + d = std::chrono::duration(r); +} + +template +void round_trip_encode(const std::chrono::time_point& t, + ceph::bufferlist &bl) { + round_trip_encode(t.time_since_epoch(), bl); +} + +template +void round_trip_decode(std::chrono::time_point& t, + bufferlist::const_iterator& p) { + Duration dur; + round_trip_decode(dur, p); + t = std::chrono::time_point(dur); +} + // ----------------------------- // STL container types -- cgit v1.2.3