1 | /*! |
---|---|
2 | * \file simple_time.hpp |
3 | * \brief file simple_time.hpp |
4 | * |
5 | * Adapted from: WRATHTime.hpp of WRATH: |
6 | * |
7 | * Copyright 2013 by Nomovok Ltd. |
8 | * Contact: info@nomovok.com |
9 | * This Source Code Form is subject to the |
10 | * terms of the Mozilla Public License, v. 2.0. |
11 | * If a copy of the MPL was not distributed with |
12 | * this file, You can obtain one at |
13 | * http://mozilla.org/MPL/2.0/. |
14 | * |
15 | * \author Kevin Rogovin <kevin.rogovin@nomovok.com> |
16 | * \author Kevin Rogovin <kevin.rogovin@gmail.com> |
17 | * |
18 | */ |
19 | |
20 | |
21 | |
22 | #ifndef FASTUIDRAW_DEMO_SIMPLE_TIME_HPP |
23 | #define FASTUIDRAW_DEMO_SIMPLE_TIME_HPP |
24 | |
25 | #include <chrono> |
26 | |
27 | #include <stdint.h> |
28 | |
29 | class simple_time |
30 | { |
31 | public: |
32 | simple_time(void) |
33 | { |
34 | m_start_time = std::chrono::steady_clock::now(); |
35 | } |
36 | |
37 | int32_t |
38 | elapsed(void) const |
39 | { |
40 | return time_difference_ms(std::chrono::steady_clock::now(), m_start_time); |
41 | } |
42 | |
43 | int32_t |
44 | restart(void) |
45 | { |
46 | auto current_time = std::chrono::steady_clock::now(); |
47 | int32_t return_value = time_difference_ms(current_time, m_start_time); |
48 | m_start_time = current_time; |
49 | return return_value; |
50 | } |
51 | |
52 | int64_t |
53 | elapsed_us(void) const |
54 | { |
55 | return time_difference_us(std::chrono::steady_clock::now(), m_start_time); |
56 | } |
57 | |
58 | int64_t |
59 | restart_us(void) |
60 | { |
61 | auto current_time = std::chrono::steady_clock::now(); |
62 | int64_t return_value = time_difference_us(current_time, m_start_time); |
63 | m_start_time = current_time; |
64 | return return_value; |
65 | } |
66 | |
67 | private: |
68 | |
69 | static |
70 | int32_t |
71 | time_difference_ms(const std::chrono::time_point<std::chrono::steady_clock> &end, |
72 | const std::chrono::time_point<std::chrono::steady_clock> &begin) |
73 | { |
74 | return std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count(); |
75 | } |
76 | |
77 | static |
78 | int64_t |
79 | time_difference_us(const std::chrono::time_point<std::chrono::steady_clock> &end, |
80 | const std::chrono::time_point<std::chrono::steady_clock> &begin) |
81 | { |
82 | return std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count(); |
83 | } |
84 | |
85 | std::chrono::time_point<std::chrono::steady_clock> m_start_time; |
86 | }; |
87 | |
88 | #endif |
89 |