From d78e98711643c04879a43dc2a1494fa866a5933a Mon Sep 17 00:00:00 2001 From: Casey Bodley Date: Tue, 8 May 2018 15:07:43 -0400 Subject: [PATCH] encoding: add overloads for chrono durations std::chrono::durations can be encoded in the same format as time_points and utime_t Signed-off-by: Casey Bodley (cherry picked from commit 5c08f2cc49063053d98fa77b68bfd8fde28454b7) Conflicts: src/test/encoding/test_ceph_time.h src/test/encoding/types.h --- src/include/encoding.h | 22 ++++++++++++++++++++++ src/test/encoding/test_ceph_time.h | 25 +++++++++++++++++++++++++ src/test/encoding/types.h | 1 + 3 files changed, 48 insertions(+) diff --git a/src/include/encoding.h b/src/include/encoding.h index 187c174a1e607..45c8f66efeea1 100644 --- a/src/include/encoding.h +++ b/src/include/encoding.h @@ -307,6 +307,28 @@ void decode(std::chrono::time_point& t, t = Clock::from_timespec(ts); } +template>* = nullptr> +void encode(const std::chrono::duration& d, + ceph::bufferlist &bl) { + using namespace std::chrono; + uint32_t s = duration_cast(d).count(); + uint32_t ns = (duration_cast(d) % seconds(1)).count(); + encode(s, bl); + encode(ns, bl); +} + +template>* = nullptr> +void decode(std::chrono::duration& d, + bufferlist::iterator& p) { + uint32_t s; + uint32_t ns; + decode(s, p); + decode(ns, p); + d = std::chrono::seconds(s) + std::chrono::nanoseconds(ns); +} + // ----------------------------- // STL container types diff --git a/src/test/encoding/test_ceph_time.h b/src/test/encoding/test_ceph_time.h index bfd250f98ffdc..b04dd28e7a69b 100644 --- a/src/test/encoding/test_ceph_time.h +++ b/src/test/encoding/test_ceph_time.h @@ -40,4 +40,29 @@ WRITE_CLASS_ENCODER(real_time_wrapper) using coarse_real_time_wrapper = time_point_wrapper; WRITE_CLASS_ENCODER(coarse_real_time_wrapper) +// wrapper for ceph::timespan that implements the dencoder interface +class timespan_wrapper { + ceph::timespan d; + public: + timespan_wrapper() = default; + explicit timespan_wrapper(const ceph::timespan& d) : d(d) {} + + void encode(bufferlist& bl) const { + using ceph::encode; + encode(d, bl); + } + void decode(bufferlist::iterator &p) { + using ceph::decode; + decode(d, p); + } + void dump(Formatter* f) { + f->dump_int("timespan", d.count()); + } + static void generate_test_instances(std::list& ls) { + constexpr std::chrono::seconds d{7377}; // marathon world record (2:02:57) + ls.push_back(new timespan_wrapper(d)); + } +}; +WRITE_CLASS_ENCODER(timespan_wrapper) + #endif diff --git a/src/test/encoding/types.h b/src/test/encoding/types.h index d89ab36a39275..f2623a523eb30 100644 --- a/src/test/encoding/types.h +++ b/src/test/encoding/types.h @@ -17,6 +17,7 @@ TYPE(compressible_bloom_filter) #include "test_ceph_time.h" TYPE(real_time_wrapper) TYPE(coarse_real_time_wrapper) +TYPE(timespan_wrapper) #include "test_sstring.h" TYPE(sstring_wrapper) -- 2.39.5