1 // Copyright (C) 2015-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 
6 #ifndef STOP_WATCH_H_
7 #define STOP_WATCH_H_
8 
9 #include <cstdint>
10 
11 
12 class stop_watch
13 {
14 public:
15     typedef uint64_t usec_t;
16 
stop_watch()17     stop_watch() :
18                     started_(false),
19                     start_time_point_(0),
20                     total_elapsed_(0)
21     {
22     }
23 
reset()24     inline void reset()
25     {
26         started_ = false;
27         total_elapsed_ = 0;
28     }
29 
start()30     inline void start()
31     {
32         start_time_point_ = now();
33         started_ = true;
34     }
35 
stop()36     inline void stop()
37     {
38         total_elapsed_ += get_elapsed();
39         started_ = false;
40     }
41 
42     usec_t get_total_elapsed_microseconds() const;
43     usec_t get_total_elapsed_seconds() const;
44 
45 private:
get_elapsed() const46     inline usec_t get_elapsed() const
47     {
48         return now() - start_time_point_;
49     }
50 
51     static usec_t now();
52 
53     bool started_;
54     usec_t start_time_point_;
55     usec_t total_elapsed_;
56 };
57 
58 #endif // STOP_WATCH_H_
59