]> 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)
committerNathan Cutler <ncutler@suse.com>
Tue, 2 Oct 2018 17:20:00 +0000 (19:20 +0200)
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>
(cherry picked from commit 427655e39180640fb1de56cef13c4ed8c34fa3bb)

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

index 73a11eef6b72cbfdf9284a1be8f5e7a0b695625d..a9f9195d371b7fde0b36f9046179eda20a6b566f 100644 (file)
@@ -476,6 +476,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;