sequencer
sequencer_clock.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include <chrono>
6 #include <type_traits>
7 
8 namespace sequencer::chrono
9 {
10 
11  template < typename UnderlyingClock = clock_object_adapter< std::chrono::steady_clock > >
13  {
14  public:
15  using underlying_clock_type = std::decay_t< UnderlyingClock >;
16  using underlying_time_point = typename underlying_clock_type::time_point;
17  using rep = typename underlying_clock_type::rep;
18  using period = typename underlying_clock_type::period;
19  using duration = std::chrono::duration< rep, period >;
20  using time_point = std::chrono::time_point< sequencer_clock >;
21 
22  static_assert( underlying_clock_type::is_steady, "Underlying clock must be steady" );
23 
24  constexpr static bool is_steady = false;
25 
26  explicit sequencer_clock( const underlying_clock_type& underlying_clock ) noexcept
27  : underlying_clock_( underlying_clock )
28  {
29  }
30 
31  bool is_running() const noexcept
32  {
33  return is_running_;
34  }
35 
36  time_point now() const noexcept
37  {
38  if ( is_running_ )
39  {
40  return time_point{elapsed_ + ( underlying_clock_.now() - start_time_ )};
41  }
42  return time_point{elapsed_};
43  }
44 
45  void reset() noexcept
46  {
47  elapsed_ = duration::zero();
48  is_running_ = false;
49  }
50 
51  void start() noexcept
52  {
53  if ( !is_running_ )
54  {
55  start_time_ = underlying_clock_.now();
56  is_running_ = true;
57  }
58  }
59 
60  void stop() noexcept
61  {
62  if ( is_running_ )
63  {
64  elapsed_ += underlying_clock_.now() - start_time_;
65  is_running_ = false;
66  }
67  }
68 
69  private:
70  UnderlyingClock underlying_clock_;
71  bool is_running_ = false;
72  underlying_time_point start_time_ = underlying_time_point{duration::zero()};
73  duration elapsed_ = duration::zero();
74  };
75 
76 } // namespace sequencer::chrono
void reset() noexcept
Definition: sequencer_clock.hpp:45
void start() noexcept
Definition: sequencer_clock.hpp:51
bool is_running() const noexcept
Definition: sequencer_clock.hpp:31
typename underlying_clock_type::rep rep
Definition: sequencer_clock.hpp:17
static constexpr bool is_steady
Definition: sequencer_clock.hpp:24
std::chrono::duration< rep, period > duration
Definition: sequencer_clock.hpp:19
Definition: sequencer_clock.hpp:12
sequencer_clock(const underlying_clock_type &underlying_clock) noexcept
Definition: sequencer_clock.hpp:26
typename underlying_clock_type::period period
Definition: sequencer_clock.hpp:18
std::decay_t< UnderlyingClock > underlying_clock_type
Definition: sequencer_clock.hpp:15
std::chrono::time_point< sequencer_clock > time_point
Definition: sequencer_clock.hpp:20
void stop() noexcept
Definition: sequencer_clock.hpp:60
time_point now() const noexcept
Definition: sequencer_clock.hpp:36
typename underlying_clock_type::time_point underlying_time_point
Definition: sequencer_clock.hpp:16
Definition: chrono_adapter.hpp:5