encoding: use concepts for chrono encoding

Signed-off-by: Casey Bodley <cbodley@redhat.com>
This commit is contained in:
Casey Bodley 2023-11-15 15:28:46 -05:00
parent 1fd4309fbb
commit b33e08657e
2 changed files with 10 additions and 12 deletions

View File

@ -529,6 +529,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<

View 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;