]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
encoding: add overloads for chrono durations
authorCasey Bodley <cbodley@redhat.com>
Tue, 8 May 2018 19:07:43 +0000 (15:07 -0400)
committerNathan Cutler <ncutler@suse.com>
Tue, 2 Oct 2018 17:20:00 +0000 (19:20 +0200)
std::chrono::durations can be encoded in the same format as time_points
and utime_t

Signed-off-by: Casey Bodley <cbodley@redhat.com>
(cherry picked from commit 5c08f2cc49063053d98fa77b68bfd8fde28454b7)

Conflicts:
src/test/encoding/test_ceph_time.h
src/test/encoding/types.h

src/include/encoding.h
src/test/encoding/test_ceph_time.h
src/test/encoding/types.h

index 187c174a1e60747ed74d6688e0b7ccda044e64f6..45c8f66efeea1995734d0eef9e96eb9727101023 100644 (file)
@@ -307,6 +307,28 @@ void decode(std::chrono::time_point<Clock, Duration>& t,
   t = Clock::from_timespec(ts);
 }
 
+template<typename Rep, typename Period,
+         typename std::enable_if_t<std::is_integral_v<Rep>>* = nullptr>
+void encode(const std::chrono::duration<Rep, Period>& d,
+           ceph::bufferlist &bl) {
+  using namespace std::chrono;
+  uint32_t s = duration_cast<seconds>(d).count();
+  uint32_t ns = (duration_cast<nanoseconds>(d) % seconds(1)).count();
+  encode(s, bl);
+  encode(ns, bl);
+}
+
+template<typename Rep, typename Period,
+         typename std::enable_if_t<std::is_integral_v<Rep>>* = nullptr>
+void decode(std::chrono::duration<Rep, Period>& 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
 
index bfd250f98ffdc6a2d5c12ab414e40c71f542133b..b04dd28e7a69bb7f711129617983f8e7572cf20b 100644 (file)
@@ -40,4 +40,29 @@ WRITE_CLASS_ENCODER(real_time_wrapper)
 using coarse_real_time_wrapper = time_point_wrapper<ceph::coarse_real_clock>;
 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<timespan_wrapper*>& 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
index d89ab36a392750b3f558d06640ac71e581825dee..f2623a523eb3049f8c8ce73cae41ca0bf4e5e62e 100644 (file)
@@ -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)