Fix the build for older macOS.
[dcpomatic.git] / src / lib / timer.h
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 /** @file src/timer.h
23  *  @brief Some timing classes for debugging and profiling.
24  */
25
26
27 #ifndef DCPOMATIC_TIMER_H
28 #define DCPOMATIC_TIMER_H
29
30
31 #include <sys/time.h>
32 #include <boost/optional.hpp>
33 #include <string>
34 #include <map>
35
36
37 /** @class PeriodTimer
38  *  @brief A class to allow timing of a period within the caller.
39  *
40  *  On destruction, it will output the time since its construction.
41  */
42 class PeriodTimer
43 {
44 public:
45         explicit PeriodTimer (std::string n);
46         ~PeriodTimer ();
47
48 private:
49
50         /** name to use when giving output */
51         std::string _name;
52         /** time that this class was constructed */
53         struct timeval _start;
54 };
55
56
57 /** @class StateTimer
58  *  @brief A class to allow measurement of the amount of time a program
59  *  spends in one of a set of states.
60  *
61  *  Once constructed, the caller can call set_state() whenever
62  *  its state changes.  When StateTimer is destroyed, it will
63  *  output (to cout) a summary of the time spent in each state.
64  */
65 class StateTimer
66 {
67 public:
68         explicit StateTimer (std::string n);
69         /** @param n Name to use when giving output.
70          *  @param s Initial state.
71          */
72         StateTimer (std::string n, std::string s);
73         ~StateTimer ();
74
75         /** @param s New state that the caller is in */
76         void set (std::string s);
77         void unset ();
78
79         std::string name () const {
80                 return _name;
81         }
82
83         class Counts
84         {
85         public:
86                 double total_time = 0;
87                 int number = 0;
88         };
89
90         std::map<std::string, Counts> counts () const {
91                 return _counts;
92         }
93
94 private:
95         void set_internal (boost::optional<std::string> s);
96
97         /** name to add to the output */
98         std::string _name;
99         /** current state */
100         boost::optional<std::string> _state;
101         /** time that _state was entered */
102         double _time;
103         /** total time and number of entries for each state */
104         std::map<std::string, Counts> _counts;
105 };
106
107
108 #endif