ElapsedTimer.h 935 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Time.h>
  8. namespace Core {
  9. enum class TimerType {
  10. Precise,
  11. Coarse
  12. };
  13. class ElapsedTimer {
  14. public:
  15. static ElapsedTimer start_new();
  16. ElapsedTimer(TimerType timer_type = TimerType::Coarse)
  17. : m_timer_type(timer_type)
  18. {
  19. }
  20. bool is_valid() const { return m_valid; }
  21. void start();
  22. void reset();
  23. i64 elapsed_milliseconds() const;
  24. Duration elapsed_time() const;
  25. // FIXME: Move callers to elapsed_milliseconds(), remove this.
  26. i64 elapsed() const // milliseconds
  27. {
  28. return elapsed_milliseconds();
  29. }
  30. MonotonicTime const& origin_time() const { return m_origin_time; }
  31. private:
  32. MonotonicTime m_origin_time { MonotonicTime::now() };
  33. TimerType m_timer_type { TimerType::Coarse };
  34. bool m_valid { false };
  35. };
  36. }