]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
encoding: use concepts for chrono encoding 54674/head
authorCasey Bodley <cbodley@redhat.com>
Wed, 15 Nov 2023 20:28:46 +0000 (15:28 -0500)
committerCasey Bodley <cbodley@redhat.com>
Wed, 10 Jan 2024 18:16:36 +0000 (13:16 -0500)
Signed-off-by: Casey Bodley <cbodley@redhat.com>
(cherry picked from commit b33e08657ecb7949505c6eae920c83e1e84096bb)

src/common/ceph_time.h
src/include/encoding.h

index 292fa91ac24b594eb1d3f1420804c754d4fe5e66..fc96a80a7e9826a661fed7a38a4102ba7f5f1da0 100644 (file)
@@ -528,6 +528,9 @@ struct converts_to_timespec<Clock, std::void_t<decltype(
 template <typename Clock>
 constexpr bool converts_to_timespec_v = converts_to_timespec<Clock>::value;
 
+template <typename Clock>
+concept clock_with_timespec = converts_to_timespec_v<Clock>;
+
 template<typename Rep, typename T>
 static Rep to_seconds(T t) {
   return std::chrono::duration_cast<
index aac63a7e6cc93ab5f3ced1ab7a5fc628022f4776..08c67c33eecf8a73aef6a9759f10c59aa815ec88 100644 (file)
@@ -14,6 +14,7 @@
 #ifndef CEPH_ENCODING_H
 #define CEPH_ENCODING_H
 
+#include <concepts>
 #include <set>
 #include <map>
 #include <deque>
@@ -326,8 +327,7 @@ inline void decode_nohead(int len, bufferlist& s, bufferlist::const_iterator& p)
 // 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<typename Clock, typename Duration,
-         typename std::enable_if_t<converts_to_timespec_v<Clock>>* = nullptr>
+template<clock_with_timespec Clock, typename Duration>
 void encode(const std::chrono::time_point<Clock, Duration>& t,
            ceph::bufferlist &bl) {
   auto ts = Clock::to_timespec(t);
@@ -338,8 +338,7 @@ void encode(const std::chrono::time_point<Clock, Duration>& t,
   encode(ns, bl);
 }
 
-template<typename Clock, typename Duration,
-         typename std::enable_if_t<converts_to_timespec_v<Clock>>* = nullptr>
+template<clock_with_timespec Clock, typename Duration>
 void decode(std::chrono::time_point<Clock, Duration>& t,
            bufferlist::const_iterator& p) {
   uint32_t s;
@@ -353,8 +352,7 @@ 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>
+template<std::integral Rep, typename Period>
 void encode(const std::chrono::duration<Rep, Period>& d,
            ceph::bufferlist &bl) {
   using namespace std::chrono;
@@ -364,8 +362,7 @@ void encode(const std::chrono::duration<Rep, Period>& d,
   encode(ns, bl);
 }
 
-template<typename Rep, typename Period,
-         typename std::enable_if_t<std::is_integral_v<Rep>>* = nullptr>
+template<std::integral Rep, typename Period>
 void decode(std::chrono::duration<Rep, Period>& d,
            bufferlist::const_iterator& p) {
   int32_t s;
@@ -378,16 +375,14 @@ void decode(std::chrono::duration<Rep, Period>& d,
 // Provide encodings for chrono::time_point and duration that use
 // the underlying representation so are guaranteed to round-trip.
 
-template <typename Rep, typename Period,
-          typename std::enable_if_t<std::is_integral_v<Rep>>* = nullptr>
+template <std::integral Rep, typename Period>
 void round_trip_encode(const std::chrono::duration<Rep, Period>& d,
                        ceph::bufferlist &bl) {
   const Rep r = d.count();
   encode(r, bl);
 }
 
-template <typename Rep, typename Period,
-          typename std::enable_if_t<std::is_integral_v<Rep>>* = nullptr>
+template <std::integral Rep, typename Period>
 void round_trip_decode(std::chrono::duration<Rep, Period>& d,
                        bufferlist::const_iterator& p) {
   Rep r;