CARLA
StopWatch.h
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include <chrono>
10 #include <cstdint>
11 
12 namespace carla {
13 namespace detail {
14 
15  template <typename CLOCK>
16  class StopWatchTmpl {
17  static_assert(CLOCK::is_steady, "The StopWatch's clock must be steady");
18  public:
19 
20  using clock = CLOCK;
21 
22  StopWatchTmpl() : _start(clock::now()), _end(), _is_running(true) {}
23 
24  void Restart() {
25  _is_running = true;
26  _start = clock::now();
27  }
28 
29  void Stop() {
30  _end = clock::now();
31  _is_running = false;
32  }
33 
34  typename clock::duration GetDuration() const {
35  return _is_running ? clock::now() - _start : _end - _start;
36  }
37 
38  template <class RESOLUTION=std::chrono::milliseconds>
39  size_t GetElapsedTime() const {
40  return static_cast<size_t>(std::chrono::duration_cast<RESOLUTION>(GetDuration()).count());
41  }
42 
43  bool IsRunning() const {
44  return _is_running;
45  }
46 
47  private:
48 
49  typename clock::time_point _start;
50 
51  typename clock::time_point _end;
52 
54  };
55 
56 } // namespace detail
57 
59 
60 } // namespace carla
clock::time_point _end
Definition: StopWatch.h:51
clock::time_point _start
Definition: StopWatch.h:49
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
clock::duration GetDuration() const
Definition: StopWatch.h:34
size_t GetElapsedTime() const
Definition: StopWatch.h:39