31470a92c36bb273c23845cf755af584752af676
[ardour.git] / libs / surfaces / mackie / timer.h
1 /*
2   Copyright (C) 1998, 1999, 2000, 2007 John Anderson
3
4   This program is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Library General Public License
6   as published by the Free Software Foundation; either version 2
7   of the License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU Library General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18
19 #ifndef timer_h
20 #define timer_h
21
22 #ifdef _WIN32
23 #include "windows.h"
24 #else
25 #include <sys/time.h>
26 #endif
27
28 namespace ArdourSurface {
29
30 namespace Mackie
31 {
32
33 /**
34         millisecond timer class.
35 */
36 class Timer
37 {
38 public:
39         
40         /**
41                 start the timer running if true, or just create the
42                 object if false.
43         */
44         Timer( bool shouldStart = true )
45         {
46                 if ( shouldStart )
47                         start();
48         }
49         
50         /**
51                 Start the timer running. Return the current timestamp, in milliseconds
52         */
53         unsigned long start()
54         {
55                 _start = g_get_monotonic_time();
56                 return _start / 1000;
57         }
58
59         /**
60                 returns the number of milliseconds since start
61                 also stops the timer running
62         */
63         unsigned long stop()
64         {
65                 _stop = g_get_monotonic_time();
66                 return elapsed();
67         }
68
69         /**
70                 returns the number of milliseconds since start
71         */
72         unsigned long elapsed() const
73         {
74                 if ( running )
75                 {
76                         uint64_t now = g_get_monotonic_time();
77                         return (now - _start) / 1000;
78                 }
79                 else
80                 {
81                         return (_stop - _start) / 1000;
82                 }
83         }
84         
85         /**
86                 Call stop and then start. Return the value from stop.
87         */
88         unsigned long restart()
89         {
90                 unsigned long retval = stop();
91                 start();
92                 return retval;
93         }
94
95 private:
96         uint64_t _start;
97         uint64_t _stop;
98         bool running;
99 };
100
101 } // Mackie namespace
102 } // ArdourSurface namespace
103
104 #endif