]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/ceph_time: add operator<< for signedspan
authorKefu Chai <kchai@redhat.com>
Thu, 7 Jan 2021 03:56:25 +0000 (11:56 +0800)
committerDavid Galloway <dgallowa@redhat.com>
Wed, 11 May 2022 16:26:05 +0000 (12:26 -0400)
* templatize operator<<(ostream&, duration<>), so it works for more
  duration<> classes with minimal efforts -- we just need to explicitly
  instantiate these template operators
* explicitly instantiate operator<< for timespan, signedspan, seconds
  and milliseconds. they are most likely to be used in Ceph. we can add
  more of them when necessary.

Signed-off-by: Kefu Chai <kchai@redhat.com>
(cherry picked from commit a64b96dba14df1e61ee6eb449535a6ff4a9d64b3)

src/common/ceph_time.cc
src/common/ceph_time.h

index 9b85fc88224d3edb807176b0f21c1e2255127a8d..603165efb43c550dfe8a6baf1ccc6fdb2687b509 100644 (file)
@@ -322,10 +322,31 @@ std::chrono::seconds parse_timespan(const std::string& s)
 }
 
 namespace std {
-ostream& operator<<(ostream& m, const ::ceph::timespan& t) {
-  static_assert(is_unsigned_v<::ceph::timespan::rep>);
-  using seconds_t = chrono::duration<float>;
-  ::fmt::print(m, "{:.9}", chrono::duration_cast<seconds_t>(t));
+template<typename Rep, typename Period>
+ostream& operator<<(ostream& m, const chrono::duration<Rep, Period>& t) {
+  if constexpr (chrono::treat_as_floating_point_v<Rep>) {
+    using seconds_t = chrono::duration<float>;
+    ::fmt::print(m, "{:.9}", chrono::duration_cast<seconds_t>(t));
+  } else {
+    ::fmt::print(m, "{}", t);
+  }
   return m;
 }
+
+template ostream&
+operator<< <::ceph::timespan::rep,
+            ::ceph::timespan::period> (ostream&, const ::ceph::timespan&);
+
+template ostream&
+operator<< <::ceph::signedspan::rep,
+            ::ceph::signedspan::period> (ostream&, const ::ceph::signedspan&);
+
+template ostream&
+operator<< <chrono::seconds::rep,
+            chrono::seconds::period> (ostream&, const chrono::seconds&);
+
+template ostream&
+operator<< <chrono::milliseconds::rep,
+            chrono::milliseconds::period> (ostream&, const chrono::milliseconds&);
+
 } // namespace std
index 663d05f8feb2b0f14f5979cb378aa60261ad7514..2f99188e439b474ed8dc004e406d960b09951ed0 100644 (file)
@@ -519,7 +519,8 @@ static Rep to_microseconds(T t) {
 } // namespace ceph
 
 namespace std {
-ostream& operator<<(ostream& m, const ::ceph::timespan& t);
+template<typename Rep, typename Period>
+ostream& operator<<(ostream& m, const chrono::duration<Rep, Period>& t);
 }
 
 #endif // COMMON_CEPH_TIME_H