Make some AsyncTest methods virtual to allow mocking them using gtest/gmock
[folly.git] / folly / Chrono.h
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <chrono>
20 #include <type_traits>
21
22 /***
23  *  include or backport:
24  *  * std::chrono::ceil
25  *  * std::chrono::floor
26  *  * std::chrono::round
27  */
28
29 #if __cpp_lib_chrono >= 201510 || _MSC_VER
30
31 namespace folly {
32 namespace chrono {
33
34 /* using override */ using std::chrono::ceil;
35 /* using override */ using std::chrono::floor;
36 /* using override */ using std::chrono::round;
37 }
38 }
39
40 #else
41
42 namespace folly {
43 namespace chrono {
44
45 namespace detail {
46
47 //  from: http://en.cppreference.com/w/cpp/chrono/duration/ceil, CC-BY-SA
48 template <typename T>
49 struct is_duration : std::false_type {};
50 template <typename Rep, typename Period>
51 struct is_duration<std::chrono::duration<Rep, Period>> : std::true_type {};
52
53 template <typename To, typename Duration>
54 constexpr To ceil_impl(Duration const& d, To const& t) {
55   return t < d ? t + To{1} : t;
56 }
57
58 template <typename To, typename Duration>
59 constexpr To floor_impl(Duration const& d, To const& t) {
60   return t > d ? t - To{1} : t;
61 }
62
63 template <typename To, typename Diff>
64 constexpr To round_impl(To const& t0, To const& t1, Diff diff0, Diff diff1) {
65   return diff0 < diff1 ? t0 : diff1 < diff0 ? t1 : t0.count() & 1 ? t1 : t0;
66 }
67
68 template <typename To, typename Duration>
69 constexpr To round_impl(Duration const& d, To const& t0, To const& t1) {
70   return round_impl(t0, t1, d - t0, t1 - d);
71 }
72
73 template <typename To, typename Duration>
74 constexpr To round_impl(Duration const& d, To const& t0) {
75   return round_impl(d, t0, t0 + To{1});
76 }
77 }
78
79 //  mimic: std::chrono::ceil, C++17
80 //  from: http://en.cppreference.com/w/cpp/chrono/duration/ceil, CC-BY-SA
81 template <
82     typename To,
83     typename Rep,
84     typename Period,
85     typename = typename std::enable_if<detail::is_duration<To>::value>::type>
86 constexpr To ceil(std::chrono::duration<Rep, Period> const& d) {
87   return detail::ceil_impl(d, std::chrono::duration_cast<To>(d));
88 }
89
90 //  mimic: std::chrono::ceil, C++17
91 //  from: http://en.cppreference.com/w/cpp/chrono/time_point/ceil, CC-BY-SA
92 template <
93     typename To,
94     typename Clock,
95     typename Duration,
96     typename = typename std::enable_if<detail::is_duration<To>::value>::type>
97 constexpr std::chrono::time_point<Clock, To> ceil(
98     std::chrono::time_point<Clock, Duration> const& tp) {
99   return std::chrono::time_point<Clock, To>{ceil<To>(tp.time_since_epoch())};
100 }
101
102 //  mimic: std::chrono::floor, C++17
103 //  from: http://en.cppreference.com/w/cpp/chrono/duration/floor, CC-BY-SA
104 template <
105     typename To,
106     typename Rep,
107     typename Period,
108     typename = typename std::enable_if<detail::is_duration<To>::value>::type>
109 constexpr To floor(std::chrono::duration<Rep, Period> const& d) {
110   return detail::floor_impl(d, std::chrono::duration_cast<To>(d));
111 }
112
113 //  mimic: std::chrono::floor, C++17
114 //  from: http://en.cppreference.com/w/cpp/chrono/time_point/floor, CC-BY-SA
115 template <
116     typename To,
117     typename Clock,
118     typename Duration,
119     typename = typename std::enable_if<detail::is_duration<To>::value>::type>
120 constexpr std::chrono::time_point<Clock, To> floor(
121     std::chrono::time_point<Clock, Duration> const& tp) {
122   return std::chrono::time_point<Clock, To>{floor<To>(tp.time_since_epoch())};
123 }
124
125 //  mimic: std::chrono::round, C++17
126 //  from: http://en.cppreference.com/w/cpp/chrono/duration/round, CC-BY-SA
127 template <
128     typename To,
129     typename Rep,
130     typename Period,
131     typename = typename std::enable_if<
132         detail::is_duration<To>::value &&
133         !std::chrono::treat_as_floating_point<typename To::rep>::value>::type>
134 constexpr To round(std::chrono::duration<Rep, Period> const& d) {
135   return detail::round_impl(d, floor<To>(d));
136 }
137
138 //  mimic: std::chrono::round, C++17
139 //  from: http://en.cppreference.com/w/cpp/chrono/time_point/round, CC-BY-SA
140 template <
141     typename To,
142     typename Clock,
143     typename Duration,
144     typename = typename std::enable_if<
145         detail::is_duration<To>::value &&
146         !std::chrono::treat_as_floating_point<typename To::rep>::value>::type>
147 constexpr std::chrono::time_point<Clock, To> round(
148     std::chrono::time_point<Clock, Duration> const& tp) {
149   return std::chrono::time_point<Clock, To>{round<To>(tp.time_since_epoch())};
150 }
151 }
152 }
153
154 #endif