Gaia
timer.h
1 /*
2  * Copyright (C) 2006-2013 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Gaia
5  *
6  * Gaia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef GAIA_TIMER_H
21 #define GAIA_TIMER_H
22 
23 #include "osdetect.h"
24 
25 #ifndef OS_WIN32
26 #include <sys/time.h>
27 
28 namespace gaia2 {
29 
38 class Timer {
39  double _total;
40  struct timeval _begin, _end;
41  bool _isRunning;
42 
43  static double diff(const struct timeval& end, const struct timeval& begin) {
44  return (end.tv_sec - begin.tv_sec) + 1e-6 * (end.tv_usec - begin.tv_usec);
45  }
46 
47  public:
51  Timer();
52 
56  void start();
57 
62  void restart() {
63  reset();
64  start();
65  }
66 
70  void stop();
71 
75  void reset();
76 
85  double elapsed() const;
86 
87 };
88 
89 } // namespace gaia2
90 
91 #else // OS_WIN32
92 
93 namespace gaia2 {
94 
103 class Timer {
104 
105  public:
109  Timer() {}
110 
114  void start() {}
115 
120  void restart() {
121  reset();
122  start();
123  }
124 
128  void stop() {}
129 
133  void reset() {}
134 
143  double elapsed() const { return 0; }
144 
145 };
146 
147 } // namespace gaia2
148 
149 #endif // OS_WIN32
150 
151 #endif // GAIA_TIMER_H
double elapsed() const
Returns the cumulative number of seconds elapsed between each call to the start()/stop() pair...
Definition: timer.cpp:51
void restart()
Restarts the timer.
Definition: timer.h:62
void start()
Starts the timer.
Definition: timer.cpp:32
Timer()
The constructor of the class.
Definition: timer.cpp:25
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
void reset()
Reset the total to 0.
Definition: timer.cpp:37
void stop()
Stops the timer.
Definition: timer.cpp:42
This class allows you to measure a certain amount of time, for instance if you want to know how long ...
Definition: timer.h:38