]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
encoding: constrain the time_point templates
authorCasey Bodley <cbodley@redhat.com>
Tue, 8 May 2018 19:06:10 +0000 (15:06 -0400)
committerCasey Bodley <cbodley@redhat.com>
Tue, 8 May 2018 21:05:41 +0000 (17:05 -0400)
disable use of encode/decode for time_points that don't provide
conversions to/from timespec. these are only intended for use by
real_clock and coarse_real_clock

Signed-off-by: Casey Bodley <cbodley@redhat.com>
src/common/ceph_time.h
src/include/encoding.h

index 4dd83e8acd500221e453a94f96c23d9a088a9ab1..f6ea4014da9ab0ad66e64a6f455cbe9641aa0b4f 100644 (file)
@@ -468,6 +468,20 @@ inline timespan to_timespan(signedspan z) {
   ceph_assert(z >= signedspan::zero());
   return std::chrono::duration_cast<timespan>(z);
 }
+
+// detects presence of Clock::to_timespec() and from_timespec()
+template <typename Clock, typename = std::void_t<>>
+struct converts_to_timespec : std::false_type {};
+
+template <typename Clock>
+struct converts_to_timespec<Clock, std::void_t<decltype(
+    Clock::from_timespec(Clock::to_timespec(
+        std::declval<typename Clock::time_point>()))
+  )>> : std::true_type {};
+
+template <typename Clock>
+constexpr bool converts_to_timespec_v = converts_to_timespec<Clock>::value;
+
 } // namespace ceph
 
 #endif // COMMON_CEPH_TIME_H
index a13393ccfd42064b0fe3502974b2b15276e39db3..187c174a1e60747ed74d6688e0b7ccda044e64f6 100644 (file)
@@ -280,7 +280,8 @@ inline void decode_nohead(int len, bufferlist& s, bufferlist::iterator& p)
 
 // Time, since the templates are defined in std::chrono
 
-template<typename Clock, typename Duration>
+template<typename Clock, typename Duration,
+         typename std::enable_if_t<converts_to_timespec_v<Clock>>* = nullptr>
 void encode(const std::chrono::time_point<Clock, Duration>& t,
            ceph::bufferlist &bl) {
   auto ts = Clock::to_timespec(t);
@@ -291,7 +292,8 @@ void encode(const std::chrono::time_point<Clock, Duration>& t,
   encode(ns, bl);
 }
 
-template<typename Clock, typename Duration>
+template<typename Clock, typename Duration,
+         typename std::enable_if_t<converts_to_timespec_v<Clock>>* = nullptr>
 void decode(std::chrono::time_point<Clock, Duration>& t,
            bufferlist::iterator& p) {
   uint32_t s;