added iOS source code
[wl-app.git] / iOS / Pods / Realm / include / core / realm / metrics / metric_timer.hpp
1 /*************************************************************************
2  *
3  * Copyright 2016 Realm Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  **************************************************************************/
18
19 #ifndef REALM_METRIC_TIMER_HPP
20 #define REALM_METRIC_TIMER_HPP
21
22 #include <realm/util/features.h>
23
24 #include <chrono>
25 #include <memory>
26 #include <ostream>
27
28 #if REALM_METRICS
29
30 namespace realm {
31 namespace metrics {
32
33
34 class MetricTimerResult
35 {
36 public:
37     MetricTimerResult();
38     ~MetricTimerResult();
39     double get_elapsed_seconds() const;
40     virtual void report_seconds(double time);
41 protected:
42     double m_elapsed_seconds;
43 };
44
45
46 class MetricTimer {
47 public:
48     MetricTimer(std::shared_ptr<MetricTimerResult> destination = nullptr);
49     ~MetricTimer();
50
51     void reset();
52
53     /// Returns elapsed time in seconds since last call to reset().
54     double get_elapsed_time() const;
55     /// Same as get_elapsed_time().
56     operator double() const;
57
58     /// Format the elapsed time on the form 0h00m, 00m00s, 00.00s, or
59     /// 000.0ms depending on magnitude.
60     static void format(double seconds, std::ostream&);
61
62     static std::string format(double seconds);
63
64 private:
65     using clock_type = std::chrono::high_resolution_clock;
66     using time_point = std::chrono::time_point<clock_type>;
67     time_point m_start;
68     time_point m_paused_at;
69     std::shared_ptr<MetricTimerResult> m_dest;
70
71     time_point get_timer_ticks() const;
72     double calc_elapsed_seconds(time_point begin, time_point end) const;
73 };
74
75
76 inline void MetricTimer::reset()
77 {
78     m_start = get_timer_ticks();
79 }
80
81 inline double MetricTimer::get_elapsed_time() const
82 {
83     return calc_elapsed_seconds(m_start, get_timer_ticks());
84 }
85
86 inline MetricTimer::operator double() const
87 {
88     return get_elapsed_time();
89 }
90
91 inline std::ostream& operator<<(std::ostream& out, const MetricTimer& timer)
92 {
93     MetricTimer::format(timer, out);
94     return out;
95 }
96
97
98 } // namespace metrics
99 } // namespace realm
100
101 #endif // REALM_METRICS
102
103 #endif // REALM_METRIC_TIMER_HPP